function chat<TAdapter, TSchema, TStream, TTools, TMiddleware>(options): TextActivityResult<TSchema, TStream>;function chat<TAdapter, TSchema, TStream, TTools, TMiddleware>(options): TextActivityResult<TSchema, TStream>;Defined in: packages/ai/src/activities/chat/index.ts:2537
Text activity - handles agentic text generation, one-shot text generation, and agentic structured output.
This activity supports four modes:
TAdapter extends AnyTextAdapter
TSchema extends SchemaInput | undefined = undefined
TStream extends boolean = boolean
TTools extends | ( | Omit<Tool<any, any, any, any>, "execute"> & object & object | ProviderTool<string, TAdapter["~types"]["toolCapabilities"][number]>)[] | undefined = | ( | Omit<Tool<any, any, any, any>, "execute"> & object & object | ProviderTool<string, TAdapter["~types"]["toolCapabilities"][number]>)[] | undefined
TMiddleware extends | ChatMiddleware<any>[] | undefined = | ChatMiddleware<any>[] | undefined
TextActivityOptionsWithContext<TAdapter, TSchema, TStream, TTools, TMiddleware>
TextActivityResult<TSchema, TStream>
import { chat } from '@tanstack/ai'
import { openaiText } from '@tanstack/ai-openai'
for await (const chunk of chat({
adapter: openaiText('gpt-4o'),
messages: [{ role: 'user', content: 'What is the weather?' }],
tools: [weatherTool]
})) {
if (chunk.type === 'TEXT_MESSAGE_CONTENT') {
console.log(chunk.delta)
}
}import { chat } from '@tanstack/ai'
import { openaiText } from '@tanstack/ai-openai'
for await (const chunk of chat({
adapter: openaiText('gpt-4o'),
messages: [{ role: 'user', content: 'What is the weather?' }],
tools: [weatherTool]
})) {
if (chunk.type === 'TEXT_MESSAGE_CONTENT') {
console.log(chunk.delta)
}
}for await (const chunk of chat({
adapter: openaiText('gpt-4o'),
messages: [{ role: 'user', content: 'Hello!' }]
})) {
console.log(chunk)
}for await (const chunk of chat({
adapter: openaiText('gpt-4o'),
messages: [{ role: 'user', content: 'Hello!' }]
})) {
console.log(chunk)
}const text = await chat({
adapter: openaiText('gpt-4o'),
messages: [{ role: 'user', content: 'Hello!' }],
stream: false
})
// text is a string with the full responseconst text = await chat({
adapter: openaiText('gpt-4o'),
messages: [{ role: 'user', content: 'Hello!' }],
stream: false
})
// text is a string with the full responseimport { z } from 'zod'
const result = await chat({
adapter: openaiText('gpt-4o'),
messages: [{ role: 'user', content: 'Research and summarize the topic' }],
tools: [researchTool, analyzeTool],
outputSchema: z.object({
summary: z.string(),
keyPoints: z.array(z.string())
})
})
// result is { summary: string, keyPoints: string[] }import { z } from 'zod'
const result = await chat({
adapter: openaiText('gpt-4o'),
messages: [{ role: 'user', content: 'Research and summarize the topic' }],
tools: [researchTool, analyzeTool],
outputSchema: z.object({
summary: z.string(),
keyPoints: z.array(z.string())
})
})
// result is { summary: string, keyPoints: string[] }