main
  1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2
  3from __future__ import annotations
  4
  5from typing import Dict, Union, Iterable, Optional
  6from typing_extensions import Literal, Required, TypedDict
  7
  8from .._types import SequenceNotStr
  9from .chat.chat_completion_stream_options_param import ChatCompletionStreamOptionsParam
 10
 11__all__ = ["CompletionCreateParamsBase", "CompletionCreateParamsNonStreaming", "CompletionCreateParamsStreaming"]
 12
 13
 14class CompletionCreateParamsBase(TypedDict, total=False):
 15    model: Required[Union[str, Literal["gpt-3.5-turbo-instruct", "davinci-002", "babbage-002"]]]
 16    """ID of the model to use.
 17
 18    You can use the
 19    [List models](https://platform.openai.com/docs/api-reference/models/list) API to
 20    see all of your available models, or see our
 21    [Model overview](https://platform.openai.com/docs/models) for descriptions of
 22    them.
 23    """
 24
 25    prompt: Required[Union[str, SequenceNotStr[str], Iterable[int], Iterable[Iterable[int]], None]]
 26    """
 27    The prompt(s) to generate completions for, encoded as a string, array of
 28    strings, array of tokens, or array of token arrays.
 29
 30    Note that <|endoftext|> is the document separator that the model sees during
 31    training, so if a prompt is not specified the model will generate as if from the
 32    beginning of a new document.
 33    """
 34
 35    best_of: Optional[int]
 36    """
 37    Generates `best_of` completions server-side and returns the "best" (the one with
 38    the highest log probability per token). Results cannot be streamed.
 39
 40    When used with `n`, `best_of` controls the number of candidate completions and
 41    `n` specifies how many to return – `best_of` must be greater than `n`.
 42
 43    **Note:** Because this parameter generates many completions, it can quickly
 44    consume your token quota. Use carefully and ensure that you have reasonable
 45    settings for `max_tokens` and `stop`.
 46    """
 47
 48    echo: Optional[bool]
 49    """Echo back the prompt in addition to the completion"""
 50
 51    frequency_penalty: Optional[float]
 52    """Number between -2.0 and 2.0.
 53
 54    Positive values penalize new tokens based on their existing frequency in the
 55    text so far, decreasing the model's likelihood to repeat the same line verbatim.
 56
 57    [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation)
 58    """
 59
 60    logit_bias: Optional[Dict[str, int]]
 61    """Modify the likelihood of specified tokens appearing in the completion.
 62
 63    Accepts a JSON object that maps tokens (specified by their token ID in the GPT
 64    tokenizer) to an associated bias value from -100 to 100. You can use this
 65    [tokenizer tool](/tokenizer?view=bpe) to convert text to token IDs.
 66    Mathematically, the bias is added to the logits generated by the model prior to
 67    sampling. The exact effect will vary per model, but values between -1 and 1
 68    should decrease or increase likelihood of selection; values like -100 or 100
 69    should result in a ban or exclusive selection of the relevant token.
 70
 71    As an example, you can pass `{"50256": -100}` to prevent the <|endoftext|> token
 72    from being generated.
 73    """
 74
 75    logprobs: Optional[int]
 76    """
 77    Include the log probabilities on the `logprobs` most likely output tokens, as
 78    well the chosen tokens. For example, if `logprobs` is 5, the API will return a
 79    list of the 5 most likely tokens. The API will always return the `logprob` of
 80    the sampled token, so there may be up to `logprobs+1` elements in the response.
 81
 82    The maximum value for `logprobs` is 5.
 83    """
 84
 85    max_tokens: Optional[int]
 86    """
 87    The maximum number of [tokens](/tokenizer) that can be generated in the
 88    completion.
 89
 90    The token count of your prompt plus `max_tokens` cannot exceed the model's
 91    context length.
 92    [Example Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken)
 93    for counting tokens.
 94    """
 95
 96    n: Optional[int]
 97    """How many completions to generate for each prompt.
 98
 99    **Note:** Because this parameter generates many completions, it can quickly
100    consume your token quota. Use carefully and ensure that you have reasonable
101    settings for `max_tokens` and `stop`.
102    """
103
104    presence_penalty: Optional[float]
105    """Number between -2.0 and 2.0.
106
107    Positive values penalize new tokens based on whether they appear in the text so
108    far, increasing the model's likelihood to talk about new topics.
109
110    [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation)
111    """
112
113    seed: Optional[int]
114    """
115    If specified, our system will make a best effort to sample deterministically,
116    such that repeated requests with the same `seed` and parameters should return
117    the same result.
118
119    Determinism is not guaranteed, and you should refer to the `system_fingerprint`
120    response parameter to monitor changes in the backend.
121    """
122
123    stop: Union[Optional[str], SequenceNotStr[str], None]
124    """Not supported with latest reasoning models `o3` and `o4-mini`.
125
126    Up to 4 sequences where the API will stop generating further tokens. The
127    returned text will not contain the stop sequence.
128    """
129
130    stream_options: Optional[ChatCompletionStreamOptionsParam]
131    """Options for streaming response. Only set this when you set `stream: true`."""
132
133    suffix: Optional[str]
134    """The suffix that comes after a completion of inserted text.
135
136    This parameter is only supported for `gpt-3.5-turbo-instruct`.
137    """
138
139    temperature: Optional[float]
140    """What sampling temperature to use, between 0 and 2.
141
142    Higher values like 0.8 will make the output more random, while lower values like
143    0.2 will make it more focused and deterministic.
144
145    We generally recommend altering this or `top_p` but not both.
146    """
147
148    top_p: Optional[float]
149    """
150    An alternative to sampling with temperature, called nucleus sampling, where the
151    model considers the results of the tokens with top_p probability mass. So 0.1
152    means only the tokens comprising the top 10% probability mass are considered.
153
154    We generally recommend altering this or `temperature` but not both.
155    """
156
157    user: str
158    """
159    A unique identifier representing your end-user, which can help OpenAI to monitor
160    and detect abuse.
161    [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
162    """
163
164
165class CompletionCreateParamsNonStreaming(CompletionCreateParamsBase, total=False):
166    stream: Optional[Literal[False]]
167    """Whether to stream back partial progress.
168
169    If set, tokens will be sent as data-only
170    [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format)
171    as they become available, with the stream terminated by a `data: [DONE]`
172    message.
173    [Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions).
174    """
175
176
177class CompletionCreateParamsStreaming(CompletionCreateParamsBase):
178    stream: Required[Literal[True]]
179    """Whether to stream back partial progress.
180
181    If set, tokens will be sent as data-only
182    [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format)
183    as they become available, with the stream terminated by a `data: [DONE]`
184    message.
185    [Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions).
186    """
187
188
189CompletionCreateParams = Union[CompletionCreateParamsNonStreaming, CompletionCreateParamsStreaming]