Building Custom AI Providers
Learn how to build custom AI provider integrations to connect any LLM service with BoxLang AI.
🎯 Why Custom Providers?
🏗️ Provider Architecture
📝 IAiService Interface
interface {
/**
* Get the name of the LLM
*/
function getName();
/**
* Configure the service with an API key
* @apiKey - The API key to use with the provider
* @return The service instance
*/
IAiService function configure( required any apiKey );
/**
* Invoke the provider service with a AiRequest object
* @aiRequest The AiRequest object to send to the provider
* @return The response from the service
*/
function invoke( required AiRequest aiRequest );
/**
* Invoke the provider service in streaming mode
* @aiRequest The AiRequest object to send to the provider
* @callback A callback function called with each chunk: function( chunk )
* @return void
*/
function invokeStream( required AiRequest aiRequest, required function callback );
/**
* Generate embeddings for the given input text(s)
* @embeddingRequest The embedding request object
* @return The embeddings response from the provider
*/
function embeddings( required AiEmbeddingRequest embeddingRequest );
}🚀 Quick Start: Simple Custom Provider
🎨 Extending BaseService
Inherited Properties
Inherited Methods
💡 Provider Types
Type 1: OpenAI-Compatible (Simplest)
Type 2: Custom Authentication
Type 3: Custom Request/Response Format
Type 4: Custom Streaming
🛠️ Advanced Features
Tool/Function Calling Support
Custom Headers
Embeddings Support
🔧 Real-World Example: Complete Custom Provider
📦 Registering Custom Providers
Module Registration
Application Registration
Direct Usage
✅ Best Practices
1. Configuration Validation
2. Error Handling
3. Event Announcements
4. Logging Support
5. Defensive Programming
🧪 Testing Custom Providers
Unit Tests
Integration Tests
📚 Next Steps
🎓 Summary
Last updated