Skip to content

Commit ed71be8

Browse files
authored
feat(js/ai/chat): use predefined agent's models on multi-agent systems (#3777)
1 parent a28afe0 commit ed71be8

24 files changed

+709
-1
lines changed

js/ai/src/generate/action.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ function applyTransferPreamble(
221221
return rawRequest;
222222
}
223223

224+
// if the transfer preamble has a model, use it for the next request
225+
if (transferPreamble?.model) {
226+
rawRequest.model = transferPreamble.model;
227+
}
228+
224229
return stripUndefinedProps({
225230
...rawRequest,
226231
messages: [
@@ -402,8 +407,10 @@ async function generate(
402407

403408
let nextRequest = {
404409
...rawRequest,
410+
405411
messages: [...rawRequest.messages, generatedMessage.toJSON(), toolMessage!],
406412
};
413+
407414
nextRequest = applyTransferPreamble(nextRequest, transferPreamble);
408415

409416
// then recursively call for another loop

js/ai/src/generate/resolve-tool-requests.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ export async function resolveToolRequest(
101101

102102
// if it's a prompt action, go ahead and render the preamble
103103
if (isPromptAction(tool)) {
104-
const preamble = await tool(part.toolRequest.input);
104+
const metadata = tool.__action.metadata as Record<string, any>;
105+
const preamble = {
106+
...(await tool(part.toolRequest.input)),
107+
model: metadata.prompt?.model,
108+
};
105109
const response = {
106110
toolResponse: {
107111
name: part.toolRequest.name,

js/doc-snippets/src/multi-agent/multi.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const reservationListTool = reservationTool;
3131
// Define a prompt that represents a specialist agent
3232
const reservationAgent = ai.definePrompt({
3333
name: 'reservationAgent',
34+
model: 'googleai/gemini-2.5-flash',
3435
description: 'Reservation Agent can help manage guest reservations',
3536
tools: [reservationTool, reservationCancelationTool, reservationListTool],
3637
system: 'Help guests make and manage reservations',
@@ -43,6 +44,7 @@ const complaintAgent = ai.prompt('complaintAgent');
4344
// The triage agent is the agent that users interact with initially
4445
const triageAgent = ai.definePrompt({
4546
name: 'triageAgent',
47+
model: 'googleai/gemini-2.5-flash-lite',
4648
description: 'Triage Agent',
4749
tools: [reservationAgent, menuInfoAgent, complaintAgent],
4850
system: `You are an AI customer service agent for Pavel's Cafe.

js/pnpm-lock.yaml

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "multiagents-demo",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "lib/index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "node lib/index.js",
9+
"build": "tsc",
10+
"build:watch": "tsc --watch",
11+
"genkit:dev": "genkit start -- npx tsx --watch src/index.ts"
12+
},
13+
"keywords": [],
14+
"author": "",
15+
"license": "ISC",
16+
"dependencies": {
17+
"genkit": "workspace:*",
18+
"@genkit-ai/firebase": "workspace:*",
19+
"@genkit-ai/google-cloud": "workspace:*",
20+
"@genkit-ai/google-genai": "workspace:*",
21+
"express": "^4.20.0",
22+
"node-fetch": "3.3.2",
23+
"wav": "^1.0.2"
24+
},
25+
"devDependencies": {
26+
"@types/wav": "^1.0.4",
27+
"dotenv": "^16.4.5",
28+
"typescript": "^5.6.2"
29+
}
30+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { ai } from '../config/genkit';
18+
19+
import {
20+
fetchCatalogTool,
21+
fetchMostPopularItemsTool,
22+
getProductDetailsTool,
23+
searchCatalogTool,
24+
} from '../tools';
25+
26+
// Simple agent for browsing and searching products
27+
export const agent = ai.definePrompt({
28+
name: 'catalogAgent',
29+
description: 'Catalog Agent can help customers browse and search products',
30+
model: 'googleai/gemini-2.5-pro',
31+
tools: [
32+
fetchCatalogTool,
33+
fetchMostPopularItemsTool,
34+
searchCatalogTool,
35+
getProductDetailsTool,
36+
],
37+
system: `You are a helpful catalog agent for TechStore Computer Shop.
38+
Help customers browse products, search for items, and get product details.
39+
Be friendly and provide accurate information about products, prices, and availability.`,
40+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export { agent as catalogAgent } from './catalogAgent';
18+
export { agent as paymentAgent } from './paymentAgent';
19+
export { agent as representativeAgent } from './representativeAgent';
20+
export { agent as triageAgent } from './triageAgent';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { ai } from '../config/genkit';
18+
import { processPaymentTool } from '../tools';
19+
20+
// Simple agent for processing payments
21+
export const agent = ai.definePrompt({
22+
name: 'paymentAgent',
23+
model: 'googleai/gemini-2.5-flash-lite',
24+
description: 'Payment Agent can help process payments',
25+
tools: [processPaymentTool],
26+
system: `You are a payment agent for TechStore Computer Shop.
27+
Help customers process payments.
28+
Always confirm payment details before processing and provide clear transaction information.`,
29+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { ai } from '../config/genkit';
18+
import { getStoreInfoTool } from '../tools';
19+
20+
// Simple agent for providing store information
21+
export const agent = ai.definePrompt({
22+
name: 'representativeAgent',
23+
model: 'googleai/gemini-2.0-flash',
24+
description: 'Representative Agent can provide store info',
25+
tools: [getStoreInfoTool],
26+
system: `You are a customer service representative for TechStore Computer Shop.
27+
Help customers provide store information.`,
28+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { ai } from '../config/genkit';
18+
import { agent as catalogAgent } from './catalogAgent';
19+
import { agent as paymentAgent } from './paymentAgent';
20+
import { agent as representativeAgent } from './representativeAgent';
21+
22+
// The triage agent routes customers to the appropriate specialist
23+
export const agent = ai.definePrompt({
24+
name: 'triageAgent',
25+
description: 'Triage Agent',
26+
model: 'googleai/gemini-2.5-flash',
27+
tools: [catalogAgent, paymentAgent, representativeAgent],
28+
system: `You are an AI customer service agent for TechStore Computer Shop.
29+
Greet the user and ask them how you can help. Route them to the appropriate specialist agent:
30+
- Use catalogAgent for browsing products, searching items, or getting product information
31+
- Use paymentAgent for payment processing
32+
- Use representativeAgent for providing store information
33+
If you cannot help the customer with the available tools, politely explain so.`,
34+
});

0 commit comments

Comments
 (0)