API V1 Base Views

LLMClient api/v1/llm_clients base view, for invoking a LLMClient.

class smarter.apps.llm_client.api.v1.views.base.LLMClientApiBaseViewSet(**kwargs)[source]

Bases: SmarterAuthenticatedNeverCachedWebView

Base viewset for all LLMClient API endpoints.

This class serves as the foundational viewset for all llm_client-related APIs in the Smarter platform, including prompt completions that leverage the Smarter LLM Tool Call Plugin architecture.

Key Responsibilities:

  • API Key Authentication and Request Validation: Enforces authentication for all API requests, rejecting those without a valid API key.

  • Lifecycle Management: Handles initialization of Account, LLMClient, LLMClientHelper, and PromptHelper objects, and manages request dispatching and routing to the appropriate handler methods.

  • Plugin Discovery and Extensibility: Discovers and initializes plugins for llm_client extensibility, supporting the Smarter LLM Tool Call Plugin architecture.

  • Logging and Observability: Provides robust logging and observability for all major lifecycle events, including error handling.

Django Integration:

  • Subclasses Django’s view-template system (not DRF), participating in the standard request/response lifecycle.

  • Overrides and extends methods such as setup(), dispatch(), get(), and post() to provide llm_client-specific logic.

  • CSRF-exempt to support API clients.

Prompt Completion & LLM Tool Call Plugins:

  • This base view is designed to support prompt completion endpoints that utilize Smarter’s LLM Tool Call Plugin architecture.

  • Plugins can be discovered and invoked as part of the llm_client’s response generation, enabling extensible and dynamic tool use.

Examples:

  • https://customer-support.3141-5926-5359.api.example.com/

  • https://platform.smarter/workbench/example/

  • https://platform.smarter/api/v1/workbench/1/prompt/

Notes:

  • Intended to be subclassed by concrete llm_client API views.

  • Provides robust error handling and logging for all major operations.

  • Authentication is enforced by default.

  • CSRF-exempt for API compatibility.

See Also:
property chat_helper: PromptHelper

Returns the PromptHelper instance.

Lazily initializes the PromptHelper if it hasn’t been created yet.

Returns:

The PromptHelper instance.

Return type:

PromptHelper

dispatch(request, *args, name=None, **kwargs)[source]

Dispatch method for the LLMClient API base viewset.

This method is invoked as part of the Django REST Framework (DRF) view lifecycle. It is responsible for preparing the viewset for request processing, including initializing the LLMClientHelper and PromptHelper instances, setting up the request context, and logging relevant information for observability and debugging.

The dispatch method performs the following key actions:

  • Extracts and sets the llm_client ID from the URL parameters, if present.

  • Initializes the LLMClient and Account context for the request.

  • Validates the existence and readiness of the LLMClientHelper and LLMClient instances.

  • Handles error conditions such as missing or invalid llm_client configuration, returning appropriate HTTP error responses.

  • Loads and attaches plugins for the llm_client, if available.

  • Emits signals and logs key request metadata for auditing and debugging.

  • Calls the parent class’s dispatch method to continue the DRF request/response lifecycle.

Parameters:
  • request (ASGIRequest) – The HTTP request object provided by Django, containing all request data, headers, and user context.

  • *args – Additional positional arguments passed to the view.

  • name (Optional[str]) – The name of the llm_client, if provided as a URL parameter.

  • **kwargs – Additional keyword arguments passed to the view, often including URL parameters.

Returns:

A Django JsonResponse or HttpResponse object representing the result of the request, or an error response if initialization fails.

Return type:

JsonResponse or HttpResponse

Notes

  • This method is a critical integration point with DRF’s request/response lifecycle.

  • It ensures that all necessary context, helpers, and plugins are available before the main handler methods are called.

  • Subclasses may override this method to provide additional dispatch logic, but should always call super().dispatch() to preserve base functionality.

See also

-, -

property formatted_class_name: str

Returns the class name in a formatted string.

along with the name of this mixin.

Returns:

Formatted class name string.

Return type:

str

functions: list[str] | None = None
get(request, *args, name=None, **kwargs)[source]

GET request handler for the Smarter Prompt API.

Currently, GET requests are not supported and will return a message indicating that POST should be used instead.

Parameters:

request (ASGIRequest) – The HTTP request object.

Returns:

A JsonResponse indicating that GET is not supported.

Return type:

JsonResponse

helper_logger(message)[source]

Create a log entry.

Parameters:

message (str) – The message to log.

