OpenAICompatibleBackend¶
Backend adapter for OpenAI-compatible APIs.
Quick Example¶
from mamba_agents.backends import OpenAICompatibleBackend
# Direct instantiation
backend = OpenAICompatibleBackend(
model="my-model",
base_url="http://localhost:8000/v1",
api_key="optional-key",
)
# Or use factory functions
from mamba_agents.backends import (
create_ollama_backend,
create_vllm_backend,
create_lmstudio_backend,
)
# Ollama
backend = create_ollama_backend("llama3.2")
# vLLM
backend = create_vllm_backend("meta-llama/Llama-3.2-3B-Instruct")
# LM Studio
backend = create_lmstudio_backend()
Factory Functions¶
| Function | Default URL | Description |
|---|---|---|
create_ollama_backend |
localhost:11434 |
Ollama |
create_vllm_backend |
localhost:8000 |
vLLM |
create_lmstudio_backend |
localhost:1234 |
LM Studio |
API Reference¶
OpenAICompatibleBackend
¶
OpenAICompatibleBackend(
model: str,
*,
base_url: str = "https://api.openai.com/v1",
api_key: SecretStr | str | None = None,
timeout: float = 60.0,
profile: ModelProfile | None = None,
)
Bases: ModelBackend
Backend for OpenAI-compatible APIs.
Works with any API that follows the OpenAI chat completions format. Automatically handles differences between providers.
Initialize the backend.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
Model identifier.
TYPE:
|
base_url
|
API base URL.
TYPE:
|
api_key
|
API key for authentication.
TYPE:
|
timeout
|
Request timeout in seconds.
TYPE:
|
profile
|
Custom model profile.
TYPE:
|
Source code in src/mamba_agents/backends/openai_compat.py
complete
async
¶
complete(
messages: list[dict[str, Any]],
*,
tools: list[dict[str, Any]] | None = None,
temperature: float | None = None,
max_tokens: int | None = None,
**kwargs: Any,
) -> ModelResponse
Generate a completion.
| PARAMETER | DESCRIPTION |
|---|---|
messages
|
Conversation messages.
TYPE:
|
tools
|
Available tools.
TYPE:
|
temperature
|
Sampling temperature.
TYPE:
|
max_tokens
|
Maximum tokens to generate.
TYPE:
|
**kwargs
|
Additional options.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ModelResponse
|
ModelResponse with generation results. |
| RAISES | DESCRIPTION |
|---|---|
ModelBackendError
|
On API error. |
RateLimitError
|
On rate limit. |
AuthenticationError
|
On auth failure. |
Source code in src/mamba_agents/backends/openai_compat.py
stream
async
¶
stream(
messages: list[dict[str, Any]],
*,
tools: list[dict[str, Any]] | None = None,
temperature: float | None = None,
max_tokens: int | None = None,
**kwargs: Any,
) -> AsyncIterator[StreamChunk]
Generate a streaming completion.
| PARAMETER | DESCRIPTION |
|---|---|
messages
|
Conversation messages.
TYPE:
|
tools
|
Available tools.
TYPE:
|
temperature
|
Sampling temperature.
TYPE:
|
max_tokens
|
Maximum tokens to generate.
TYPE:
|
**kwargs
|
Additional options.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
AsyncIterator[StreamChunk]
|
StreamChunk objects with partial content. |
Source code in src/mamba_agents/backends/openai_compat.py
health_check
async
¶
Check if the backend is healthy.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if reachable. |
Source code in src/mamba_agents/backends/openai_compat.py
create_ollama_backend
¶
create_ollama_backend(
model: str,
*,
base_url: str = "http://localhost:11434/v1",
**kwargs: Any,
) -> OpenAICompatibleBackend
Create a backend configured for Ollama.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
Model name (e.g., "llama3.2", "mistral").
TYPE:
|
base_url
|
Ollama API URL.
TYPE:
|
**kwargs
|
Additional backend options.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
OpenAICompatibleBackend
|
Configured OpenAICompatibleBackend. |
Source code in src/mamba_agents/backends/openai_compat.py
create_vllm_backend
¶
create_vllm_backend(
model: str,
*,
base_url: str = "http://localhost:8000/v1",
api_key: str | None = None,
**kwargs: Any,
) -> OpenAICompatibleBackend
Create a backend configured for vLLM.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
Model name.
TYPE:
|
base_url
|
vLLM API URL.
TYPE:
|
api_key
|
API key if required.
TYPE:
|
**kwargs
|
Additional backend options.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
OpenAICompatibleBackend
|
Configured OpenAICompatibleBackend. |
Source code in src/mamba_agents/backends/openai_compat.py
create_lmstudio_backend
¶
create_lmstudio_backend(
model: str = "local-model",
*,
base_url: str = "http://localhost:1234/v1",
**kwargs: Any,
) -> OpenAICompatibleBackend
Create a backend configured for LM Studio.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
Model identifier (can be any name).
TYPE:
|
base_url
|
LM Studio API URL.
TYPE:
|
**kwargs
|
Additional backend options.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
OpenAICompatibleBackend
|
Configured OpenAICompatibleBackend. |