Coverage for src/fastoai/models/generated/run.py: 86%

1 from datetime import datetime

2 from typing import TYPE_CHECKING, Annotated

3

4 from openai.types.beta.assistant_response_format_option import (

5 AssistantResponseFormatOption,

6)

7 from openai.types.beta.assistant_tool import AssistantTool

8 from openai.types.beta.assistant_tool_choice_option import AssistantToolChoiceOption

9 from openai.types.beta.threads.run import (

10 IncompleteDetails,

11 LastError,

12 RequiredAction,

13 TruncationStrategy,

14 Usage,

15)

16 from openai.types.beta.threads.run import Run as _Run

17 from openai.types.beta.threads.run_status import RunStatus

18 from pydantic import field_serializer

19 from sqlalchemy.ext.asyncio import AsyncAttrs

20 from sqlmodel import Field, Relationship

21

22 from .._metadata import WithMetadata

23 from .._types import as_sa_type

24 from .._utils import now, random_id_with_prefix

25 from .assistant import Assistant

26 from .thread import Thread

27

28 if TYPE_CHECKING:

29 from .message import Message

30 from .run_step import RunStep

31

32 class Run(AsyncAttrs, WithMetadata, table=True):

33 id: Annotated[str, Field(primary_key=True, default_factory=random_id_with_prefix('run_'))]

34 assistant_id: Annotated[str, Field(foreign_key='assistant.id')]

35 cancelled_at: datetime | None = None

36 completed_at: datetime | None = None

37 created_at: Annotated[datetime, Field(default_factory=now)]

38 expires_at: datetime | None = None

39 failed_at: datetime | None = None

40 incomplete_details: Annotated[IncompleteDetails | None, Field(sa_type=as_sa_type(IncompleteDetails), nullable=True)] = None

41 instructions: str

42 last_error: Annotated[LastError | None, Field(sa_type=as_sa_type(LastError), nullable=True)] = None

43 max_completion_tokens: int | None = None

44 max_prompt_tokens: int | None = None

45 model: str

46 parallel_tool_calls: bool

47 required_action: Annotated[RequiredAction | None, Field(sa_type=as_sa_type(RequiredAction), nullable=True)] = None

48 response_format: Annotated[AssistantResponseFormatOption | None, Field(sa_type=as_sa_type(AssistantResponseFormatOption), nullable=True)] = None

49 started_at: datetime | None = None

50 status: Annotated[RunStatus, Field(sa_type=as_sa_type(RunStatus))]

51 thread_id: Annotated[str, Field(foreign_key='thread.id')]

52 tool_choice: Annotated[AssistantToolChoiceOption | None, Field(sa_type=as_sa_type(AssistantToolChoiceOption), nullable=True)] = None

53 tools: Annotated[list[AssistantTool], Field(default_factory=list, sa_type=as_sa_type(list[AssistantTool]))]

54 truncation_strategy: Annotated[TruncationStrategy | None, Field(sa_type=as_sa_type(TruncationStrategy), nullable=True)] = None

55 usage: Annotated[Usage | None, Field(sa_type=as_sa_type(Usage), nullable=True)] = None

56 temperature: float | None = None

57 top_p: float | None = None

58

59 async def to_openai_model(self) -> _Run:

60 value = self.model_dump(by_alias=True)

61 value['object'] = 'thread.run'

62 return _Run.model_validate(value)

63

64 @field_serializer('cancelled_at', 'completed_at', 'created_at', 'expires_at', 'failed_at', 'started_at')

65 def serialize_datetime(self, dt: datetime | None) -> int | None:

66 if dt is None:

67 return None

68 return int(dt.timestamp())

69 assistant: Assistant = Relationship(back_populates='runs')

70 thread: Thread = Relationship(back_populates='runs')

71 messages: list['Message'] = Relationship(back_populates='run')

72 steps: list['RunStep'] = Relationship(back_populates='run')