Skip to content

Commit ba637d2

Browse files
committed
added embeddings manager class
1 parent 855f0d5 commit ba637d2

File tree

13 files changed

+293
-46
lines changed

13 files changed

+293
-46
lines changed

app/base/abstracts/Models/AbstractLLMAdapter.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,23 @@ public function ask(string $prompt, ?string $model = null, ?array $previousMessa
6161
{
6262
$contents = $this->buildConversation(
6363
$previousMessages ?? [],
64-
$prompt
64+
$prompt,
65+
$model
6566
);
6667

6768
$raw = $this->sendRaw($contents, $model, self::COMPLETIONS_ENDPOINT);
68-
$norm = $this->normalizeResponse($raw);
69+
$norm = $this->normalizeCompletionsResponse($raw);
6970

7071
return trim($norm['assistantText'] ?? '');
7172
}
7273

7374
public function embed(string $input, ?string $model = null) : array
7475
{
75-
$payload = $this->buildEmbeddingRequest($input);
76+
$payload = $this->buildEmbeddingRequest($input, $model);
7677

7778
$raw = $this->sendRaw($payload, $model, self::EMBEDDINGS_ENDPOINT);
79+
$norm = $this->normalizeEmbeddingsResponse($raw);
7880

79-
if (!empty($raw['data'][0]['embedding']) && is_array($raw['data'][0]['embedding'])) {
80-
return $raw['data'][0]['embedding'];
81-
}
82-
83-
return [];
81+
return $norm['embedding'] ?? [];
8482
}
8583
}

app/base/ai/Actions/Orchestrator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function runFlow(BaseFlow $flow, string $userPrompt) : array
4747

4848
$response = $this->llm->sendRaw($payload);
4949

50-
$normalized = $this->llm->normalizeResponse($response);
50+
$normalized = $this->llm->normalizeCompletionsResponse($response);
5151

5252
while (!empty($normalized['functionCalls'])) {
5353

@@ -90,7 +90,7 @@ public function runFlow(BaseFlow $flow, string $userPrompt) : array
9090
$messages
9191
);
9292

93-
$normalized = $this->llm->normalizeResponse($response);
93+
$normalized = $this->llm->normalizeCompletionsResponse($response);
9494
}
9595

9696

app/base/ai/Models/ChatGPT.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,18 @@ public function buildConversation(array $previousMessages, string $prompt, ?stri
123123
];
124124
}
125125

126-
public function normalizeResponse(array $raw): array
126+
public function normalizeCompletionsResponse(array $raw): array
127127
{
128128
$assistantText = null;
129129
$functionCalls = [];
130130

131131
if (isset($raw['choices'][0]['message'])) {
132132
$msg = $raw['choices'][0]['message'];
133133

134-
// testo normale
135134
if (!empty($msg['content'])) {
136135
$assistantText = $msg['content'];
137136
}
138137

139-
// tool calls
140138
if (!empty($msg['tool_calls'])) {
141139
foreach ($msg['tool_calls'] as $call) {
142140
$functionCalls[] = [
@@ -224,6 +222,14 @@ public function buildEmbeddingRequest(string $input, ?string $model = null): arr
224222
];
225223
}
226224

225+
public function normalizeEmbeddingsResponse(array $raw) : array
226+
{
227+
return [
228+
'embedding' => $raw['data'][0]['embedding'] ?? [],
229+
'raw' => $raw
230+
];
231+
}
232+
227233
public function getAvailableModels(bool $reset = false) : array
228234
{
229235
$models_key = "ai.chatgpt.models_list";

app/base/ai/Models/Claude.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function buildConversation(array $previousMessages, string $prompt, ?stri
122122
];
123123
}
124124

125-
public function normalizeResponse(array $raw): array
125+
public function normalizeCompletionsResponse(array $raw): array
126126
{
127127
$assistantText = '';
128128
$functionCalls = [];
@@ -225,6 +225,14 @@ public function buildEmbeddingRequest(string $input, ?string $model = null): arr
225225
];
226226
}
227227

228+
public function normalizeEmbeddingsResponse(array $raw) : array
229+
{
230+
return [
231+
'embedding' => $raw['data'][0]['embedding'] ?? [],
232+
'raw' => $raw
233+
];
234+
}
235+
228236
public function getAvailableModels(bool $reset = false) : array
229237
{
230238
$models_key = "ai.claude.models_list";

app/base/ai/Models/GoogleGemini.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function getEmbeddingsEndpoint(?string $model = null) : string
6363
throw new Exception("Missing Gemini Token");
6464
}
6565

66-
return "https://generativelanguage.googleapis.com/" . $this->getVersion() . "/models/" . $this->getModel($model) . ":embedText?key={$apiKey}";
66+
return "https://generativelanguage.googleapis.com/" . $this->getVersion() . "/models/" . $this->getModel($model) . ":embedContent?key={$apiKey}";
6767
}
6868

6969
public function prepareRequest(array $payload) : array
@@ -84,7 +84,6 @@ public function formatUserMessage(string $prompt): array
8484
];
8585
}
8686

