Skip to content

Commit 0866892

Browse files
committed
added initial embedding support for later use
1 parent 2d2c2db commit 0866892

File tree

8 files changed

+119
-12
lines changed

8 files changed

+119
-12
lines changed

app/base/abstracts/Models/AbstractLLMAdapter.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,23 @@
2424
abstract class AbstractLLMAdapter extends ContainerAwareObject implements AIModelInterface
2525
{
2626

27-
public function sendRaw(array $payload) : array
27+
public function sendRaw(array $payload, ?string $model = null, string $endpoint = self::COMPLETIONS_ENDPOINT) : array
2828
{
2929
$client = $this->getGuzzle();
3030

31+
$endopointUrl = match ($endpoint) {
32+
self::COMPLETIONS_ENDPOINT => $this->getCompletionsEndpoint($model),
33+
self::EMBEDDINGS_ENDPOINT => $this->getEmbeddingsEndpoint($model),
34+
default => throw new Exception("Invalid endpoint type: " . $endpoint),
35+
};
3136
$prepared = $this->prepareRequest($payload);
3237

3338
if (App::getInstance()->getEnvironment()->canDebug()) {
34-
$this->getApplicationLogger()->debug("Sending LLM request <pre>" . json_encode(['endpoint' => $this->getEndpoint(), 'payload' => $prepared], JSON_PRETTY_PRINT) . "</pre>");
39+
$this->getApplicationLogger()->debug("Sending LLM request <pre>" . json_encode(['endpoint' => $endopointUrl, 'payload' => $prepared], JSON_PRETTY_PRINT) . "</pre>");
3540
}
3641

3742
$resp = $client->post(
38-
$this->getEndpoint(),
43+
$endopointUrl,
3944
$prepared
4045
);
4146

@@ -59,9 +64,22 @@ public function ask(string $prompt, ?string $model = null, ?array $previousMessa
5964
$prompt
6065
);
6166

62-
$raw = $this->sendRaw($contents);
67+
$raw = $this->sendRaw($contents, $model, self::COMPLETIONS_ENDPOINT);
6368
$norm = $this->normalizeResponse($raw);
6469

6570
return trim($norm['assistantText'] ?? '');
6671
}
72+
73+
public function embed(string $input, ?string $model = null) : array
74+
{
75+
$payload = $this->buildEmbeddingRequest($input);
76+
77+
$raw = $this->sendRaw($payload, $model, self::EMBEDDINGS_ENDPOINT);
78+
79+
if (!empty($raw['data'][0]['embedding']) && is_array($raw['data'][0]['embedding'])) {
80+
return $raw['data'][0]['embedding'];
81+
}
82+
83+
return [];
84+
}
6785
}

app/base/ai/Models/ChatGPT.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@ public static function isEnabled() : bool
4646
return !empty(App::getInstance()->getSiteData()->getConfigValue(self::CHATGPT_TOKEN_PATH));
4747
}
4848

49-
public function getEndpoint(?string $model = null) : string
49+
public function getCompletionsEndpoint(?string $model = null) : string
5050
{
5151
return "https://api.openai.com/" . $this->getVersion() . "/chat/completions";
5252
}
5353

