This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_ai.module
More file actions
100 lines (88 loc) · 3.07 KB
/
chat_ai.module
File metadata and controls
100 lines (88 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* @file
* Primary module hooks for Chat AI module.
*/
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function chat_ai_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.chat_ai':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Chat AI module provides a chat bot that can answer questions based on your website content. Allows visitors to interact with your website in natural language.</p>');
$output .= '<p>' . t('Is its based on <a href="https://platform.openai.com">Open AI API</a> and <a href="https://supabase.com">Supabase</a> as vector store provider.</p>');
$output .= '<p>' . t('Before using this module you have to setup a new Supabase project. </p>');
$output .= '<p>' . t('Please check the full installation instructions on the <a href="">module\'s repository page</a>.');
$output .= '</dl>';
return $output;
}
}
/**
* Implements hook_entity_insert().
*/
function chat_ai_entity_insert(EntityInterface $entity) {
$embeddings = \Drupal::service('chat_ai.embeddings');
$shouldIndex = $entity instanceof ContentEntityInterface
? $embeddings->shouldIndex($entity) : NULL;
if ($shouldIndex) {
$embeddings->insertDatabaseEmbedding($entity);
$embeddings->insertToQueue($entity);
}
}
/**
* Implements hook_entity_update().
*/
function chat_ai_entity_update(EntityInterface $entity) {
$embeddings = \Drupal::service('chat_ai.embeddings');
$shouldIndex = $entity instanceof ContentEntityInterface && $embeddings->shouldIndex($entity);
if ($shouldIndex) {
$embeddings->insertDatabaseEmbedding($entity);
$embeddings->insertToQueue($entity);
}
}
/**
* Implements hook_entity_delete().
*/
function chat_ai_entity_delete(EntityInterface $entity) {
$embeddings = \Drupal::service('chat_ai.embeddings');
$shouldIndex = $entity instanceof ContentEntityInterface
? $embeddings->shouldIndex($entity) : NULL;
if ($shouldIndex) {
$embeddings->clearIndexedDataByEntity($entity);
}
}
/**
* Implements hook_entity_base_field_info().
*/
function chat_ai_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
if ($entity_type->id() === 'file') {
$fields['ai_indexed'] = BaseFieldDefinition::create('boolean')
->setLabel(t('AI indexed'))
->setDescription(t('Indicates whether the file is indexed for use with the AI chatbot.'))
->setDefaultValue(FALSE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
])
->setDisplayConfigurable('form', FALSE)
->setDisplayOptions('view', [
'type' => 'boolean',
'label' => 'inline',
'settings' => [
'format' => 'yes-no',
],
])
->setDisplayConfigurable('view', FALSE);
}
return $fields;
}