OpenAI GPT For Python Developers 第三章 使用GPT的Text Completions

发布于 2023年10月06日

一旦您已经验证了您的应用程序,您就可以开始使用OpenAI API来执行完成操作。为此,您需要使用OpenAI的completion API。

OpenAI completion API使开发人员能够访问OpenAI的数据集和模型,使完成操作变得轻松。

首先提供句子的开头。然后,模型将预测一个或多个可能的完成,每个都有一个关联的分数。

基本的完成示例

例如,我们将句子“Once upon a time”输入API,它将返回下一个可能的句子。

激活开发环境:

workon chatgpt for python developers

创建一个新的Python文件“app.py”,在其中添加以下代码:

import os 

import openai

def init_api():
    with open(".env") as env:
        for line in env:
            key, value = line.strip().split("=")
            os.environ[key] = value
    openai.api_key = os.environ.get("API_KEY")
    openai.organization = os.environ.get("ORG_ID")

init_api()

next = openai.Completion.create(
    model="text-davinci-003",
    prompt="Once upon a time",
    max_tokens=7,
    temperature=0
)

print(next)

现在执行文件:

python app.py

API应该返回类似以下的内容:

{

 "choices": [ {

 "finish_reason": "length",

 "index": 0,

 "logprobs": null,

 "text": "	.	there was a little girl named Alice"

}

],

"created": 1674510189,

"id": "cmpl-6bysnKOUj0QWOu5DSiJAGcwIVMfNh", 

"model": "text-davinci-003",

"object": "text_completion",

"usage": {

"completion_tokens": 7, 

"prompt_tokens": 4, 

"total_tokens": 11

} }

在上面的示例中,我们只有一个选择:“there was a little girl named Alice ”。这个结果的索引是0。

API还返回了“finish_reason”,在这种情况下是“length”。

输出的长度由API确定,基于用户提供的“max_tokens”值。在我们的情况下,我们将这个值设置为7。

注意:标记(token),根据定义,是输出文本中的常见字符序列。一个好的记法是,一个标记通常表示普通英语单词的约4个字母。这意味着100个标记大约等于75个单词。理解这一点将有助于您理解定价。在本书的后面,我们将更深入地探讨定价细节。

控制输出的标记数

让我们测试一个更长的示例,这意味着更多的标记(15个):

import os 
import openai

def init_api():
    with open(".env") as env:
        for line in env:
            key, value = line.strip().split("=")
            os.environ[key] = value
    openai.api_key = os.environ.get("API_KEY")
    openai.organization = os.environ.get("ORG_ID")

init_api()

next = openai.Completion.create(
    model="text-davinci-003",
    prompt="Once upon a time",
    max_tokens=15,
    temperature=0
)

print(next)

API返回了更长的文本:

{

 "choices": [{

 }

"finish_reason": "length",

"index": 0,

"logprobs": null,

"text": "there was a little girl named Alice. She lived in a small village with"

],

"created": 1674510438,

"id": "cmpl-6bywotGlkujbrEF0emJ8iJmJBEEnO",

"model": "text-davinci-003",

"object": "text_completion", 
"usage": {
  "completion_tokens": 15, 
  "prompt_tokens": 4, 
  "total_tokens": 19

 } 
}

Logprobs

为增加可能性,我们可以使用“logprobs”参数。例如,将logprobs设置为2将返回每个标记的两个版本。

import os
import openai

def init_api():
    with open(".env") as env:
        for line in env:
            key, value = line.strip().split("=")
            os.environ[key] = value
    openai.api_key = os.environ.get("API_KEY")
    openai.organization = os.environ.get("ORG_ID")

init_api()

next = openai.Completion.create(
    model="text-davinci-003",
    prompt="Once upon a time",
    max_tokens=15,
    temperature=0,
    logprobs=3
)

print(next)

这是API返回的内容:

