import injector
from ai.analysis.cost_analyzer_decorator import cost_analyzer
from ai.analysis.dataset_usage_analyzer import DatasetUsageAnalyzer
from ai.assistant.assistant import Assistant
[docs]
class ChatAssistantFactory:
"""Factory for creating Assistant instances instrumented with cost analysis."""
_instance = None
[docs]
@injector.inject
def __init__(self, dataset_usage_analyzer: DatasetUsageAnalyzer):
"""Initialize the factory with a DatasetUsageAnalyzer.
Args:
dataset_usage_analyzer (DatasetUsageAnalyzer): Analyzer to track usage.
"""
self._dataset_usage_analyzer = dataset_usage_analyzer
[docs]
def create(self, assistant: Assistant) -> Assistant:
"""Create a cost-instrumented Assistant from an existing one.
Applies the cost_analyzer decorator to the Assistant class,
then instantiates it using the original assistant’s attributes.
Args:
assistant (Assistant): The base assistant to instrument.
Returns:
Assistant: A new Assistant instance with cost logging enabled.
"""
# Decorate the Assistant class with cost analysis
UsageAnalyzedAssistant = cost_analyzer(
dataset_usage_analyzer=self._dataset_usage_analyzer
)(Assistant)
# Instantiate using the original assistant’s serialized state
return UsageAnalyzedAssistant(**assistant.model_dump())