Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

chat.md

96 lines2.2 KB

1---
2id: chat
3title: chat
4---
5​
6# Function: chat()
7​
8```ts
9function chat<TAdapter, TSchema, TStream>(options): TextActivityResult<TSchema, TStream>;
10```
11​
12Defined in: [activities/chat/index.ts:969](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/index.ts#L969)
13​
14Text activity - handles agentic text generation, one-shot text generation, and agentic structured output.
15​
16This activity supports four modes:
171. **Streaming agentic text**: Stream responses with automatic tool execution
182. **Streaming one-shot text**: Simple streaming request/response without tools
193. **Non-streaming text**: Returns collected text as a string (stream: false)
204. **Agentic structured output**: Run tools, then return structured data
21​
22## Type Parameters
23​
24### TAdapter
25​
26`TAdapter` *extends* [`AnyTextAdapter`](../type-aliases/AnyTextAdapter.md)
27​
28### TSchema
29​
30`TSchema` *extends*
31 \| `ZodType`\<`unknown`, `unknown`, `$ZodTypeInternals`\<`unknown`, `unknown`\>\>
32 \| `undefined` = `undefined`
33​
34### TStream
35​
36`TStream` *extends* `boolean` = `true`
37​
38## Parameters
39​
40### options
41​
42`TextActivityOptions`\<`TAdapter`, `TSchema`, `TStream`\>
43​
44## Returns
45​
46`TextActivityResult`\<`TSchema`, `TStream`\>
47​
48## Examples
49​
50```ts
51import { chat } from '@tanstack/ai'
52import { openaiText } from '@tanstack/ai-openai'
53​
54for await (const chunk of chat({
55 adapter: openaiText('gpt-4o'),
56 messages: [{ role: 'user', content: 'What is the weather?' }],
57 tools: [weatherTool]
58})) {
59 if (chunk.type === 'content') {
60 console.log(chunk.delta)
61 }
63```
64​
65```ts
66for await (const chunk of chat({
67 adapter: openaiText('gpt-4o'),
68 messages: [{ role: 'user', content: 'Hello!' }]
69})) {
70 console.log(chunk)
72```
73​
74```ts
75const text = await chat({
76 adapter: openaiText('gpt-4o'),
77 messages: [{ role: 'user', content: 'Hello!' }],
78 stream: false
79})
80// text is a string with the full response
81```
82​
83```ts
84import { z } from 'zod'
85​
86const result = await chat({
87 adapter: openaiText('gpt-4o'),
88 messages: [{ role: 'user', content: 'Research and summarize the topic' }],
89 tools: [researchTool, analyzeTool],
90 outputSchema: z.object({
91 summary: z.string(),
92 keyPoints: z.array(z.string())
93 })
94})
95// result is { summary: string, keyPoints: string[] }
96```