{

 "choices": [ {

 "finish_reason": "length",

 "index": 0,

 "logprobs": {

 "text_offset": [

 ],

 "token_logprobs": [

 -0.9263134,

 -0.2422086,

 -0.039050072,

 -1.8855333,

 -0.15475112,

 -0.30592665,

 -1.9697434,

 -0.34726024,

 -0.46498245,

 -0.46052673,

 -0.14448218,

 -0.0038384167,

 -0.029725535,

 -0.34297562,

 -0.4261593

 ],

 "tokens": [

 " there",

 " was",

  " a",

  " little",

  " girl",

  " named",

  " Alice",

  ".",

  " She",

  " lived",

  " in",

  " a",

  " small",

  " village",

  " with"

],

"top_logprobs": [

{

"\n": -1.1709108,

" there": -0.9263134

}, {

    " lived": -2.040701,

    " was": -0.2422086

  },

  {

    " a": -0.039050072,

    " an": -3.403554

}, {

    " little": -1.8855333,

    " young": -2.02082

  },

  {

    " boy": -2.449015,

    " girl": -0.15475112

}, {

    " named": -0.30592665,

    " who": -1.7700866

  },

  {

    " Alice": -1.9697434,

    " Sarah": -2.9232333

},

{

            " who": -1.3002346,

            ".": -0.34726024

}, {

            " Alice": -1.2721952,

            " She": -0.46498245

          },

          {

            " lived": -0.46052673,

            " was": -1.7077477

}, {

            " in": -0.14448218,

            " with": -2.0538774

          },

          {

            " a": -0.0038384167,

            " the": -5.8157005

}, {

            " quaint": -4.941383,

            " small": -0.029725535

          },

          {

            " town": -1.454277,

            " village": -0.34297562

}, {

            " in": -1.7855972,

            " with": -0.4261593

}

] },

"text": " there was a little girl named Alice. She lived in a small village with"

} ],

  "created": 1674511658,

  "id": "cmpl-6bzGUTuc5AmbjsoNLNJULTaG0WWUP",

  "model": "text-davinci-003",

  "object": "text_completion",

  "usage": {

		"completion_tokens": 15,

    "prompt_tokens": 4,

    "total_tokens": 19

} }

您可以看到,每个标记都有一个与之相关的概率或分数。API将在“\n”和“ there”之间返回“there”,因为-1.1709108小于-0.9263134。

API将选择“was”而不是“lived”,因为-0.2422086大于-2.040701。类似地,对于其他值也是如此。

每个标记都有两个可能的值。API返回每个值的概率以及具有最高概率的标记组成的句子。

"tokens": [ " there", " was", " a",

          " little",

          " girl",

          " named",

          " Alice",

          ".",

		  " She",

 		  "lived",

          " in",

          " a",

          " small",

          " village",

          " with"
],

我们可以将大小增加到5。根据OpenAI的说法,logprobs的最大值为5。如果您需要更多,请联系他们的帮助中心并解释您的用途。

控制创造力:采样温度

我们可以自定义的下一个参数是温度(temperature)。这可以用于增加模型的创造力,但创造力伴随着一些风险。

要实现更具创造性的应用,我们可以使用较高的温度,例如0.2、0.3、0.4、0.5和0.6。最大温度值为2。

import os

import openai

def init_api():
    with open(".env") as env:
        for line in env:
            key, value = line.strip().split("=")
            os.environ[key] = value

    openai.api_key = os.environ.get("API_KEY")
    openai.organization = os.environ.get("ORG_ID")

init_api()

next = openai.Completion.create(
    model="text-davinci-003",
    prompt="Once upon a time",
    max_tokens=15,
    temperature=2
)

print(next)

API返回:

{

"choices": [

{

"finish_reason": "length",

"index": 0,

"logprobs": null,

"text": " there lived\ntwo travellers who ravret for miles through desert forest carwhen"

}

],

"created": 1674512348,

"id": "cmpl-6bzRc4nJXBKBaOE6pr5d4bLCLI7N5",

"model": "text-davinci-003",

"object": "text_completion",

"usage": {

"completion_tokens": 15,

"prompt_tokens": 4,

"total_tokens": 19

}

}

温度被设置为其最大值,因此执行相同的脚本应返回不同的结果。这是创造性发挥作用的地方。

使用“top_p”进行采样

或者,我们可以使用top_p参数。例如,使用0.5表示只考虑概率质量最高的标记,占50%。使用0.1表示只考虑概率质量最高的标记,占10%。

import os

import openai

def init_api():
    with open(".env") as env:
        for line in env:
            key, value = line.strip().split("=")
            os.environ[key] = value

    openai.api_key = os.environ.get("API_KEY")
    openai.organization = os.environ.get("ORG_ID")

init_api()

next = openai.Completion.create(
    model="text-davinci-003",
    prompt="Once upon a time",
    max_tokens=15,
    top_p=0.9
)

