# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from typing import Dict, List, Union, Optional
from typing_extensions import Literal, Annotated, TypeAlias

from ..._utils import PropertyInfo
from ..._models import BaseModel
from ..completion_usage import CompletionUsage
from .chat_completion_token_logprob import ChatCompletionTokenLogprob

__all__ = [
    "ChatCompletionChunk",
    "Choice",
    "ChoiceDelta",
    "ChoiceDeltaFunctionCall",
    "ChoiceDeltaToolCall",
    "ChoiceDeltaToolCallFunction",
    "ChoiceLogprobs",
    "Moderation",
    "ModerationInput",
    "ModerationInputModerationResults",
    "ModerationInputModerationResultsResult",
    "ModerationInputError",
    "ModerationOutput",
    "ModerationOutputModerationResults",
    "ModerationOutputModerationResultsResult",
    "ModerationOutputError",
]


class ChoiceDeltaFunctionCall(BaseModel):
    """Deprecated and replaced by `tool_calls`.

    The name and arguments of a function that should be called, as generated by the model.
    """

    arguments: Optional[str] = None
    """
    The arguments to call the function with, as generated by the model in JSON
    format. Note that the model does not always generate valid JSON, and may
    hallucinate parameters not defined by your function schema. Validate the
    arguments in your code before calling your function.
    """

    name: Optional[str] = None
    """The name of the function to call."""


class ChoiceDeltaToolCallFunction(BaseModel):
    arguments: Optional[str] = None
    """
    The arguments to call the function with, as generated by the model in JSON
    format. Note that the model does not always generate valid JSON, and may
    hallucinate parameters not defined by your function schema. Validate the
    arguments in your code before calling your function.
    """

    name: Optional[str] = None
    """The name of the function to call."""


class ChoiceDeltaToolCall(BaseModel):
    index: int

    id: Optional[str] = None
    """The ID of the tool call."""

    function: Optional[ChoiceDeltaToolCallFunction] = None

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


class ChoiceDelta(BaseModel):
    """A chat completion delta generated by streamed model responses."""

    content: Optional[str] = None
    """The contents of the chunk message."""

    function_call: Optional[ChoiceDeltaFunctionCall] = None
    """Deprecated and replaced by `tool_calls`.

    The name and arguments of a function that should be called, as generated by the
    model.
    """

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

    role: Optional[Literal["developer", "system", "user", "assistant", "tool"]] = None
    """The role of the author of this message."""

    tool_calls: Optional[List[ChoiceDeltaToolCall]] = None


class ChoiceLogprobs(BaseModel):
    """Log probability information for the choice."""

    content: Optional[List[ChatCompletionTokenLogprob]] = None
    """A list of message content tokens with log probability information."""

    refusal: Optional[List[ChatCompletionTokenLogprob]] = None
    """A list of message refusal tokens with log probability information."""


class Choice(BaseModel):
    delta: ChoiceDelta
    """A chat completion delta generated by streamed model responses."""

    finish_reason: Optional[Literal["stop", "length", "tool_calls", "content_filter", "function_call"]] = None
    """The reason the model stopped generating tokens.

    This will be `stop` if the model hit a natural stop point or a provided stop
    sequence, `length` if the maximum number of tokens specified in the request was
    reached, `content_filter` if content was omitted due to a flag from our content
    filters, `tool_calls` if the model called a tool, or `function_call`
    (deprecated) if the model called a function.
    """

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

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


class ModerationInputModerationResultsResult(BaseModel):
    """A moderation result produced for the response input or output."""

    categories: Dict[str, bool]
    """
    A dictionary of moderation categories to booleans, True if the input is flagged
    under this category.
    """

    category_applied_input_types: Dict[str, List[Literal["text", "image"]]]
    """Which modalities of input are reflected by the score for each category."""

    category_scores: Dict[str, float]
    """A dictionary of moderation categories to scores."""

    flagged: bool
    """A boolean indicating whether the content was flagged by any category."""

    model: str
    """The moderation model that produced this result."""

    type: Literal["moderation_result"]
    """
    The object type, which was always `moderation_result` for successful moderation
    results.
    """


class ModerationInputModerationResults(BaseModel):
    """Successful moderation results for the request input or generated output."""

    model: str
    """The moderation model used to generate the results."""

    results: List[ModerationInputModerationResultsResult]
    """A list of moderation results."""

    type: Literal["moderation_results"]
    """The object type, which is always `moderation_results`."""


