Skip to content

Commit 0509c34

Browse files
lgrammelegoist
andauthored
fix (provider/google): add name/content details to tool responses (#3599)
Co-authored-by: EGOIST <[email protected]>
1 parent 03d951f commit 0509c34

File tree

4 files changed

+66
-9
lines changed

4 files changed

+66
-9
lines changed

.changeset/yellow-books-develop.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/google': patch
3+
---
4+
5+
fix (provider/google): add name/content details to tool responses

examples/ai-core/src/stream-text/google-chatbot.ts

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { google } from '@ai-sdk/google';
2-
import { CoreMessage, streamText } from 'ai';
2+
import { CoreMessage, streamText, tool } from 'ai';
33
import 'dotenv/config';
44
import * as readline from 'node:readline/promises';
5+
import { z } from 'zod';
56

67
const terminal = readline.createInterface({
78
input: process.stdin,
@@ -12,25 +13,35 @@ const messages: CoreMessage[] = [];
1213

1314
async function main() {
1415
while (true) {
15-
const userInput = await terminal.question('You: ');
16-
17-
messages.push({ role: 'user', content: userInput });
16+
messages.push({ role: 'user', content: await terminal.question('You: ') });
1817

1918
const result = await streamText({
2019
model: google('gemini-1.5-pro-latest'),
21-
system: `You are a helpful, respectful and honest assistant.`,
20+
tools: {
21+
weather: tool({
22+
description: 'Get the weather in a location',
23+
parameters: z.object({
24+
location: z
25+
.string()
26+
.describe('The location to get the weather for'),
27+
}),
28+
execute: async ({ location }) => ({
29+
location,
30+
temperature: 72 + Math.floor(Math.random() * 21) - 10,
31+
}),
32+
}),
33+
},
34+
maxSteps: 5,
2235
messages,
2336
});
2437

25-
let fullResponse = '';
2638
process.stdout.write('\nAssistant: ');
2739
for await (const delta of result.textStream) {
28-
fullResponse += delta;
2940
process.stdout.write(delta);
3041
}
3142
process.stdout.write('\n\n');
3243

33-
messages.push({ role: 'assistant', content: fullResponse });
44+
messages.push(...(await result.response).messages);
3445
}
3546
}
3647

packages/google/src/convert-to-google-generative-ai-messages.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,41 @@ describe('user messages', () => {
8383
});
8484
});
8585
});
86+
87+
describe('tool messages', () => {
88+
it('should convert tool result messages to function responses', async () => {
89+
const result = convertToGoogleGenerativeAIMessages([
90+
{
91+
role: 'tool',
92+
content: [
93+
{
94+
type: 'tool-result',
95+
toolName: 'testFunction',
96+
toolCallId: 'testCallId',
97+
result: { someData: 'test result' },
98+
},
99+
],
100+
},
101+
]);
102+
103+
expect(result).toEqual({
104+
systemInstruction: undefined,
105+
contents: [
106+
{
107+
role: 'user',
108+
parts: [
109+
{
110+
functionResponse: {
111+
name: 'testFunction',
112+
response: {
113+
name: 'testFunction',
114+
content: { someData: 'test result' },
115+
},
116+
},
117+
},
118+
],
119+
},
120+
],
121+
});
122+
});
123+
});

packages/google/src/convert-to-google-generative-ai-messages.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ export function convertToGoogleGenerativeAIMessages(
133133
parts: content.map(part => ({
134134
functionResponse: {
135135
name: part.toolName,
136-
response: part.result,
136+
response: {
137+
name: part.toolName,
138+
content: part.result,
139+
},
137140
},
138141
})),
139142
});

0 commit comments

Comments
 (0)