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:
ChatDbMixinBase 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_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:
- append_message_plugin_selected(plugin)[source]
Append a message indicating that a plugin was selected.
- 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:
- 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]
- 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]
- 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 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
systemmessage, which would replace the LLMClient’s system role, and earlieruser,assistantortoolturns, 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.
- 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:
- Returns:
None
- Return type:
- property messages: List[Dict[str, str]] | None
Get the list of messages in the prompt.
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.
- 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:
- 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.