87-
8887
public function formatAssistantMessage(mixed $message, ?string $messageType = null): array
8988
{
9089
return [
@@ -119,33 +118,25 @@ public function buildConversation(array $previousMessages, string $prompt, ?stri
119118
];
120119
}
121120

122-
public function normalizeResponse(array $raw): array
121+
public function normalizeCompletionsResponse(array $raw): array
123122
{
124123
$assistantText = null;
125124
$functionCalls = [];
126125
$rawFunctionMessages = [];
127126

128127
if (!empty($raw['candidates']) && is_array($raw['candidates'])) {
129-
130128
foreach ($raw['candidates'] as $candidate) {
131-
132-
// Gemini mette sempre role/parts qui
133129
if (!isset($candidate['content']['parts'])) {
134130
continue;
135131
}
136132

137133
foreach ($candidate['content']['parts'] as $part) {
138134

139-
//
140-
// 1) function_call (nome ufficiale) oppure functionCall (variante)
141-
//
142135
$fc = $part['function_call']
143136
?? $part['functionCall']
144137
?? null;
145138

146139
if ($fc) {
147-
148-
// normalizza args (stringa JSON → array)
149140
$args = $fc['args'] ?? ($fc['arguments'] ?? []);
150141
if (is_string($args)) {
151142
$decoded = json_decode($args, true);
@@ -154,23 +145,17 @@ public function normalizeResponse(array $raw): array
154145
}
155146
}
156147

157-
// normalizzazione functionCalls per l'orchestrator
158148
$functionCalls[] = [
159149
'name' => $fc['name'] ?? null,
160150
'args' => $args ?? []
161151
];
162152

163-
// 2) SALVO IL MESSAGGIO ORIGINALE (importantissimo)
164-
// questo è ciò che Gemini vuole a history invariata
165153
$rawFunctionMessages[] = [
166154
'role' => 'model',
167-
'parts' => [$part] // il part contiene function_call originale
155+
'parts' => [$part]
168156
];
169157
}
170158

171-
//
172-
// 3) assistant text
173-
//
174159
if (isset($part['text'])) {
175160
$assistantText .= $part['text'];
176161
}
@@ -239,7 +224,19 @@ public function buildEmbeddingRequest(string $input, ?string $model = null): arr
239224
{
240225
return [
241226
'model' => $this->getModel($model),
242-
'text' => $input
227+
'content' => [
228+
'parts' => [[
229+
'text' => $input
230+
]]
231+
]
232+
];
233+
}
234+
235+
public function normalizeEmbeddingsResponse(array $raw) : array
236+
{
237+
return [
238+
'embedding' => $raw['embedding']['values'] ?? [],
239+
'raw' => $raw
243240
];
244241
}
245242

app/base/ai/Models/Groq.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,18 @@ public function buildConversation(array $previousMessages, string $prompt, ?stri
104104
];
105105
}
106106

107-
public function normalizeResponse(array $raw): array
107+
public function normalizeCompletionsResponse(array $raw): array
108108
{
109109
$assistantText = null;
110110
$functionCalls = [];
111111

112112
if (isset($raw['choices'][0]['message'])) {
113113
$msg = $raw['choices'][0]['message'];
114114

115-
// testo normale
116115
if (!empty($msg['content'])) {
117116
$assistantText = $msg['content'];
118117
}
119118

120-
// tool calls
121119
if (!empty($msg['tool_calls'])) {
122120
foreach ($msg['tool_calls'] as $call) {
123121
$functionCalls[] = [
@@ -183,6 +181,14 @@ public function buildEmbeddingRequest(string $input, ?string $model = null): arr
183181
];
184182
}
185183

184+
public function normalizeEmbeddingsResponse(array $raw) : array
185+
{
186+
return [
187+
'embedding' => $raw['data'][0]['embedding'] ?? [],
188+
'raw' => $raw
189+
];
190+
}
191+
186192
public function getAvailableModels(bool $reset = false): array
187193
{
188194
$cacheKey = "ai.groq.models_list";

app/base/ai/Models/Mistral.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,18 @@ public function buildConversation(array $previousMessages, string $prompt, ?stri
109109
];
110110
}
111111

112-
public function normalizeResponse(array $raw): array
112+
public function normalizeCompletionsResponse(array $raw): array
113113
{
114114
$assistantText = null;
115115
$functionCalls = [];
116116

117117
if (isset($raw['choices'][0]['message'])) {
118118
$msg = $raw['choices'][0]['message'];
119119

120-
// testo normale
121120
if (!empty($msg['content'])) {
122121
$assistantText = $msg['content'];
123122
}
124123

125-
// tool calls
126124
if (!empty($msg['tool_calls'])) {
127125
foreach ($msg['tool_calls'] as $call) {
128126
$functionCalls[] = [
@@ -198,6 +196,14 @@ public function buildEmbeddingRequest(string $input, ?string $model = null): arr
198196
];
199197
}
200198

199+
public function normalizeEmbeddingsResponse(array $raw) : array
200+
{
201+
return [
202+
'embedding' => $raw['data'][0]['embedding'] ?? [],
203+
'raw' => $raw
204+
];
205+
}
206+
201207
public function getAvailableModels(bool $reset = false) : array
202208
{
203209
$models_key = "ai.mistral.models_list";

app/base/ai/Models/Perplexity.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function buildConversation(array $previousMessages, string $prompt, ?stri
101101
];
102102
}
103103

104-
public function normalizeResponse(array $raw): array
104+
public function normalizeCompletionsResponse(array $raw): array
105105
{
106106
$text = $raw['choices'][0]['text'] ?? null;
107107

@@ -156,6 +156,14 @@ public function buildEmbeddingRequest(string $input, ?string $model = null): arr
156156
];
157157
}
158158

159+
public function normalizeEmbeddingsResponse(array $raw) : array
160+
{
161+
return [
162+
'embedding' => $raw['data'][0]['embedding'] ?? [],
163+
'raw' => $raw
164+
];
165+
}
166+
159167
public function getAvailableModels(bool $reset = false) : array
160168
{
161169
// perplexity has no endpoint for models list, currently

app/base/controllers/Admin/Elasticsearch.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ protected function reindexModels(string $modelClass)
226226
{
227227
$results = [];
228228

229+
$this->getSearch()->ensureIndex();
229230
$response = $this->getSearch()->indexFrontendCollection($this->containerCall([$modelClass, 'getCollection']));
230231
foreach (($response['items'] ?? []) as $item) {
231232
if (isset($item['index']['result'])) {

app/base/interfaces/AI/AIModelInterface.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,29 @@ interface AIModelInterface
2323
public static function getCode() : string;
2424
public static function getName() : string;
2525
public static function isEnabled() : bool;
26+
2627
public function getCompletionsEndpoint(?string $model = null) : string;
2728
public function getEmbeddingsEndpoint(?string $model = null) : string;
28-
public function prepareRequest(array $payload) : array;
29+
2930
public function formatUserMessage(string $prompt): array;
3031
public function formatAssistantMessage(mixed $message, ?string $messageType = null): array;
3132
public function formatAssistantFunctionCallMessage(string $functionName, array $args, ?string $id = null): ?array;
33+
3234
public function buildConversation(array $previousMessages, string $prompt, ?string $model = null): array;
3335
public function buildEmbeddingRequest(string $input, ?string $model = null): array;
34-
public function sendRaw(array $payload, ?string $model = null, string $endpoint = self::COMPLETIONS_ENDPOINT) : array;
35-
public function ask(string $prompt, ?string $model = null, ?array $previousMessages = null) : string;
36-
public function normalizeResponse(array $raw) : array;
36+
37+
public function normalizeCompletionsResponse(array $raw) : array;
38+
public function normalizeEmbeddingsResponse(array $raw) : array;
39+
3740
public function sendFunctionResponse(string $functionName, array $result, array &$history = [], ?string $id = null): array;
3841
public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, ?string $model = null): array;
42+
43+
public function prepareRequest(array $payload) : array;
44+
public function sendRaw(array $payload, ?string $model = null, string $endpoint = self::COMPLETIONS_ENDPOINT) : array;
45+
46+
public function ask(string $prompt, ?string $model = null, ?array $previousMessages = null) : string;
47+
public function embed(string $input, ?string $model = null) : array;
48+
3949
public function getAvailableModels(bool $reset = false) : array;
4050
public function getVersion() : string;
4151
public function getModel(?string $model = null) : string;

0 commit comments

Comments
 (0)