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 llm_client. 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=0.5, default_max_tokens=2048, valid_chat_completion_models=None, add_built_in_tools=False, **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 built-in tools (e.g., weather, date calculator) and plugin integration.

  • 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 llm_client. 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=0.5, default_max_tokens=2048, valid_chat_completion_models=None, add_built_in_tools=False, **kwargs)[source]

Initialize the SmarterChatProviderBase with the given parameters.

Parameters:
  • provider (Optional[Provider]) – The Provider instance for the prompt provider.

  • provider_name (Optional[str]) – The name of the prompt provider_name (e.g., “openai”, “google”).

  • base_url (Optional[str]) – The base URL for the prompt provider_name’s API.

  • api_key (Optional[SecretStr]) – The API key for authenticating with the prompt provider_name.

  • default_model (Optional[str]) – The default model to use for prompt completions.

  • default_system_role (Optional[str]) – The default system role to use in the message thread.

  • default_temperature (Optional[float]) – The default temperature to use for prompt completions.

  • default_max_tokens (Optional[int]) – The default maximum number of tokens for prompt completions.

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

  • add_built_in_tools (Optional[bool]) – Whether to add built-in tools (weather and date calculator) to 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 (Union[dict[str, Any], list, str, None]) – The content of the message. Can be a string, dict[str, Any], or list.

  • message (Optional[dict[str, Any]]) – 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)[source]

Append a message indicating that a tool was called.

Parameters:

tool_call (Union[ChatCompletionMessageFunctionToolCall, ChatCompletionMessageCustomToolCall]) – The tool call object containing function name and arguments.

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 a new message thread with a system prompt.

and the incoming data. This method ensures that the system role is present in the message thread.

Raises:

SmarterValueError – If the request body is invalid.

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
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]]]

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

Return type:

Optional[dict[str, Any]]

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

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