Providers

Smarter.apps.provider.services.text_completion.providers

Service-level entry point for text completions supporting multiple LLM provider companies. This module provides a unified interface for accessing and managing prompt completion providers, enabling seamless integration with a variety of large language model (LLM) backends.

Protocols Supported:

  1. Smarter Prompt Protocol
    • Implements: SmarterChatHandlerProtocol

    • Indirect service layer for /api/v1/prompts/smarter/<str:provider>/

    • Returns: SmarterChatCompletionResponseType

    • Used for native Smarter prompt API requests, supporting Smarter’s extensibility model.

  2. OpenAI-Compatible Passthrough Protocol
    • Implements: OpenAICompatiblePassthroughProtocol

    • Indirect service layer for /api/v1/prompts/passthrough/<str:provider>/

    • Returns: OpenAICompatibleChatCompletionResponseType

    • Used for OpenAI-compatible API passthrough, enabling direct proxying to third-party LLM providers.

Key Features:

  • Centralized access to all configured prompt providers and their handlers.

  • Supports both Smarter-native and OpenAI-compatible request/response formats.

  • Provides default provider selection and handler resolution.

  • Abstracts provider-specific complexities, including authentication and model selection.

  • Enables dynamic handler retrieval for both protocols, facilitating flexible integration patterns.

Common Features:

  • Both protocols support dynamic provider selection based on the incoming request and user context.

  • Handlers for both protocols are designed to abstract away provider-specific details, such as authentication and model selection, allowing for flexible integration patterns.

  • The factory class provides caching for provider ORM retrieval and client instantiation to optimize performance and reduce redundant database queries.

  • Internal billing records are generated in a consistent manner regardless of the protocol used, ensuring accurate usage tracking and billing across all providers.

  • Application-level logging is fully integrated into both protocols, with support for logging based on waffle switches to facilitate debugging and monitoring in production environments.

Singletons:

smarter.apps.provider.services.text_completion.providers.smarter_compatible_client: OpenAICompatibleClientFactory

Singleton instance of OpenAICompatibleClientFactory configured for the Smarter-native protocol. This is the main entry point for consumers needing Smarter-native prompt completion handling.

smarter.apps.provider.services.text_completion.providers.openai_compatible_client: OpenAICompatibleClientFactory

Singleton instance of OpenAICompatibleClientFactory configured for the OpenAI-compatible passthrough protocol. This is the main entry point for consumers needing OpenAI-compatible prompt completion handling and passthrough.

class smarter.apps.provider.services.text_completion.providers.ClientTypeEnum(*values)[source]

Bases: SmarterEnumAbstract

Client type distinguishes between the kind of handler we want.

from the provider.

PASSTHROUGH = 'OpenAIPassthroughClient'
SMARTER = 'OpenAISmarterClient'
class smarter.apps.provider.services.text_completion.providers.OpenAICompatibleClientFactory(client_type=ClientTypeEnum.SMARTER)[source]

Bases: SmarterHelperMixin

Service-level factory for OpenAI-compatible prompt completion clients.

This class provides a unified interface for instantiating and managing prompt completion clients that support both Smarter-native and OpenAI-compatible passthrough protocols. It enables seamless integration with multiple LLM provider backends, abstracting provider-specific complexities such as authentication, model selection, and handler resolution.

Key Features:

  • Centralized access to all configured prompt providers and their handlers.

  • Supports both Smarter-native and OpenAI-compatible request/response formats.

  • Provides default provider selection and handler resolution.

  • Abstracts provider-specific details, including authentication and model selection.

  • Enables dynamic handler retrieval for both protocols, facilitating flexible integration patterns.

Parameters:

client_type (Optional[ClientTypeEnum]) – The type of client to instantiate (SMARTER or PASSTHROUGH).

__init__(client_type=ClientTypeEnum.SMARTER)[source]

Note: this needs to exist.

something in the Python MRO requires it, even if it does nothing. If you remove this, you will get a mysterious error about something downstream expecting exactly one object.

property all: List[str][source]

Returns a list of all provider names.

Returns:

A list of all active provider names.

Return type:

List[str]

property client_type: ClientTypeEnum

Returns the client type of this factory instance.

Returns:

The client type (SMARTER or PASSTHROUGH).

Return type:

ClientTypeEnum

default_handler(request, **kwargs)[source]

A convenience method to get the default handler.

Parameters:

request (Union[ASGIRequest, Request, HttpRequest]) – The incoming HTTP request object.

Returns:

A handler function that can be used to process prompt completion requests according to the specified protocol.

Return type:

Union[SmarterChatHandlerProtocol, OpenAICompatiblePassthroughProtocol]

property default_handler_name: str[source]

Returns the name of the platform-wide default provider.

If no default provider is found, it raises a SmarterValueError.

Returns:

The name of the default provider.

Return type:

str

Raises:

SmarterValueError – If no default provider is found.

get_client_orm_by_provider_name_and_user(provider_name, user)[source]

Retrieves the Provider ORM instance for the given provider name and user.

Parameters:
  • provider_name (str) – The name of the provider to retrieve.

  • user (User) – The user for whom to retrieve the provider.

Returns:

The Provider ORM instance.

Return type:

Provider

Raises:

SmarterValueError – If the provider is not found for the user.

get_openai_client_for_provider(provider_name, user)[source]

Instantiates an OpenAIPassthroughClient for the given provider name and user.

Parameters:
  • provider_name (str) – The name of the provider for which to instantiate the client.

  • user (User) – The user for whom to instantiate the client.

Returns:

An instance of OpenAIPassthroughClient configured for the specified provider and user.

Return type:

OpenAIPassthroughClient

get_passthrough_handler(request, provider_name=None, **kwargs)[source]

A convenience method to get an OpenAI-compatible passthrough handler by provider name.

Parameters:
  • request (Union[ASGIRequest, Request, HttpRequest]) – The incoming HTTP request object.

  • provider_name (Optional[str]) – The name of the provider for which to retrieve the handler. If not provided, the default provider will be used.

Returns:

An OpenAI-compatible passthrough handler function that can be used to process prompt completion requests.

Return type:

OpenAICompatiblePassthroughProtocol

get_smarter_handler(request, provider_name=None, **kwargs)[source]

A convenience method to get a handler by provider name.

Parameters:
  • request (Union[ASGIRequest, Request, HttpRequest]) – The incoming HTTP request object.

  • provider_name (Optional[str]) – The name of the provider for which to retrieve the handler. If not provided, the default provider will be used.

Returns:

A handler function that can be used to process prompt completion requests according to the Smarter prompt protocol.

Return type:

SmarterChatHandlerProtocol

handler(request, provider_name=None, **kwargs)[source]

A convenience method to get a handler by provider name.

Parameters:
  • request (Union[ASGIRequest, Request, HttpRequest]) – The incoming HTTP request object.

  • provider_name (Optional[str]) – The name of the provider for which to retrieve the handler. If not provided, the default provider will be used.

Returns:

A handler function that can be used to process prompt completion requests according to the specified protocol.

Return type:

Union[SmarterChatHandlerProtocol, OpenAICompatiblePassthroughProtocol]

smarter.apps.provider.services.text_completion.providers.should_log(level)[source]

Check if logging should be done based on the waffle switch.