http_method_names: list[str] = ['get', 'post', 'options']
property is_web_platform

Determine if the request is from the web platform domain.

Returns:

True if the request is from the web platform domain, False otherwise.

Return type:

bool

property llm_client

Returns the LLMClient instance.

Returns:

The LLMClient instance.

Return type:

Optional[LLMClient]

property llm_client_helper: LLMClientHelper | None

Returns the LLMClientHelper instance.

Lazily initializes the LLMClientHelper if it hasn’t been created yet.

Returns:

The LLMClientHelper instance.

Return type:

Optional[LLMClientHelper]

property llm_client_id

Returns the llm_client ID.

Returns:

The llm_client ID.

Return type:

Optional[int]

property name

Returns the name of the llm_client.

Returns:

The name of the llm_client.

Return type:

Optional[str]

options(request, *args, **kwargs)[source]

OPTIONS request handler for the Smarter Prompt API.

Sets CORS headers to allow cross-origin requests from the Smarter environment URL.

Parameters:

request (ASGIRequest) – The HTTP request object.

plugins: List[PluginBase] | None = None
post(request, *args, name=None, **kwargs)[source]

POST request handler for the Smarter Prompt API.

This method processes POST requests to the llm_client API endpoint. It determines which LLMClient instance to use based on the request’s host, supporting both default API domains and custom domains. The logic ensures that the correct LLMClient is selected for each request, and that all necessary context and helpers are available for downstream processing.

Hostname Resolution

The LLMClient instance is determined by parsing the request host. There are two supported formats:

  1. URL with default API domain

    Example: https://customer-support.3141-5926-5359.api.example.com/llm-client/ - customer-support: The llm_client’s name. - 3141-5926-5359: The llm_client’s account number. - api.example.com: The default API domain.

  2. URL with custom domain

    Example: https://api.example.com/llm-client/ - api.example.com: The llm_client’s custom domain. - The custom domain must be verified (LLMClientCustomDomain.is_verified == True).

The LLMClient instance hostname is determined by: llm_client.hostname == llm_client.custom_domain or llm_client.default_host

Processing Steps

  • Logs key request and context information for observability.

  • Validates that a LLMClient instance is available; returns an error response if not found.

  • Retrieves the appropriate prompt provider handler for the LLMClient.

  • Ensures a valid PromptHelper instance is available; returns an error response if not found.

  • Invokes the prompt provider handler with the prompt session, request data, plugins, and user context.

  • Wraps the response in a SmarterJournaledJsonResponse for consistent API output.

param request:

The HTTP request object provided by Django, containing all request data, headers, and user context.

type request:

ASGIRequest

param *args:

Additional positional arguments passed to the view.

param name:

The name of the llm_client, if provided as a URL parameter.

type name:

Optional[str]

param **kwargs:

Additional keyword arguments passed to the view, often including URL parameters.

returns:

A structured JSON response containing the result of the prompt operation, or an error response if the LLMClient or PromptHelper could not be initialized.

rtype:

SmarterJournaledJsonResponse

Notes

  • This method is a critical integration point for llm_client conversations in the Smarter platform.

  • It enforces domain-based routing and robust error handling for missing or invalid llm_client context.

  • The response format is standardized for journaling and auditing purposes.

See also

-, -, -

setup(request, *args, **kwargs)[source]

Set up the LLMClient API base viewset for request processing.

This method is called as part of the Django REST Framework (DRF) view lifecycle, immediately after the view instance is created and before the request is dispatched to the appropriate handler method (such as get() or post()).

The primary responsibilities of this method are to:

  • Initialize the SmarterRequestMixin with the current request and any additional arguments.

  • Prepare and set up the LLMClientHelper and PromptHelper instances, which are used throughout the request lifecycle for llm_client-specific logic and prompt session management.

  • Log key setup events for observability and debugging.

Parameters:
  • request (ASGIRequest) – The HTTP request object provided by Django, containing all request data, headers, and user context.

  • *args – Additional positional arguments passed to the view.

  • **kwargs – Additional keyword arguments passed to the view, often including URL parameters.

Notes

  • This method is a critical integration point with DRF’s request/response lifecycle.

  • It ensures that all necessary context and helper objects are available before the main handler methods are called.

  • Subclasses may override this method to provide additional setup logic, but should always call super().setup() to preserve base functionality.

See also

-, -, -

property url: ParseResult | None

Returns the URL of the llm_client.

Returns:

The URL of the llm_client.

Return type:

Optional[ParseResult]