Skip to main content

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)
BeeAI framework’s backend is designed with a provider-based architecture, allowing you to switch between different AI service providers while maintaining a consistent API.
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.
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

The Backend 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

The ChatModel 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
Method 2: Directly specifying the provider class

File / Document Inputs (PDF etc.)

You can attach files (e.g. PDFs) to a UserMessage 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
These content parts serialize to the flattened schema (legacy nested { "file": {...} } removed):
If neither 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

The EmbedingModel class provides functionality for generating vector embeddings from text.

Embedding model initialization

You can initialize an embedding model in multiple ways: Method 1: Using the from_name method
Method 2: Directly specifying the provider class

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:
  1. Authentication errors: Ensure all required environment variables are set correctly
  2. Model not found: Verify that the model ID is correct and available for the selected provider

Examples

Python

Explore reference backend implementations in Python

TypeScript

Explore reference backend implementations in TypeScript