# 问答

根据提示，模型将返回一个或多个预测答案，并且还可以在每个位置返回替代令牌的概率。

### 创建问答

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

为提供的提示和参数创建一个问答。

#### 请求示例

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

```sh
curl https://api.openai.com/v1/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "text-davinci-003",
    "prompt": "Say this is a test",
    "max_tokens": 7,
    "temperature": 0
  }'
```

{% endcode %}
{% endtab %}

{% tab title="python" %}

```python
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Completion.create(
  model="text-davinci-003",
  prompt="Say this is a test",
  max_tokens=7,
  temperature=0
)

```

{% 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 response = await openai.createCompletion({
  model: "text-davinci-003",
  prompt: "Say this is a test",
  max_tokens: 7,
  temperature: 0,
});

```

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

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

```json
{
  "model": "text-davinci-003",
  "prompt": "Say this is a test",
  "max_tokens": 7,
  "temperature": 0,
  "top_p": 1,
  "n": 1,
  "stream": false,
  "logprobs": null,
  "stop": "\n"
}
```

{% endcode %}

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

```json
{
  "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
  "object": "text_completion",
  "created": 1589478378,
  "model": "text-davinci-003",
  "choices": [
    {
      "text": "\n\nThis is indeed a test",
      "index": 0,
      "logprobs": null,
      "finish_reason": "length"
    }
  ],
  "usage": {
    "prompt_tokens": 5,
    "completion_tokens": 7,
    "total_tokens": 12
  }
}

```

{% endcode %}

#### 请求主体

* model：字符串 必需 要使用的模型的ID。您可以使用List models API查看所有可用模型，或者查看我们的模型概述以获取它们的描述。
* prompt：字符串或数组 可选的 默认值<|endoftext|> 要生成完成的提示，可编码为字符串、字符串数组、令牌数组或令牌数组数组。注意：<|endoftext|>该符号是模型所看到的文档分隔符，因此如果未指定提示，模型将生成好像从新文档的开头开始一样。
* max\_tokens：整数 可选的 默认为16 完成中生成的最大令牌数。

  您的提示的令牌数加上max\_tokens不能超过模型的上下文长度。大多数模型的上下文长度为2048个令牌（除了最新的支持4096个令牌的模型）。
* temperature：数字 可选的 默认为1 使用的采样温度，介于0和2之间。较高的值（例如0.8）将使输出更随机，而较低的值（例如0.2）将使其更加集中和确定性。

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

  我们通常建议更改这个或temperature，但不要同时更改两个。
* n：整数 可选的 默认为1 每个提示要生成多少个完成。

  注意：由于此参数生成许多完成，因此可能会快速消耗您的令牌配额。请谨慎使用，并确保您的max\_tokens和stop设置合理。
* stream： 布尔值 可选的 默认为false 是否返回部分进度的流。如果设置，令牌将作为只含数据的服务器推送事件发送，一旦可用，流将由数据：\[DONE]消息终止。
* logprobs： 整数 可选的 默认为null 包括logprobs最有可能的令牌的对数概率，以及选择的令牌。例如，如果logprobs是5，则API将返回最有可能的5个令牌的列表。API将始终返回采样令牌的logprob，因此响应中可能有多达logprobs + 1个元素。

  logprobs的最大值为5。如果您需要更多，请通过我们的帮助中心与我们联系，并描述您的用例。
* echo： 布尔值 可选的 默认为false 除了完成之外，还回显提示。
* stop： 字符串或数组 可选的 默认为null 最多4个序列，其中API将停止生成更多的令牌。返回的文本将不包含停止序列。
* presence\_penalty： 数字 可选的 默认为0 -2.0至2.0之间的数字。正值会根据令牌在迄今为止的文本中是否出现来惩罚新令牌，从而增加模型谈论新主题的可能性。
* frequency\_penalty： 数字 可选的 默认为0 -2.0至2.0之间的数字。正值会根据令牌在迄今为止的文本中的现有频率来惩罚新令牌，从而降低模型按原样重复相同行的可能性。
* best\_of： 整数 可选的 默认为1 在服务器端生成best\_of完成并返回“最佳”（每个令牌的对数概率最高的完成）。结果无法被流式传输。

  当与n一起使用时，best\_of控制候选完成的数量，n指定要返回多少个-best\_of必须大于n。

  注意：由于此参数生成许多完成，因此可能会快速消耗您的令牌配额。请谨慎使用，并确保您的max\_tokens和stop设置合理。
* logit\_bias： 映射 可选的 默认为null 修改完成中指定令牌出现的可能性。

  接受将令牌（由其在GPT分词器中的令牌ID指定）映射到从-100到100的相关偏差值的json对象。您可以使用此分词器工具（适用于GPT-2和GPT-3）将文本转换为令牌ID。在数学上，在采样之前，将偏差添加到模型生成的对数概率中。确切的效果
* 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/wen-da.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.
