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

chat.md

114 lines3.0 KB

1---
2id: chat
3title: chat
4---
5​
6# Function: chat()
7​
8```ts
9function chat<TAdapter, TSchema, TStream, TTools, TMiddleware>(options): TextActivityResult<TSchema, TStream>;
10```
11​
12Defined in: [packages/ai/src/activities/chat/index.ts:2794](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/index.ts#L2794)
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### TTools
37​
38`TTools` *extends*
39 \| (
40 \| `Omit`\<[`Tool`](../interfaces/Tool.md)\<`any`, `any`, `any`, `any`\>, `"execute"`\> & `object` & `object`
41 \| [`ProviderTool`](../interfaces/ProviderTool.md)\<`string`, `TAdapter`\[`"~types"`\]\[`"toolCapabilities"`\]\[`number`\]\>)[]
42 \| `undefined` =
43 \| (
44 \| `Omit`\<[`Tool`](../interfaces/Tool.md)\<`any`, `any`, `any`, `any`\>, `"execute"`\> & `object` & `object`
45 \| [`ProviderTool`](../interfaces/ProviderTool.md)\<`string`, `TAdapter`\[`"~types"`\]\[`"toolCapabilities"`\]\[`number`\]\>)[]
46 \| `undefined`
47​
48### TMiddleware
49​
50`TMiddleware` *extends*
51 \| [`ChatMiddleware`](../interfaces/ChatMiddleware.md)\<`any`\>[]
52 \| `undefined` =
53 \| [`ChatMiddleware`](../interfaces/ChatMiddleware.md)\<`any`\>[]
54 \| `undefined`
55​
56## Parameters
57​
58### options
59​
60`TextActivityOptionsWithContext`\<`TAdapter`, `TSchema`, `TStream`, `TTools`, `TMiddleware`\>
61​
62## Returns
63​
64`TextActivityResult`\<`TSchema`, `TStream`\>
65​
66## Examples
67​
68```ts
69import { chat } from '@tanstack/ai'
70import { openaiText } from '@tanstack/ai-openai'
71​
72for await (const chunk of chat({
73 adapter: openaiText('gpt-4o'),
74 messages: [{ role: 'user', content: 'What is the weather?' }],
75 tools: [weatherTool]
76})) {
77 if (chunk.type === 'TEXT_MESSAGE_CONTENT') {
78 console.log(chunk.delta)
79 }
81```
82​
83```ts
84for await (const chunk of chat({
85 adapter: openaiText('gpt-4o'),
86 messages: [{ role: 'user', content: 'Hello!' }]
87})) {
88 console.log(chunk)
90```
91​
92```ts
93const text = await chat({
94 adapter: openaiText('gpt-4o'),
95 messages: [{ role: 'user', content: 'Hello!' }],
96 stream: false
97})
98// text is a string with the full response
99```
100​
101```ts
102import { z } from 'zod'
103​
104const result = await chat({
105 adapter: openaiText('gpt-4o'),
106 messages: [{ role: 'user', content: 'Research and summarize the topic' }],
107 tools: [researchTool, analyzeTool],
108 outputSchema: z.object({
109 summary: z.string(),
110 keyPoints: z.array(z.string())
111 })
112})
113// result is { summary: string, keyPoints: string[] }
114```