54+
public function getEmbeddingsEndpoint(?string $model = null) : string
55+
{
56+
return "https://api.openai.com/" . $this->getVersion() . "/embeddings";
57+
}
58+
5459
public function prepareRequest(array $payload) : array
5560
{
5661
$apiKey = $this->getSiteData()->getConfigValue(self::CHATGPT_TOKEN_PATH);
@@ -211,6 +216,14 @@ public function ask(string $prompt, ?string $model = null, ?array $previousMessa
211216
return $generatedText;
212217
}
213218

219+
public function buildEmbeddingRequest(string $input, ?string $model = null): array
220+
{
221+
return [
222+
'model' => $this->getModel($model),
223+
'input' => $input
224+
];
225+
}
226+
214227
public function getAvailableModels(bool $reset = false) : array
215228
{
216229
$models_key = "ai.chatgpt.models_list";

app/base/ai/Models/Claude.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@ public static function isEnabled() : bool
4646
return !empty(App::getInstance()->getSiteData()->getConfigValue(self::CLAUDE_TOKEN_PATH));
4747
}
4848

49-
public function getEndpoint(?string $model = null) : string
49+
public function getCompletionsEndpoint(?string $model = null) : string
5050
{
5151
return "https://api.anthropic.com/" . $this->getVersion() . "/messages";
5252
}
5353

54+
public function getEmbeddingsEndpoint(?string $model = null) : string
55+
{
56+
return "https://api.anthropic.com/" . $this->getVersion() . "/embeddings";
57+
}
58+
5459
public function prepareRequest(array $payload) : array
5560
{
5661
$apiKey = $this->getSiteData()->getConfigValue(self::CLAUDE_TOKEN_PATH);
@@ -212,6 +217,14 @@ public function ask(string $prompt, ?string $model = null, ?array $previousMessa
212217
return $generatedText;
213218
}
214219

220+
public function buildEmbeddingRequest(string $input, ?string $model = null): array
221+
{
222+
return [
223+
'model' => $this->getModel($model),
224+
'input' => $input
225+
];
226+
}
227+
215228
public function getAvailableModels(bool $reset = false) : array
216229
{
217230
$models_key = "ai.claude.models_list";

app/base/ai/Models/GoogleGemini.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static function isEnabled() : bool
4444
return !empty(App::getInstance()->getSiteData()->getConfigValue(self::GEMINI_TOKEN_PATH));
4545
}
4646

47-
public function getEndpoint(?string $model = null) : string
47+
public function getCompletionsEndpoint(?string $model = null) : string
4848
{
4949
$apiKey = $this->getSiteData()->getConfigValue(self::GEMINI_TOKEN_PATH);
5050

@@ -55,6 +55,17 @@ public function getEndpoint(?string $model = null) : string
5555
return "https://generativelanguage.googleapis.com/" . $this->getVersion() . "/models/" . $this->getModel($model) . ":generateContent?key={$apiKey}";
5656
}
5757

58+
public function getEmbeddingsEndpoint(?string $model = null) : string
59+
{
60+
$apiKey = $this->getSiteData()->getConfigValue(self::GEMINI_TOKEN_PATH);
61+
62+
if (empty($apiKey)) {
63+
throw new Exception("Missing Gemini Token");
64+
}
65+
66+
return "https://generativelanguage.googleapis.com/" . $this->getVersion() . "/models/" . $this->getModel($model) . ":embedText?key={$apiKey}";
67+
}
68+
5869
public function prepareRequest(array $payload) : array
5970
{
6071
return [
@@ -224,6 +235,14 @@ public function sendFunctionResponse(string $name, array $result, array &$histor
224235
]);
225236
}
226237

238+
public function buildEmbeddingRequest(string $input, ?string $model = null): array
239+
{
240+
return [
241+
'model' => $this->getModel($model),
242+
'text' => $input
243+
];
244+
}
245+
227246
public function getAvailableModels(bool $reset = false) : array
228247
{
229248
$models_key = "ai.googlegemini.models_list";

app/base/ai/Models/Groq.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,16 @@ public static function isEnabled(): bool
4444
return !empty(App::getInstance()->getSiteData()->getConfigValue(self::GROQ_TOKEN_PATH));
4545
}
4646

47-
public function getEndpoint(?string $model = null) : string
47+
public function getCompletionsEndpoint(?string $model = null) : string
4848
{
4949
return "https://api.groq.com/openai/" . $this->getVersion() . "/chat/completions";
5050
}
5151

52+
public function getEmbeddingsEndpoint(?string $model = null) : string
53+
{
54+
return "https://api.groq.com/openai/" . $this->getVersion() . "/embeddings";
55+
}
56+
5257
public function prepareRequest(array $payload) : array
5358
{
5459
$apiKey = $this->getSiteData()->getConfigValue(self::GROQ_TOKEN_PATH);
@@ -170,6 +175,14 @@ public function sendFunctionResponse(string $functionName, array $result, array
170175
]);
171176
}
172177

178+
public function buildEmbeddingRequest(string $input, ?string $model = null): array
179+
{
180+
return [
181+
'model' => $this->getModel($model),
182+
'input' => $input
183+
];
184+
}
185+
173186
public function getAvailableModels(bool $reset = false): array
174187
{
175188
$cacheKey = "ai.groq.models_list";

app/base/ai/Models/Mistral.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@ public static function isEnabled() : bool
4646
return !empty(App::getInstance()->getSiteData()->getConfigValue(self::MISTRAL_TOKEN_PATH));
4747
}
4848

49-
public function getEndpoint(?string $model = null) : string
49+
public function getCompletionsEndpoint(?string $model = null) : string
5050
{
5151
return "https://api.mistral.ai/" . $this->getVersion() . "/chat/completions";
5252
}
5353

54+
public function getEmbeddingsEndpoint(?string $model = null) : string
55+
{
56+
return "https://api.mistral.ai/" . $this->getVersion() . "/embeddings";
57+
}
58+
5459
public function prepareRequest(array $payload) : array
5560
{
5661
$apiKey = $this->getSiteData()->getConfigValue(self::MISTRAL_TOKEN_PATH);
@@ -185,6 +190,14 @@ public function sendFunctionResponse(string $functionName, array $result, array
185190
]);
186191
}
187192

