Advanced Chatting

Master advanced AI interaction techniques including multi-turn conversations, AI tools, async operations, and streaming responses.

Master advanced AI interaction techniques including multi-turn conversations, AI tools, async operations, and streaming responses.

πŸ“‹ Table of Contents


πŸ’¬ Multi-Message Conversations

Create rich, contextual conversations with system prompts and conversation history.

πŸ”„ Conversation Flow

Conversation Arrays

Message Roles

  • system: Sets AI behavior and personality

  • user: Your messages/questions

  • assistant: AI's responses (for conversation history)

Building Conversations Dynamically

Conversation Manager

πŸ› οΈ AI Tools

Enable AI to call functions and access real-time data.

Creating Tools

Using Tools

Multiple Tools

Tool Examples

Database Query Tool:

API Integration Tool:

Message Builder

Use aiMessage() for structured message composition:

Basic Usage

Chaining Messages

Reusable Templates

Async Chat Requests

Perform non-blocking AI operations.

Basic Async

With Callbacks

Multiple Concurrent Requests

Timeout Handling

Multimodal Content

Work with images, audio, video, and documents in your AI conversations.

Images

Vision-capable models can analyze images alongside text.

Using Image URLs

Embedding Local Images

Multiple Images

Detail Levels

  • "auto" (default): Model decides

  • "low": Faster, cheaper, 512x512 resolution

  • "high": Full detail for complex images

Audio

Process audio files with AI models that support audio understanding.

Supported by: OpenAI (GPT-4o-audio), Gemini

Supported formats: mp3, mp4, mpeg, mpga, m4a, wav, webm

Video

Analyze video content with AI models that support video understanding.

Supported by: Gemini (gemini-1.5-pro, gemini-2.0-flash)

Supported formats: mp4, mpeg, mov, avi, flv, mpg, webm, wmv

Documents and PDFs

Analyze documents, PDFs, and text files.

Supported by: Claude (Opus, Sonnet), OpenAI (GPT-4o)

Supported formats: pdf, doc, docx, txt, xls, xlsx

Mixed Multimodal

Combine multiple media types in a single conversation:

Provider Support Matrix

Feature
OpenAI
Claude
Gemini
Ollama

Images

βœ… GPT-4o, GPT-4-turbo

βœ… Claude 3+

βœ… All vision models

βœ… LLaVA, Bakllava

Audio

βœ… GPT-4o-audio

❌

βœ… Gemini 1.5+, 2.0

❌

Video

❌

❌

βœ… Gemini 1.5+, 2.0

❌

Documents

βœ… GPT-4o

βœ… Claude 3+

⚠️ Via OCR

❌

File Size Guidelines

  • Images: Up to 20MB per image

  • Audio: Up to 25MB (OpenAI), 2GB (Gemini)

  • Video: Up to 2GB (Gemini only)

  • Documents: ~10MB for inline base64, larger files need upload API

Note: Large files consume significant context tokens. For files >10MB, consider using provider-specific file upload APIs (OpenAI /v1/files, Gemini File API).

Streaming Responses

Get real-time responses as they're generated.

Basic Streaming

With Parameters

Collecting Stream Data

Web Streaming Example

Markdown Streaming Parser

Structured Data with JSON and XML

JSON Return Format for Complex Data

Use returnFormat: "json" to automatically parse structured responses:

Multi-Turn Conversation with JSON

JSON with Tools

Structured Output

Get type-safe, validated responses using BoxLang classes or struct templates. Unlike returnFormat: "json", structured output provides compile-time type safety and automatic validation.

Why Structured Output?

  • Type Safety: Get validated objects with proper types, not generic structs

  • Automatic Validation: Schema constraints ensure correct data structure

  • Better Reliability: Reduces hallucinations by strictly constraining format

  • IDE Support: Full autocomplete and type checking with classes

  • No Manual Parsing: Direct access to typed properties and methods

Using Classes

Using Struct Templates

Multi-Turn Conversations with Structured Output

Extracting Arrays

Multiple Schemas (Extract Different Types)

With Tools

Structured Output vs JSON Return Format

Feature
Structured Output
JSON Return Format

Type Safety

βœ… Full type safety with classes

❌ Generic structs

Validation

βœ… Schema validation

⚠️ Manual validation needed

IDE Support

βœ… Autocomplete, type hints

❌ No type information

Reliability

βœ… Strict schema enforcement

⚠️ May return invalid JSON

Complexity

Simple classes/templates

Manual parsing logic

Best For

Production code, type safety

Quick prototypes, flexible data

When to use Structured Output:

  • Production applications requiring reliability

  • Type-safe code with compile-time checks

  • Complex nested data structures

  • When consistency is critical

When to use JSON Return Format:

  • Quick prototypes or scripts

  • Dynamic/unknown data structures

  • When flexibility > type safety

Learn More

For complete details on structured output including inheritance, validation, and advanced patterns, see:

XML Return Format for Documents

XML Report Generation

Async JSON Requests

Streaming with JSON Accumulation

Practical Examples

Interactive Chat Application

Smart Document Analyzer

Real-Time Code Assistant

Best Practices

  1. Use System Prompts: Set clear context and behavior

  2. Manage Context Window: Trim old messages for long conversations

  3. Handle Errors Gracefully: Always use try/catch

  4. Stream Long Responses: Better UX for detailed answers

  5. Cache When Possible: Save costs and time

  6. Use Tools Wisely: Only when real-time data is needed

  7. Test Async Operations: Handle timeouts and failures

Next Steps

Last updated