aiChat

Initiate an AI chat conversation against the default or custom AI Provider with a simple, synchronous interface.

Syntax

aiChat(messages, params, options)

Parameters

Parameter
Type
Required
Description

messages

any

Yes

The messages to pass to the AI model. Can be a string, struct, array of messages, or AiMessage object

params

struct

No

Request parameters for the AI provider (e.g., { temperature: 0.5, max_tokens: 100, model: "gpt-3.5-turbo" })

options

struct

No

Request options for controlling behavior

Options Structure

Option
Type
Default
Description

provider

string

(config)

The AI provider to use (openai, claude, etc.)

apiKey

string

(config/env)

API key for the provider

returnFormat

string

"single"

Response format: "single", "all", "raw", "json", "xml"

timeout

numeric

30

Request timeout in seconds

logResponse

boolean

false

Log the AI response to ai.log

logRequest

boolean

false

Log the AI request to ai.log

Returns

Returns the AI response based on returnFormat:

  • "single" (default): String content of the first message

  • "all": Array of all response messages

  • "raw": Complete API response with metadata

  • "json": Parsed JSON object from response

  • "xml": Parsed XML document from response

Examples

Simple Chat

With Parameters

Multiple Messages

Using AiMessage Object

Different Providers

Return Formats

Structured Output

With Logging

Notes

  • Defaults to "single" format: Unlike pipelines, aiChat() defaults to returning just the content string for convenience

  • Automatic provider selection: If no provider specified, uses module configuration default

  • API key detection: Automatically detects keys from environment variables like OPENAI_API_KEY, CLAUDE_API_KEY, etc.

  • Synchronous: Blocks until response received. Use aiChatAsync() for async execution

  • Message normalization: Simple strings are automatically converted to { role: "user", content: "..." } format

  • Parameter merging: Passed params are merged with module default params (passed params take precedence)

Error Handling

Last updated