Skip to content

Commit 62307df

Browse files
committed
feat: add a support for local chat for free text prompts
1 parent 960a20e commit 62307df

File tree

8 files changed

+83
-14
lines changed

8 files changed

+83
-14
lines changed

assets/components/modai/js/mgr/history.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ modAI.history = {
8484
getData: () => {
8585
return this._formatOutput(key);
8686
},
87+
getAll: () => {
88+
return this._cache[key].values;
89+
},
8790
syncUI: () => {
8891
if (typeof syncUI === 'function') {
8992
this._cache[key].syncUI(this._formatOutput(key), false);

assets/components/modai/js/mgr/widgets/text_prompt.window.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ Ext.extend(modAI.window.TextPrompt,MODx.Window, {
3737
info.update({currentPage: data.current, total: data.total})
3838
info.show();
3939

40-
this.prompt.setValue(data.value.prompt)
40+
this.prompt.setValue(data.value.user);
4141
this.preview.show();
42-
this.preview.setValue(data.value.content);
42+
this.preview.setValue(data.value.assistant);
4343

4444
if (noStore) {
4545
this.preview.el.dom.scrollTop = this.preview.el.dom.scrollHeight;
@@ -58,7 +58,7 @@ Ext.extend(modAI.window.TextPrompt,MODx.Window, {
5858
}
5959
}
6060

61-
const addItem = (item, noStore = false) => {
61+
this.addItem = (item, noStore = false) => {
6262
this._history.insert(item, noStore);
6363

6464
this.preview.show();
@@ -133,10 +133,6 @@ Ext.extend(modAI.window.TextPrompt,MODx.Window, {
133133
disabled: true
134134
});
135135

136-
this.pagination = {
137-
addItem,
138-
};
139-
140136
this._history = modAI.history.init(config.cacheKey, syncUI);
141137

142138
if (this._history.cachedItem.values.length > 0) {
@@ -176,11 +172,12 @@ Ext.extend(modAI.window.TextPrompt,MODx.Window, {
176172
try {
177173
const result = await modAI.executor.mgr.prompt.freeText({
178174
prompt: this.prompt.getValue(),
179-
field: config.fieldName || ''
175+
field: config.fieldName || '',
176+
messages: this._history.getAll(),
180177
}, (data) => {
181-
this.pagination.addItem({prompt: this.prompt.getValue(), content: data.content}, true);
178+
this.addItem({user: this.prompt.getValue(), assistant: data.content}, true);
182179
});
183-
this.pagination.addItem({prompt: this.prompt.getValue(), content: result.content});
180+
this.addItem({user: this.prompt.getValue(), assistant: result.content});
184181
Ext.Msg.hide();
185182
} catch (err) {
186183
Ext.Msg.hide();

core/components/modai/src/API/Prompt/FreeText.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function post(ServerRequestInterface $request): void
2020
$field = $this->modx->getOption('field', $data, '');
2121
$context = $this->modx->getOption('context', $data, '');
2222
$namespace = $this->modx->getOption('namespace', $data, 'modai');
23+
$messages = $this->modx->getOption('messages', $data);
2324

2425
if (empty($prompt)) {
2526
throw new LexiconException('modai.error.prompt_required');
@@ -52,6 +53,7 @@ public function post(ServerRequestInterface $request): void
5253
$result = $aiService->getCompletions(
5354
[$prompt],
5455
CompletionsConfig::new($model)
56+
->messages($messages)
5557
->customOptions($customOptions)
5658
->maxTokens($maxTokens)
5759
->temperature($temperature)

core/components/modai/src/Services/ChatGPT.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ public function getCompletions(array $data, CompletionsConfig $config): AIRespon
3737
];
3838
}
3939

40+
foreach ($config->getMessages() as $msg) {
41+
$messages[] = [
42+
'role' => 'user',
43+
'content' => $msg['user']
44+
];
45+
46+
$messages[] = [
47+
'role' => 'assistant',
48+
'content' => $msg['assistant']
49+
];
50+
}
51+
4052
foreach ($data as $msg) {
4153
$messages[] = [
4254
'role' => 'user',

core/components/modai/src/Services/Claude.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ public function getCompletions(array $data, CompletionsConfig $config): AIRespon
2828

2929
$messages = [];
3030

31+
foreach ($config->getMessages() as $msg) {
32+
$messages[] = [
33+
'role' => 'user',
34+
'content' => $msg['user']
35+
];
36+
37+
$messages[] = [
38+
'role' => 'assistant',
39+
'content' => $msg['assistant']
40+
];
41+
}
42+
3143
foreach ($data as $msg) {
3244
$messages[] = [
3345
'role' => 'user',

core/components/modai/src/Services/Config/CompletionsConfig.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class CompletionsConfig {
88
private int $maxTokens;
99
private string $systemInstructions = '';
1010
private bool $stream = false;
11+
private array $messages = [];
1112

1213
public function temperature(float $temperature): self {
1314
$this->temperature = $temperature;
@@ -33,6 +34,12 @@ public function stream(bool $stream): self {
3334
return $this;
3435
}
3536

37+
public function messages(array $messages): self {
38+
$this->messages = $messages;
39+
40+
return $this;
41+
}
42+
3643
public function getTemperature(): float
3744
{
3845
return $this->temperature;
@@ -53,4 +60,9 @@ public function isStream(): bool
5360
return $this->stream;
5461
}
5562

63+
public function getMessages(): array
64+
{
65+
return $this->messages;
66+
}
67+
5668
}

core/components/modai/src/Services/CustomChatGPT.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ public function getCompletions(array $data, CompletionsConfig $config): AIRespon
4242
];
4343
}
4444

45+
foreach ($config->getMessages() as $msg) {
46+
$messages[] = [
47+
'role' => 'user',
48+
'content' => $msg['user']
49+
];
50+
51+
$messages[] = [
52+
'role' => 'assistant',
53+
'content' => $msg['assistant']
54+
];
55+
}
56+
4557
foreach ($data as $msg) {
4658
$messages[] = [
4759
'role' => 'user',

core/components/modai/src/Services/Gemini.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,35 @@ public function getCompletions(array $data, CompletionsConfig $config): AIRespon
4646
}
4747

4848
$messages = [];
49+
50+
foreach ($config->getMessages() as $msg) {
51+
$messages[] = [
52+
'role' => 'user',
53+
'parts' => [
54+
['text' => $msg['user']]
55+
]
56+
];
57+
58+
$messages[] = [
59+
'role' => 'model',
60+
'parts' => [
61+
['text' => $msg['assistant']]
62+
]
63+
];
64+
}
65+
4966
foreach ($data as $msg) {
5067
$messages[] = [
51-
'text' => $msg
68+
'role' => 'user',
69+
'parts' => [
70+
['text' => $msg]
71+
]
5272
];
5373
}
5474

5575
$input = $config->getCustomOptions();
56-
$input["contents"] = [
57-
"parts" => $messages,
58-
];
76+
$input["contents"] = $messages;
77+
5978
$input["generationConfig"] = [
6079
"temperature" => $config->getTemperature(),
6180
"maxOutputTokens" => $config->getMaxTokens(),

0 commit comments

Comments
 (0)