Skip to content

Commit f16c924

Browse files
authored
fix: converse tool calls (#9)
1 parent 45ffb1f commit f16c924

12 files changed

+117
-22
lines changed

src/Schemas/Converse/ConverseTextHandler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function handle(Request $request): TextResponse
6868
/**
6969
* @return array<string,mixed>
7070
*/
71-
public static function buildPayload(Request $request): array
71+
public static function buildPayload(Request $request, int $stepCount = 0): array
7272
{
7373
return array_filter([
7474
'inferenceConfig' => array_filter([
@@ -82,7 +82,7 @@ public static function buildPayload(Request $request): array
8282
? null
8383
: array_filter([
8484
'tools' => ToolMap::map($request->tools()),
85-
'toolChoice' => ToolChoiceMap::map($request->toolChoice()),
85+
'toolChoice' => $stepCount === 0 ? ToolChoiceMap::map($request->toolChoice()) : null,
8686
]),
8787
]);
8888
}
@@ -92,7 +92,7 @@ protected function sendRequest(Request $request): void
9292
try {
9393
$this->httpResponse = $this->client->post(
9494
'converse',
95-
static::buildPayload($request)
95+
static::buildPayload($request, $this->responseBuilder->steps->count())
9696
);
9797
} catch (Throwable $e) {
9898
throw PrismException::providerRequestError($request->model(), $e);

src/Schemas/Converse/Maps/MessageMap.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,17 @@ protected static function mapToolResultMessage(ToolResultMessage $message): arra
8686
{
8787
return [
8888
'role' => 'user',
89-
'content' => [
90-
[
91-
'toolResult' => array_map(fn (ToolResult $toolResult): array => [
92-
'status' => $toolResult->result !== null ? 'success' : 'error',
93-
'toolUseId' => $toolResult->toolCallId,
94-
'content' => $toolResult->result,
95-
], $message->toolResults),
89+
'content' => array_map(fn (ToolResult $toolResult): array => [
90+
'toolResult' => [
91+
'status' => $toolResult->result !== null ? 'success' : 'error',
92+
'toolUseId' => $toolResult->toolCallId,
93+
'content' => [
94+
[
95+
'text' => $toolResult->result,
96+
],
97+
],
9698
],
97-
],
99+
], $message->toolResults),
98100
];
99101
}
100102

@@ -125,11 +127,11 @@ protected static function mapAssistantMessage(AssistantMessage $message): array
125127

126128
return [
127129
'role' => 'assistant',
128-
'content' => array_filter([
129-
['text' => $message->content],
130+
'content' => array_values(array_filter([
131+
$message->content === '' || $message->content === '0' ? null : ['text' => $message->content],
130132
...self::mapToolCalls($message->toolCalls),
131133
$cacheType ? ['cachePoint' => ['type' => $cacheType]] : null,
132-
]),
134+
])),
133135
];
134136
}
135137

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"metrics":{"latencyMs":417},"output":{"message":{"content":[{"toolUse":{"input":{"city":"London UK"},"name":"weather","toolUseId":"tooluse_VqTXvNLSTYKBpzH2madWgg"}}],"role":"assistant"}},"stopReason":"tool_use","usage":{"inputTokens":469,"outputTokens":16,"totalTokens":485}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"metrics":{"latencyMs":433},"output":{"message":{"content":[{"text":"The weather in London, UK today will be 75°F and sunny. If you need any further information or assistance, feel free to ask!"}],"role":"assistant"}},"stopReason":"end_turn","usage":{"inputTokens":512,"outputTokens":31,"totalTokens":543}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"metrics":{"latencyMs":422},"output":{"message":{"content":[{"text":"<thinking> To find out the weather in Detroit today, I will use the \"weather\" tool with the city parameter set to \"Detroit\". </thinking>\n"},{"toolUse":{"input":{"city":"Detroit"},"name":"weather","toolUseId":"tooluse_fcGzeM2lSDeK8J2xouQtjQ"}}],"role":"assistant"}},"stopReason":"tool_use","usage":{"inputTokens":460,"outputTokens":60,"totalTokens":520}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"metrics":{"latencyMs":329},"output":{"message":{"content":[{"text":"The weather in Detroit today will be 75°F and sunny."}],"role":"assistant"}},"stopReason":"end_turn","usage":{"inputTokens":541,"outputTokens":15,"totalTokens":556}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"metrics":{"latencyMs":267},"output":{"message":{"content":[{"toolUse":{"input":{"city":"London, UK"},"name":"weather","toolUseId":"tooluse_wX6sIcn-TFGXVQ0JUh_B-A"}}],"role":"assistant"}},"stopReason":"tool_use","usage":{"inputTokens":469,"outputTokens":18,"totalTokens":487}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"metrics":{"latencyMs":816},"output":{"message":{"content":[{"text":"<thinking> The User is asking for the location of a Tigers game and the weather forecast. To answer this, I need to first determine the team they are referring to and then find the current weather for the city where the game is taking place. I will use the \"search\" tool to find the location of the game and then use the \"weather\" tool to find the weather forecast.</thinking>\n"},{"toolUse":{"input":{"query":"Tigers game location"},"name":"search","toolUseId":"tooluse_xt0bzTmBTmub9jKo81XH2Q"}}],"role":"assistant"}},"stopReason":"tool_use","usage":{"inputTokens":464,"outputTokens":115,"totalTokens":579}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"metrics":{"latencyMs":504},"output":{"message":{"content":[{"text":"<thinking> I have found the location of the Tigers game, which is in Detroit. Now, I need to find the current weather in Detroit to provide the User with the weather forecast.</thinking> "},{"toolUse":{"input":{"city":"Detroit"},"name":"weather","toolUseId":"tooluse_Q37MLijeSgyhtnyKziek7Q"}}],"role":"assistant"}},"stopReason":"tool_use","usage":{"inputTokens":598,"outputTokens":84,"totalTokens":682}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"metrics":{"latencyMs":532},"output":{"message":{"content":[{"text":"<thinking> I have found the current weather in Detroit. Now, I can provide the User with the information they requested.</thinking> \n\nThe Tigers game is taking place in Detroit at 3pm. The weather forecast for Detroit is 75°F and sunny."}],"role":"assistant"}},"stopReason":"end_turn","usage":{"inputTokens":687,"outputTokens":55,"totalTokens":742}}

0 commit comments

Comments
 (0)