Chat Provider Base

Base class for prompt providers.

class smarter.apps.provider.services.text_completion.lib.chat_provider_base.SmarterChatProviderBase(provider, provider_name, base_url, api_key, default_model, default_system_role='You are a helpful llmclient. When given the opportunity to utilize function calling, you should always do so. This will allow you to provide the best possible responses to the user. If you are unable to provide a response, you should prompt the user for more information. If you are still unable to provide a response, you should inform the user that you are unable to help them at this time.', default_temperature=1.0, default_max_tokens=2048, valid_chat_completion_models=None, **kwargs)[source]

Bases: ChatDbMixin

Base class for all Smarter prompt providers.

This class defines the core interface and shared logic for prompt providers that interact with LLMClients via the Smarter API, including both the Reactapp and deployed LLMClients accessible through named URLs. It is designed to be subclassed by specific provider_name implementations.

The API is largely a superset of the OpenAI prompt completion API, with additional properties and methods for configuring the Reactapp and for managing additional message content types that are proprietary to Smarter.

Key Features:

  • Provides a unified interface for prompt providers, supporting both OpenAI-compatible and proprietary Smarter features.

  • Handles message thread management, including system prompts, user/assistant messages, and plugin/tool messages.

  • Supports tools from plugins, MCPClients and extensions.

  • Manages provider_name configuration, validation, and readiness checks.

  • Tracks token usage and charge insertion for billing/auditing.

Usage:

Subclass this base class to implement a new prompt provider_name. Override or extend methods as needed for provider_name-specific logic.

Example:
class MyProvider(SmarterChatProviderBase):
    def my_custom_method(self):
        # Custom logic here
        pass
__init__(provider, provider_name, base_url, api_key, default_model, default_system_role='You are a helpful llmclient. When given the opportunity to utilize function calling, you should always do so. This will allow you to provide the best possible responses to the user. If you are unable to provide a response, you should prompt the user for more information. If you are still unable to provide a response, you should inform the user that you are unable to help them at this time.', default_temperature=1.0, default_max_tokens=2048, valid_chat_completion_models=None, **kwargs)[source]

Initialize the SmarterChatProviderBase with the given parameters.

Parameters:
  • provider (Provider | None) – The Provider instance for the prompt provider.

  • provider_name (str | None) – The name of the prompt provider_name (e.g., “openai”, “google”).

  • base_url (str | None) – The base URL for the prompt provider_name’s API.

  • api_key (SecretStr | None) – The API key for authenticating with the prompt provider_name.

  • default_model (str | None) – The default model to use for prompt completions.

  • default_system_role (str | None) – The default system role to use in the message thread.

  • default_temperature (float | None) – The default temperature to use for prompt completions.

  • default_max_tokens (int | None) – The default maximum number of tokens for prompt completions.

  • valid_chat_completion_models (list[str] | None) – A list of valid prompt completion models for the provider_name.

property api_key: str | None

Get the API key of the prompt provider_name.

This property returns the unmasked internal _api_key attribute.

Returns:

The unmasked API key of the prompt provider_name.

Return type:

Optional[str]

append_error_response(error_message)[source]

Append a message indicating that an error occurred.

Parameters:

error_message (str) – The error message to append.

Returns:

None

Return type:

None

append_message(role, content, message=None)[source]

Append a message to the internal message list.

This method validates the role and content before appending the message.

Parameters:
  • role (str) – The role of the message (e.g., “user”, “assistant”, “system”).

  • content (dict[str, Any] | list | str | None) – The content of the message. Can be a string, SmarterDictType, or list.

  • message (dict[str, Any] | None) – An optional message dictionary to append. If provided, it will be used instead of creating a new message.

Raises:

SmarterValueError – If the role is invalid or if both content and message are empty.

Returns:

None

Return type:

None

append_message_plugin_selected(plugin)[source]

Append a message indicating that a plugin was selected.

Parameters:

plugin (str) – The name of the selected plugin.

Returns:

None

Return type:

None

append_message_tool_called(tool_call, tool_name=None)[source]

Append a message indicating that a tool was called.

Parameters:
  • tool_call (ChatCompletionMessageFunctionToolCall | ChatCompletionMessageCustomToolCall) – The tool call object containing function name and arguments.

  • tool_name (str | None) – The tool’s name as users know it, e.g. a plugin’s name rather than its symbolic function name, smarter_plugin_0000000016. Defaults to the function name. The tool call’s json, below the message, keeps the function name that the LLM called.

Returns:

None

Return type:

None

available_functions: dict[str, Any]
property base_url: str | None

Get the base URL of the prompt provider_name.

This property returns the internal _base_url attribute.

Returns:

The base URL of the prompt provider_name.

Return type:

Optional[str]

completion_tokens: int | None
data: dict[str, Any] | None
property default_max_tokens: int | None

Get the default max completion tokens of the prompt provider_name.

This property returns the internal _default_max_completion_tokens attribute.

Returns:

The default max completion tokens of the prompt provider_name.

Return type:

Optional[int]

property default_model: str | None

Get the default model of the prompt provider_name.

This property returns the internal _default_model attribute.

Returns:

The default model of the prompt provider_name.

Return type:

Optional[str]

property default_system_role: str | None

Get the default system role of the prompt provider_name.

This property returns the internal _default_system_role attribute.

Returns:

The default system role of the prompt provider_name.

Return type:

Optional[str]

property default_temperature: float | None

Get the default temperature of the prompt provider_name.

This property returns the internal _default_temperature attribute.

Returns:

The default temperature of the prompt provider_name.

