llm_openai_compatible#
- class baf.nlp.llm.llm_openai_compatible.LLMOpenAICompatible(agent, name, parameters, num_previous_messages=1, global_context=None)[source]#
Bases:
LLMShared implementation for all OpenAI-compatible LLM providers.
Any provider that exposes an OpenAI-compatible chat-completions endpoint can subclass this class and only override two class attributes:
DEFAULT_BASE_URL– the provider’s base URL (None→ use the OpenAI SDK default).API_KEY_PROPERTY– thePropertyconstant frombaf.nlpthat holds the API key for the provider.
Per-instance overrides are also accepted via the
parametersdict: ifparameterscontains"base_url"or"api_key"keys, they take priority over the class defaults and the config-file values respectively. Both keys are popped fromparametersininitialize()so they are never forwarded tochat.completions.create.- Parameters:
agent (Agent) – the agent the LLM belongs to
name (str) – the LLM name / model identifier (e.g.
"gpt-4o-mini")parameters (dict) – the LLM parameters forwarded to
chat.completions.create(e.g.{"temperature": 0.7}). May also contain the one-time connection overrides"base_url"and"api_key"which are consumed byinitialize()and not forwarded to the API.num_previous_messages (int) – number of previous conversation turns to include in
chat()calls (must be > 0). Requires a connection toMonitoringDB.global_context (str) – system-level context injected into every request.
- API_KEY_PROPERTY = <baf.core.property.Property object>#
Property constant used to look up the API key from the agent config file.
- DEFAULT_BASE_URL = None#
Provider base URL.
None→ use the OpenAI SDK default (api.openai.com).
- _abc_impl = <_abc._abc_data object>#
- chat(session, parameters=None, system_message=None)[source]#
Make a prediction, i.e., generate an output.
This function can provide the chat history to the LLM for the output generation, simulating a conversation or remembering previous messages.
- initialize()[source]#
Instantiate the OpenAI-compatible client.
Reads
base_urlandapi_keyfromself.parametersfirst (popping them so they are not forwarded to the completion call), then falls back to the classDEFAULT_BASE_URLand the config-file propertyAPI_KEY_PROPERTY.
- intent_classification(intent_classifier, message, parameters=None)[source]#
Predict the intent of a given message.
Instead of returning only the intent with the highest likelihood, return all predictions. Predictions include not only the intent scores but other information extracted from the message.
- Parameters:
intent_classifier (LLMIntentClassifier) – the intent classifier that is running the intent classification process
message (str) – the message to predict the intent
parameters (dict) – the LLM parameters. If none is provided, the RAG’s default value will be used
- Returns:
the list of predictions made by the LLM.
- Return type:
- predict(message, parameters=None, session=None, system_message=None)[source]#
Make a prediction, i.e., generate an output.
- Parameters:
message (Any) – the LLM input text
session (Session) – the ongoing session, can be None if no context needs to be applied
parameters (dict) – the LLM parameters to use in the prediction. If none is provided, the default LLM parameters will be used
system_message (str) – system message to give high priority context to the LLM
- Returns:
the LLM output
- Return type:
- predict_with_tools(messages, tools, parameters=None, system_message=None)[source]#
Make a tool-calling prediction using the OpenAI function-calling API.
- Parameters:
messages (list[dict]) – list of OpenAI-style chat messages. Tool result messages must include
"tool_call_id"matching the call they answer.tools (list[dict]) – list of OpenAI-style tool schemas. If empty, no tools are sent and the call behaves like a normal chat completion.
parameters (dict) – extra LLM parameters. If none is provided,
self.parametersis used.system_message (str) – high-priority system message inserted before
messages.
- Returns:
either a final text response or a list of tool calls to execute.
- Return type: