# 对话

根据聊天会话，模型将返回聊天完成响应。

### 创建聊天完成

```
POST https://api.openai.com/v1/chat/completions
```

为聊天消息创建完成

#### 请求示例

{% tabs %}
{% tab title="curl" %}
{% code lineNumbers="true" %}

```sh
curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-3.5-turbo",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'
```

{% endcode %}
{% endtab %}

{% tab title="python" %}
{% code lineNumbers="true" %}

```python
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")

completion = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "user", "content": "Hello!"}
  ]
)

print(completion.choices[0].message)

```

{% endcode %}
{% endtab %}

{% tab title="node.js" %}
{% code lineNumbers="true" %}

```javascript
const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

const completion = await openai.createChatCompletion({
  model: "gpt-3.5-turbo",
  messages: [{role: "user", content: "Hello world"}],
});
console.log(completion.data.choices[0].message);

```

{% endcode %}
{% endtab %}
{% endtabs %}

{% code title="请求参数" lineNumbers="true" %}

```json
{
  "model": "gpt-3.5-turbo",
  "messages": [{"role": "user", "content": "Hello!"}]
}
```

{% endcode %}

{% code title="返回结果" lineNumbers="true" %}

```json
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "\n\nHello there, how may I assist you today?",
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}
```

{% endcode %}

#### 请求主体

* model 字符串 必需 要使用的模型的ID。有关哪些模型适用于Chat API的详细信息，请参见模型端点兼容性表。
* messages 数组 必需 要为其生成聊天完成的
* temperature： 数字 可选的 默认为1 使用的采样温度，介于0和2之间。较高的值（例如0.8）将使输出更随机，而较低的值（例如0.2）将使其更加集中和确定性。

  我们通常建议更改这个或top\_p，但不要同时更改两个。
* top\_p： 数字 可选的 默认为1 与采用温度进行采样的另一种方法称为nucleus采样，其中模型考虑具有top\_p概率质量的令牌的结果。因此，0.1表示仅考虑组成前10％概率质量的令牌。

  我们通常建议更改这个或temperature，但不要同时更改两个。
* n： 整数 可选的 默认为1 为每个输入消息生成多少个聊天完成选项。
* stream： 布尔值 可选的 默认为false 如果设置，将发送部分消息增量，就像在ChatGPT中一样。令牌将作为只含数据的服务器推送事件发送，一旦可用，流将由数据：\[DONE]消息终止。请参见OpenAI Cookbook的示例代码。
* stop： 字符串或数组 可选的 默认为null 最多4个序列，其中API将停止生成更多的令牌。
* max\_tokens： 整数 可选的 默认为inf 在聊天完成中生成的最大令牌数。

  输入令牌和生成令牌的总长度受模型上下文长度的限制。
* presence\_penalty： 数字 可选的 默认为0 -2.0至2.0之间的数字。正值会根据令牌在迄今为止的文本中是否出现来惩罚新令牌，从而增加模型谈论新主题的可能性。
* frequency\_penalty： 数字 可选的 默认为0 -2.0至2.0之间的数字。正值会根据令牌在迄今为止的文本中的现有频率来惩罚新令牌，从而降低模型按原样重复相同行的可能性。
* logit\_bias： 映射 可选的 默认为null 修改完成中指定令牌出现的可能性。

  接受将令牌（由其在分词器中的令牌ID指定）映射到从-100到100的相关偏差值的json对象。在数学上，在采样之前，将偏差添加到模型生成的对数概率中。确切的效果将因模型而异，但值在-1和1之间应减少或增加选择的可能性；值如-100或100应导致禁止或独占选择相关令牌。
* user： 字符串 可选的 代表您的终端用户的唯一标识符，可帮助OpenAI监视和检测滥用。了解更多。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.luojixiangliang.com/reference/openai-api-can-kao-wen-dang/dui-hua.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
