Dav*_*ayn 17 python openai-api
class OpenaiClassifier():
def __init__(self, api_keys):
openai.api_key = api_keys['Openai']
def get_ratings(self, review):
prompt = f"Rate the following review as an integer from 1 to 5, where 1 is the worst and 5 is the best: \"{review}\""
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
n=1,
max_tokens=5,
temperature=0.5,
top_p=1
)
try:
rating = int(response.choices[0].text.strip())
return rating
except ValueError:
return None
Run Code Online (Sandbox Code Playgroud)
我想知道 /v1/completions 和 /v1/chat/completions 端点之间的主要区别是什么,以及如何使用这些模型进行文本分类:gpt-4、gpt-4-0314、gpt-4-32k、gpt-4 -32k-0314,gpt-3.5-turbo,gpt-3.5-turbo-0301
小智 27
/completions端点提供单个 提示的完成,并采用单个字符串作为输入,而端点提供给定对话框/chat/completions的响应,并需要与消息历史记录相对应的特定格式的输入。
如果你想使用聊天gpt模型,你需要使用/chat/completionsAPI,但你的请求必须调整。
prompt = f"Rate the following review as an integer from 1 to 5, where 1 is the worst and 5 is the best: \"{review}\""
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": prompt}
]
)
Run Code Online (Sandbox Code Playgroud)
yot*_*das 19
奥列格的答案很好而且正确,但更完整的答案是:
/v1/completions 端点适用于旧模型,例如 DaVinci。这是一个非常强大的模型,可以获取指令并产生输出。
/v1/chat/completions API 适用于较新的聊天模型(如 Oleg 提到的)。gpt-3.5-turbo 很棒,因为它可以完成 DaVinci 可以做的所有事情,但它更便宜(成本的 1/10),缺点是 - 要使其与 DaVinci 执行相同的操作,可能需要更大的输入,并且输入可能更复杂。
当您给出示例时,聊天模型表现最佳。
对于达芬奇(或基于 /v1/completions API 的其他模型),输入看起来像一条指令:“根据主题‘风’创建两到三句话的短恐怖故事。”
对于聊天模型,输入将看起来像聊天:
Two-Sentence Horror Story: He always stops crying when I pour the milk on his cereal. I just have to remember not to let him see his face on the carton.
Topic: Wind
Two-Sentence Horror Story:
Run Code Online (Sandbox Code Playgroud)
输出将是 聊天完成。例如:The wind howled through the night, shaking the windows of the house with a sinister force. As I stepped outside, I could feel it calling out to me, beckoning me to follow its chilling path.
这是OpenAI 文档中的一个真实示例(我添加了一些有关指令 API 的上下文)。
所以需要考虑的点是:
根据我的使用经验,有一些差异
/v1/chat/completions: gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, gpt-3.5-turbo, gpt-3.5-turbo-0301
/v1/completions: text-davinci-003, text-davinci-002, text-curie-001, text-babbage-001, text-ada-001
Run Code Online (Sandbox Code Playgroud)
角色选择:在 中/chat/completions,您可以选择不同的角色{用户、助理、系统},当您希望模型始终处于某个上下文中时,系统角色很有帮助,例如系统可能是:You are a customer service。
追加对话:在 中/chat/completions,你可以以对话的方式输入更长的上下文,例如,你可以去
{"role": "user", "content": "I am Peter"}
{"role": "user", "content": "Hello, Peter!"}
{"role": "user", "content": "What is my name?"}
Run Code Online (Sandbox Code Playgroud)
你可以不断地附加对话,让模型记住你之前的对话,当你想应用它来构建带有记忆的chatBot时,这是非常有用和重要的。虽然这也可以在端点中完成/completions,但并不那么明确。
总的来说,我认为从长远来看,/chat/completions终点才是出路。
这里的其他答案很有帮助,我认为这chat_completion只是一个更高级别的 api (将消息历史记录与最新的“用户”消息连接起来,将整个内容制定为 json,然后在其completion上执行一个停止标准,以防完成超出“助理”的消息并开始以“用户”身份交谈)。这是某种受控的。请参阅两者的实现示例:completionllama-cpp-pyhton
create_chat_completion()制定一个提示然后调用self()哪个调用create_completion()。
其他想法:不幸的是OpenAI 似乎推荐了 chat_completion后者completion(并且没有提供后者的所有模型),也许是因为大多数 API 用例都是“聊天”类型,但我在原始完成 API 中看到了更多潜力,因为人们可以将自己的创造性结构编造为一种json或其他东西。