-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstructured-output.ts
More file actions
31 lines (28 loc) · 912 Bytes
/
structured-output.ts
File metadata and controls
31 lines (28 loc) · 912 Bytes
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
import {
type DurableContext,
withDurableExecution,
} from "@aws/durable-execution-sdk-js";
import { z } from "zod";
import { MODEL } from "./utils/constants.js";
import { converse } from "./utils/converse.js";
const ExtractedContact = z.object({
name: z.string(),
email: z.string(),
company: z.string(),
});
async function extractContact(text: string) {
const raw = await converse(
MODEL.SMALL,
`Extract contact info as JSON with keys "name", "email", "company": ${text}`,
);
const match = raw.match(/\{[^}]+\}/);
if (!match) throw new Error("No JSON found in response");
return ExtractedContact.parse(JSON.parse(match[0]));
}
export const handler = withDurableExecution(
async (event: { text?: string }, context: DurableContext) => {
const text =
event.text ?? "John Smith from Acme Corp, email: john@acme.com";
return await context.step("extract", () => extractContact(text));
},
);