print(next)

建议要么使用top_p参数,要么使用温度参数,但不要同时使用两者。

top_p参数也称为核采样或top-p采样。

流式返回结果

另一个常见的参数是stream。可以指示API返回令牌流而不是包含所有令牌的块。在这种情况下,API将返回一个生成器,按照它们生成的顺序生成令牌。

import os
import openai

def init_api():
    with open(".env") as env:
        for line in env:
            key, value = line.strip().split("=")
            os.environ[key] = value

    openai.api_key = os.environ.get("API_KEY")
    openai.organization = os.environ.get("ORG_ID")

init_api()

next = openai.Completion.create(
    model="text-davinci-003",
    prompt="Once upon a time",
    max_tokens=7,
    stream=True
)

# 这将打印出下一个类型,即<class 'generator'>,然后是令牌:
print(type(next))
# 解压生成器
print(*next, sep='\n')

这将打印出下一个类型,即<class 'generator'>,然后是令牌。

如果您只想获取文本,可以使用类似以下的代码:

import os
import openai

def init_api()
    with open(".env") as env:
        for line in env:
            key, value = line.strip().split("=")
            os.environ[key] = value

    openai.api_key = os.environ.get("API_KEY")
    openai.organization = os.environ.get("ORG_ID")

init_api()

next = openai.Completion.create(
    model="text-davinci-003",
    prompt="Once upon a time",
    max_tokens=7,
    stream=True
)

# 逐个读取生成器的文本元素
for i in next:
    print(i['choices'][0]['text'])

这将打印出:

there
was
a
small
village
that
was

控制重复性:频率和存在惩罚

Completions API 具有两个功能,可用于阻止相同的单词被建议得太频繁。这些功能通过在 logits(显示单词建议的可能性的数字)上添加奖励或惩罚来改变某些单词被建议的机会。

这些功能可以使用两个参数启用:

  • presence_penalty 是一个可以在 -2.0 到 2.0 之间的数字。如果数字为正数,它会使模型更有可能谈论新话题,因为如果使用已经使用过的单词,它将受到惩罚。

  • frequency_penalty 是一个在 -2.0 到 2.0 之间的数字。正值会使模型更不可能重复已经使用过的文本行。

为了了解这些参数的影响,让我们在以下代码中使用它们:

import os
import openai

def init_api():
  with open(".env") as env:
    for line in env:
       key, value = line.strip().split("=")
       os.environ[key] = value

  openai.api_key = os.environ.get("API_KEY")
  openai.organization = os.environ.get("ORG_ID")

init_api()

next = openai.Completion.create(
  model="text-davinci-003",
  prompt="Once upon a time",
  max_tokens=100,
  frequency_penalty=2.0,
  presence_penalty=2.0,
)

print("=== Frequency and presence penalty 2.0 ===")
print(next["choices"][0]["text"])

next = openai.Completion.create(
  model="text-davinci-003",
  prompt="Once upon a time",
  max_tokens=100,
  frequency_penalty=-2.0,
  presence_penalty=-2.0,
)

print("=== Frequency and presence penalty -2.0 ===")
print(next["choices"][0]["text"])

如您所见,第一次执行将在文本中产生更多的多样性(frequency_penalty=2.0 和 presence_penalty=2.0),而第二次执行则完全相反(frequency_penalty=-2.0, presence_penalty=-2.0)。

执行上述代码后,输出如下:

=== Frequency and presence penalty 2.0 ===

Once upon a time, there was a beautiful princess named Cinderella. She lived with her wicked stepmother and two jealous stepsisters who treated her like their servant. One day, an invitation arrived to the palace ball where all of the eligible young ladies in town were invited by the prince himself! Cinderella's determination kept fueling when she noticed how cruelly she had been mistreated as if it gave her more strength to reach for what she desired most - to attend that magnificent event alongside everyone else but not just only that.

=== Frequency and presence penalty -2.0 ===

Once upon a time, there lived a little girl named Lucy. She lived a very happy life with her family. She lived a very simple life. She lived a very happy life. She lived a very happy life. She lived a very happy life. She lived a very happy life. She lived a very happy life. She lived a very happy life. She lived a very happy life. She lived a very happy life.

如您所见,第二次输出不断产生类似 "She lived a very happy life" 这样的输出,达到了一定的数量。

