@@ -47,23 +47,23 @@ public function formatPayload(array $rawPayload): array
47
47
'contents ' => $ formattedMessages ,
48
48
'stream ' => $ rawPayload ['stream ' ] && $ this ->supportsStreaming ($ modelId ),
49
49
];
50
-
50
+
51
51
// Set complete optional fields with content (default values if not present in $rawPayload)
52
52
$ payload ['safetySettings ' ] = $ rawPayload ['safetySettings ' ] ?? [
53
53
[
54
54
'category ' => 'HARM_CATEGORY_DANGEROUS_CONTENT ' ,
55
55
'threshold ' => 'BLOCK_ONLY_HIGH '
56
56
]
57
57
];
58
-
58
+
59
59
$ payload ['generationConfig ' ] = $ rawPayload ['generationConfig ' ] ?? [
60
60
// 'stopSequences' => ["Title"],
61
61
'temperature ' => 1.0 ,
62
- 'maxOutputTokens ' => 800 ,
62
+ 'maxOutputTokens ' => 3000 ,
63
63
'topP ' => 0.8 ,
64
64
'topK ' => 10
65
65
];
66
-
66
+
67
67
// Google Search only works with gemini >= 2.0
68
68
// Search tool is context sensitive, this means the llm decides if a search is necessary for an answer
69
69
if ($ this ->config ['allow_search ' ] && $ this ->getModelDetails ($ modelId )['search_tool ' ]){
@@ -75,7 +75,7 @@ public function formatPayload(array $rawPayload): array
75
75
}
76
76
return $ payload ;
77
77
}
78
-
78
+
79
79
/**
80
80
* Format the complete response from Google
81
81
*
@@ -85,7 +85,7 @@ public function formatPayload(array $rawPayload): array
85
85
public function formatResponse ($ response ): array
86
86
{
87
87
$ responseContent = $ response ->getContent ();
88
- $ jsonContent = json_decode ($ responseContent , true );
88
+ $ jsonContent = json_decode ($ responseContent , true );
89
89
90
90
$ content = $ jsonContent ['candidates ' ][0 ]['content ' ]['parts ' ][0 ]['text ' ] ?? '' ;
91
91
$ groundingMetadata = $ jsonContent ['candidates ' ][0 ]['groundingMetadata ' ] ?? '' ;
@@ -98,7 +98,7 @@ public function formatResponse($response): array
98
98
'usage ' => $ this ->extractUsage ($ jsonContent )
99
99
];
100
100
}
101
-
101
+
102
102
/**
103
103
* Format a single chunk from a streaming response from Google
104
104
*
@@ -109,12 +109,12 @@ public function formatStreamChunk(string $chunk): array
109
109
{
110
110
111
111
$ jsonChunk = json_decode ($ chunk , true );
112
-
112
+
113
113
$ content = '' ;
114
114
$ groundingMetadata = '' ;
115
115
$ isDone = false ;
116
116
$ usage = null ;
117
-
117
+
118
118
// Extract content if available
119
119
if (isset ($ jsonChunk ['candidates ' ][0 ]['content ' ]['parts ' ][0 ]['text ' ])) {
120
120
$ content = $ jsonChunk ['candidates ' ][0 ]['content ' ]['parts ' ][0 ]['text ' ];
@@ -124,18 +124,18 @@ public function formatStreamChunk(string $chunk): array
124
124
if (isset ($ jsonChunk ['candidates ' ][0 ]['groundingMetadata ' ])) {
125
125
$ groundingMetadata = $ jsonChunk ['candidates ' ][0 ]['groundingMetadata ' ];
126
126
}
127
-
127
+
128
128
// Check for completion
129
- if (isset ($ jsonChunk ['candidates ' ][0 ]['finishReason ' ]) &&
129
+ if (isset ($ jsonChunk ['candidates ' ][0 ]['finishReason ' ]) &&
130
130
$ jsonChunk ['candidates ' ][0 ]['finishReason ' ] !== 'FINISH_REASON_UNSPECIFIED ' ) {
131
131
$ isDone = true ;
132
132
}
133
-
133
+
134
134
// Extract usage if available
135
135
if (isset ($ jsonChunk ['usageMetadata ' ])) {
136
136
$ usage = $ this ->extractUsage ($ jsonChunk );
137
137
}
138
-
138
+
139
139
return [
140
140
'content ' => [
141
141
'text ' => $ content ,
@@ -145,7 +145,7 @@ public function formatStreamChunk(string $chunk): array
145
145
'usage ' => $ usage
146
146
];
147
147
}
148
-
148
+
149
149
/**
150
150
* Extract usage information from Google response
151
151
*
@@ -166,7 +166,7 @@ protected function extractUsage(array $data): ?array
166
166
}
167
167
return null ;
168
168
}
169
-
169
+
170
170
/**
171
171
* Override common HTTP headers for Google API requests without Authorization header
172
172
*
@@ -192,7 +192,7 @@ public function makeNonStreamingRequest(array $payload)
192
192
{
193
193
// Ensure stream is set to false
194
194
$ payload ['stream ' ] = false ;
195
-
195
+
196
196
// Construct the URL with API key
197
197
$ url = $ this ->config ['api_url ' ] . $ payload ['model ' ] . ':generateContent?key= ' . $ this ->config ['api_key ' ];
198
198
// Extract just the necessary parts for Google's API
@@ -211,29 +211,29 @@ public function makeNonStreamingRequest(array $payload)
211
211
if (isset ($ payload ['tools ' ])) {
212
212
$ requestPayload ['tools ' ] = $ payload ['tools ' ];
213
213
}
214
-
214
+
215
215
// Initialize cURL
216
216
$ ch = curl_init ();
217
217
curl_setopt ($ ch , CURLOPT_URL , $ url );
218
-
218
+
219
219
// Set common cURL options
220
220
$ this ->setCommonCurlOptions ($ ch , $ requestPayload , $ this ->getHttpHeaders ());
221
-
221
+
222
222
// Execute the request
223
223
$ response = curl_exec ($ ch );
224
-
224
+
225
225
// Handle errors
226
226
if (curl_errno ($ ch )) {
227
227
$ error = 'Error: ' . curl_error ($ ch );
228
228
curl_close ($ ch );
229
229
return response ()->json (['error ' => $ error ], 500 );
230
230
}
231
-
231
+
232
232
curl_close ($ ch );
233
-
233
+
234
234
return response ($ response )->header ('Content-Type ' , 'application/json ' );
235
235
}
236
-
236
+
237
237
/**
238
238
* Make a streaming request to the OpenAI API
239
239
*
@@ -251,7 +251,7 @@ public function makeStreamingRequest(array $payload, callable $streamCallback)
251
251
'system_instruction ' => $ payload ['system_instruction ' ],
252
252
'contents ' => $ payload ['contents ' ]
253
253
];
254
-
254
+
255
255
// Add aditional config parameters if present
256
256
if (isset ($ payload ['safetySettings ' ])) {
257
257
$ requestPayload ['safetySettings ' ] = $ payload ['safetySettings ' ];
@@ -264,30 +264,30 @@ public function makeStreamingRequest(array $payload, callable $streamCallback)
264
264
}
265
265
266
266
set_time_limit (120 );
267
-
267
+
268
268
// Set headers for SSE
269
269
header ('Content-Type: text/event-stream ' );
270
270
header ('Cache-Control: no-cache ' );
271
271
header ('Connection: keep-alive ' );
272
272
header ('Access-Control-Allow-Origin: * ' );
273
-
273
+
274
274
// Initialize cURL
275
275
$ ch = curl_init ();
276
276
curl_setopt ($ ch , CURLOPT_URL , $ url );
277
-
277
+
278
278
// Set common cURL options
279
279
$ this ->setCommonCurlOptions ($ ch , $ requestPayload , $ this ->getHttpHeaders (true ));
280
-
280
+
281
281
// Set streaming-specific options
282
282
$ this ->setStreamingCurlOptions ($ ch , $ streamCallback );
283
-
283
+
284
284
// Log the full curl command (simulated)
285
285
$ httpHeaders = $ this ->getHttpHeaders (true );
286
286
$ headerString = '' ;
287
287
foreach ($ httpHeaders as $ header ) {
288
288
$ headerString .= "-H ' " . $ header . "' " ;
289
289
}
290
- $ command = "curl -X POST ' " . $ url . "' " . $ headerString . "-d ' " . json_encode ($ requestPayload ) . "' " ;
290
+ $ command = "curl -X POST ' " . $ url . "' " . $ headerString . "-d ' " . json_encode ($ requestPayload ) . "' " ;
291
291
292
292
// Execute the cURL session
293
293
curl_exec ($ ch );
@@ -300,13 +300,13 @@ public function makeStreamingRequest(array $payload, callable $streamCallback)
300
300
}
301
301
flush ();
302
302
}
303
-
303
+
304
304
curl_close ($ ch );
305
-
305
+
306
306
// Flush any remaining data
307
307
if (ob_get_length ()) {
308
308
ob_flush ();
309
309
}
310
310
flush ();
311
311
}
312
- }
312
+ }
0 commit comments