|
4 | 4 |
|
5 | 5 | namespace App\Services;
|
6 | 6 |
|
7 |
| -use GuzzleHttp\Client; |
8 |
| -use GuzzleHttp\Exception\GuzzleException; |
9 |
| -use OpenAI; |
10 |
| -use function json_decode; |
| 7 | +use App\Services\LLM\Anthropic; |
| 8 | +use App\Services\LLM\CloudflareWorkersAI; |
| 9 | +use App\Services\LLM\GoogleAI; |
| 10 | +use App\Services\LLM\HuggingFace; |
| 11 | +use App\Services\LLM\OpenAI; |
| 12 | +use App\Services\LLM\VertexAI; |
11 | 13 |
|
12 | 14 | final class LLM
|
13 | 15 | {
|
14 |
| - /** |
15 |
| - * @throws GuzzleException |
16 |
| - */ |
17 |
| - public static function genTextResponse(string $q): string |
| 16 | + public static function getBackend(): VertexAI|CloudflareWorkersAI|GoogleAI|HuggingFace|OpenAI|Anthropic |
18 | 17 | {
|
19 |
| - if ($q === '') { |
20 |
| - return 'No question provided'; |
21 |
| - } |
22 |
| - |
23 | 18 | return match ($_ENV['llm_backend']) {
|
24 |
| - 'openai' => self::textPromptGPT($q), |
25 |
| - 'google-ai' => self::textPromptGoogleAI($q), |
26 |
| - 'vertex-ai' => self::textPromptVertexAI($q), |
27 |
| - 'huggingface' => self::textPromptHF($q), |
28 |
| - 'cf-workers-ai' => self::textPromptCF($q), |
29 |
| - default => 'No LLM backend configured', |
| 19 | + 'google-ai' => new GoogleAI(), |
| 20 | + 'vertex-ai' => new VertexAI(), |
| 21 | + 'huggingface' => new HuggingFace(), |
| 22 | + 'cf-workers-ai' => new CloudflareWorkersAI(), |
| 23 | + 'anthropic' => new Anthropic(), |
| 24 | + default => new OpenAI(), |
30 | 25 | };
|
31 | 26 | }
|
32 | 27 |
|
33 |
| - public static function textPromptGPT(string $q): string |
34 |
| - { |
35 |
| - if ($_ENV['openai_api_key'] === '') { |
36 |
| - return 'OpenAI API key not set'; |
37 |
| - } |
38 |
| - |
39 |
| - $client = OpenAI::client($_ENV['openai_api_key']); |
40 |
| - |
41 |
| - $response = $client->chat()->create([ |
42 |
| - 'model' => $_ENV['openai_model'], |
43 |
| - 'messages' => [ |
44 |
| - [ |
45 |
| - 'role' => 'user', |
46 |
| - 'content' => $q, |
47 |
| - ], |
48 |
| - ], |
49 |
| - ]); |
50 |
| - |
51 |
| - return $response->choices[0]->message->content; |
52 |
| - } |
53 |
| - |
54 |
| - /** |
55 |
| - * @throws GuzzleException |
56 |
| - */ |
57 |
| - public static function textPromptGoogleAI(string $q): string |
58 |
| - { |
59 |
| - if ($_ENV['google_ai_api_key'] === '') { |
60 |
| - return 'Google AI API key not set'; |
61 |
| - } |
62 |
| - |
63 |
| - $client = new Client(); |
64 |
| - |
65 |
| - $api_url = 'https://generativelanguage.googleapis.com/v1/models/' . |
66 |
| - $_ENV['google_ai_model_id'] . ':generateContent?key=' . $_ENV['google_ai_api_key']; |
67 |
| - |
68 |
| - $headers = [ |
69 |
| - 'Content-Type' => 'application/json', |
70 |
| - ]; |
71 |
| - |
72 |
| - $data = [ |
73 |
| - 'contents' => [ |
74 |
| - 'parts' => [ |
75 |
| - [ |
76 |
| - 'text' => $q, |
77 |
| - ], |
78 |
| - ], |
79 |
| - ], |
80 |
| - 'generationConfig' => [ |
81 |
| - 'temperature' => 1, |
82 |
| - 'topK' => 1, |
83 |
| - 'topP' => 1, |
84 |
| - 'candidateCount' => 1, |
85 |
| - 'maxOutputTokens' => 2048, |
86 |
| - 'stopSequences' => [], |
87 |
| - ], |
88 |
| - 'safetySettings' => [ |
89 |
| - [ |
90 |
| - 'category' => 'HARM_CATEGORY_HARASSMENT', |
91 |
| - 'threshold' => 'BLOCK_NONE', |
92 |
| - ], |
93 |
| - [ |
94 |
| - 'category' => 'HARM_CATEGORY_HATE_SPEECH', |
95 |
| - 'threshold' => 'BLOCK_NONE', |
96 |
| - ], |
97 |
| - [ |
98 |
| - 'category' => 'HARM_CATEGORY_SEXUALLY_EXPLICIT', |
99 |
| - 'threshold' => 'BLOCK_NONE', |
100 |
| - ], |
101 |
| - [ |
102 |
| - 'category' => 'HARM_CATEGORY_DANGEROUS_CONTENT', |
103 |
| - 'threshold' => 'BLOCK_NONE', |
104 |
| - ], |
105 |
| - ], |
106 |
| - ]; |
107 |
| - |
108 |
| - $response = json_decode($client->post($api_url, [ |
109 |
| - 'headers' => $headers, |
110 |
| - 'json' => $data, |
111 |
| - 'timeout' => 10, |
112 |
| - ])->getBody()->getContents()); |
113 |
| - |
114 |
| - return $response->candidates[0]->content->parts[0]->text; |
115 |
| - } |
116 |
| - |
117 |
| - /** |
118 |
| - * @throws GuzzleException |
119 |
| - */ |
120 |
| - public static function textPromptVertexAI(string $q): string |
| 28 | + public static function genTextResponse(string $q): string |
121 | 29 | {
|
122 |
| - if ($_ENV['vertex_ai_access_token'] === '') { |
123 |
| - return 'Vertex AI API key not set'; |
| 30 | + if ($q === '') { |
| 31 | + return 'No question provided'; |
124 | 32 | }
|
125 | 33 |
|
126 |
| - $client = new Client(); |
127 |
| - |
128 |
| - $api_url = 'https://' . $_ENV['vertex_ai_location'] .'-aiplatform.googleapis.com/v1/projects/' . |
129 |
| - $_ENV['vertex_ai_project_id'] . '/locations/' . $_ENV['vertex_ai_location'] . '/publishers/google/models/' . |
130 |
| - $_ENV['vertex_ai_model_id'] . ':streamGenerateContent'; |
131 |
| - |
132 |
| - $headers = [ |
133 |
| - 'Authorization' => 'Bearer ' . $_ENV['vertex_ai_access_token'], |
134 |
| - 'Content-Type' => 'application/json', |
135 |
| - ]; |
136 |
| - |
137 |
| - $data = [ |
138 |
| - 'contents' => [ |
139 |
| - 'parts' => [ |
140 |
| - [ |
141 |
| - 'text' => $q, |
142 |
| - ], |
143 |
| - ], |
144 |
| - ], |
145 |
| - 'generationConfig' => [ |
146 |
| - 'temperature' => 1, |
147 |
| - 'topK' => 1, |
148 |
| - 'topP' => 1, |
149 |
| - 'candidateCount' => 1, |
150 |
| - 'maxOutputTokens' => 2048, |
151 |
| - 'stopSequences' => [], |
152 |
| - ], |
153 |
| - 'safetySettings' => [ |
154 |
| - [ |
155 |
| - 'category' => 'HARM_CATEGORY_HARASSMENT', |
156 |
| - 'threshold' => 'BLOCK_NONE', |
157 |
| - ], |
158 |
| - [ |
159 |
| - 'category' => 'HARM_CATEGORY_HATE_SPEECH', |
160 |
| - 'threshold' => 'BLOCK_NONE', |
161 |
| - ], |
162 |
| - [ |
163 |
| - 'category' => 'HARM_CATEGORY_SEXUALLY_EXPLICIT', |
164 |
| - 'threshold' => 'BLOCK_NONE', |
165 |
| - ], |
166 |
| - [ |
167 |
| - 'category' => 'HARM_CATEGORY_DANGEROUS_CONTENT', |
168 |
| - 'threshold' => 'BLOCK_NONE', |
169 |
| - ], |
170 |
| - ], |
171 |
| - ]; |
172 |
| - |
173 |
| - $response = json_decode($client->post($api_url, [ |
174 |
| - 'headers' => $headers, |
175 |
| - 'json' => $data, |
176 |
| - 'timeout' => 10, |
177 |
| - ])->getBody()->getContents()); |
178 |
| - |
179 |
| - return $response->candidates[0]->content->parts[0]->text; |
| 34 | + return self::getClient()->textPrompt($q); |
180 | 35 | }
|
181 | 36 |
|
182 |
| - /** |
183 |
| - * @throws GuzzleException |
184 |
| - */ |
185 |
| - public static function textPromptHF(string $q): string |
| 37 | + public static function genTextResponseWithContext(string $q, array $context = []): string |
186 | 38 | {
|
187 |
| - if ($_ENV['huggingface_api_key'] === '' || $_ENV['huggingface_endpoint_url'] === '') { |
188 |
| - return 'Hugging Face API key or Endpoint URL not set'; |
| 39 | + if ($q === '') { |
| 40 | + return 'No question provided'; |
189 | 41 | }
|
190 | 42 |
|
191 |
| - $client = new Client(); |
192 |
| - |
193 |
| - $headers = [ |
194 |
| - 'Authorization' => 'Bearer ' . $_ENV['huggingface_api_key'], |
195 |
| - 'Content-Type' => 'application/json', |
196 |
| - ]; |
197 |
| - |
198 |
| - $data = [ |
199 |
| - 'inputs' => [ |
200 |
| - 'question' => $q, |
201 |
| - ], |
202 |
| - ]; |
203 |
| - |
204 |
| - $response = json_decode($client->post($_ENV['huggingface_endpoint_url'], [ |
205 |
| - 'headers' => $headers, |
206 |
| - 'json' => $data, |
207 |
| - 'timeout' => 10, |
208 |
| - ])->getBody()->getContents()); |
209 |
| - |
210 |
| - return $response->answer; |
211 |
| - } |
212 |
| - |
213 |
| - /** |
214 |
| - * @throws GuzzleException |
215 |
| - */ |
216 |
| - public static function textPromptCF(string $q): string |
217 |
| - { |
218 |
| - if ($_ENV['cf_workers_ai_account_id'] === '' || $_ENV['cf_workers_ai_api_token'] === '') { |
219 |
| - return 'Cloudflare Workers AI Account ID or API Token not set'; |
| 43 | + if ($context === []) { |
| 44 | + return self::getClient()->textPrompt($q); |
220 | 45 | }
|
221 | 46 |
|
222 |
| - $client = new Client(); |
223 |
| - |
224 |
| - $api_url = 'https://api.cloudflare.com/client/v4/accounts/' . |
225 |
| - $_ENV['cf_workers_ai_account_id'] . '/ai/run/' . $_ENV['cf_workers_ai_model_id']; |
226 |
| - |
227 |
| - $headers = [ |
228 |
| - 'Authorization' => 'Bearer ' . $_ENV['cf_workers_ai_api_token'], |
229 |
| - 'Content-Type' => 'application/json', |
230 |
| - ]; |
231 |
| - |
232 |
| - $data = [ |
233 |
| - 'prompt' => $q, |
234 |
| - ]; |
235 |
| - |
236 |
| - $response = json_decode($client->post($api_url, [ |
237 |
| - 'headers' => $headers, |
238 |
| - 'json' => $data, |
239 |
| - 'timeout' => 10, |
240 |
| - ])->getBody()->getContents()); |
241 |
| - |
242 |
| - return $response->result->response; |
| 47 | + return self::getClient()->textPromptWithContext($q, $context); |
243 | 48 | }
|
244 | 49 | }
|
0 commit comments