From f3facc4c474dc9133f5c2e794461cd4f0d9a47ee Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Mon, 16 Dec 2024 18:10:00 +0100 Subject: [PATCH] adjustments for context_agent: format the tool_calls in chat response, expect a list of JSON object strings as history Signed-off-by: Julien Veyssier --- lib/Service/OpenAiAPIService.php | 28 +++++++++---------- .../TextToTextChatWithToolsProvider.php | 6 ++-- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/lib/Service/OpenAiAPIService.php b/lib/Service/OpenAiAPIService.php index 3e6c3a4..61fb514 100644 --- a/lib/Service/OpenAiAPIService.php +++ b/lib/Service/OpenAiAPIService.php @@ -389,21 +389,11 @@ public function createChatCompletion( } if ($history !== null) { foreach ($history as $i => $historyEntry) { - if (str_starts_with($historyEntry, 'system:')) { - $historyEntry = preg_replace('/^system:/', '', $historyEntry); - $messages[] = ['role' => 'system', 'content' => $historyEntry]; - } elseif (str_starts_with($historyEntry, 'user:')) { - $historyEntry = preg_replace('/^user:/', '', $historyEntry); - $messages[] = ['role' => 'user', 'content' => $historyEntry]; - } elseif (str_starts_with($historyEntry, 'tool:')) { - $historyEntry = preg_replace('/^tool:/', '', $historyEntry); - $messages[] = ['role' => 'tool', 'content' => $historyEntry]; - } elseif (((int)$i) % 2 === 0) { - // we assume even indexes are user messages and odd ones are system ones - $messages[] = ['role' => 'user', 'content' => $historyEntry]; - } else { - $messages[] = ['role' => 'system', 'content' => $historyEntry]; + $message = json_decode($historyEntry, true); + if ($message['role'] === 'human') { + $message['role'] = 'user'; } + $messages[] = $message; } } if ($userPrompt !== null) { @@ -459,7 +449,15 @@ public function createChatCompletion( // get tool calls only if this is the finish reason and it's defined and it's an array if ($choice['finish_reason'] === 'tool_calls' && isset($choice['message']['tool_calls']) - && is_array($choice['message']['tool_calls'])) { + && is_array($choice['message']['tool_calls']) + ) { + // fix the tool_calls format, make it like expected by the context_agent app + $choice['message']['tool_calls'] = array_map(static function ($toolCall) { + $toolCall['function']['id'] = $toolCall['id']; + $toolCall['function']['args'] = json_decode($toolCall['function']['arguments']); + unset($toolCall['function']['arguments']); + return $toolCall['function']; + }, $choice['message']['tool_calls']); $completions['tool_calls'][] = json_encode($choice['message']['tool_calls']); } // always try to get a message diff --git a/lib/TaskProcessing/TextToTextChatWithToolsProvider.php b/lib/TaskProcessing/TextToTextChatWithToolsProvider.php index e130616..0631cab 100644 --- a/lib/TaskProcessing/TextToTextChatWithToolsProvider.php +++ b/lib/TaskProcessing/TextToTextChatWithToolsProvider.php @@ -106,12 +106,12 @@ public function process(?string $userId, array $input, callable $reportProgress) if (!isset($input['tools']) || !is_string($input['tools'])) { throw new RuntimeException('Invalid tools'); } - $tools = json_decode($input['tools'], true); - if (!is_array($tools)) { + $tools = json_decode($input['tools']); + if (!is_array($tools) || !array_is_list($tools)) { throw new RuntimeException('Invalid JSON tools'); } - if (!isset($input['history']) || !is_array($input['history'])) { + if (!isset($input['history']) || !is_array($input['history']) || !array_is_list($input['history'])) { throw new RuntimeException('Invalid history'); } $history = $input['history'];