-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathremote-agents.ts
More file actions
82 lines (74 loc) · 2.39 KB
/
Copy pathremote-agents.ts
File metadata and controls
82 lines (74 loc) · 2.39 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
81
82
/**
* 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 { ModelMessage } from "ai";
import { Context } from "@restatedev/restate-sdk";
import {
createTools,
zodPrompt,
billingAgent,
accountAgent,
productAgent,
} from "./utils/utils";
import llmCall from "./utils/llm";
const examplePrompt =
"I can't log into my account. Keep getting invalid password errors.";
// <start_here>
// Define your agents as tools as your AI SDK requires (here Vercel AI SDK)
const SPECIALISTS = {
BillingAgent: { description: "Expert in payments, charges, and refunds" },
AccountAgent: { description: "Expert in login issues and security" },
ProductAgent: { description: "Expert 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 messages: ModelMessage[] = [
{
role: "system",
content:
"You are a routing agent. Route the question to a specialist or respond directly if no specialist is needed.",
},
{ role: "user", content: message },
];
const routingDecision = await ctx.run(
"Pick specialist",
// Use your preferred LLM SDK here - specify agents as tools
async () => llmCall(messages, 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. Call the specialist over HTTP
return ctx.genericCall<string, string>({
service: specialist,
method: "run",
parameter: message,
inputSerde: restate.serde.json,
outputSerde: restate.serde.json,
});
}
// <end_here>
const remoteAgents = restate.service({
name: "RemoteAgentRouter",
handlers: {
answer: restate.createServiceHandler(
{ input: zodPrompt(examplePrompt) },
answer,
),
},
});
restate.serve({
services: [remoteAgents, billingAgent, accountAgent, productAgent],
port: 9080,
});