
from typing import Optional, List
from pydantic import BaseModel


class CompletionUsage(BaseModel):
    prompt_tokens: int
    """The number of tokens used by the prompt."""

    completion_tokens: int
    """Number of tokens in the generated completion."""

    total_tokens: int
    """The total number of tokens used by the request."""

class Function(BaseModel):
    arguments: str
    name: Optional[str] = None
    """The name of the function to call."""


class ChatCompletionMessageToolCall(BaseModel):
    id: Optional[str] = None
    """The ID of the tool call."""

    function: Function
    """The function that the model called."""

    type: Optional[str] = None
    """The type of the tool. Currently, only `function` is supported."""


class ChatCompletionAudio(BaseModel):
    id: str
    """Unique identifier for this audio response."""

    data: str
    """
    Base64 encoded audio bytes generated by the model, in the format specified in
    the request.
    """

    expires_at: int
    """
    The Unix timestamp (in seconds) for when this audio response will no longer be
    accessible on the server for use in multi-turn conversations.
    """

    transcript: str
    """Transcript of the audio generated by the model."""


class ChatCompletionMessage(BaseModel):
    content: Optional[str] = None
    """The contents of the message."""

    refusal: Optional[str] = None
    """The refusal message generated by the model."""

    role: Optional[str] = None
    """The role of the author of this message."""

    audio: Optional[ChatCompletionAudio] = None
    """
    If the audio output modality is requested, this object contains data about the
    audio response from the model.
    """

    tool_calls: Optional[List[ChatCompletionMessageToolCall]] = None
    """The tool calls generated by the model, such as function calls."""


class Choice(BaseModel):
    finish_reason: Optional[str] = None
    """The reason the model stopped generating tokens."""

    index: int
    """The index of the choice in the list of choices."""

    # logprobs: Optional[ChoiceLogprobs] = None
    # """Log probability information for the choice."""

    message: ChatCompletionMessage
    """A chat completion message generated by the model."""


class ChoiceStream(BaseModel):
    finish_reason: Optional[str] = None
    """The reason the model stopped generating tokens."""

    index: int
    """The index of the choice in the list of choices."""

    # logprobs: Optional[ChoiceLogprobs] = None
    # """Log probability information for the choice."""

    delta: ChatCompletionMessage
    """A chat completion message generated by streamed model."""


class ChatCompletionBase(BaseModel):
    id: str
    """A unique identifier for the chat completion."""

    created: int
    """The Unix timestamp (in seconds) of when the chat completion was created."""

    model: str
    """The model used for the chat completion."""

    system_fingerprint: Optional[str] = None
    """Backend configuration that the model runs with."""

    usage: Optional[CompletionUsage] = None
    """Usage statistics for the completion request."""


class ChatCompletion(ChatCompletionBase):
    choices: List[Choice]
    """A list of chat completion choices."""

class ChatCompletionChunk(ChatCompletionBase):
    choices: List[ChoiceStream]
    """A list of chat completion choices."""
