-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmulti-agent.ts
More file actions
75 lines (68 loc) · 2.33 KB
/
Copy pathmulti-agent.ts
File metadata and controls
75 lines (68 loc) · 2.33 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
import * as restate from "@restatedev/restate-sdk";
import { openai } from "@ai-sdk/openai";
import {generateText, LanguageModel, stepCountIs, tool, wrapLanguageModel} from "ai";
import {
ClaimInput,
ClaimInputSchema,
InsuranceClaim,
InsuranceClaimSchema,
} from "./utils/types";
import { eligibilityAgent, fraudCheckAgent } from "./utils/utils";
import { durableCalls } from "@restatedev/vercel-ai-middleware";
const schema = restate.serde.schema;
// <start_here>
async function runEligibilityAgent(model: LanguageModel, claim: InsuranceClaim){
const { text } = await generateText({
model,
system:
"Decide whether the following claim is eligible for reimbursement." +
"Respond with eligible if it's a medical claim, and not eligible otherwise.",
prompt: JSON.stringify(claim),
});
return text;
}
async function runFraudAgent(model: LanguageModel, claim: InsuranceClaim){
const { text } = await generateText({
model,
system:
"Decide whether the claim is fraudulent." +
"Always respond with low risk, medium risk, or high risk.",
prompt: JSON.stringify(claim),
});
return text;
}
const run = async (ctx: restate.Context, claim: ClaimInput) => {
const model = wrapLanguageModel({
model: openai("gpt-5.4"),
middleware: durableCalls(ctx, { maxRetryAttempts: 3 }),
});
const { text } = await generateText({
model,
prompt: `Claim: ${JSON.stringify(claim)}`,
system:
"Analyze the insurance claim and use your tools to decide whether to approve.",
tools: {
analyzeEligibility: tool({
description: "Analyze claim eligibility.",
inputSchema: InsuranceClaimSchema,
execute: async (claim: InsuranceClaim) => runEligibilityAgent(model, claim),
}),
analyzeFraud: tool({
description: "Analyze probability of fraud.",
inputSchema: InsuranceClaimSchema,
execute: async (claim: InsuranceClaim) => runFraudAgent(model, claim),
}),
},
stopWhen: [stepCountIs(10)],
providerOptions: { openai: { parallelToolCalls: false } },
});
return text;
};
// <end_here>
const agent = restate.service({
name: "MultiAgentClaimApproval",
handlers: {
run: restate.createServiceHandler({ input: schema(ClaimInputSchema) }, run),
},
});
restate.serve({ services: [agent, eligibilityAgent, fraudCheckAgent] });