Comprehensive examples demonstrating all features of the BoxLang AI module, organized by complexity and use case.
You will need CommandBox in order to install the dependencies:
bx-ai- This modulebx-derby- For examples using DerbyDB for memory, caching, or whatever you needbx-mysql- For examples using MySQL for memory, caching, or whatever you needbx-postgres- For examples using PostgreSQL for memory, caching, or whatever you needbx-sqlite- For examples using SQLite for memory, caching, or whatever you need
You can get CommandBox from here: https://www.ortussolutions.com/products/commandbox or the installation instructions here: https://commandbox.ortusbooks.com/setup/installation. Then just run the install command:
box installThat's it! Now you are ready to run the examples
If you want to test against this same repo you will have to first install the module locally by creating a symlink to the build output.
Linux/Mac:
ln -s build/module boxlang_modules/bx-aiWindows/PowerShell:
New-Item -ItemType SymbolicLink -Path boxlang_modules/bx-ai -Target build/moduleTo run the build you will do:
./gradlew build -x testMost examples require an API key fr
om your chosen AI provider. You have two options:
Option 1: Environment Variables (Recommended)
Set the appropriate environment variable for your provider:
# OpenAI (GPT models)
export OPENAI_API_KEY="sk-..."
# Anthropic (Claude models)
export CLAUDE_API_KEY="sk-ant-..."
# Google (Gemini models)
export GEMINI_API_KEY="..."
# Cohere
export COHERE_API_KEY="..."
# Voyage AI (embeddings)
export VOYAGE_API_KEY="..."
# Groq
export GROQ_API_KEY="..."
# DeepSeek
export DEEPSEEK_API_KEY="..."Option 2: Pass API Key Directly
You can pass the API key in the options parameter:
response = aiChat(
messages: [ aiMessage().user( "Hello!" ) ],
options: {
provider: "openai",
apiKey: "sk-..."
}
)No API Key Required:
- Ollama examples work locally without any API key
- Perfect for learning, development, and privacy-focused applications
- OpenAI: https://platform.openai.com/api-keys
- Anthropic (Claude): https://console.anthropic.com/
- Google (Gemini): https://makersuite.google.com/app/apikey
- Cohere: https://dashboard.cohere.com/api-keys
- Voyage AI: https://www.voyageai.com/
- Groq: https://console.groq.com/
- DeepSeek: https://platform.deepseek.com/
All examples are organized into folders by topic for easy navigation.
Essential examples for learning the basics:
- ollama-example.bxs - Using local AI with Ollama (no API key needed)
- streaming-example.bxs - Real-time streaming output
- return-formats-example.bxs - Different response formats (single, all, raw)
- json-xml-formats-example.bxs - JSON and XML output formats
Type-safe AI responses with classes and templates:
- 01-basic-class.bxs - Extract data into typed classes
- 02-struct-template.bxs - Using struct templates for quick extraction
- 03-array-extraction.bxs - Extracting multiple items into arrays
- 04-multiple-schemas.bxs - Extract different entity types simultaneously
- 05-aipopulate.bxs - Manual population from JSON (testing/caching)
- 06-pipeline-structured-output.bxs - Structured output in reusable pipelines
Load various file formats and data sources into Document objects:
- 01-text-loader.bxs - Load plain text files with chunking for RAG
- 02-csv-loader.bxs - Load CSV files, each row becomes a Document
- 03-json-loader.bxs - Parse JSON files with jqFilter field extraction
- 04-xml-loader.bxs - Parse XML files, RSS feeds, SOAP responses
- 05-markdown-loader.bxs - Load markdown documentation with header extraction
- 06-directory-loader.bxs - Batch load multiple files from folders
- 07-http-loader.bxs - Fetch web content and API responses
- 08-web-crawler.bxs - Crawl websites for documentation indexing
- 09-sql-loader.bxs - Load database query results into Documents
Composable multi-step AI workflows:
- runnable-example.bxs - Basic pipeline workflows and runnables
- 01-simple-pipeline.bxs - Message templates, transformations, and reusable patterns
- 02-multi-model-pipeline.bxs - Using different AI models in workflows
- 03-streaming-pipeline.bxs - Real-time pipeline execution with progress tracking
- 04-message-templates.bxs - Reusable prompt templates and few-shot learning
- 06-code-extractor.bxs - Extract clean code from AI markdown responses 🆕
- 07-text-cleaner.bxs - Clean and normalize messy AI responses 🆕
- 08-transformer-pipeline.bxs - Chain transformers for pre/post processing 🆕
Autonomous agents with memory and tools:
- 01-basic-agent.bxs - Simple agent with memory and context
- 02-agent-with-tools.bxs - Agent with weather, calculator, and database tools
- 03-customer-support-agent.bxs - Production support bot with hybrid memory (recent + past tickets)
- 04-research-agent.bxs - Research agent with vector memory for semantic article search
- 05-multi-memory-agent.bxs - One agent using multiple memory types simultaneously
- 06-sub-agents.bxs - Agent orchestration with specialized sub-agents
Advanced capabilities and memory systems:
- cohere-embeddings-example.bxs - Cohere embeddings with multilingual support (100+ languages)
- cohere-tools-example.bxs - Cohere function calling and tool use
- voyage-embeddings-example.bxs - Voyage AI state-of-the-art embeddings
- embeddings-example.bxs - Vector embeddings for semantic search (OpenAI)
- multimodal-example.bxs - Images, audio, video, and documents in AI conversations
- vision-example.bxs - Image analysis with vision-capable models
- token-counting-example.bxs - Estimating and managing token usage
- memory-file.bxs - File-based persistent conversation memory
- memory-windowed.bxs - Windowed memory (keeps last N messages)
- memory-summary.bxs - Summary memory (auto-summarizes old messages)
- memory-session.bxs - Session-based memory for web applications
- memory-cache.bxs - Distributed cache memory with CacheBox
- memory-comparison.bxs - Side-by-side comparison of memory types
- test-multimodal.bxs - Quick verification script for multimodal methods
Vector-based memory for semantic search and knowledge retrieval:
- boxvector-memory.bxs - In-memory vector storage (no external dependencies)
- hybrid-memory.bxs - Combines recent messages + semantic search (best of both)
- memory-type-comparison.bxs - Standard vs vector memory comparison
- vector-memory-demo.bxs - ChromaDB vector memory demonstration (requires ChromaDB)
# Local AI (no API key needed)
boxlang examples/basic/ollama-example.bxs
# Streaming responses
boxlang examples/basic/streaming-example.bxs
# Structured output - Extract data into typed classes
boxlang examples/structured/01-basic-class.bxs
# Structured output - Extract multiple items
boxlang examples/structured/03-array-extraction.bxs
# Vision/image analysis
boxlang examples/advanced/vision-example.bxs
# Vector embeddings (OpenAI)
boxlang examples/advanced/embeddings-example.bxs
# Cohere embeddings (multilingual)
boxlang examples/advanced/cohere-embeddings-example.bxs
# Voyage AI embeddings (state-of-the-art)
boxlang examples/advanced/voyage-embeddings-example.bxs
# AI agents with tools
boxlang examples/agents/02-agent-with-tools.bxs
# Agent with sub-agents
boxlang examples/agents/06-sub-agents.bxs
# Pipeline workflows
boxlang examples/pipelines/runnable-example.bxs
# Document loaders - Load text files (NEW!)
boxlang examples/loaders/01-text-loader.bxs
# Document loaders - Load CSV data (NEW!)
boxlang examples/loaders/02-csv-loader.bxs
# Pipeline transformers - Extract code (NEW!)
boxlang examples/pipelines/06-code-extractor.bxs
# Pipeline transformers - Clean text (NEW!)
boxlang examples/pipelines/07-text-cleaner.bxs
# Full transformer pipeline (NEW!)
boxlang examples/pipelines/08-transformer-pipeline.bxsSeveral examples use Ollama for local AI - no API key required!
Setup Ollama:
# Install from https://ollama.ai
# Pull a model
ollama pull llama3.2
# Start service (usually auto-starts)
ollama serve🟢 Beginner
basic/ollama-example.bxs- Local AI basicsbasic/streaming-example.bxs- Real-time responsesbasic/return-formats-example.bxs- Understanding responsesstructured/01-basic-class.bxs- Type-safe data extraction
🟡 Intermediate
structured/02-struct-template.bxs- Quick structured extractionstructured/03-array-extraction.bxs- Extract multiple itemsstructured/04-multiple-schemas.bxs- Multiple entity typesloaders/01-text-loader.bxs- Load text files with chunking 🆕loaders/02-csv-loader.bxs- CSV data to Documents 🆕loaders/03-json-loader.bxs- JSON file loading 🆕loaders/06-directory-loader.bxs- Batch load files 🆕pipelines/01-simple-pipeline.bxs- Basic pipeline patternspipelines/04-message-templates.bxs- Reusable templatespipelines/06-code-extractor.bxs- Extract code from responses 🆕pipelines/07-text-cleaner.bxs- Clean AI responses 🆕agents/01-basic-agent.bxs- Agent with memoryagents/02-agent-with-tools.bxs- Agent with function callingadvanced/vision-example.bxs- Image analysisadvanced/cohere-embeddings-example.bxs- Multilingual embeddingsadvanced/voyage-embeddings-example.bxs- Advanced embeddingsadvanced/memory-file.bxs- Managing conversation historypipelines/runnable-example.bxs- Pipeline fundamentalsbasic/json-xml-formats-example.bxs- JSON/XML output
🔴 Advanced
structured/05-aipopulate.bxs- Manual population & cachingstructured/06-pipeline-structured-output.bxs- Pipeline integrationloaders/04-xml-loader.bxs- XML and RSS feeds 🆕loaders/05-markdown-loader.bxs- Markdown documentation 🆕loaders/07-http-loader.bxs- Fetch web content 🆕loaders/08-web-crawler.bxs- Crawl websites 🆕loaders/09-sql-loader.bxs- Database to Documents 🆕pipelines/02-multi-model-pipeline.bxs- Multi-model workflowspipelines/03-streaming-pipeline.bxs- Streaming pipelinespipelines/08-transformer-pipeline.bxs- Advanced transformations 🆕agents/03-customer-support-agent.bxs- Production support botagents/04-research-agent.bxs- Multi-source research agentagents/06-sub-agents.bxs- Agent orchestration patternsadvanced/embeddings-example.bxs- Vector search and semantic similarityadvanced/cohere-tools-example.bxs- Function calling with Cohereadvanced/token-counting-example.bxs- Cost optimizationadvanced/multimodal-example.bxs- Multi-format AI interactions
💬 Chat & Conversation
basic/streaming-example.bxs- Real-time chat responsesadvanced/memory-windowed.bxs- Recent message memoryadvanced/memory-summary.bxs- Auto-summarizing memoryadvanced/memory-file.bxs- Persistent conversation storageadvanced/memory-comparison.bxs- Compare all memory types
🧠 Memory & Context Management (NEW!)
advanced/memory-windowed.bxs- Keeps last N messages (cost-effective)advanced/memory-summary.bxs- Auto-summarizes old messages (context preservation)advanced/memory-session.bxs- Web session-based memoryadvanced/memory-cache.bxs- Distributed cache with CacheBoxadvanced/memory-comparison.bxs- Side-by-side memory comparisonvector-memory/boxvector-memory.bxs- In-memory semantic searchvector-memory/hybrid-memory.bxs- Recent + semantic combinationvector-memory/memory-type-comparison.bxs- Standard vs vector memory
🤖 AI Agents
agents/01-basic-agent.bxs- Simple conversational agentagents/02-agent-with-tools.bxs- Function calling and tool useagents/03-customer-support-agent.bxs- Hybrid memory (recent + tickets)agents/04-research-agent.bxs- Vector memory for knowledge retrievalagents/05-multi-memory-agent.bxs- Multiple memory types in one agentagents/06-sub-agents.bxs- Agent orchestration with specialized sub-agents
🎯 Structured Output (NEW!)
structured/01-basic-class.bxs- Extract into typed classesstructured/02-struct-template.bxs- Quick struct templatesstructured/03-array-extraction.bxs- Extract multiple itemsstructured/04-multiple-schemas.bxs- Multiple entity typesstructured/05-aipopulate.bxs- Manual population & cachingstructured/06-pipeline-structured-output.bxs- Pipeline integration
👁️ Vision & Images
advanced/vision-example.bxs- Image analysis, document scanning, multi-image comparison
🔄 Workflows & Pipelines
pipelines/runnable-example.bxs- Pipeline basics and chainingpipelines/01-simple-pipeline.bxs- Message templates and transformationspipelines/02-multi-model-pipeline.bxs- Multi-model workflowspipelines/03-streaming-pipeline.bxs- Real-time streamingpipelines/04-message-templates.bxs- Reusable prompt patternspipelines/06-code-extractor.bxs- Extract code from AI responses 🆕pipelines/07-text-cleaner.bxs- Clean and normalize text 🆕pipelines/08-transformer-pipeline.bxs- Chain transformers (pre/post processing) 🆕basic/return-formats-example.bxs- Response format options
📄 Document Loaders (NEW!)
loaders/01-text-loader.bxs- Plain text files with chunkingloaders/02-csv-loader.bxs- CSV data to Documentsloaders/03-json-loader.bxs- JSON files and API responsesloaders/04-xml-loader.bxs- XML, RSS feeds, SOAPloaders/05-markdown-loader.bxs- Markdown documentationloaders/06-directory-loader.bxs- Batch load multiple filesloaders/07-http-loader.bxs- Fetch web contentloaders/08-web-crawler.bxs- Crawl websitesloaders/09-sql-loader.bxs- Database queries to Documents
🤖 AI Agents
agents/01-basic-agent.bxs- Memory and contextagents/02-agent-with-tools.bxs- Function callingagents/03-customer-support-agent.bxs- Production support botagents/04-research-agent.bxs- Research and synthesisagents/05-multi-memory-agent.bxs- Multiple memory typesagents/06-sub-agents.bxs- Agent orchestration
📊 Data Processing & Embeddings
advanced/embeddings-example.bxs- OpenAI embeddings for semantic searchadvanced/cohere-embeddings-example.bxs- Cohere multilingual embeddings (100+ languages)advanced/voyage-embeddings-example.bxs- Voyage AI state-of-the-art embeddingsadvanced/cohere-tools-example.bxs- Cohere function callingadvanced/token-counting-example.bxs- Cost estimationbasic/json-xml-formats-example.bxs- JSON/XML formats
☁️ Cloud Providers
- OpenAI - GPT models (including vision with gpt-4o)
- Claude - Anthropic models (claude-3 with vision)
- Gemini - Google AI models (gemini-pro-vision)
💻 Local AI
ollama-example.bxs- Ollama basics- Privacy-focused, offline-capable AI
ollama-example.bxs- Start with local AI (no API key)return-formats-example.bxs- Understand responsesstreaming-example.bxs- Real-time outputmemory-windowed.bxs- Basic conversation memoryagents/01-basic-agent.bxs- Simple agent
memory-windowed.bxs- Start with simple windowed memorymemory-summary.bxs- Learn auto-summarizationmemory-comparison.bxs- Compare standard memory typesvector-memory/boxvector-memory.bxs- Semantic search basicsvector-memory/hybrid-memory.bxs- Best of both worldsagents/05-multi-memory-agent.bxs- Multiple memories together
agents/01-basic-agent.bxs- Basic agent with memoryagents/02-agent-with-tools.bxs- Add tool callingagents/03-customer-support-agent.bxs- Hybrid memory in productionagents/04-research-agent.bxs- Vector memory for knowledgeagents/05-multi-memory-agent.bxs- Advanced memory strategiesagents/06-sub-agents.bxs- Agent orchestration and delegation
loaders/01-text-loader.bxs- Start with basic text loadingloaders/02-csv-loader.bxs- Load structured dataloaders/03-json-loader.bxs- Parse JSON filesloaders/06-directory-loader.bxs- Batch load documentspipelines/07-text-cleaner.bxs- Clean document contentpipelines/08-transformer-pipeline.bxs- Build RAG pipelines
streaming-example.bxs- Start here (needs API key)vision-example.bxs- Image analysisjson-xml-formats-example.bxs- Structured outputembeddings-example.bxs- Semantic searchtoken-counting-example.bxs- Optimize costs
ollama-example.bxs- Local AI setupmemory-file.bxs- File-based memoryvector-memory/boxvector-memory.bxs- In-memory vectors (no external DB)runnable-example.bxs- Build offline workflows- Build privacy-first applications
- AI Agents
- Working with Models
- Message Templates
- Memory Systems
- Vector Memory
- Custom Memory
- Document Loaders
- Transformers
Have a great example? Please contribute!
// example-name.bxs
/**
* [Example Title]
*
* Brief description of what this example demonstrates.
*
* Prerequisites:
* - List any requirements
* - API keys needed
* - External services
*
* Learn more: [Link to relevant docs]
*/
// Clear, commented code here
// Explain key concepts as you go
println( "✅ Example complete!" )- Keep examples focused on one concept
- Include clear comments
- Show expected output
- Mention prerequisites
- Link to relevant documentation
- Issues: GitHub Issues
- Docs: Full Documentation
- Community: BoxLang Community
All examples are released under the same license as the bx-ai module (Apache 2.0).