gpt4 book ai didi

python - OpenAI API、ChatCompletion 和 Completion 使用相同的参数给出完全不同的答案。为什么?

转载 作者:行者123 更新时间:2023-12-02 22:47:05 28 4
gpt4 key购买 nike

我正在探索 gpt3.5-turbo 上不同提示的用法。

调查“ChatCompletion”和“Completion”之间的差异,一些引用文献说它们应该或多或少相同,例如:https://platform.openai.com/docs/guides/gpt/chat-completions-vs-completions

其他消息来源称,正如预期的那样,ChatCompletion 对于聊天机器人来说更有用,因为您拥有“角色”(系统、用户和助理),因此您可以编排一些事情,例如少数镜头示例和/或先前聊天的内存消息。而补全对于摘要或文本生成更有用。

但差别似乎更大。我找不到解释幕后发生的事情的引用资料。

即使使用具有相同参数的相同模型,以下实验也给出了完全不同的结果。

使用聊天完成功能

import os
import openai
openai.api_type = "azure"
openai.api_version = "2023-03-15-preview"
openai.api_base = ...
openai.api_key = ...

chat_response = openai.ChatCompletion.create(
engine="my_model", # gpt-35-turbo
messages = [{"role":"user","content":"Give me something intresting:\n"}],
temperature=0,
max_tokens=800,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
stop=None)

print(chat_response.choices[0]['message']['content'])

结果是关于 war 的事实:

Did you know that the shortest war in history was between Britain and Zanzibar in 1896? It lasted only 38 minutes!

完成

regular_response = openai.Completion.create(
engine="my_model", # gpt-35-turbo
prompt="Give me something intresting:\n",
temperature=0,
max_tokens=800,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
stop=None)

print(regular_response['choices'][0]['text'])

结果是一个 python 代码以及对其作用的一些解释:

    ```
import random
import string

def random_string(length):
return ''.join(random.choice(string.ascii_letters) for i in range(length))

print(random_string(10))
```
Output:
```
'JvJvJvJvJv'
```
This code generates a random string of length `length` using `string.ascii_letters` and `random.choice()`. `string.ascii_letters` is a string containing all ASCII letters (uppercase and lowercase). `random.choice()` returns a random element from a sequence. The `for` loop generates `length` number of random letters and `join()` concatenates them into a single string. The result is a random string of length `length`. This can be useful for generating random passwords or other unique identifiers.<|im_end|>

注释

  1. 我使用相同的参数(温度、top_p 等)。唯一的区别是 ChatCompletion/Completion api。
  2. 两种情况下的型号相同:gpt-35-turbo。
  3. 我保持较低的温度,以便获得更一致的结果。
  4. 其他提示也会给出完全不同的答案,例如我尝试“歌曲的定义是什么?”之类的内容

问题

  • 为什么会发生这种情况?
  • 既然使用相同的模型,相同的提示难道不应该给出相似的结果吗?
  • 是否有任何引用资料可以解释 OpenAI 的底层功能?

最佳答案

实际上,我在查看一些旧笔记本时偶然发现了答案。

这一切都在隐藏标签上,或者正如我现在发现的,聊天标记语言(ChatML):https://github.com/openai/openai-python/blob/main/chatml.md

此提示使用 Completion api 现在返回与 ChatCompletion 几乎相同的答案:

prompt = """<|im_start|>system
<|im_end|>
<|im_start|>user
Give me something intresting:
<|im_end|>
<|im_start|>assistant
"""

regular_response = openai.Completion.create(
engine="my_model", # gpt-35-turbo
prompt=prompt,
temperature=0,
max_tokens=800,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
stop=None)

print(regular_response['choices'][0]['text'])

现在的结果是关于 war 的相同事实(带有结束标签):

Did you know that the shortest war in history was between Britain and Zanzibar in 1896? The war lasted only 38 minutes, with the British emerging victorious.<|im_end|>

看来 ChatCompletion api 所做的就是在提示之间添加这些标签。

关于python - OpenAI API、ChatCompletion 和 Completion 使用相同的参数给出完全不同的答案。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76672343/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com