Overview
Backend
is an umbrella module that encapsulates a unified way to work with the following functionalities:
- Chat Models (
ChatModel
class) - Embedding Models (
EmbeddingModel
class) - Audio Models (coming soon)
- Image Models (coming soon)
Supported in Python and TypeScript.
Supported providers
The following table depicts supported providers. Each provider requires specific configuration through environment variables. Ensure all required variables are set before initializing a provider.Name | Chat | Embedding | Environment Variables |
---|---|---|---|
Ollama | ✅ | ✅ | OLLAMA_CHAT_MODEL OLLAMA_BASE_URL |
OpenAI | ✅ | ✅ | OPENAI_CHAT_MODEL OPENAI_EMBEDDING_MODEL OPENAI_API_BASE OPENAI_API_KEY OPENAI_ORGANIZATION OPENAI_API_HEADERS |
Watsonx | ✅ | ✅ | WATSONX_CHAT_MODEL WATSONX_API_KEY WATSONX_PROJECT_ID WATSONX_SPACE_ID WATSONX_TOKEN WATSONX_ZENAPIKEY WATSONX_URL WATSONX_REGION |
Anthropic | ✅ | ✅ | ANTHROPIC_CHAT_MODEL ANTHROPIC_API_KEY ANTHROPIC_API_HEADERS |
Groq | ✅ | ✅ | GROQ_CHAT_MODEL GROQ_EMBEDDING_MODEL GROQ_API_KEY |
Amazon Bedrock | ✅ | ✅ | AWS_CHAT_MODEL AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_REGION AWS_API_HEADERS |
Google Vertex | ✅ | ✅ | GOOGLE_VERTEX_CHAT_MODEL GOOGLE_VERTEX_PROJECT GOOGLE_APPLICATION_CREDENTIALS GOOGLE_APPLICATION_CREDENTIALS_JSON GOOGLE_CREDENTIALS GOOGLE_VERTEX_API_HEADERS |
Azure OpenAI | ✅ | ✅ | AZURE_OPENAI_CHAT_MODEL AZURE_OPENAI_API_KEY AZURE_OPENAI_API_BASE AZURE_OPENAI_API_VERSION AZURE_AD_TOKEN AZURE_API_TYPE AZURE_API_HEADERS |
xAI | ✅ | ✅ | XAI_CHAT_MODEL XAI_API_KEY |
Google Gemini | ✅ | ✅ | GEMINI_CHAT_MODEL GEMINI_API_KEY GEMINI_API_HEADERS |
MistralAI | ✅ | ✅ | MISTRALAI_CHAT_MODEL MISTRALAI_EMBEDDING_MODEL MISTRALAI_API_KEY MISTRALAI_API_BASE |
Transformers | ✅ | ✅ | TRANSFORMERS_CHAT_MODEL HF_TOKEN |
If you don’t see your provider raise an issue here.
Meanwhile, you can use the Ollama for local models in Python
or TypeScript or the Langchain adapter for hosted providers.
Google Gemini, MistralAI, and Transformers are supported in Python only. The Transformers chat model does not support tool calling.
Backend initialization
TheBackend
class serves as a central entry point to access models from your chosen provider. This example illustrate how to leverage the framework’s unified interface for different provider model operations by showcasing various interaction patterns including:
- Basic chat completion
- Streaming responses with abort functionality
- Structured output generation
- Real-time response parsing
- Tool calling with external APIs
- Text embedding generation
Explore more provider examples in Python or TypeScript
See the events documentation for more information on standard emitter events.
Chat model
TheChatModel
class represents a Chat Language Model and provides methods for text generation, streaming responses, and more. You can initialize a chat model in multiple ways:
Method 1: Using the from_name
method
File / Document Inputs (PDF etc.)
You can attach files (e.g. PDFs) to aUserMessage
using the MessageFileContent
part or the convenience factory UserMessage.from_file
. Provide either a remote file_id
/URL or an inline base64 data URI (file_data
). Optionally specify a MIME format
.
Python
{ "file": {...} }
removed):
file_id
nor file_data
is supplied a validation error is raised.
Chat model configuration
You can configure various parameters for your chat model.Text generation
The most basic usage is to generate text responses:Execution parameters (those passed to
model.create({...})
) take precedent over ones defined via config
.Streaming responses
For applications requiring real-time responses:Structured generation
Generate structured data according to a schema:Tool calling
Integrate external tools with your AI model:Embedding model
TheEmbedingModel
class provides functionality for generating vector embeddings from text.
Embedding model initialization
You can initialize an embedding model in multiple ways: Method 1: Using thefrom_name
method
Embedding model usage
Generate embeddings for one or more text strings:Adding a Provider Using the LangChain Adapter
If your preferred provider isn’t directly supported, you can use the LangChain adapter as a bridge as long as that provider has LangChain compatibility.Troubleshooting
Common issues and their solutions:- Authentication errors: Ensure all required environment variables are set correctly
- Model not found: Verify that the model ID is correct and available for the selected provider