ai.assistant package

Subpackages

Submodules

ai.assistant.assistant module

exception ai.assistant.assistant.CompletionNotFoundException[source]

Bases: Exception

Raised when OpenAI returns no completion in its response.

exception ai.assistant.assistant.InvalidCharacterException[source]

Bases: Exception

Raised when the response contains characters outside the allowed set.

class ai.assistant.assistant.AssistantName(*values)[source]

Bases: Enum

Identifiers for various assistant roles.

Email_Answer = 'Email Answer'
Topic_Finder = 'Topic Finder'
Email_Generation = 'Email Generation'
Email_Translation = 'Email Translation'
Paraphrase = 'Paraphrase'
Tag_Generation = 'Tag Generation'
ai.assistant.assistant.log_error(retry_state)[source]

Log exceptions that occur during retry operations.

Parameters:

retry_state – Tenacity retry state, containing exception info.

class ai.assistant.assistant.Assistant(**data)[source]

Bases: Generic[T]

AI assistant wrapper for OpenAI chat, with JSON parsing and retry logic.

name

Role identifier.

Type:

AssistantName

ai_model

Model selection for OpenAI.

Type:

AssistantChatModel

base_prompt

System prompt prefix for each call.

Type:

str

ai_model_provider

API provider configuration.

Type:

ModelProvider

temperature

Controls randomness of responses.

Type:

float

name: AssistantName
ai_model: AssistantChatModel
base_prompt: str
ai_model_provider: ModelProvider
temperature: float = 1.0
class Config[source]

Bases: object

arbitrary_types_allowed = True
__init__(**data)[source]

Initialize the Assistant and OpenAI client.

Warns if temperature is outside recommended bounds.

Parameters:

**data – Fields for name, ai_model, base_prompt, ai_model_provider, temperature.

async run_openai(prompt: str) ChatCompletion[source]

Invoke the OpenAI chat completion API.

Parameters:

prompt (str) – User prompt to append to the base prompt.

Returns:

Raw API response.

Return type:

ChatCompletion

async save_parse(prompt: str, output_type: type[T], allowed_chars: str | None = None) T[source]

Call OpenAI, parse JSON from Markdown, and instantiate a dataclass.

Parameters:
  • prompt (str) – User prompt.

  • output_type (type[T]) – Dataclass type to map parsed JSON onto.

  • allowed_chars (str | None) – If provided, characters allowed in the raw response.

Returns:

An instance of output_type with parsed data.

Return type:

T

Raises:
  • InvalidCharacterException – If has_invalid_characters detects forbidden chars.

  • JSONDecodeError – If JSON parsing fails.

async save_chat(prompt: str, allowed_chars: str | None = None) str[source]

Call chat and enforce character validity on the response.

Parameters:
  • prompt (str) – User prompt.

  • allowed_chars (str | None) – Allowed character set regex.

Returns:

Validated response content.

Return type:

str

Raises:

InvalidCharacterException – When forbidden characters are present.

async chat(prompt: str) str[source]

High-level chat method with retry on network and rate errors.

Parameters:

prompt (str) – User prompt.

Returns:

Assistant’s response text.

Return type:

str

Raises:

CompletionNotFoundException – If no choice is returned in the API response.

ai.assistant.assistant_test module

Module contents