|
| 1 | +/** |
| 2 | + * extract-recipe.ts - generateObject example with Zod schema |
| 3 | + * |
| 4 | + * Demonstrates structured data extraction using Zod schemas for type-safe output. |
| 5 | + * |
| 6 | + * Run from project root: |
| 7 | + * bun run docs/examples/typescript/extract-recipe.ts |
| 8 | + */ |
| 9 | + |
| 10 | +import { |
| 11 | + type KoineConfig, |
| 12 | + KoineError, |
| 13 | + generateObject, |
| 14 | +} from "@patternzones/koine-sdk"; |
| 15 | +import { z } from "zod"; |
| 16 | + |
| 17 | +// Bun automatically loads .env from current working directory |
| 18 | +const authKey = process.env.CLAUDE_CODE_GATEWAY_API_KEY; |
| 19 | +if (!authKey) { |
| 20 | + throw new Error("CLAUDE_CODE_GATEWAY_API_KEY is required in .env"); |
| 21 | +} |
| 22 | + |
| 23 | +const config: KoineConfig = { |
| 24 | + baseUrl: `http://localhost:${process.env.GATEWAY_PORT || "3100"}`, |
| 25 | + authKey, |
| 26 | + timeout: 300000, |
| 27 | +}; |
| 28 | + |
| 29 | +// Define the schema for a recipe |
| 30 | +const RecipeSchema = z.object({ |
| 31 | + name: z.string().describe("Name of the recipe"), |
| 32 | + ingredients: z.array(z.string()).describe("List of ingredients"), |
| 33 | + steps: z.array(z.string()).describe("Cooking instructions"), |
| 34 | + prepTime: z.number().describe("Preparation time in minutes"), |
| 35 | + cookTime: z.number().describe("Cooking time in minutes"), |
| 36 | +}); |
| 37 | + |
| 38 | +async function main() { |
| 39 | + console.log("Extracting recipe from natural language...\n"); |
| 40 | + |
| 41 | + const result = await generateObject(config, { |
| 42 | + prompt: `Extract the recipe from this description: |
| 43 | +
|
| 44 | +Make classic pancakes by mixing 1 cup flour, 1 egg, 1 cup milk, and 2 tbsp melted butter. |
| 45 | +First combine the dry ingredients, then whisk in the wet ingredients until smooth. |
| 46 | +Heat a griddle and pour 1/4 cup batter per pancake. Cook until bubbles form, flip, |
| 47 | +and cook until golden. Takes about 5 minutes to prep and 15 minutes to cook.`, |
| 48 | + schema: RecipeSchema, |
| 49 | + }); |
| 50 | + |
| 51 | + console.log("Recipe extracted:"); |
| 52 | + console.log(JSON.stringify(result.object, null, 2)); |
| 53 | + console.log( |
| 54 | + `\nTokens used: ${result.usage.totalTokens} (input: ${result.usage.inputTokens}, output: ${result.usage.outputTokens})`, |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +main().catch((error) => { |
| 59 | + if (error instanceof KoineError) { |
| 60 | + console.error(`\nKoine Error [${error.code}]: ${error.message}`); |
| 61 | + if (error.code === "VALIDATION_ERROR") { |
| 62 | + console.error(" → The response didn't match the expected schema"); |
| 63 | + if (error.rawText) { |
| 64 | + console.error(` → Raw response: ${error.rawText}`); |
| 65 | + } |
| 66 | + } else if (error.code === "HTTP_ERROR" && error.message.includes("401")) { |
| 67 | + console.error(" → Check that CLAUDE_CODE_GATEWAY_API_KEY is correct"); |
| 68 | + } |
| 69 | + } else if (error?.cause?.code === "ECONNREFUSED") { |
| 70 | + console.error("\nConnection refused. Is the gateway running?"); |
| 71 | + console.error( |
| 72 | + " → Start it with: docker run -d --env-file .env -p 3100:3100 ghcr.io/pattern-zones-co/koine:latest", |
| 73 | + ); |
| 74 | + } else { |
| 75 | + console.error("\nUnexpected error:", error); |
| 76 | + } |
| 77 | + process.exit(1); |
| 78 | +}); |
0 commit comments