openai.resources.chat
The chat
module provides classes for creating and managing chat sessions that leverage OpenAI's language models to generate conversational responses.
The module supports both synchronous and asynchronous operations, offering interfaces for direct interaction with the completion endpoints tailored for chat applications. Designed for developers looking to integrate AI-powered chat functionalities into their applications and features like raw and streaming response handling for more flexible integration.
Modules:
Name | Description |
---|---|
chat |
The The module supports both synchronous and asynchronous operations, offering interfaces for direct interaction with the completion endpoints tailored for chat applications. Designed for developers looking to integrate AI-powered chat functionalities into their applications and features like raw and streaming response handling for more flexible integration. |
completions |
The This module interacts with the |
Classes:
AsyncChat
AsyncChat(client: AsyncOpenAI)
AsyncChatWithRawResponse
AsyncChatWithRawResponse(chat: AsyncChat)
AsyncChatWithStreamingResponse
AsyncChatWithStreamingResponse(chat: AsyncChat)
AsyncCompletions
AsyncCompletions(client: AsyncOpenAI)
Methods:
Name | Description |
---|---|
create |
Generates a chat completion by submitting a conversation history and model parameters, supporting various configurations to tailor the chat completion process, including penalties, response formats, and streaming outputs. |
with_raw_response |
|
with_streaming_response |
|
create
async
create(
*,
messages: Iterable[ChatCompletionMessageParam],
model: Union[
str,
Literal[
"gpt-4-0125-preview",
"gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4",
"gpt-4-0314",
"gpt-4-0613",
"gpt-4-32k",
"gpt-4-32k-0314",
"gpt-4-32k-0613",
"gpt-3.5-turbo",
"gpt-3.5-turbo-16k",
"gpt-3.5-turbo-0301",
"gpt-3.5-turbo-0613",
"gpt-3.5-turbo-1106",
"gpt-3.5-turbo-0125",
"gpt-3.5-turbo-16k-0613",
],
],
frequency_penalty: (
Optional[float] | NotGiven
) = NOT_GIVEN,
function_call: FunctionCall | NotGiven = NOT_GIVEN,
functions: Iterable[Function] | NotGiven = NOT_GIVEN,
logit_bias: (
Optional[Dict[str, int]] | NotGiven
) = NOT_GIVEN,
logprobs: Optional[bool] | NotGiven = NOT_GIVEN,
max_tokens: Optional[int] | NotGiven = NOT_GIVEN,
n: Optional[int] | NotGiven = NOT_GIVEN,
presence_penalty: (
Optional[float] | NotGiven
) = NOT_GIVEN,
response_format: ResponseFormat | NotGiven = NOT_GIVEN,
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: (
Union[Optional[str], List[str]] | NotGiven
) = NOT_GIVEN,
stream: Optional[Literal[False]] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
tool_choice: (
ChatCompletionToolChoiceOptionParam | NotGiven
) = NOT_GIVEN,
tools: (
Iterable[ChatCompletionToolParam] | NotGiven
) = NOT_GIVEN,
top_logprobs: Optional[int] | NotGiven = NOT_GIVEN,
top_p: Optional[float] | NotGiven = NOT_GIVEN,
user: str | NotGiven = NOT_GIVEN,
extra_headers: Headers | None = None,
extra_query: Query | None = None,
extra_body: Body | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN
) -> ChatCompletion
create(
*,
messages: Iterable[ChatCompletionMessageParam],
model: Union[
str,
Literal[
"gpt-4-0125-preview",
"gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4",
"gpt-4-0314",
"gpt-4-0613",
"gpt-4-32k",
"gpt-4-32k-0314",
"gpt-4-32k-0613",
"gpt-3.5-turbo",
"gpt-3.5-turbo-16k",
"gpt-3.5-turbo-0301",
"gpt-3.5-turbo-0613",
"gpt-3.5-turbo-1106",
"gpt-3.5-turbo-0125",
"gpt-3.5-turbo-16k-0613",
],
],
stream: Literal[True],
frequency_penalty: (
Optional[float] | NotGiven
) = NOT_GIVEN,
function_call: FunctionCall | NotGiven = NOT_GIVEN,
functions: Iterable[Function] | NotGiven = NOT_GIVEN,
logit_bias: (
Optional[Dict[str, int]] | NotGiven
) = NOT_GIVEN,
logprobs: Optional[bool] | NotGiven = NOT_GIVEN,
max_tokens: Optional[int] | NotGiven = NOT_GIVEN,
n: Optional[int] | NotGiven = NOT_GIVEN,
presence_penalty: (
Optional[float] | NotGiven
) = NOT_GIVEN,
response_format: ResponseFormat | NotGiven = NOT_GIVEN,
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: (
Union[Optional[str], List[str]] | NotGiven
) = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
tool_choice: (
ChatCompletionToolChoiceOptionParam | NotGiven
) = NOT_GIVEN,
tools: (
Iterable[ChatCompletionToolParam] | NotGiven
) = NOT_GIVEN,
top_logprobs: Optional[int] | NotGiven = NOT_GIVEN,
top_p: Optional[float] | NotGiven = NOT_GIVEN,
user: str | NotGiven = NOT_GIVEN,
extra_headers: Headers | None = None,
extra_query: Query | None = None,
extra_body: Body | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN
) -> AsyncStream[ChatCompletionChunk]
create(
*,
messages: Iterable[ChatCompletionMessageParam],
model: Union[
str,
Literal[
"gpt-4-0125-preview",
"gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4",
"gpt-4-0314",
"gpt-4-0613",
"gpt-4-32k",
"gpt-4-32k-0314",
"gpt-4-32k-0613",
"gpt-3.5-turbo",
"gpt-3.5-turbo-16k",
"gpt-3.5-turbo-0301",
"gpt-3.5-turbo-0613",
"gpt-3.5-turbo-1106",
"gpt-3.5-turbo-0125",
"gpt-3.5-turbo-16k-0613",
],
],
stream: bool,
frequency_penalty: (
Optional[float] | NotGiven
) = NOT_GIVEN,
function_call: FunctionCall | NotGiven = NOT_GIVEN,
functions: Iterable[Function] | NotGiven = NOT_GIVEN,
logit_bias: (
Optional[Dict[str, int]] | NotGiven
) = NOT_GIVEN,
logprobs: Optional[bool] | NotGiven = NOT_GIVEN,
max_tokens: Optional[int] | NotGiven = NOT_GIVEN,
n: Optional[int] | NotGiven = NOT_GIVEN,
presence_penalty: (
Optional[float] | NotGiven
) = NOT_GIVEN,
response_format: ResponseFormat | NotGiven = NOT_GIVEN,
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: (
Union[Optional[str], List[str]] | NotGiven
) = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
tool_choice: (
ChatCompletionToolChoiceOptionParam | NotGiven
) = NOT_GIVEN,
tools: (
Iterable[ChatCompletionToolParam] | NotGiven
) = NOT_GIVEN,
top_logprobs: Optional[int] | NotGiven = NOT_GIVEN,
top_p: Optional[float] | NotGiven = NOT_GIVEN,
user: str | NotGiven = NOT_GIVEN,
extra_headers: Headers | None = None,
extra_query: Query | None = None,
extra_body: Body | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN
) -> ChatCompletion | AsyncStream[ChatCompletionChunk]
create(
*,
messages: Iterable[ChatCompletionMessageParam],
model: Union[
str,
Literal[
"gpt-4-0125-preview",
"gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4",
"gpt-4-0314",
"gpt-4-0613",
"gpt-4-32k",
"gpt-4-32k-0314",
"gpt-4-32k-0613",
"gpt-3.5-turbo",
"gpt-3.5-turbo-16k",
"gpt-3.5-turbo-0301",
"gpt-3.5-turbo-0613",
"gpt-3.5-turbo-1106",
"gpt-3.5-turbo-0125",
"gpt-3.5-turbo-16k-0613",
],
],
frequency_penalty: (
Optional[float] | NotGiven
) = NOT_GIVEN,
function_call: FunctionCall | NotGiven = NOT_GIVEN,
functions: Iterable[Function] | NotGiven = NOT_GIVEN,
logit_bias: (
Optional[Dict[str, int]] | NotGiven
) = NOT_GIVEN,
logprobs: Optional[bool] | NotGiven = NOT_GIVEN,
max_tokens: Optional[int] | NotGiven = NOT_GIVEN,
n: Optional[int] | NotGiven = NOT_GIVEN,
presence_penalty: (
Optional[float] | NotGiven
) = NOT_GIVEN,
response_format: ResponseFormat | NotGiven = NOT_GIVEN,
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: (
Union[Optional[str], List[str]] | NotGiven
) = NOT_GIVEN,
stream: (
Optional[Literal[False]] | Literal[True] | NotGiven
) = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
tool_choice: (
ChatCompletionToolChoiceOptionParam | NotGiven
) = NOT_GIVEN,
tools: (
Iterable[ChatCompletionToolParam] | NotGiven
) = NOT_GIVEN,
top_logprobs: Optional[int] | NotGiven = NOT_GIVEN,
top_p: Optional[float] | NotGiven = NOT_GIVEN,
user: str | NotGiven = NOT_GIVEN,
extra_headers: Headers | None = None,
extra_query: Query | None = None,
extra_body: Body | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN
) -> ChatCompletion | AsyncStream[ChatCompletionChunk]
Generates a chat completion by submitting a conversation history and model parameters, supporting various configurations to tailor the chat completion process, including penalties, response formats, and streaming outputs.
Returns:
Name | Type | Description |
---|---|---|
ChatCompletion | AsyncStream[ChatCompletionChunk]
|
AsyncStream[ChatCompletionChunk]: Stream of chat completion chunks for incremental output consumption. |
|
ChatCompletion |
ChatCompletion | AsyncStream[ChatCompletionChunk]
|
An object containing the generated chat completion and other relevant details, such as the chosen tokens and any associated metadata. |
Raises:
Type | Description |
---|---|
OpenAIError
|
If there's an issue with API request or parsing the response. |
Examples:
completion = client.Completions().create(
messages=[{"role": "user", "content": "Tell me a joke."}],
model="text-davinci-003",
max_tokens=60
)
print(completion.choices[0].text)
completion = client.Completions().create(
messages=[{"role": "user", "content": "How do you make a latte?"}],
model="gpt-3.5-turbo",
max_tokens=150
)
print(completion.choices[0].text)
async with client.AsyncCompletions().create(
messages=[{"role": "user", "content": "What's the weather like in Tokyo?"}],
model="text-davinci-003",
frequency_penalty=0.5,
presence_penalty=0.5
) as stream:
async for chunk in stream:
print(chunk.text)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
messages
|
Iterable[ChatCompletionMessageParam]
|
A sequence of message objects that make up the
conversation so far. Each message is a dictionary that should include the keys |
required |
model
|
Union[str, Literal[...]]
|
The model ID of the OpenAI GPT model to use for generating the chat
completion. This parameter specifies which version of the model to use, for example,
|
required |
stream
|
Optional[bool]
|
Whether the response should be streamed. Streaming can be useful for receiving responses as they are generated, especially for long completions. This approach enables real-time interaction and processing of the model's output. |
NOT_GIVEN
|
frequency_penalty
|
Optional[float]
|
Adjusts the likelihood of the model repeating the same line verbatim. Values can range from -2.0 to 2.0, with positive values making repeats less likely. This parameter helps in controlling the diversity of the generated text. |
NOT_GIVEN
|
function_call
|
Optional[FunctionCall]
|
Deprecated. Originally used to specify whether the model
should execute a function call. While it is still accepted for backward compatibility, the
|
NOT_GIVEN
|
functions
|
Optional[Iterable[Function]]
|
Deprecated. Originally provided a list of functions the
model could choose to call during generation. Like |
NOT_GIVEN
|
logit_bias
|
Optional[Dicst[str, int]]
|
Modifies token likelihood, mapping token IDs to bias values (-100 to 100), influencing selection. This mechanism allows for fine-tuning the presence or avoidance of specific words or phrases in the generated text. |
NOT_GIVEN
|
logprobs
|
Optional[bool]
|
Whether to return the log probabilities of the tokens generated in the completion. Useful for model introspection and understanding the model's decision-making process. Note: This feature might not be available for all models. |
NOT_GIVEN
|
max_tokens
|
Optional[int]
|
The maximum number of tokens to generate in the completion. This limit includes the tokens in the prompt. Careful management of this parameter is crucial for controlling the length and computational cost of model operations. |
NOT_GIVEN
|
n
|
Optional[int]
|
The number of completions to generate for each prompt. Higher values allow for more diversity in the responses but increase computational cost. Balancing this parameter is key to achieving desired output variability while managing resource use. |
NOT_GIVEN
|
presence_penalty
|
Optional[float]
|
Encourages new content by penalizing token presence, with values from -2.0 to 2.0. This parameter influences the model to explore new topics and ideas, enhancing the creativity and variability of the output. |
NOT_GIVEN
|
response_format
|
Optional[ResponseFormat]
|
Specifies how the model's responses should be formatted. For example, responses can be structured as plain text, JSON objects, or other formats depending on the model's capabilities. This flexibility allows for tailored outputs to suit different applications and processing needs. |
NOT_GIVEN
|
seed
|
Optional[int]
|
Seed for deterministic randomness, ensuring same inputs yield consistent outputs. This feature is beneficial for reproducibility and debugging purposes, allowing users to obtain the same results from identical requests. |
NOT_GIVEN
|
stop
|
Optional[Union[str, List[str]]]
|
Sequences where the API will stop generating further tokens. This can be used to indicate the end of a message or section, providing a mechanism to control the scope and completion of the generated content. |
NOT_GIVEN
|
temperature
|
Optional[float]
|
Controls the randomness of the output. A lower temperature results in more deterministic output, while a higher temperature results in more diversity. Adjusting this parameter allows users to balance between predictability and creativity in the model's responses. |
NOT_GIVEN
|
tool_choice
|
Optional[ToolChoiceOptionParam]
|
Specifies which tool the model should use if it
decides to make an external call. This replaces |
NOT_GIVEN
|
tools
|
Optional[Iterable[ToolParam]]
|
Lists tools available for response generation, including functions and APIs. This parameter expands the model's ability to incorporate external data and functionalities into its responses, broadening the scope of possible applications. |
NOT_GIVEN
|
top_logprobs
|
Optional[int]
|
Specifies the number of top logits to return, providing insight into the model's decision-making process. This information can be valuable for analyzing the model's preferences and the statistical distribution of its predictions. |
NOT_GIVEN
|
top_p
|
Optional[float]
|
Nucleus sampling strategy focusing on top P% probability mass, influencing token selection. This parameter adjusts the scope of the token set considered at each step, effectively adjusting the diversity and unpredictability of the output. A balance between coherence and variety can be achieved by tuning this parameter. |
NOT_GIVEN
|
user
|
Optional[str]
|
Identifies the end user of the API, aiding in monitoring usage patterns and detecting potential misuse. This parameter helps maintain the security and integrity of the system, ensuring that the API is used responsibly and in accordance with OpenAI's guidelines. End user identification best practices |
NOT_GIVEN
|
extra_headers
|
Optional[Headers]
|
Additional HTTP headers to send with the request. This allows for customizing the request headers for scenarios that require specific HTTP header entries. |
None
|
extra_query
|
Optional[Query]
|
Extra query parameters for the request URL. These parameters are appended to the URL query string, providing a flexible way to extend the request with additional data. |
None
|
extra_body
|
Optional[Body]
|
Additional body parameters to include in the POST request. This can be used to pass extra information that is not covered by the standard parameters. |
None
|
timeout
|
Optional[float | Timeout]
|
Custom timeout for the request, overriding the default. Setting this parameter ensures that operations adhere to specific timing requirements, preventing excessively long waits or premature termination of requests. |
NOT_GIVEN
|
AsyncCompletionsWithRawResponse
AsyncCompletionsWithRawResponse(
completions: AsyncCompletions,
)
Attributes:
Name | Type | Description |
---|---|---|
create |
|
AsyncCompletionsWithStreamingResponse
AsyncCompletionsWithStreamingResponse(
completions: AsyncCompletions,
)
Attributes:
Name | Type | Description |
---|---|---|
create |
|
Chat
Chat(client: OpenAI)
ChatWithRawResponse
ChatWithRawResponse(chat: Chat)
ChatWithStreamingResponse
ChatWithStreamingResponse(chat: Chat)
Completions
Completions(client: OpenAI)
Methods:
Name | Description |
---|---|
create |
Creates a model response for the given chat conversation, tailored by a variety of customizable parameters. |
with_raw_response |
|
with_streaming_response |
|
create
create(
*,
messages: Iterable[ChatCompletionMessageParam],
model: Union[
str,
Literal[
"gpt-4-0125-preview",
"gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4",
"gpt-4-0314",
"gpt-4-0613",
"gpt-4-32k",
"gpt-4-32k-0314",
"gpt-4-32k-0613",
"gpt-3.5-turbo",
"gpt-3.5-turbo-16k",
"gpt-3.5-turbo-0301",
"gpt-3.5-turbo-0613",
"gpt-3.5-turbo-1106",
"gpt-3.5-turbo-0125",
"gpt-3.5-turbo-16k-0613",
],
],
frequency_penalty: (
Optional[float] | NotGiven
) = NOT_GIVEN,
function_call: FunctionCall | NotGiven = NOT_GIVEN,
functions: Iterable[Function] | NotGiven = NOT_GIVEN,
logit_bias: (
Optional[Dict[str, int]] | NotGiven
) = NOT_GIVEN,
logprobs: Optional[bool] | NotGiven = NOT_GIVEN,
max_tokens: Optional[int] | NotGiven = NOT_GIVEN,
n: Optional[int] | NotGiven = NOT_GIVEN,
presence_penalty: (
Optional[float] | NotGiven
) = NOT_GIVEN,
response_format: ResponseFormat | NotGiven = NOT_GIVEN,
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: (
Union[Optional[str], List[str]] | NotGiven
) = NOT_GIVEN,
stream: Optional[Literal[False]] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
tool_choice: (
ChatCompletionToolChoiceOptionParam | NotGiven
) = NOT_GIVEN,
tools: (
Iterable[ChatCompletionToolParam] | NotGiven
) = NOT_GIVEN,
top_logprobs: Optional[int] | NotGiven = NOT_GIVEN,
top_p: Optional[float] | NotGiven = NOT_GIVEN,
user: str | NotGiven = NOT_GIVEN,
extra_headers: Headers | None = None,
extra_query: Query | None = None,
extra_body: Body | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN
) -> ChatCompletion
create(
*,
messages: Iterable[ChatCompletionMessageParam],
model: Union[
str,
Literal[
"gpt-4-0125-preview",
"gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4",
"gpt-4-0314",
"gpt-4-0613",
"gpt-4-32k",
"gpt-4-32k-0314",
"gpt-4-32k-0613",
"gpt-3.5-turbo",
"gpt-3.5-turbo-16k",
"gpt-3.5-turbo-0301",
"gpt-3.5-turbo-0613",
"gpt-3.5-turbo-1106",
"gpt-3.5-turbo-0125",
"gpt-3.5-turbo-16k-0613",
],
],
stream: Literal[True],
frequency_penalty: (
Optional[float] | NotGiven
) = NOT_GIVEN,
function_call: FunctionCall | NotGiven = NOT_GIVEN,
functions: Iterable[Function] | NotGiven = NOT_GIVEN,
logit_bias: (
Optional[Dict[str, int]] | NotGiven
) = NOT_GIVEN,
logprobs: Optional[bool] | NotGiven = NOT_GIVEN,
max_tokens: Optional[int] | NotGiven = NOT_GIVEN,
n: Optional[int] | NotGiven = NOT_GIVEN,
presence_penalty: (
Optional[float] | NotGiven
) = NOT_GIVEN,
response_format: ResponseFormat | NotGiven = NOT_GIVEN,
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: (
Union[Optional[str], List[str]] | NotGiven
) = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
tool_choice: (
ChatCompletionToolChoiceOptionParam | NotGiven
) = NOT_GIVEN,
tools: (
Iterable[ChatCompletionToolParam] | NotGiven
) = NOT_GIVEN,
top_logprobs: Optional[int] | NotGiven = NOT_GIVEN,
top_p: Optional[float] | NotGiven = NOT_GIVEN,
user: str | NotGiven = NOT_GIVEN,
extra_headers: Headers | None = None,
extra_query: Query | None = None,
extra_body: Body | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN
) -> Stream[ChatCompletionChunk]
create(
*,
messages: Iterable[ChatCompletionMessageParam],
model: Union[
str,
Literal[
"gpt-4-0125-preview",
"gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4",
"gpt-4-0314",
"gpt-4-0613",
"gpt-4-32k",
"gpt-4-32k-0314",
"gpt-4-32k-0613",
"gpt-3.5-turbo",
"gpt-3.5-turbo-16k",
"gpt-3.5-turbo-0301",
"gpt-3.5-turbo-0613",
"gpt-3.5-turbo-1106",
"gpt-3.5-turbo-0125",
"gpt-3.5-turbo-16k-0613",
],
],
stream: bool,
frequency_penalty: (
Optional[float] | NotGiven
) = NOT_GIVEN,
function_call: FunctionCall | NotGiven = NOT_GIVEN,
functions: Iterable[Function] | NotGiven = NOT_GIVEN,
logit_bias: (
Optional[Dict[str, int]] | NotGiven
) = NOT_GIVEN,
logprobs: Optional[bool] | NotGiven = NOT_GIVEN,
max_tokens: Optional[int] | NotGiven = NOT_GIVEN,
n: Optional[int] | NotGiven = NOT_GIVEN,
presence_penalty: (
Optional[float] | NotGiven
) = NOT_GIVEN,
response_format: ResponseFormat | NotGiven = NOT_GIVEN,
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: (
Union[Optional[str], List[str]] | NotGiven
) = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
tool_choice: (
ChatCompletionToolChoiceOptionParam | NotGiven
) = NOT_GIVEN,
tools: (
Iterable[ChatCompletionToolParam] | NotGiven
) = NOT_GIVEN,
top_logprobs: Optional[int] | NotGiven = NOT_GIVEN,
top_p: Optional[float] | NotGiven = NOT_GIVEN,
user: str | NotGiven = NOT_GIVEN,
extra_headers: Headers | None = None,
extra_query: Query | None = None,
extra_body: Body | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN
) -> ChatCompletion | Stream[ChatCompletionChunk]
create(
*,
messages: Iterable[ChatCompletionMessageParam],
model: Union[
str,
Literal[
"gpt-4-0125-preview",
"gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4",
"gpt-4-0314",
"gpt-4-0613",
"gpt-4-32k",
"gpt-4-32k-0314",
"gpt-4-32k-0613",
"gpt-3.5-turbo",
"gpt-3.5-turbo-16k",
"gpt-3.5-turbo-0301",
"gpt-3.5-turbo-0613",
"gpt-3.5-turbo-1106",
"gpt-3.5-turbo-0125",
"gpt-3.5-turbo-16k-0613",
],
],
frequency_penalty: (
Optional[float] | NotGiven
) = NOT_GIVEN,
function_call: FunctionCall | NotGiven = NOT_GIVEN,
functions: Iterable[Function] | NotGiven = NOT_GIVEN,
logit_bias: (
Optional[Dict[str, int]] | NotGiven
) = NOT_GIVEN,
logprobs: Optional[bool] | NotGiven = NOT_GIVEN,
max_tokens: Optional[int] | NotGiven = NOT_GIVEN,
n: Optional[int] | NotGiven = NOT_GIVEN,
presence_penalty: (
Optional[float] | NotGiven
) = NOT_GIVEN,
response_format: ResponseFormat | NotGiven = NOT_GIVEN,
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: (
Union[Optional[str], List[str]] | NotGiven
) = NOT_GIVEN,
stream: (
Optional[Literal[False]] | Literal[True] | NotGiven
) = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
tool_choice: (
ChatCompletionToolChoiceOptionParam | NotGiven
) = NOT_GIVEN,
tools: (
Iterable[ChatCompletionToolParam] | NotGiven
) = NOT_GIVEN,
top_logprobs: Optional[int] | NotGiven = NOT_GIVEN,
top_p: Optional[float] | NotGiven = NOT_GIVEN,
user: str | NotGiven = NOT_GIVEN,
extra_headers: Headers | None = None,
extra_query: Query | None = None,
extra_body: Body | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN
) -> ChatCompletion | Stream[ChatCompletionChunk]
Creates a model response for the given chat conversation, tailored by a variety of customizable parameters.
This method allows for detailed control over the chat completion process, including model selection, response formatting, and dynamic interaction through streaming.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
messages
|
Iterable[ChatCompletionMessageParam]
|
Messages comprising the conversation so far. Example Python code available at How to format inputs to ChatGPT models. |
required |
model
|
str | Literal[...]
|
ID of the model to use. Refer to the model endpoint compatibility table for details on which models are compatible with the Chat API. |
required |
stream
|
Literal[True]
|
If True, enables streaming of message deltas. Tokens are sent as server-sent events as they become available,
terminating with a |
NOT_GIVEN
|
frequency_penalty
|
Optional[float], default=NOT_GIVEN
|
Adjusts token generation frequency to discourage repetition, with a range between -2.0 and 2.0. More details at frequency and presence penalties. |
NOT_GIVEN
|
function_call
|
FunctionCall
|
Deprecated in favor of |
NOT_GIVEN
|
functions
|
Iterable[Function]
|
Deprecated in favor of |
NOT_GIVEN
|
logit_bias
|
Optional[Dict[str, int]], default=NOT_GIVEN
|
Modifies the likelihood of specified tokens. Accepts a dict mapping token IDs to bias values (-100 to 100). |
NOT_GIVEN
|
logprobs
|
Optional[bool], default=NOT_GIVEN
|
Includes log probabilities of output tokens when True. Not available for |
NOT_GIVEN
|
max_tokens
|
Optional[int], default=NOT_GIVEN
|
Sets the maximum token count for the chat completion. See How to count tokens with TikToken for token counting examples. |
NOT_GIVEN
|
n
|
Optional[int], default=NOT_GIVEN
|
Number of chat completion choices to generate for each message. Affects cost. |
NOT_GIVEN
|
presence_penalty
|
Optional[float], default=NOT_GIVEN
|
Adjusts for token presence to promote topic diversity, with a range between -2.0 and 2.0. |
NOT_GIVEN
|
response_format
|
ResponseFormat
|
Specifies the model output format, compatible with GPT-4 Turbo and GPT-3.5 Turbo models newer than
|
NOT_GIVEN
|
seed
|
Optional[int], default=NOT_GIVEN
|
Seeds the RNG for deterministic outputs. Beta feature. |
NOT_GIVEN
|
stop
|
Union[Optional[str], List[str]], default=NOT_GIVEN
|
Sequences indicating when to halt token generation. |
NOT_GIVEN
|
temperature
|
Optional[float], default=NOT_GIVEN
|
Controls output randomness. Recommended to adjust this or |
NOT_GIVEN
|
tool_choice
|
ChatCompletionToolChoiceOptionParam
|
Selects a tool or function for the model to use. |
NOT_GIVEN
|
tools
|
Iterable[ChatCompletionToolParam]
|
Specifies available tools for the model, currently limited to functions. |
NOT_GIVEN
|
top_logprobs
|
Optional[int], default=NOT_GIVEN
|
Returns top log probabilities for each token position. Requires |
NOT_GIVEN
|
top_p
|
Optional[float], default=NOT_GIVEN
|
Nucleus sampling parameter, considering only the top probability mass for generation. |
NOT_GIVEN
|
user
|
str | NotGiven
|
Unique identifier for the end-user, assisting in abuse monitoring. Learn more at End-user IDs. |
NOT_GIVEN
|
extra_headers
|
Headers
|
Additional HTTP headers for the request. |
None
|
extra_query
|
Query
|
Additional query parameters for the request. |
None
|
extra_body
|
Body
|
Additional body content for the request. |
None
|
timeout
|
float | httpx.Timeout | None | NotGiven, default=NOT_GIVEN
|
Custom timeout for this request, overriding the default settings. |
NOT_GIVEN
|
Returns:
Type | Description |
---|---|
ChatCompletion | Stream[ChatCompletionChunk]
|
Stream[ChatCompletionChunk]: A stream of chat completion chunks for real-time interaction. |
Examples:
CompletionsWithRawResponse
CompletionsWithRawResponse(completions: Completions)
Attributes:
Name | Type | Description |
---|---|---|
create |
|
CompletionsWithStreamingResponse
CompletionsWithStreamingResponse(completions: Completions)
Attributes:
Name | Type | Description |
---|---|---|
create |
|