Chat Provider Base
Base class for chat 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 chatbot. 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:
ChatDbMixinBase class for all Smarter chat providers.
This class defines the core interface and shared logic for chat providers that interact with ChatBots via the Smarter API, including both the Reactapp and deployed ChatBots accessible through named URLs. It is designed to be subclassed by specific provider_name implementations.
The API is largely a superset of the OpenAI chat 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 chat 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 chat 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 chatbot. 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 chat provider.provider_name (
Optional[str]) – The name of the chat provider_name (e.g., “openai”, “google”).base_url (
Optional[str]) – The base URL for the chat provider_name’s API.api_key (
Optional[SecretStr]) – The API key for authenticating with the chat provider_name.default_model (
Optional[str]) – The default model to use for chat 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 chat completions.default_max_tokens (
Optional[int]) – The default maximum number of tokens for chat completions.valid_chat_completion_models (
Optional[list[str]]) – A list of valid chat 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 chat provider_name. This property returns the unmasked internal _api_key attribute.
- Returns:
The unmasked API key of the chat provider_name.
- Return type:
Optional[str]
- 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:
- append_message_plugin_selected(plugin)[source]
Append a message indicating that a plugin was selected.
- 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:
- property base_url: str | None
Get the base URL of the chat provider_name. This property returns the internal _base_url attribute.
- Returns:
The base URL of the chat provider_name.
- Return type:
Optional[str]
- property default_max_tokens: int | None
Get the default max completion tokens of the chat provider_name. This property returns the internal _default_max_completion_tokens attribute.
- Returns:
The default max completion tokens of the chat provider_name.
- Return type:
Optional[int]
- property default_model: str | None
Get the default model of the chat provider_name. This property returns the internal _default_model attribute.
- Returns:
The default model of the chat provider_name.
- Return type:
Optional[str]
- property default_system_role: str | None
Get the default system role of the chat provider_name. This property returns the internal _default_system_role attribute.
- Returns:
The default system role of the chat provider_name.
- Return type:
Optional[str]
- property default_temperature: float | None
Get the default temperature of the chat provider_name. This property returns the internal _default_temperature attribute.
- Returns:
The default temperature of the chat provider_name.
- Return type:
Optional[float]
- 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:
- 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.
- 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.
- property messages: List[Dict[str, str]] | None
Get the list of messages in the chat. This property returns the internal _messages attribute.
- 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.
- plugins: List[PluginBase] | 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.
- property ready: bool[source]
Check if the chat provider_name is ready to process requests.
- Returns:
True if the chat provider_name is ready, False otherwise.
- Return type:
- property url: str | None
Get the full URL for chat completions. This property constructs the URL by appending the chat 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/chat/completions”.
- Returns:
The full URL for chat completions.
- Return type:
Optional[str]
- property valid_chat_completion_models: list[str] | None
Get the list of valid chat completion models for the chat provider_name. This property returns the internal _valid_chat_completion_models attribute.
A valid chat completion model is one that is supported by the chat provider_name’s API for chat completions.