Basic Chatting
Learn the fundamentals of interacting with AI using simple, straightforward chat requests.
Learn the fundamentals of interacting with AI using simple, straightforward chat requests.
📋 Table of Contents
The aiChat() Function
aiChat() FunctionThe aiChat() function is your primary interface for AI interactions. It's designed to be simple yet powerful.
🔄 Request-Response Flow
Signature
Parameters:
message(string|array) - Your question or conversationparams(struct) - Model parameters (temperature, max_tokens, etc.)options(struct) - Provider, API key, return format
Returns: String, Array, or Struct depending on returnFormat
Simple Questions
The most basic usage - ask a question, get an answer:
Working with Parameters
Parameters control how the AI behaves and responds.
Temperature
Controls randomness and creativity (0.0 = focused, 1.0 = creative):
Usage Guide:
0.0 - 0.3: Math, facts, code, technical docs
0.5 - 0.7: General chat, balanced responses
0.8 - 1.0: Creative writing, brainstorming
Max Tokens
Limits response length:
Model Selection
Choose specific models:
Multiple Parameters
Combine parameters for precise control:
Switching Providers
Override the default provider for any request:
With Custom API Keys
Return Formats
Control how responses are returned:
Single (Default)
Returns just the content as a string:
All Messages
Returns the complete conversation array:
Raw Response
Returns the complete API response:
JSON Format (Auto-Parse)
Automatically parses JSON responses into BoxLang structs/arrays:
More JSON Examples:
XML Format (Auto-Parse)
Automatically parses XML responses into BoxLang XML objects:
More XML Examples:
Practical Examples
FAQ Bot
Code Explainer
Content Summarizer
Multi-Language Support
Data Extraction with JSON
Form Data Generation
Code Generator
Error Handling
Always wrap AI calls in try/catch for production:
Specific Error Handling
Tips and Best Practices
1. Use Appropriate Temperature
2. Be Specific in Prompts
3. Set Token Limits
4. Choose Right Provider
5. Cache Repeated Requests
Debugging
Log Requests and Responses
Enable in configuration:
Use Raw Format for Debugging
Next Steps
Advanced Chatting - Multi-turn conversations, tools, async, streaming
Service-Level Chatting - Direct service control
Message Templates - Reusable prompt templates
Message Context - Inject security and RAG data
Last updated