PromptHelper

PromptHelper for the prompt app.

class smarter.apps.prompt.models.prompt_helper.PromptHelper(request, session_key, *args, llmclient=None, **kwargs)[source]

Bases: SmarterRequestMixin

Helper class for working with Prompt objects.

This class provides methods for creating and retrieving Prompt objects, as well as managing the cache for prompt sessions. It is designed to simplify the process of interacting with prompt-related data and to ensure consistent handling of prompt sessions, llmclients, and associated metadata.

Features

  • Abstracts the logic for creating and retrieving prompt sessions.

  • Manages caching of prompt objects to improve performance and reduce database queries.

  • Provides access to related prompt history, tool calls, and plugin usage.

  • Integrates with Django’s request and session handling.

  • Ensures that prompt sessions are always associated with a valid LLMClient and Account.

Usage

Typically, this class is instantiated with a Django HttpRequest object and a session key. Optionally, a LLMClient instance can be provided to associate the prompt session with a specific llmclient.

Example

helper = PromptHelper(request, session_key)
if helper.ready:
    prompt = helper.prompt
    llmclient = helper.llmclient
    history = helper.history
Parameters:
  • request (HttpRequest) – The Django HttpRequest object for the current session.

  • session_key (Optional[str]) – The session key identifying the prompt session.

  • llmclient (Optional[LLMClient]) – An optional LLMClient instance to associate with the prompt session.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Raises:
  • SmarterValueError – If neither a session key nor a LLMClient instance is provided.

  • SmarterConfigurationError – If there is an error creating a new Prompt object.

Note

This class is intended for internal use within the Smarter platform and should not be used directly in user-facing code without proper validation.

Todo

  • Remove the session_key parameter and rely solely on the LLMClient instance for prompt session management.

See also

__init__(request, session_key, *args, llmclient=None, **kwargs)[source]

Initialize the PromptHelper instance.

Parameters:
  • request (HttpRequest) – The Django HttpRequest object for the current session.

  • session_key (Optional[str]) – The session key identifying the prompt session.

  • llmclient (Optional[LLMClient]) – An optional LLMClient instance to associate with the prompt session.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Raises:
  • SmarterValueError – If neither a session key nor a LLMClient instance is provided.

  • SmarterConfigurationError – If there is an error creating a new Prompt object.

property formatted_class_name: str[source]

Returns the formatted class name for the PromptHelper.

This property returns a string representation of the class name, formatted to include the parent class’s formatted name and the PromptHelper class. This is useful for logging and debugging purposes, as it provides a clear and consistent identifier for instances of this helper class.

Example

helper = PromptHelper(request, session_key)
helper.formatted_class_name
# 'SmarterRequestMixin.PromptHelper()'
Returns:

The formatted class name as a string, including the parent class name.

Return type:

str

get_cached_chat()[source]

Get the prompt instance for the current request.

This method retrieves the Prompt instance associated with the current session key from the cache. If the Prompt instance is not found in the cache, it attempts to retrieve it from the database. If it still cannot be found, a new Prompt instance is created using the provided LLMClient and request metadata.

Returns:

The Prompt instance associated with the current session, or None if not found.

Return type:

Optional[Prompt]

property history: dict

Serialize the most recent logged history output for the prompt session.

Returns:

A dictionary containing serialized prompt, prompt history, tool calls, and plugin usage.

Return type:

dict

property llmclient

Returns a lazy instance of the LLMClient.

Examples

  • https://hr.3141-5926-5359.alpha.api.example.com/llm-client/ returns LLMClient(name='hr', account=Account(...))

Returns:

The LLMClient instance.

Return type:

LLMClient

property prompt

Get the prompt instance for the current request.

Returns:

The Prompt instance associated with the current session.

Return type:

Prompt

property prompt_history: QuerySet | list

Get the most recent prompt history for the current prompt session.

Returns:

The most recent PromptHistory instance’s prompt_history field, or an empty list if none found.

Return type:

Union[models.QuerySet, list]

property prompt_plugin_usage: QuerySet | list

Get the most recent prompt plugin usage history for the current prompt session.

Returns:

A queryset of PromptPluginUsage instances for the current prompt session, ordered by creation date.

Return type:

Union[models.QuerySet, list]

property prompt_tool_call: QuerySet | list

Get the most recent prompt tool call history for the current prompt session.

Returns:

A queryset of PromptToolCall instances for the current prompt session, ordered by creation date.

Return type:

Union[models.QuerySet, list]

property ready: bool

Check if the PromptHelper is ready to use.

This property returns True if the prompt instance is available and all required attributes are set, otherwise returns False. It is useful for determining whether the PromptHelper is fully initialized and ready for prompt operations.

Returns:

True if the PromptHelper is ready to use, otherwise False.

Return type:

bool

to_json()[source]

Convert the PromptHelper instance to a JSON serializable dictionary.

This method returns a dictionary representation of the PromptHelper instance, including key metadata and related objects such as the prompt, llmclient, prompt history, and a unique client string.

Returns:

A dictionary containing the serialized state of the PromptHelper.

Return type:

dict[str, Any]

class smarter.apps.prompt.models.prompt_helper.PromptPluginUsageSerializer(*args, **kwargs)[source]

Bases: ModelSerializer

Serializer for the PromptPluginUsage model.

class smarter.apps.prompt.models.prompt_helper.PromptSerializer(*args, **kwargs)[source]

Bases: ModelSerializer

class smarter.apps.prompt.models.prompt_helper.PromptToolCallSerializer(*args, **kwargs)[source]

Bases: ModelSerializer

Serializer for the PromptToolCall model.

smarter.apps.prompt.models.prompt_helper.should_log_verbose(level)[source]
Return type:

bool