Skip to content

Latest commit

 

History

History
94 lines (70 loc) · 2.18 KB

File metadata and controls

94 lines (70 loc) · 2.18 KB
id chat
title chat

Function: chat()

function chat<TAdapter, TSchema, TStream>(options): TextActivityResult<TSchema, TStream>;

Defined in: packages/typescript/ai/src/activities/chat/index.ts:1512

Text activity - handles agentic text generation, one-shot text generation, and agentic structured output.

This activity supports four modes:

  1. Streaming agentic text: Stream responses with automatic tool execution
  2. Streaming one-shot text: Simple streaming request/response without tools
  3. Non-streaming text: Returns collected text as a string (stream: false)
  4. Agentic structured output: Run tools, then return structured data

Type Parameters

TAdapter

TAdapter extends AnyTextAdapter

TSchema

TSchema extends SchemaInput | undefined = undefined

TStream

TStream extends boolean = true

Parameters

options

TextActivityOptions<TAdapter, TSchema, TStream>

Returns

TextActivityResult<TSchema, TStream>

Examples

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 === 'content') {
    console.log(chunk.delta)
  }
}
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 response
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[] }