Coverage for src/fastoai/models/generated/assistant.py: 92%

1 from datetime import datetime

2 from typing import TYPE_CHECKING, Annotated

3

4 from openai.types.beta.assistant import Assistant as _Assistant

5 from openai.types.beta.assistant import ToolResources

6 from openai.types.beta.assistant_response_format_option import (

7 AssistantResponseFormatOption,

8)

9 from openai.types.beta.assistant_tool import AssistantTool

10 from pydantic import field_serializer

11 from sqlalchemy.ext.asyncio import AsyncAttrs

12 from sqlmodel import Field, Relationship

13

14 from .._metadata import WithMetadata

15 from .._types import as_sa_type

16 from .._utils import now, random_id_with_prefix

17

18 if TYPE_CHECKING:

19 from .message import Message

20 from .run import Run

21 from .run_step import RunStep

22

23 class Assistant(AsyncAttrs, WithMetadata, table=True):

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

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

26 description: str | None = None

27 instructions: str | None = None

28 model: str

29 name: str | None = None

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

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

32 temperature: float | None = None

33 tool_resources: Annotated[ToolResources | None, Field(sa_type=as_sa_type(ToolResources), nullable=True)] = None

34 top_p: float | None = None

35

36 async def to_openai_model(self) -> _Assistant:

37 value = self.model_dump(by_alias=True)

38 value['object'] = 'assistant'

39 return _Assistant.model_validate(value)

40

41 @field_serializer('created_at')

42 def serialize_datetime(self, dt: datetime) -> int:

43 return int(dt.timestamp())

44 messages: list['Message'] = Relationship(back_populates='assistant')

45 runs: list['Run'] = Relationship(back_populates='assistant')

46 steps: list['RunStep'] = Relationship(back_populates='assistant')