class ModerationInputError(BaseModel):
    """An error produced while attempting moderation."""

    code: str
    """The error code."""

    message: str
    """The error message."""

    type: Literal["error"]
    """The object type, which is always `error`."""


ModerationInput: TypeAlias = Annotated[
    Union[ModerationInputModerationResults, ModerationInputError], PropertyInfo(discriminator="type")
]


class ModerationOutputModerationResultsResult(BaseModel):
    """A moderation result produced for the response input or output."""

    categories: Dict[str, bool]
    """
    A dictionary of moderation categories to booleans, True if the input is flagged
    under this category.
    """

    category_applied_input_types: Dict[str, List[Literal["text", "image"]]]
    """Which modalities of input are reflected by the score for each category."""

    category_scores: Dict[str, float]
    """A dictionary of moderation categories to scores."""

    flagged: bool
    """A boolean indicating whether the content was flagged by any category."""

    model: str
    """The moderation model that produced this result."""

    type: Literal["moderation_result"]
    """
    The object type, which was always `moderation_result` for successful moderation
    results.
    """


class ModerationOutputModerationResults(BaseModel):
    """Successful moderation results for the request input or generated output."""

    model: str
    """The moderation model used to generate the results."""

    results: List[ModerationOutputModerationResultsResult]
    """A list of moderation results."""

    type: Literal["moderation_results"]
    """The object type, which is always `moderation_results`."""


class ModerationOutputError(BaseModel):
    """An error produced while attempting moderation."""

    code: str
    """The error code."""

    message: str
    """The error message."""

    type: Literal["error"]
    """The object type, which is always `error`."""


ModerationOutput: TypeAlias = Annotated[
    Union[ModerationOutputModerationResults, ModerationOutputError], PropertyInfo(discriminator="type")
]


class Moderation(BaseModel):
    """Moderation results for the request input and generated output.

    Present
    on the moderation chunk when moderated completions are requested.
    """

    input: ModerationInput
    """Moderation for the request input."""

    output: ModerationOutput
    """Moderation for the generated output."""


class ChatCompletionChunk(BaseModel):
    """
    Represents a streamed chunk of a chat completion response returned
    by the model, based on the provided input.
    [Learn more](https://platform.openai.com/docs/guides/streaming-responses).
    """

    id: str
    """A unique identifier for the chat completion. Each chunk has the same ID."""

    choices: List[Choice]
    """A list of chat completion choices.

    Can contain more than one elements if `n` is greater than 1. Can also be empty
    for the last chunk if you set `stream_options: {"include_usage": true}`.
    """

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

    Each chunk has the same timestamp.
    """

    model: str
    """The model to generate the completion."""

    object: Literal["chat.completion.chunk"]
    """The object type, which is always `chat.completion.chunk`."""

    moderation: Optional[Moderation] = None
    """Moderation results for the request input and generated output.

    Present on the moderation chunk when moderated completions are requested.
    """

    service_tier: Optional[Literal["auto", "default", "flex", "scale", "priority", "fast"]] = None
    """Specifies the processing type used for serving the request.

    - If set to 'auto', then the request will be processed with the service tier
      configured in the Project settings. Unless otherwise configured, the Project
      will use 'default'.
    - If set to 'default', then the request will be processed with the standard
      pricing and performance for the selected model.
    - If set to '[flex](https://platform.openai.com/docs/guides/flex-processing)',
      then the request will be processed with the Flex Processing service tier.
    - To opt-in to [Fast mode](/api/docs/guides/fast-mode) at the request level,
      include the `service_tier=fast` or `service_tier=priority` parameter for
      Responses or Chat Completions. The response will show `service_tier=priority`
      regardless of if you specify `service_tier=fast` or `priority` in your
      request.
    - When not set, the default behavior is 'auto'.

    When the `service_tier` parameter is set, the response body will include the
    `service_tier` value based on the processing mode actually used to serve the
    request. This response value may be different from the value set in the
    parameter.
    """

    system_fingerprint: Optional[str] = None
    """
    This fingerprint represents the backend configuration that the model runs with.
    Can be used in conjunction with the `seed` request parameter to understand when
    backend changes have been made that might impact determinism.
    """

    usage: Optional[CompletionUsage] = None
    """
    An optional field that will only be present when you set
    `stream_options: {"include_usage": true}` in your request. When present, it
    contains a null value **except for the last chunk** which contains the token
    usage statistics for the entire request.

    **NOTE:** If the stream is interrupted or cancelled, you may not receive the
    final usage chunk which contains the total token usage for the request.
    """
