Adding a backendΒΆ
Every backend subclasses llm_base.BaseLLM. The base class already implements
from_config(), query() with retries, message
building and parameter merging. A new provider fills in the connection hooks only.
Create
LLMMyProvider/LLMMyProvider.pywith a classLLMMyProvider(BaseLLM).Set
PROVIDER, andSUPPORTS_IMAGESwhen the provider accepts images.Implement the three abstract hooks:
_create_client()β build the SDK client or load the model._send()β send the message list, return the raw response._extract_text()β pull the answer out of the response.
Optionally override
_setup()to read provider keys fromself.config,_extract_usage()for token counts andimage_part()for images.Register the slug in
llm_factory.PROVIDER_ALIASESandllm_factory.PROVIDER_MODULES.
A minimal example:
from llm_base import BaseLLM
class LLMMyProvider(BaseLLM):
PROVIDER = "myprovider"
def _setup(self):
self.api_key_name = self.config.get("API_KEY_NAME") or "MYPROVIDER_API_KEY"
def _create_client(self):
import os
from myprovider import Client
return Client(api_key=os.environ[self.api_key_name])
def _send(self, client, messages):
return client.chat(model=self.model, messages=messages, **self.request_params())
def _extract_text(self, response):
return response.text