|
1 | | -import 'dotenv-flow/config' |
2 | | -import { generateText, tool, streamText } from 'ai'; |
3 | | -import { langtail } from '../src/vercel-ai'; |
4 | | -import fs from 'fs/promises'; |
5 | | -import { z } from 'zod'; |
6 | | - |
| 1 | +import "dotenv-flow/config" |
| 2 | +import { stepCountIs, streamText, tool } from "ai" |
| 3 | +import { langtail } from "../src/vercel-ai" |
| 4 | +import { z } from "zod/v4" |
7 | 5 |
|
8 | 6 | async function main() { |
9 | | - const content = await fs.readFile('./playground/text.txt', 'utf-8'); |
10 | | - |
11 | | - const { text, reasoning } = await generateText({ |
12 | | - model: langtail('vtip'), |
| 7 | + const result = streamText({ |
| 8 | + model: langtail("vtip"), |
13 | 9 | messages: [ |
14 | 10 | { |
15 | | - role: 'user', |
| 11 | + role: "user", |
16 | 12 | content: "What is the weather in Tokyo?", |
17 | 13 | }, |
18 | 14 | ], |
| 15 | + stopWhen: stepCountIs(5), |
| 16 | + onStepFinish: (step) => { |
| 17 | + console.log(step.content) |
| 18 | + }, |
19 | 19 | tools: { |
20 | 20 | weather: tool({ |
21 | | - description: 'Get the weather in a location', |
22 | | - parameters: z.object({ |
23 | | - location: z.string().describe('The location to get the weather for'), |
| 21 | + description: "Get the weather in a location", |
| 22 | + inputSchema: z.object({ |
| 23 | + location: z.string().describe("The location to get the weather for"), |
24 | 24 | }), |
25 | | - execute: async ({ location }) => { |
| 25 | + async execute({ location }) { |
26 | 26 | return { |
27 | | - location, |
28 | | - temperature: 12, |
| 27 | + type: "image", |
| 28 | + data: "https://stickerapp.co.uk/cdn-assets/images/stickers/608t.png", |
29 | 29 | } |
30 | 30 | }, |
31 | | - }), |
32 | | - }, |
33 | | - }); |
34 | 31 |
|
35 | | - const { text: text2, reasoning: reasoning2 } = await generateText({ |
36 | | - model: langtail('vtip'), |
37 | | - providerOptions: { |
38 | | - anthropic: { |
39 | | - thinking: { |
40 | | - budgetTokens: 1025, |
41 | | - type: "enabled", |
42 | | - } |
43 | | - } |
44 | | - }, |
45 | | - messages: [ |
46 | | - { |
47 | | - role: 'user', |
48 | | - content: "What is the weather in Tokyo?", |
49 | | - }, |
50 | | - { |
51 | | - role: 'assistant', |
52 | | - content: text, |
53 | | - }, |
54 | | - { |
55 | | - role: 'user', |
56 | | - content: "What is the weather in Prague?", |
57 | | - }, |
58 | | - ], |
59 | | - tools: { |
60 | | - weather: tool({ |
61 | | - description: 'Get the weather in a location', |
62 | | - parameters: z.object({ |
63 | | - location: z.string().describe('The location to get the weather for'), |
64 | | - }), |
65 | | - execute: async ({ location }) => { |
| 32 | + // map to tool result content for LLM consumption: |
| 33 | + toModelOutput(result) { |
66 | 34 | return { |
67 | | - location, |
68 | | - temperature: 12, |
| 35 | + type: "content", |
| 36 | + value: |
| 37 | + typeof result === "string" |
| 38 | + ? [{ type: "text", text: result }] |
| 39 | + : [ |
| 40 | + { |
| 41 | + type: "media", |
| 42 | + data: result.data, |
| 43 | + mediaType: "image/png", |
| 44 | + }, |
| 45 | + ], |
69 | 46 | } |
70 | 47 | }, |
71 | 48 | }), |
72 | 49 | }, |
73 | | - }); |
74 | | - |
75 | | - console.log(text); |
76 | | - console.log(reasoning); |
77 | | - |
78 | | - console.log('--------------------------------'); |
79 | | - |
80 | | - console.log(text2); |
81 | | - console.log(reasoning2); |
| 50 | + }) |
82 | 51 |
|
| 52 | + for await (const chunk of result.fullStream) { |
| 53 | + console.log("chunk", chunk) |
| 54 | + } |
83 | 55 | } |
84 | 56 |
|
85 | | -main(); |
86 | | - |
| 57 | +main() |
0 commit comments