2424abstract 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}
0 commit comments