|
| 1 | +from __future__ import annotations |
| 2 | +from typing import Optional, TypeVar, Generic, TYPE_CHECKING |
| 3 | +from datetime import timedelta |
| 4 | +import enum |
| 5 | + |
| 6 | +if TYPE_CHECKING: |
| 7 | + from ravendb.documents.operations.ai.agents import AiUsage |
| 8 | + |
| 9 | +TAnswer = TypeVar("TAnswer") |
| 10 | + |
| 11 | + |
| 12 | +class AiConversationStatus(enum.Enum): |
| 13 | + """ |
| 14 | + Represents the status of an AI conversation. |
| 15 | + """ |
| 16 | + |
| 17 | + DONE = "Done" |
| 18 | + ACTION_REQUIRED = "ActionRequired" |
| 19 | + |
| 20 | + def __str__(self): |
| 21 | + return self.value |
| 22 | + |
| 23 | + |
| 24 | +class AiAnswer(Generic[TAnswer]): |
| 25 | + """ |
| 26 | + Represents the answer from an AI conversation turn. |
| 27 | +
|
| 28 | + This class contains the AI's response, the conversation status, |
| 29 | + token usage statistics, and timing information. |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__( |
| 33 | + self, |
| 34 | + answer: Optional[TAnswer] = None, |
| 35 | + status: AiConversationStatus = AiConversationStatus.DONE, |
| 36 | + usage: Optional[AiUsage] = None, |
| 37 | + elapsed: Optional[timedelta] = None, |
| 38 | + ): |
| 39 | + """ |
| 40 | + Initialize an AiAnswer instance. |
| 41 | +
|
| 42 | + Args: |
| 43 | + answer: The answer content produced by the AI |
| 44 | + status: The status of the conversation (Done or ActionRequired) |
| 45 | + usage: Token usage reported by the model |
| 46 | + elapsed: The total time elapsed to produce the answer |
| 47 | + """ |
| 48 | + self.answer = answer |
| 49 | + self.status = status |
| 50 | + self.usage = usage |
| 51 | + self.elapsed = elapsed |
| 52 | + |
| 53 | + def __str__(self) -> str: |
| 54 | + """String representation for debugging.""" |
| 55 | + return ( |
| 56 | + f"AiAnswer(status={self.status.value}, " |
| 57 | + f"has_answer={self.answer is not None}, " |
| 58 | + f"elapsed={self.elapsed})" |
| 59 | + ) |
| 60 | + |
| 61 | + def __repr__(self) -> str: |
| 62 | + """Detailed representation for debugging.""" |
| 63 | + return ( |
| 64 | + f"AiAnswer(answer={self.answer!r}, " |
| 65 | + f"status={self.status!r}, " |
| 66 | + f"usage={self.usage!r}, " |
| 67 | + f"elapsed={self.elapsed!r})" |
| 68 | + ) |
0 commit comments