trading_agent.backends.openai_compatible_backend
¶
A backend for OpenAI, or any server that speaks OpenAI's Chat Completions protocol.
That covers OpenAI itself and local servers such as vLLM, LM Studio and llama.cpp. Tool calls stream in pieces: the arguments arrive as fragments of a JSON string, which are joined and parsed once the reply is complete.
Typical usage example:
backend = OpenAICompatibleBackend('openai', backend_configuration, api_key) reply = await backend.complete(conversation, tool_definitions, on_text)
OpenAICompatibleBackend
¶
Bases: ModelBackend
One model on one OpenAI-compatible server.
Creates the backend without contacting the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The backend's name in trading_agent.toml. |
required |
backend_configuration
|
BackendConfiguration
|
Its settings, of kind "openai_compatible". |
required |
api_key
|
str
|
The server's API key, or an empty string for a local server that needs none. |
required |
Source code in src/trading_agent/backends/openai_compatible_backend.py
complete
async
¶
Streams one reply from the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conversation
|
Conversation
|
The conversation so far. |
required |
tool_definitions
|
list[dict[str, Any]]
|
The tools the model may call. |
required |
on_text
|
Callable[[str], None]
|
Called with each piece of text as it streams in. |
required |
Returns:
| Type | Description |
|---|---|
ModelReply
|
The complete reply. |
Raises:
| Type | Description |
|---|---|
BackendUnavailableError
|
The server cannot be reached, refused the request, or is rate limiting. |