控制输出数量

如果您想要多个结果,可以使用 n 参数。以下示例将生成 2 个结果:

import os
import openai

def init_api():
  with open(".env") as env:
    for line in env:
      key, value = line.strip().split("=")
      os.environ[key] = value

  openai.api_key = os.environ.get("API_KEY")
  openai.organization = os.environ.get("ORG_ID")

init_api()

next = openai.Completion.create(
  model="text-davinci-003",
  prompt="Once upon a time",
  max_tokens=5,
  n=2,
)

print(next)

这是上述 Python 代码生成的示例输出:

{

"choices": [

{

"finish_reason": "length",

"index": 0,

"logprobs": null,

"text": " there was a kind old"

},

{

"finish_reason": "length",

"index": 1,

"logprobs": null,

"text": ", there was a king"

}

],

"created": 1674597690,

"id": "cmpl-6cLe6CssOiH4AYcLPz8eFyy53apdR",

"model": "text-davinci-003",

"object": "text_completion",

"usage": {

"completion_tokens": 10,

"prompt_tokens": 4,

"total_tokens": 14

}
}

获取“最佳”的结果

可以要求 AI 模型在服务器端为给定任务生成可能的完成,并选择最有可能正确的完成结果。这可以使用 best_of 参数来完成。

在使用 best_of 时,您需要指定两个数字:`n` 和 best_of

就像之前所看到的,`n` 是您希望看到的候选完成的数量。请注意:确保 best_of 大于 n

让我们看一个示例:

import os
import openai

def init_api():
  with open(".env") as env:
    for line in env:
      key, value = line.strip().split("=")
      os.environ[key] = value
  
  openai.api_key = os.environ.get("API_KEY")
  openai.organization = os.environ.get("ORG_ID")

init_api()

next = openai.Completion.create(
  model="text-davinci-003",
  prompt="Once upon a time",
  max_tokens=5,
  n=1,
  best_of=2,
)

print(next)

控制完成的停止时间

在大多数情况下,有必要停止 API 生成更多文本。

假设我们想生成一个段落,不再生成其他文本。在这种情况下,我们可以要求 API 在遇到新行符 (`\n`) 时停止生成文本。可以使用类似下面的代码来实现:

import os
import openai

def init_api():
  with open(".env") as env:
    for line in env:
    key, value = line.strip().split("=")
    os.environ[key] = value

  openai.api_key = os.environ.get("API_KEY")
  openai.organization = os.environ.get("ORG_ID")

init_api()

next = openai.Completion.create(
  model="text-davinci-003",
  prompt="Once upon a time",
  max_tokens=5,
  stop=["\n",],
)

print(next)

stop 参数可以包含最多四个停止词。请注意,完成结果将不包括停止序列。这是另一个示例:

import os
import openai

def init_api():
  with open(".env") as env:
    for line in env:
      key, value = line.strip().split("=")
      os.environ[key] = value
   
  openai.api_key = os.environ.get("API_KEY")
  openai.organization = os.environ.get("ORG_ID")

init_api()

next = openai.Completion.create(
  model="text-davinci-003",
  prompt="Once upon a time",
  max_tokens=5,
  stop=["\n", "Story", "End", "Once upon a time"],
)

print(next)

使用文本完成后的后缀

假设我们想创建一个包含 0 到 9 之间的质数列表的 Python 字典:

{

"primes": [2, 3, 5, 7]

}

在这种情况下,API 应该返回 2, 3, 5, 7。我们可以使用后缀参数来实现这一点。该参数配置在插入文本完成后的后缀。

让我们尝试两个示例以更好地理解。

在第一个示例中,我们将告诉 API 如何以 {\n\t\"primes\":[ 开始字典,然后插入文本完成:

import os
import openai

def init_api():
  with open(".env") as env:
    for line in env:
      key, value = line.strip().split("=")
      os.environ[key] = value
  
  openai.api_key = os.environ.get("API_KEY")
  openai.organization = os.environ.get("ORG_ID")

init_api()

next = openai.Completion.create(
  model="text-davinci-002",
  prompt="Write a JSON containing primary numbers between 0 and 9 \n\n{\n\t\"primes\":[",
)

print(next)

API 应该返回这样的文本:

2,3,5,7]\n}

在第二个示例中,我们不希望看到 API 插入结果文本的结尾时关闭数据结构,所以我们使用了 suffix 参数:

