-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmulti-agent.ts
More file actions
80 lines (70 loc) · 2.31 KB
/
Copy pathmulti-agent.ts
File metadata and controls
80 lines (70 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Agent Routing
*
* Route customer questions to specialized AI agents based on their content.
* Each routing decision is durable and can be retried if it fails.
*
* Flow: Customer Question → Classifier → Specialized Agent → Response
*/
import * as restate from "@restatedev/restate-sdk";
import { Context } from "@restatedev/restate-sdk";
import { createTools, zodPrompt } from "./utils/utils";
import llmCall from "./utils/llm";
const examplePrompt =
"I can't log into my account. Keep getting invalid password errors.";
// <start_here>
const SPECIALISTS = {
billingAgent: {
description: "Expert in payments, charges, and refunds",
prompt:
"You are a billing support agent specializing in payments, charges, and refunds.",
},
accountAgent: {
description: "Expert in login issues and security",
prompt:
"You are an account support agent specializing in login issues and security.",
},
productAgent: {
description: "Expert in features and how-to guides",
prompt:
"You are a product support agent specializing in features and how-to guides.",
},
} as const;
type Specialist = keyof typeof SPECIALISTS;
async function answer(ctx: Context, { message }: { message: string }) {
// 1. First, decide if a specialist is needed
const routingDecision = await ctx.run(
"Pick specialist",
// Use your preferred LLM SDK here - specify agents as tools
async () => llmCall(message, createTools(SPECIALISTS)),
{ maxRetryAttempts: 3 },
);
// 2. No specialist needed? Give a general answer
if (!routingDecision.toolCalls || routingDecision.toolCalls.length === 0) {
return routingDecision.text;
}
// 3. Get the specialist's name
const specialist = routingDecision.toolCalls[0].toolName as Specialist;
// 4. Ask the specialist to answer
const { text } = await ctx.run(
`Ask ${specialist}`,
async () =>
llmCall([
{ role: "user", content: message },
{ role: "system", content: SPECIALISTS[specialist].prompt },
]),
{ maxRetryAttempts: 3 },
);
return text;
}
// <end_here>
const multiAgent = restate.service({
name: "AgentRouter",
handlers: {
answer: restate.createServiceHandler(
{ input: zodPrompt(examplePrompt) },
answer,
),
},
});
restate.serve({ services: [multiAgent], port: 9080 });