Return type:

Optional[float]

first_iteration: dict[str, Any]
first_response: ChatCompletion | None
property formatted_class_name: str[source]

Returns the class name in a formatted string along with the name of this mixin.

Returns:

The formatted class name.

Return type:

str

functions: List[str] | None
get_input_text_prompt(data)[source]

Extract the input text prompt from the incoming data.

This method validates that the input text is present and is a string.

Raises:

SmarterValueError – If the input text is missing or invalid.

Parameters:

data (dict[str, Any]) – The incoming data containing the input text.

Returns:

The input text prompt.

Return type:

str

get_message_thread(data)[source]

Initialize the message thread of a session that has no stored history.

The server is the authority on the system prompt and on the conversation’s history, so the thread is built from the LLMClient’s configuration, plus the client’s latest user message, and nothing else:

  • the LLMClient’s system role, which starts with the date and time. When the prompt has no LLMClient, or the LLMClient has no system role, the date and time only.

  • the LLMClient’s welcome message and example prompts, as assistant messages, which is the introduction that Smarter Chat displays at the start of a session.

  • the client’s latest user message, which the input guardrails scan.

Everything else that the client sent is dropped: a system message, which would replace the LLMClient’s system role, and earlier user, assistant or tool turns, which no guardrail scans. Smarter Chat, the cli and the SDKs send a copy of the system role and of the introduction, which is expected. Anything else is logged as a warning, without its content. A session that has a stored history uses that history instead, so a client’s messages never reach the LLM, other than the latest user message.

Raises:

SmarterValueError – If the request body is invalid, or has no user message.

Parameters:

data (dict[str, Any]) – The incoming data containing the message thread.

Returns:

The initialized message thread.

Return type:

List[Dict[str, str]]

input_text: str | None
iteration: int
log_dropped_client_messages(client_message_thread, system_role, welcome_message, example_prompts)[source]

Log the messages of a new session’s request that get_message_thread() dropped.

Every message except the latest user message is dropped. A copy of the LLMClient’s system role, welcome message or example prompts, which Smarter Chat, the cli and the SDKs send at the start of a session, is expected, and logged at debug level. Any other message, such as a system message that would replace the system role, or an earlier turn of the conversation, is logged as a warning. Only the number of messages, and their roles, are logged, never their content.

Parameters:
  • client_message_thread (list[dict[str, Any]]) – The messages that the client sent.

  • system_role (str | None) – The LLMClient’s system role, without the date and time.

  • welcome_message (str) – The LLMClient’s welcome message.

  • example_prompts (list[str]) – The LLMClient’s example prompts.

Returns:

None

Return type:

None

max_completion_tokens: int | None
property messages: List[Dict[str, str]] | None

Get the list of messages in the prompt.

This property returns the internal _messages attribute.

Returns:

The list of messages.

Return type:

Optional[List[Dict[str, str]]]

Setter accepts:

List[Dict[str, str]]

messages_set_is_new(messages, is_new=False)[source]

Set the is_new flag for all messages in the message thread.

This is used to track which messages are new and which have already been processed.

This affects the treatment of messages in the Reactapp component, where new messages are styled differently.

Parameters:

messages (list[dict[str, Any]]) – The list of messages to set the is_new flag for.

Return type:

list[dict[str, Any]]

model: str | None
model_warnings: set[str]
plugins: List[PluginBase] | None
prompt_tokens: int | None
prune_empty_values(data)[source]

Remove empty values from a dictionary.

Some LLM providers, including MetaAI and GoogleAI will break if empty values are present in the completion request body.

Parameters:

data (dict[str, Any]) – The completion request body.

Return type:

dict[str, Any]

Returns:

A copy of data without its None values, at every depth.

Raises:

SmarterValueError – If data is not a dictionary.

raise_missing_api_key()[source]

Log, with instructions, that the provider has no API key, and raise.

A built-in Provider is created without a key when its Secret doesn’t exist, so that the platform runs without it. Its prompts fail here, with an error that the prompt’s response reports, rather than in the provider’s SDK. The key’s only source is the Provider’s API key Secret, which no service reads from the environment (ADR-038, phase 3): the error says how to set the Secret.

Raises:

SmarterConfigurationError – Always.

property ready: bool[source]

Check if the prompt provider_name is ready to process requests.

Returns:

True if the prompt provider_name is ready, False otherwise.

Return type:

bool

reasoning_effort: str | None
reference: str | None
request_meta_data: dict[str, Any]
second_iteration: dict[str, Any] | None
second_response: ChatCompletion | None
serialized_tool_calls: list[dict[str, Any]] | None
temperature: float | None
tools: list[dict[str, Any]] | None
total_tokens: int | None
property url: str | None

Get the full URL for prompt completions.

This property constructs the URL by appending the prompt completions endpoint to the base URL.

Examples:

If the base URL is “https://api.some-llm-company.com”, the full URL will be “https://api.some-llm-company.com/v1/prompt/completions”.

Returns:

The full URL for prompt completions.

Return type:

Optional[str]

property valid_chat_completion_models: list[str] | None

Get the list of valid prompt completion models for the prompt provider_name.

This property returns the internal _valid_chat_completion_models attribute.

A valid prompt completion model is one that is supported by the prompt provider_name’s API for prompt completions.

Returns:

The list of valid prompt completion models.

Return type:

Optional[list[str]]

validate()[source]

Validate that all required properties are set.

Raise SmarterValueError if any required property is missing.

Raises:

SmarterValueError – If any required property is missing.

Returns:

None

Return type:

None