llm_openai_compatible#

class baf.nlp.llm.llm_openai_compatible.LLMOpenAICompatible(agent, name, parameters, num_previous_messages=1, global_context=None)[source]#

Bases: LLM

Shared 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 – the Property constant from baf.nlp that holds the API key for the provider.

Per-instance overrides are also accepted via the parameters dict: if parameters contains "base_url" or "api_key" keys, they take priority over the class defaults and the config-file values respectively. Both keys are popped from parameters in initialize() so they are never forwarded to chat.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 by initialize() 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 to MonitoringDB.

  • 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.

Parameters:
  • session (Session) – the user session

  • parameters (dict) – the LLM parameters. If none is provided, the RAG’s default value will be used

  • system_message (str) – system message to give high priority context to the LLM

Returns:

the LLM output

Return type:

str

initialize()[source]#

Instantiate the OpenAI-compatible client.

Reads base_url and api_key from self.parameters first (popping them so they are not forwarded to the completion call), then falls back to the class DEFAULT_BASE_URL and the config-file property API_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:

list[IntentClassifierPrediction]

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:

str

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.parameters is 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:

LLMResponse

set_model(name)[source]#

Set the LLM model name.

Parameters:

name (str) – the new model identifier

set_num_previous_messages(num_previous_messages)[source]#

Set the number of previous messages used in chat().

Parameters:

num_previous_messages (int) – the new number of previous messages