@@ -332,7 +332,7 @@ public function getAdminQuotaInfo(): array {
332332 * @param string $model
333333 * @param int|null $maxTokens
334334 * @param array|null $extraParams
335- * @return array| string[]
335+ * @return string[]
336336 * @throws Exception
337337 */
338338 public function createCompletion (
@@ -389,6 +389,10 @@ public function createCompletion(
389389 $ completions = [];
390390
391391 foreach ($ response ['choices ' ] as $ choice ) {
392+ if (!isset ($ choice ['text ' ]) || !is_string ($ choice ['text ' ])) {
393+ $ this ->logger ->debug ('Text generation yielded empty or invalid response: ' . json_encode ($ choice ));
394+ continue ;
395+ }
392396 $ completions [] = $ choice ['text ' ];
393397 }
394398
@@ -408,7 +412,7 @@ public function createCompletion(
408412 * @param array|null $extraParams
409413 * @param string|null $toolMessage JSON string with role, content, tool_call_id
410414 * @param array|null $tools
411- * @return array<string, array<string>>
415+ * @return array{messages: array <string>, tool_calls: array<string>}
412416 * @throws Exception
413417 */
414418 public function createChatCompletion (
@@ -537,12 +541,19 @@ public function createChatCompletion(
537541 // fix the tool_calls format, make it like expected by the context_agent app
538542 $ choice ['message ' ]['tool_calls ' ] = array_map (static function ($ toolCall ) {
539543 $ toolCall ['function ' ]['id ' ] = $ toolCall ['id ' ];
540- $ toolCall ['function ' ]['args ' ] = json_decode ($ toolCall ['function ' ]['arguments ' ]);
544+ $ toolCall ['function ' ]['args ' ] = json_decode ($ toolCall ['function ' ]['arguments ' ]) ?: ( object )[] ;
541545 unset($ toolCall ['function ' ]['arguments ' ]);
542546 return $ toolCall ['function ' ];
543547 }, $ choice ['message ' ]['tool_calls ' ]);
544- $ completions ['tool_calls ' ][] = json_encode ($ choice ['message ' ]['tool_calls ' ]);
548+
549+ $ toolCalls = json_encode ($ choice ['message ' ]['tool_calls ' ]);
550+ if ($ toolCalls === false ) {
551+ $ this ->logger ->debug ('Tool calls JSON encoding error: ' . json_last_error_msg ());
552+ } else {
553+ $ completions ['tool_calls ' ][] = $ toolCalls ;
554+ }
545555 }
556+
546557 // always try to get a message
547558 if (isset ($ choice ['message ' ]['content ' ]) && is_string ($ choice ['message ' ]['content ' ])) {
548559 $ completions ['messages ' ][] = $ choice ['message ' ]['content ' ];
@@ -745,8 +756,8 @@ public function getExpTextProcessingTime(): int {
745756 * @return void
746757 */
747758 public function updateExpTextProcessingTime (int $ runtime ): void {
748- $ oldTime = $ this ->getExpTextProcessingTime ( );
749- $ newTime = (1 - Application::EXPECTED_RUNTIME_LOWPASS_FACTOR ) * $ oldTime + Application::EXPECTED_RUNTIME_LOWPASS_FACTOR * $ runtime ;
759+ $ oldTime = floatval ( $ this ->getExpImgProcessingTime () );
760+ $ newTime = (1.0 - Application::EXPECTED_RUNTIME_LOWPASS_FACTOR ) * $ oldTime + Application::EXPECTED_RUNTIME_LOWPASS_FACTOR * floatval ( $ runtime) ;
750761
751762 if ($ this ->isUsingOpenAi ()) {
752763 $ this ->appConfig ->setValueString (Application::APP_ID , 'openai_text_generation_time ' , strval (intval ($ newTime )));
@@ -769,8 +780,8 @@ public function getExpImgProcessingTime(): int {
769780 * @return void
770781 */
771782 public function updateExpImgProcessingTime (int $ runtime ): void {
772- $ oldTime = $ this ->getExpImgProcessingTime ();
773- $ newTime = (1 - Application::EXPECTED_RUNTIME_LOWPASS_FACTOR ) * $ oldTime + Application::EXPECTED_RUNTIME_LOWPASS_FACTOR * $ runtime ;
783+ $ oldTime = floatval ( $ this ->getExpImgProcessingTime () );
784+ $ newTime = (1.0 - Application::EXPECTED_RUNTIME_LOWPASS_FACTOR ) * $ oldTime + Application::EXPECTED_RUNTIME_LOWPASS_FACTOR * floatval ( $ runtime) ;
774785
775786 if ($ this ->isUsingOpenAi ()) {
776787 $ this ->appConfig ->setValueString (Application::APP_ID , 'openai_image_generation_time ' , strval (intval ($ newTime )));
0 commit comments