본문으로 건너뛰기

Function: chat()

function chat<TAdapter, TSchema, TStream, TTools, TInterrupts, TContext, TMiddleware>(options): TextActivityResult<TSchema, TStream, TTools>;

정의 위치: packages/ai/src/activities/chat/index.ts:4548

텍스트 활동으로, 에이전트 텍스트 생성, 단일 요청 텍스트 생성, 에이전트 구조화된 출력을 처리합니다.

이 활동은 네 가지 모드를 지원합니다.

  1. 스트리밍 에이전트 텍스트: 도구를 자동으로 실행하면서 응답을 스트리밍합니다.
  2. 스트리밍 단일 요청 텍스트: 도구 없이 간단한 스트리밍 요청/응답을 수행합니다.
  3. 비스트리밍 텍스트: 수집된 텍스트를 문자열로 반환합니다(stream: false).
  4. 에이전트 구조화된 출력: 도구를 실행한 다음 구조화된 데이터를 반환합니다.

타입 매개변수

TAdapter

TAdapter extends AnyTextAdapter

TSchema

TSchema extends SchemaInput | undefined = undefined

TStream

TStream extends boolean = boolean

TTools

TTools extends | readonly ( | Omit&lt;Tool&lt;any, any, any, any>, "execute"> & object & object | ProviderTool&lt;string, TAdapter["~types"]["toolCapabilities"][number]>)[] | undefined = | readonly ( | Omit&lt;Tool&lt;any, any, any, any>, "execute"> & object & object | ProviderTool&lt;string, TAdapter["~types"]["toolCapabilities"][number]>)[] | undefined

TInterrupts

TInterrupts extends readonly InterruptDefinition&lt;any, any, any, any, any>[] = []

TContext

TContext = unknown

TMiddleware

TMiddleware extends unknown[] | undefined = undefined

매개변수

options

TextActivityOptionsWithContext&lt;TAdapter, TSchema, TStream, TTools, TInterrupts, TContext, TMiddleware>

반환값

TextActivityResult&lt;TSchema, TStream, TTools>

예제

전체 에이전트 텍스트(도구를 사용한 스트리밍)

import { chat } from '@tanstack/ai'
import { openaiText } from '@tanstack/ai-openai'

for await (const chunk of chat({
adapter: openaiText('gpt-5.5'),
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-5.5'),
messages: [{ role: 'user', content: 'Hello!' }]
})) {
console.log(chunk)
}

비스트리밍 텍스트(stream: false)

const text = await chat({
adapter: openaiText('gpt-5.5'),
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-5.5'),
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[] }