Skip to content

Commit

Permalink
format tool_calls, fix tool message insertion in history
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Dec 17, 2024
1 parent f3facc4 commit 4361c2f
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/Service/OpenAiAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public function createCompletion(
* @param int $n
* @param int|null $maxTokens
* @param array|null $extraParams
* @param string|null $toolMessage
* @param string|null $toolMessage JSON string with role, content, tool_call_id
* @param array|null $tools
* @return array<string, array<string>>
* @throws Exception
Expand Down Expand Up @@ -393,14 +393,30 @@ public function createChatCompletion(
if ($message['role'] === 'human') {
$message['role'] = 'user';
}
if (isset($message['tool_calls']) && is_array($message['tool_calls'])) {
$message['tool_calls'] = array_map(static function ($toolCall) {
$formattedToolCall = [
'id' => $toolCall['id'],
'type' => 'function',
'function' => $toolCall,
];
unset($formattedToolCall['function']['id']);
$formattedToolCall['function']['arguments'] = json_encode($toolCall['args']);
unset($formattedToolCall['function']['args']);
// unset($formattedToolCall['function']['type']);
return $formattedToolCall;
}, $message['tool_calls']);
}
$messages[] = $message;
}
}
if ($userPrompt !== null) {
$messages[] = ['role' => 'user', 'content' => $userPrompt];
}
if ($toolMessage !== null) {
$messages[] = ['role' => 'tool', 'content' => $toolMessage];
$msg = json_decode($toolMessage, true);
$msg['role'] = 'tool';
$messages[] = $msg;
}

$params = [
Expand Down

0 comments on commit 4361c2f

Please sign in to comment.