import os
import openai

def init_api():
  with open(".env") as env:
    for line in env:
      key, value = line.strip().split("=")
      os.environ[key] = value
  
  openai.api_key = os.environ.get("API_KEY")
  openai.organization = os.environ.get("ORG_ID")

init_api()

next = openai.Completion.create(
  model="text-davinci-002",
  prompt="Write a JSON containing primary numbers between 0 and 9 \n\n{\n\t\"primes\":[",
  suffix="]\n}",
)

print(next)

API 现在应该返回:

2,3,5,7

而不是之前的(`2, 3, 5, 7]\n}`)。

这就是如何向 API 提供关于完成的后缀的信息。

示例:提取关键词

在此示例中,我们想要从文本中提取关键词。这是我们将要使用的文本:

The first programming language to be invented was Plankalkül, which was designed by Konrad Zuse in the 1940s, but not publicly known until 1972 (and not implemented until 1998). The first widely known and successful high-level programming language was Fortran, developed from 1954 to 1957 by a team of IBM researchers led by John Backus. The success of FORTRAN led to the formation of a committee of scientists to develop a "universal" computer language; the result of their effort was ALGOL 58. Separately, John McCarthy of MIT developed Lisp, the first language with origins in academia to be successful. With the success of these initial efforts, programming languages became an active topic of research in the 1960s and beyond.

这是我们将要使用的代码:

import os
import openai

def init_api():
  with open(".env") as env:
    for line in env:
      key, value = line.strip().split("=")
      os.environ[key] = value
  
  openai.api_key = os.environ.get("API_KEY")
  openai.organization = os.environ.get("ORG_ID")

init_api()

prompt = "The first programming language to be invented was Plankalkül, which was designed by Konrad Zuse in the 1940s, but not publicly known until 1972 (and not implemented until 1998). The first widely known and successful high-level programming language was Fortran, developed from 1954 to 1957 by a team of IBM researchers led by John Backus. The success of FORTRAN led to the formation of a committee of scientists to develop a \"universal\" computer language; the result of their effort was ALGOL 58. Separately, John McCarthy of MIT developed Lisp, the first language with origins in academia to be successful. With the success of these initial efforts, programming languages became an active topic of research in the 1960s and beyond\n\nKeywords:"

tweet = openai.Completion.create(
  model="text-davinci-002",
  prompt=prompt,
  temperature=0.5,
  max_tokens=300,
)

print(tweet)

这个 prompt 如下所示:

The first programming language to be invented was Plankalkül

[...]

[...]

in the 1960s and beyond

Keywords:

通过将 "Keywords:" 添加到 prompt 中的新行中,模型将识别到我们需要关键词,输出应该看起来像这样:

programming language,Plankalkül,Konrad Zuse,FORTRAN,John Backus,ALGOL 58,John McCarthy,MIT,Lisp

您可以使用 prompt 进行试验,并尝试不同的东西,例如:

The first programming language to be invented was Plankalkül

[...]

[...]

in the 1960s and beyond

Keywords:

-

这对应于以下 prompt:

prompt = "The first programming language to be invented was Plankalkül, which was designed by Konrad Zuse in the 1940s, but not publicly known until 1972 (and not implemented until 1998). The first widely known and successful high-level programming language was Fortran, developed from 1954 to 1957 by a team of IBM researchers led by John Backus. The success of FORTRAN led to the formation of a committee of scientists to develop a \"universal\" computer language; the result of their effort was ALGOL 58. Separately, John McCarthy of MIT developed Lisp, the first language with origins in academia to be successful. With the success of these initial efforts, programming languages became an active topic of research in the 1960s and beyond\n\nKeywords:\n-"

在这种情况下,我们应该有类似以下的结果:

Plankalkül

-Konrad Zuse

-FORTRAN

-John Backus

-IBM

-ALGOL 58

-John McCarthy

-MIT

-Lisp

示例:生成推文

我们将继续使用先前的示例,但是我们在关键词之后添加 Tweet 而不是关键词。

import os
import openai

def init_api():
  with open(".env") as env:
    for line in env:
      key, value = line.strip().split("=")
      os.environ[key] = value
  
  openai.api_key = os.environ.get("API_KEY")
  openai.organization = os.environ.get("ORG_ID")

init_api()