193+
public function buildEmbeddingRequest(string $input, ?string $model = null): array
194+
{
195+
return [
196+
'model' => $this->getModel($model),
197+
'input' => $input
198+
];
199+
}
200+
188201
public function getAvailableModels(bool $reset = false) : array
189202
{
190203
$models_key = "ai.mistral.models_list";

app/base/ai/Models/Perplexity.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,16 @@ public static function isEnabled() : bool
4242
return !empty(App::getInstance()->getSiteData()->getConfigValue(self::PERPLEXITY_TOKEN_PATH));
4343
}
4444

45-
public function getEndpoint(?string $model = null) : string
45+
public function getCompletionsEndpoint(?string $model = null) : string
4646
{
4747
return "https://api.perplexity.ai/chat/completions";
4848
}
4949

50+
public function getEmbeddingsEndpoint(?string $model = null) : string
51+
{
52+
return "https://api.perplexity.ai/embeddings";
53+
}
54+
5055
public function prepareRequest(array $payload) : array
5156
{
5257
$apiKey = $this->getSiteData()->getConfigValue(self::PERPLEXITY_TOKEN_PATH);
@@ -143,6 +148,14 @@ public function sendFunctionResponse(string $functionName, array $result, array
143148
]);
144149
}
145150

151+
public function buildEmbeddingRequest(string $input, ?string $model = null): array
152+
{
153+
return [
154+
'model' => $this->getModel($model),
155+
'input' => $input
156+
];
157+
}
158+
146159
public function getAvailableModels(bool $reset = false) : array
147160
{
148161
// perplexity has no endpoint for models list, currently

app/base/interfaces/AI/AIModelInterface.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,21 @@
1717

1818
interface AIModelInterface
1919
{
20+
public const COMPLETIONS_ENDPOINT = 'completions';
21+
public const EMBEDDINGS_ENDPOINT = 'embeddings';
22+
2023
public static function getCode() : string;
2124
public static function getName() : string;
2225
public static function isEnabled() : bool;
23-
public function getEndpoint(?string $model = null) : string;
26+
public function getCompletionsEndpoint(?string $model = null) : string;
27+
public function getEmbeddingsEndpoint(?string $model = null) : string;
2428
public function prepareRequest(array $payload) : array;
2529
public function formatUserMessage(string $prompt): array;
2630
public function formatAssistantMessage(mixed $message, ?string $messageType = null): array;
2731
public function formatAssistantFunctionCallMessage(string $functionName, array $args, ?string $id = null): ?array;
2832
public function buildConversation(array $previousMessages, string $prompt, ?string $model = null): array;
29-
public function sendRaw(array $payload) : array;
33+
public function buildEmbeddingRequest(string $input, ?string $model = null): array;
34+
public function sendRaw(array $payload, ?string $model = null, string $endpoint = self::COMPLETIONS_ENDPOINT) : array;
3035
public function ask(string $prompt, ?string $model = null, ?array $previousMessages = null) : string;
3136
public function normalizeResponse(array $raw) : array;
3237
public function sendFunctionResponse(string $functionName, array $result, array &$history = [], ?string $id = null): array;

0 commit comments

Comments
 (0)