Skip to content

Commit d3adaaa

Browse files
committed
Text Embedding implemented
1 parent 732ed82 commit d3adaaa

File tree

4 files changed

+63
-13
lines changed

4 files changed

+63
-13
lines changed

classes/base_instance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ final public function edit_form_definition(\MoodleQuickForm $mform, array $custo
500500
// That we have a valid connector here is being ensured by edit_instance.php.
501501
$mform->setDefault('connector', $connector);
502502
$mform->freeze('connector');
503-
503+
504504
$mform->addElement('text', 'endpoint', get_string('endpoint', 'local_ai_manager'), $textelementparams);
505505
$mform->setType('endpoint', PARAM_URL);
506506

classes/local/aitool_option_azure.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static function extend_form_definition(\MoodleQuickForm $mform, bool $sho
5252

5353
// We leave the endpoint empty on creation, because it depends if azure is being used or not.
5454
$mform->setDefault('endpoint', '');
55-
$mform->freeze('endpoint');
55+
// $mform->freeze('endpoint');
5656

5757
if (!$showmodel) {
5858
$mform->hideIf('model', 'azure_enabled', 'eq', 1);

tools/chatgpt/classes/connector.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ class connector extends \local_ai_manager\base_connector {
3636
#[\Override]
3737
public function get_models_by_purpose(): array {
3838
$chatgptmodels =
39-
['gpt-3.5-turbo', 'gpt-4-turbo', 'gpt-4o', 'gpt-4o-mini', 'o1', 'o1-mini', 'o3', 'o3-mini', 'o4-mini'];
39+
['gpt-3.5-turbo', 'gpt-4-turbo', 'gpt-4o', 'gpt-4o-mini', 'o1', 'o1-mini', 'o3', 'o3-mini', 'o4-mini', 'text-embedding-3-small', 'text-embedding-3-large'];
4040
return [
4141
'chat' => $chatgptmodels,
4242
'feedback' => $chatgptmodels,
4343
'singleprompt' => $chatgptmodels,
4444
'translate' => $chatgptmodels,
4545
'itt' => ['gpt-4-turbo', 'gpt-4o', 'gpt-4o-mini', 'o1', 'o3', 'o4-mini'],
4646
'questiongeneration' => $chatgptmodels,
47-
'embedding' => $chatgptmodels, //['text-embedding-3-small', 'text-embedding-3-large'],
47+
'embedding' =>['text-embedding-3-small', 'text-embedding-3-large'],
4848
'rag' => $chatgptmodels,
4949

5050
];
@@ -63,7 +63,14 @@ public function execute_prompt_completion(StreamInterface $result, request_optio
6363
*/
6464
// phpcs:enable moodle.Commenting.TodoComment.MissingInfoInline
6565
$content = json_decode($result->getContents(), true);
66-
66+
$purpose = $requestoptions->get_purpose()->get_plugin_name();
67+
if ($purpose === 'embedding') {
68+
return prompt_response::create_from_result(
69+
$content['model'],
70+
new usage(0, 0, 0),
71+
implode(",", $content['data'][0]['embedding'])
72+
);
73+
}
6774
return prompt_response::create_from_result(
6875
$content['model'],
6976
new usage(
@@ -77,6 +84,27 @@ public function execute_prompt_completion(StreamInterface $result, request_optio
7784
#[\Override]
7885
public function get_prompt_data(string $prompttext, request_options $requestoptions): array {
7986
$options = $requestoptions->get_options();
87+
$purpose = $requestoptions->get_purpose()->get_plugin_name();
88+
if ($purpose === 'embedding') {
89+
return $this->get_embedding_prompt_data($prompttext, $options);
90+
}
91+
return $this->get_chat_prompt_data($prompttext, $options);
92+
}
93+
/**
94+
* Returns the prompt data for an embedding request.
95+
* !! THIS IS BREAKING A DEGREE OF ENCAPSULATION BY NEEDING TO KNOW ABOUT THE PURPOSE !!
96+
*/
97+
protected function get_embedding_prompt_data(string $prompttext, array $options): array {
98+
return [
99+
'input' => $prompttext,
100+
'model' => $this->get_instance()->get_model(),
101+
'encoding_format' => 'float',
102+
];
103+
}
104+
/**
105+
* This is the "original" chat prompt data function, which is used for all purposes except embedding.
106+
*/
107+
protected function get_chat_prompt_data(string $prompttext, array $options): array {
80108
$messages = [];
81109
if (array_key_exists('conversationcontext', $options)) {
82110
foreach ($options['conversationcontext'] as $message) {

tools/chatgpt/classes/instance.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,27 @@ class instance extends base_instance {
3333

3434
#[\Override]
3535
protected function extend_form_definition(\MoodleQuickForm $mform): void {
36+
$this->extend_text_embedding_form_definition($mform);
3637
aitool_option_temperature::extend_form_definition($mform, ['o1', 'o1-mini', 'o3', 'o3-mini', 'o4-mini']);
3738
aitool_option_azure::extend_form_definition($mform);
39+
40+
}
41+
/**
42+
* Add in the text embedding specific form elements.
43+
*/
44+
protected function extend_text_embedding_form_definition(\MoodleQuickForm $mform): void {
45+
46+
$textelementparams = ['style' => 'width: 100%'];
47+
48+
$endpoints = [
49+
'https://api.openai.com/v1/chat/completions' => 'Chat Completion',
50+
'https://api.openai.com/v1/embeddings' => 'Text Embedding',
51+
];
52+
$mform->removeElement('endpoint');
53+
$endpoint = $mform->createElement('select', 'endpoint', get_string('endpoint', 'local_ai_manager'),$endpoints, $textelementparams);
54+
$mform->insertElementBefore($endpoint, 'apikey');
55+
$mform->setType('endpoint', PARAM_URL);
3856
}
39-
4057
#[\Override]
4158
protected function get_extended_formdata(): stdClass {
4259
$data = new stdClass();
@@ -58,16 +75,21 @@ protected function extend_store_formdata(stdClass $data): void {
5875
$this->set_customfield1($temperature);
5976

6077
[$enabled, $resourcename, $deploymentid, $apiversion] = aitool_option_azure::extract_azure_data_to_store($data);
61-
6278
if (!empty($enabled)) {
63-
$endpoint = 'https://' . $resourcename .
64-
'.openai.azure.com/openai/deployments/'
65-
. $deploymentid . '/chat/completions?api-version=' . $apiversion;
66-
// We have an empty model because the model is preconfigured if we're using azure.
67-
// So we overwrite the default "preconfigured" value by a better model name.
79+
if ($data->endpoint !== 'https://api.openai.com/v1/embeddings') {
80+
$endpoint = 'https://' . $resourcename .
81+
'.openai.azure.com/openai/deployments/'
82+
. $deploymentid . '/chat/embeddings?api-version=' . $apiversion;
83+
} else {
84+
$endpoint = 'https://' . $resourcename .
85+
'.openai.azure.com/openai/deployments/'
86+
. $deploymentid . '/chat/completions?api-version=' . $apiversion;
87+
// We have an empty model because the model is preconfigured if we're using azure.
88+
// So we overwrite the default "preconfigured" value by a better model name.
89+
}
6890
$this->set_model(aitool_option_azure::get_azure_model_name($this->get_connector()));
6991
} else {
70-
$endpoint = 'https://api.openai.com/v1/chat/completions';
92+
$endpoint = $data->endpoint;//'https://api.openai.com/v1/chat/completions';
7193
}
7294
$this->set_endpoint($endpoint);
7395

0 commit comments

Comments
 (0)