Skip to content

Commit f9c218e

Browse files
committed
HotFix: Google Max Token Correction
1 parent 2dcd2fa commit f9c218e

File tree

4 files changed

+100
-99
lines changed

4 files changed

+100
-99
lines changed

_docker_production/model_providers.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
| This Model is used by default before the user choses their
1010
| desired model.
1111
*/
12-
'defaultModel' => 'gpt-4o',
12+
'defaultModel' => 'gpt-4.1',
13+
'defaultSearchModel' => 'gemini-2.0-flash',
1314

1415
/*
1516
|--------------------------------------------------------------------------
@@ -23,9 +24,9 @@
2324
|
2425
*/
2526
'system_models' => [
26-
'title_generator' => 'gpt-4o-mini',
27-
'prompt_improver' => 'gpt-4o-mini',
28-
'summarizer' => 'gpt-4o-mini',
27+
'title_generator' => 'gpt-4.1-nano',
28+
'prompt_improver' => 'gpt-4.1-nano',
29+
'summarizer' => 'gpt-4.1-nano',
2930
],
3031

3132
/*
@@ -48,13 +49,13 @@
4849
'ping_url' => 'https://api.openai.com/v1/models',
4950
'models' => [
5051
[
51-
'id' => 'gpt-4o',
52-
'label' => 'OpenAI GPT 4o',
52+
'id' => 'gpt-4.1',
53+
'label' => 'OpenAI GPT 4.1',
5354
'streamable' => true,
5455
],
5556
[
56-
'id' => 'gpt-4o-mini',
57-
'label' => 'OpenAI GPT 4o mini',
57+
'id' => 'gpt-4.1-nano',
58+
'label' => 'OpenAI GPT 4.1 Nano',
5859
'streamable' => true,
5960
],
6061
[
@@ -71,7 +72,7 @@
7172
'api_key' => '',
7273
'api_url' => 'https://chat-ai.academiccloud.de/v1/chat/completions',
7374
'ping_url' => 'https://chat-ai.academiccloud.de/v1/models',
74-
'status_check' => true,
75+
'status_check' => false,
7576

7677
'models' => [
7778
[
@@ -115,22 +116,25 @@
115116
'api_url' => 'https://generativelanguage.googleapis.com/v1beta/models/',
116117
'streaming_url' => 'https://generativelanguage.googleapis.com/v1beta/models/',
117118
'ping_url' => '',
119+
'allow_search' => true,
118120
'models' => [
119121
[
120-
'id' => 'gemini-1.5-flash',
121-
'label' => 'Google Gemini 1.5 Flash',
122-
'streamable' => false,
122+
'id' => 'gemini-2.0-flash',
123+
'label' => 'Google Gemini 2.0 Flash',
124+
'streamable' => true,
125+
'search_tool'=> true,
123126
],
124127
[
125128
'id' => 'gemini-2.0-flash-lite',
126129
'label' => 'Google Gemini 2.0 Flash Lite',
127130
'streamable' => true,
128-
'search_tool' => false,
131+
'search_tool'=> false,
129132
],
130133
[
131-
'id' => 'gemini-2.0-flash-exp',
132-
'label' => 'Google Gemini 2.0 Flash Exp',
133-
'streamable' => false,
134+
'id' => 'gemini-2.5-pro',
135+
'label' => 'Google Gemini 2.5 Pro',
136+
'streamable' => true,
137+
'search_tool'=> true,
134138
]
135139
]
136140
],

app/Services/AI/Providers/GoogleProvider.php

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,23 @@ public function formatPayload(array $rawPayload): array
4747
'contents' => $formattedMessages,
4848
'stream' => $rawPayload['stream'] && $this->supportsStreaming($modelId),
4949
];
50-
50+
5151
// Set complete optional fields with content (default values if not present in $rawPayload)
5252
$payload['safetySettings'] = $rawPayload['safetySettings'] ?? [
5353
[
5454
'category' => 'HARM_CATEGORY_DANGEROUS_CONTENT',
5555
'threshold' => 'BLOCK_ONLY_HIGH'
5656
]
5757
];
58-
58+
5959
$payload['generationConfig'] = $rawPayload['generationConfig'] ?? [
6060
// 'stopSequences' => ["Title"],
6161
'temperature' => 1.0,
62-
'maxOutputTokens' => 800,
62+
'maxOutputTokens' => 3000,
6363
'topP' => 0.8,
6464
'topK' => 10
6565
];
66-
66+
6767
// Google Search only works with gemini >= 2.0
6868
// Search tool is context sensitive, this means the llm decides if a search is necessary for an answer
6969
if ($this->config['allow_search'] && $this->getModelDetails($modelId)['search_tool']){
@@ -75,7 +75,7 @@ public function formatPayload(array $rawPayload): array
7575
}
7676
return $payload;
7777
}
78-
78+
7979
/**
8080
* Format the complete response from Google
8181
*
@@ -85,7 +85,7 @@ public function formatPayload(array $rawPayload): array
8585
public function formatResponse($response): array
8686
{
8787
$responseContent = $response->getContent();
88-
$jsonContent = json_decode($responseContent, true);
88+
$jsonContent = json_decode($responseContent, true);
8989

9090
$content = $jsonContent['candidates'][0]['content']['parts'][0]['text'] ?? '';
9191
$groundingMetadata = $jsonContent['candidates'][0]['groundingMetadata'] ?? '';
@@ -98,7 +98,7 @@ public function formatResponse($response): array
9898
'usage' => $this->extractUsage($jsonContent)
9999
];
100100
}
101-
101+
102102
/**
103103
* Format a single chunk from a streaming response from Google
104104
*
@@ -109,12 +109,12 @@ public function formatStreamChunk(string $chunk): array
109109
{
110110

111111
$jsonChunk = json_decode($chunk, true);
112-
112+
113113
$content = '';
114114
$groundingMetadata = '';
115115
$isDone = false;
116116
$usage = null;
117-
117+
118118
// Extract content if available
119119
if (isset($jsonChunk['candidates'][0]['content']['parts'][0]['text'])) {
120120
$content = $jsonChunk['candidates'][0]['content']['parts'][0]['text'];
@@ -124,18 +124,18 @@ public function formatStreamChunk(string $chunk): array
124124
if (isset($jsonChunk['candidates'][0]['groundingMetadata'])) {
125125
$groundingMetadata = $jsonChunk['candidates'][0]['groundingMetadata'];
126126
}
127-
127+
128128
// Check for completion
129-
if (isset($jsonChunk['candidates'][0]['finishReason']) &&
129+
if (isset($jsonChunk['candidates'][0]['finishReason']) &&
130130
$jsonChunk['candidates'][0]['finishReason'] !== 'FINISH_REASON_UNSPECIFIED') {
131131
$isDone = true;
132132
}
133-
133+
134134
// Extract usage if available
135135
if (isset($jsonChunk['usageMetadata'])) {
136136
$usage = $this->extractUsage($jsonChunk);
137137
}
138-
138+
139139
return [
140140
'content' => [
141141
'text' => $content,
@@ -145,7 +145,7 @@ public function formatStreamChunk(string $chunk): array
145145
'usage' => $usage
146146
];
147147
}
148-
148+
149149
/**
150150
* Extract usage information from Google response
151151
*
@@ -166,7 +166,7 @@ protected function extractUsage(array $data): ?array
166166
}
167167
return null;
168168
}
169-
169+
170170
/**
171171
* Override common HTTP headers for Google API requests without Authorization header
172172
*
@@ -192,7 +192,7 @@ public function makeNonStreamingRequest(array $payload)
192192
{
193193
// Ensure stream is set to false
194194
$payload['stream'] = false;
195-
195+
196196
// Construct the URL with API key
197197
$url = $this->config['api_url'] . $payload['model'] . ':generateContent?key=' . $this->config['api_key'];
198198
// Extract just the necessary parts for Google's API
@@ -211,29 +211,29 @@ public function makeNonStreamingRequest(array $payload)
211211
if (isset($payload['tools'])) {
212212
$requestPayload['tools'] = $payload['tools'];
213213
}
214-
214+
215215
// Initialize cURL
216216
$ch = curl_init();
217217
curl_setopt($ch, CURLOPT_URL, $url);
218-
218+
219219
// Set common cURL options
220220
$this->setCommonCurlOptions($ch, $requestPayload, $this->getHttpHeaders());
221-
221+
222222
// Execute the request
223223
$response = curl_exec($ch);
224-
224+
225225
// Handle errors
226226
if (curl_errno($ch)) {
227227
$error = 'Error: ' . curl_error($ch);
228228
curl_close($ch);
229229
return response()->json(['error' => $error], 500);
230230
}
231-
231+
232232
curl_close($ch);
233-
233+
234234
return response($response)->header('Content-Type', 'application/json');
235235
}
236-
236+
237237
/**
238238
* Make a streaming request to the OpenAI API
239239
*
@@ -251,7 +251,7 @@ public function makeStreamingRequest(array $payload, callable $streamCallback)
251251
'system_instruction' => $payload['system_instruction'],
252252
'contents' => $payload['contents']
253253
];
254-
254+
255255
// Add aditional config parameters if present
256256
if (isset($payload['safetySettings'])) {
257257
$requestPayload['safetySettings'] = $payload['safetySettings'];
@@ -264,30 +264,30 @@ public function makeStreamingRequest(array $payload, callable $streamCallback)
264264
}
265265

266266
set_time_limit(120);
267-
267+
268268
// Set headers for SSE
269269
header('Content-Type: text/event-stream');
270270
header('Cache-Control: no-cache');
271271
header('Connection: keep-alive');
272272
header('Access-Control-Allow-Origin: *');
273-
273+
274274
// Initialize cURL
275275
$ch = curl_init();
276276
curl_setopt($ch, CURLOPT_URL, $url);
277-
277+
278278
// Set common cURL options
279279
$this->setCommonCurlOptions($ch, $requestPayload, $this->getHttpHeaders(true));
280-
280+
281281
// Set streaming-specific options
282282
$this->setStreamingCurlOptions($ch, $streamCallback);
283-
283+
284284
// Log the full curl command (simulated)
285285
$httpHeaders = $this->getHttpHeaders(true);
286286
$headerString = '';
287287
foreach ($httpHeaders as $header) {
288288
$headerString .= "-H '" . $header . "' ";
289289
}
290-
$command = "curl -X POST '" . $url . "' " . $headerString . "-d '" . json_encode($requestPayload) . "'";
290+
$command = "curl -X POST '" . $url . "' " . $headerString . "-d '" . json_encode($requestPayload) . "'";
291291

292292
// Execute the cURL session
293293
curl_exec($ch);
@@ -300,13 +300,13 @@ public function makeStreamingRequest(array $payload, callable $streamCallback)
300300
}
301301
flush();
302302
}
303-
303+
304304
curl_close($ch);
305-
305+
306306
// Flush any remaining data
307307
if (ob_get_length()) {
308308
ob_flush();
309309
}
310310
flush();
311311
}
312-
}
312+
}

0 commit comments

Comments
 (0)