ai.assistant package¶
Subpackages¶
Submodules¶
ai.assistant.assistant module¶
- exception ai.assistant.assistant.CompletionNotFoundException[source]¶
Bases:
ExceptionRaised when OpenAI returns no completion in its response.
- exception ai.assistant.assistant.InvalidCharacterException[source]¶
Bases:
ExceptionRaised when the response contains characters outside the allowed set.
- class ai.assistant.assistant.AssistantName(*values)[source]¶
Bases:
EnumIdentifiers 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(*, name: AssistantName, ai_model: AssistantChatModel, base_prompt: str, ai_model_provider: ModelProvider, temperature: float = 1.0)[source]¶
Bases:
BaseModel,Generic[T]AI assistant wrapper for OpenAI chat, with JSON parsing and retry logic.
- name¶
Role identifier.
- Type:
- ai_model¶
Model selection for OpenAI.
- Type:
- base_prompt¶
System prompt prefix for each call.
- Type:
str
- ai_model_provider¶
API provider configuration.
- Type:
- temperature¶
Controls randomness of responses.
- Type:
float
- name: AssistantName¶
- ai_model: AssistantChatModel¶
- base_prompt: str¶
- ai_model_provider: ModelProvider¶
- temperature: float¶
- __init__(**data: dict[str, Any]) None[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.
- model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}¶
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[dict[str, FieldInfo]] = {'ai_model': FieldInfo(annotation=AssistantChatModel, required=True), 'ai_model_provider': FieldInfo(annotation=ModelProvider, required=True), 'base_prompt': FieldInfo(annotation=str, required=True), 'name': FieldInfo(annotation=AssistantName, required=True), 'temperature': FieldInfo(annotation=float, required=False, default=1.0)}¶
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo].
This replaces Model.__fields__ from Pydantic V1.
ai.assistant.assistant_test module¶
- class ai.assistant.assistant_test.TestModule[source]¶
Bases:
Module- provide_dataset_usage_analyzer() DatasetUsageAnalyzer[source]¶