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

chat.md

94 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: [packages/typescript/ai/src/activities/chat/index.ts:2115](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/index.ts#L2115)
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* [`SchemaInput`](../type-aliases/SchemaInput.md) \| `undefined` = `undefined`
31​
32### TStream
33​
34`TStream` *extends* `boolean` = `boolean`
35​
36## Parameters
37​
38### options
39​
40`TextActivityOptions`\<`TAdapter`, `TSchema`, `TStream`\>
41​
42## Returns
43​
44`TextActivityResult`\<`TSchema`, `TStream`\>
45​
46## Examples
47​
48```ts
49import { chat } from '@tanstack/ai'
50import { openaiText } from '@tanstack/ai-openai'
51​
52for await (const chunk of chat({
53 adapter: openaiText('gpt-4o'),
54 messages: [{ role: 'user', content: 'What is the weather?' }],
55 tools: [weatherTool]
56})) {
57 if (chunk.type === 'TEXT_MESSAGE_CONTENT') {
58 console.log(chunk.delta)
59 }
61```
62​
63```ts
64for await (const chunk of chat({
65 adapter: openaiText('gpt-4o'),
66 messages: [{ role: 'user', content: 'Hello!' }]
67})) {
68 console.log(chunk)
70```
71​
72```ts
73const text = await chat({
74 adapter: openaiText('gpt-4o'),
75 messages: [{ role: 'user', content: 'Hello!' }],
76 stream: false
77})
78// text is a string with the full response
79```
80​
81```ts
82import { z } from 'zod'
83​
84const result = await chat({
85 adapter: openaiText('gpt-4o'),
86 messages: [{ role: 'user', content: 'Research and summarize the topic' }],
87 tools: [researchTool, analyzeTool],
88 outputSchema: z.object({
89 summary: z.string(),
90 keyPoints: z.array(z.string())
91 })
92})
93// result is { summary: string, keyPoints: string[] }
94```