Skip to content

Commit

Permalink
adjustments for context_agent: format the tool_calls in chat response…
Browse files Browse the repository at this point in the history
…, expect a list of JSON object strings as history

Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Dec 16, 2024
1 parent 1b436d7 commit f3facc4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
28 changes: 13 additions & 15 deletions lib/Service/OpenAiAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/TaskProcessing/TextToTextChatWithToolsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {

Check failure on line 110 in lib/TaskProcessing/TextToTextChatWithToolsProvider.php

View workflow job for this annotation

GitHub Actions / Psalm check on PHP 8.0 and OCP dev-master

UndefinedFunction

lib/TaskProcessing/TextToTextChatWithToolsProvider.php:110:29: UndefinedFunction: Function OCA\OpenAi\TaskProcessing\array_is_list does not exist (see https://psalm.dev/021)
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'])) {

Check failure on line 114 in lib/TaskProcessing/TextToTextChatWithToolsProvider.php

View workflow job for this annotation

GitHub Actions / Psalm check on PHP 8.0 and OCP dev-master

UndefinedFunction

lib/TaskProcessing/TextToTextChatWithToolsProvider.php:114:69: UndefinedFunction: Function OCA\OpenAi\TaskProcessing\array_is_list does not exist (see https://psalm.dev/021)
throw new RuntimeException('Invalid history');
}
$history = $input['history'];
Expand Down

0 comments on commit f3facc4

Please sign in to comment.