prompt = "The first programming language to be invented was Plankalkül, which was designed by Konrad Zuse in the 1940s, but not publicly known until 1972 (and not implemented until 1998). The first widely known and successful high-level programming language was Fortran, developed from 1954 to 1957 by a team of IBM researchers led by John Backus. The success of FORTRAN led to the formation of a committee of scientists to develop a \"universal\" computer language; the result of their effort was ALGOL 58. Separately, John McCarthy of MIT developed Lisp, the first language with origins in academia to be successful. With the success of these initial efforts, programming languages became an active topic of research in the 1960s and beyond\n\nTweet:"

tweet = openai.Completion.create(
  model="text-davinci-002",
  prompt=prompt,
  temperature=0.5,
  max_tokens=300,
)

print(tweet.choices[0]["text"].strip())

请注意,我们可以直接访问并删除文本,使用 my_song.choices[0]["text"].strip() 只打印歌曲:

I was born in the ghetto 2

Raised in the hood

My momma didn't have much

But she did the best she could 8

I never had much growing up

But I always had heart

I knew I was gonna make it 14

I was gonna be a star

Now I'm on top of the world 18

And I'm never looking back

您可以调整模型的“创造力”和其他参数,进行测试、调整和尝试不同的组合。

示例:生成待办事项列表

在此示例中,我们要求模型生成一个待办事项列表,以创建美国的一家公司。我们需要列表中的五个项目。

import os
import openai

def init_api():
  with open(".env") as env:
    for line in env:
      key, value = line.strip().split("=")
      os.environ[key] = value
  
  openai.api_key = os.environ.get("API_KEY")
  openai.organization = os.environ.get("ORG_ID")

init_api()

next = openai.Completion.create(
  model="text-davinci-002",
  prompt="Todo list to create a company in US\n\n1.",
  temperature=0.3,
  max_tokens=64,
  top_p=0.1,
  frequency_penalty=0,
  presence_penalty=0.5,
  stop=["6."],
)

print(next)

格式化后的文本应该是这样的:

1. Choose a business structure.

2. Register your business with the state. 4

3. Obtain a federal tax ID number.

4. Open a business bank account.

5. Establish business credit.

在我们的代码中,我们使用了以下参数:

- model="text-davinci-002"

- prompt="Todo list to create a company in US\n\n1."

- temperature=0.3

- max_tokens=64

- top_p=0.1

- frequency_penalty=0

- presence_penalty=0.5

- stop=["6."]

让我们逐个来看这些参数:

- model:指定 API 用于生成文本完成的模型。在这种情况下,它使用的是 "text-davinci-002"。

- prompt:是 API 用于生成完成的起始点文本。在我们的情况下,我们使用一个待办事项列表,用于在美国创建公司。第一项应该以 "1." 开头,因为我们要求的输出应该采用这种格式:

1. <第1项>

2. <第2项>

3. <第3项>

4. <第4项>

5. <第5项>

- temperature:控制模型生成的文本的“创造力”。较高的温度会产生更具创意和多样化的完成,而较低的温度会产生更“保守”和可预测的完成。在这种情况下,温度设置为 0.3。

- max_tokens:限制 API 将生成的最大令牌数。在我们的情况下,我们限制了生成的文本不超过 64 个令牌。

- top_p:这是一个用于控制模型生成文本的参数,它限制了模型选择概率最高的词的数量。`top_p=0.1` 表示模型将选择概率排名前 10% 的词。这有助于确保生成的文本不会太冗长。

- frequency_penaltypresence_penalty:这些参数用于控制生成的文本中词语的重复程度。在我们的情况下,我们对 presence_penalty 设置了一个较高的值(0.5),以减少重复词语的可能性,从而使生成的列表更具多样性。

- stop:指定了在生成文本时应该停止的标记。在我们的情况下,我们要求生成的文本在到达第 6 项时停止。这确保了生成的待办事项列表包含 5 个项目,就像我们要求的那样。

结论

OpenAI的完成API是一个强大的工具,用于在各种情境下生成文本。通过正确设置参数和配置,它可以生成与任务相关的自然语言文本。

通过调整一些参数的正确值,例如频率和存在惩罚,可以定制结果以产生所期望的输出。

通过控制完成何时停止的能力,用户还可以控制生成文本的长度。这也有助于减少生成的令牌数量,从而间接降低成本。



评论