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
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
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:
Structured Output Guide - Complete documentation
Pipeline Integration - Advanced patterns
Course Lesson 12 - Interactive learning
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
Use System Prompts: Set clear context and behavior
Manage Context Window: Trim old messages for long conversations
Handle Errors Gracefully: Always use try/catch
Stream Long Responses: Better UX for detailed answers
Cache When Possible: Save costs and time
Use Tools Wisely: Only when real-time data is needed
Test Async Operations: Handle timeouts and failures
Next Steps
Service-Level Chatting - Direct service control
Pipeline Overview - Learn about AI pipelines
Message Templates - Advanced templating
Message Context - Inject security and RAG data
Last updated