/v1/completions vs. /v1/chat/completions 终点

huangapple go评论69阅读模式
英文:

Openai /v1/completions vs. /v1/chat/completions end points

问题

Here's the translated code portion:

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

Please note that I won't answer your question about the main difference between /v1/completions and /v1/chat/completions endpoints or how to do text classification using specific models as you requested.

英文:
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

I wonder what's the main difference between /v1/completions and /v1/chat/completions endpoints, and how I can do text classification using these models: gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, gpt-3.5-turbo, gpt-3.5-turbo-0301

答案1

得分: 21

/completions端点提供了对单个提示的完成,并将单个字符串作为输入,而/chat/completions则提供了给定对话的响应,并需要按照消息历史记录的特定格式提供输入。

如果您想使用ChatGPT模型,您需要使用/chat/completions API,但您的请求必须进行调整。

prompt = f"将以下评论评分为1到5的整数,其中1最差,5最好: \"{review}\""

response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "user", "content": prompt}
  ]
)
英文:

/completions endpoint provides the completion for a single prompt and takes a single string as an input, whereas the /chat/completions provides the responses for a given dialog and requires the input in a specific format corresponding to the message history.

If you want to use chat gpt models, you need to use /chat/completions API, but your request has to be adjusted.

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}
  ]
)

答案2

得分: 15

Oleg的答案是好的和正确的,但更完整的答案是:

/v1/completions端点用于旧模型,例如DeVinci。这是一个非常强大的模型,接收指令并产生输出。

/v1/chat/completions API用于更新的聊天模型(正如Oleg提到的)。
gpt-3.5-turbo很棒,因为它可以做DeVinci可以做的一切,但更便宜(成本的1/10),不过它的缺点是为了与DeVinci表现相同,可能需要更大的输入,输入可能更复杂。

当您提供示例时,聊天模型的表现最佳。

对于DeVinci(或基于/v1/completions API的其他模型),输入将看起来像一条指令:
"从主题'风'创建两到三句短的恐怖故事。"

对于聊天模型,输入将看起来像一段聊天:

两句恐怖故事:每当我给他的麦片倒牛奶时,他总是停止哭泣。我只需要记住不要让他看到自己的照片在牛奶盒上。
    
主题:风
两句恐怖故事:

输出将是聊天完成
例如:风在夜晚呼啸,用邪恶的力量摇晃着房子的窗户。当我走出去时,我能感觉到它在呼唤我,引导我跟随它令人毛骨悚然的路径。

这是OpenAI文档的一个真实示例(我添加了一些关于指令API的上下文)。

因此,要考虑的要点是:

  • 定价(聊天模型更便宜 - GPT4除外,因为它仍处于测试阶段)

  • 输入差异(聊天模型的输入更复杂)

  • 未来支持 - 据我了解,新模型将侧重于聊天

  • 微调 - 目前只有GPT3模型(指令模型)支持微调

英文:

Oleg's answer is good and correct but the more complete answer is:

/v1/completions endpoint is for the old models such as DeVinci. It is a very powerful model that gets instruction and produces output.

The /v1/chat/completions API is for the newer - chat models (as Oleg mentioned).
gpt-3.5-turbo is great because it can do everything DeVinci can but its cheaper(1/10 the cost) the down side is that - for it to perform the same as DeVinci it might require bigger input and the input might be more complex.

The chat model performs best when you give examples.

For DeVinci (Or other models based on /v1/completions API) the input would look like an instruction:
"Creates two to three sentence short horror stories from the topic 'wind'."

For the chat models the input would look as a chat:

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:

The output would be completion of the chat.
For example: 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.

This is a real example from OpenAI documentation (I have added some context about the instruction API).

So the points to consider are:

  • Pricing (Chat models are cheaper - GPT4 aside as it is still in beta)

  • Input differences (The Chat models input is more complex)

  • Future support - to my understanding newer models will focus on chat

  • Fine-tuning - Currently only GPT3 models (instruction models) support fine-tuning

答案3

得分: 5

根据我的使用经验,有一些区别:

  1. 后端模型 不同:参考 文档
/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
  1. 角色选择:在 /chat/completions 中,您可以选择不同的角色 {用户,助手,系统},系统角色在您希望模型始终保持在某个上下文中时很有帮助,例如,系统可能是:您是客户服务

  2. 追加对话:在 /chat/completions 中,您可以以对话方式输入更长的上下文,例如,您可以这样写:

{"role": "user", "content": "我是彼得"}
{"role": "user", "content": "你好,彼得!"}
{"role": "user", "content": "我的名字是什么?"}

您可以不断追加对话,让模型记住您的早期对话,这在构建具有记忆的聊天机器人时非常有用和重要。虽然这也可以在 /completions 端点中完成,但不如在 /chat/completions 中明确。

总体而言,我认为从长远来看,/chat/completions 端点是更好的选择。

英文:

According to my use experience, there's a few differences

  1. Backend models are different: refer to the doc:
/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
  1. Role selection: in the /chat/completions, you get to select different roles {user, assistant, system}, the system role is helpful when you want the model to consistently stay in a certain context, for example, the system might be: You are a customer service.

  2. Append conversation: in the /chat/completions, you can input longer context in a conversational way, for example, you can go

{"role": "user", "content": "I am Peter"}
{"role": "user", "content": "Hello, Peter!"}
{"role": "user", "content": "What is my name?"}

You can constantly append the conversation and let the model to memorise your earlier conversations, which is very useful and important when you want to apply it to build chatBot with memories. While this can also be done in the /completions endpoint, it is not as explicit.

Overall, I think in the long run, the /chat/completions endpoint is the way to go.

答案4

得分: 3

以下是您要翻译的内容:

"The other answers here are helpful, and the way I see it is that 'chat_completion' is just a higher-level API (concatenates message history with the latest 'user' message, formulates the whole thing as a JSON, then does a 'completion' on that with a stopping criteria in case the completion goes beyond the 'assistant's message and start talking as 'user'). It is a controlled 'completion' of sorts. See for example 'llama-cpp-pyhton's implementation of the two:

'create_chat_completion()' formulates a prompt then calls 'self()' which calls 'create_completion()'.

Additional thoughts: It is unfortunate OpenAI seems to be recommending 'chat_completion' over 'completion' (and not offering all models with the latter), perhaps because most API use cases are of the 'chat' kind, but I see a lot more potential in the raw completion API, since one can concoct their own creative structure as a 'JSON' or something else."

英文:

The other answers here are helpful, and the way I see it is that chat_completion is just a higher-level api (concatenates message history with the latest "user" message, formulates the whole thing as a json, then does a completion on that with a stopping criteria in case the completion goes beyond the "assistant"'s message and start talking as "user"). It is a controlled completion of sorts. See for example llama-cpp-pyhton's implementation of the two:

create_chat_completion() formulates a prompt then calls self() which calls create_completion().

Additional thoughts: It is unfortunate OpenAI seems to be recommending chat_completion over completion (and not offering all models with the latter), perhaps because most API use cases are of the "chat" kind, but I see a lot more potential in the raw completion API, since one can concoct their own creative structure as a json or something else.

答案5

得分: 2

completions API 是遗留的。

完整来说,

> 我们的最新模型,gpt-4 和 gpt-3.5-turbo,通过聊天完成 API 端点访问。目前,只有旧的遗留模型可通过完成 API 端点获得。

https://platform.openai.com/docs/guides/gpt

英文:

completions API is legacy.

In full,

> Our latest models, gpt-4 and gpt-3.5-turbo, are accessed through the
> chat completions API endpoint. Currently, only the older legacy models
> are available via the completions API endpoint.

https://platform.openai.com/docs/guides/gpt

答案6

得分: 2

/v1/completions 适用于单一提示。如果您的应用提供以下服务,可以使用此端点:

  • 翻译
  • 文本或代码生成
  • 修订消息
  • 写电子邮件

另一方面,如果您的应用提供以下服务,可以使用 /v1/chat/completions

  • ChatGPT 风格的聊天机器人
  • 用于客户服务的虚拟助手
  • 互动调查和表单
英文:

/v1/completions is for single prompt. you can use this endpoints if your app provides those services

  • translation
  • text or code generation
  • revise a message
  • write an email

On the other hand you use /v1/chat/completions if your app provides those services

  • chatgpt style chatbots
  • virtual assistant for customer service
  • interactive surveys and forms

答案7

得分: 1

他们已经更新了他们的文档来回答这个问题。

TL;DR:
> 可以通过构建一个包含单个用户消息的请求来使聊天完成格式类似于完成格式

> 同样,可以使用完成API来模拟用户和助手之间的聊天,通过相应地格式化输入

> 这些API之间的区别主要来自每个API中可用的基础GPT模型

您应该使用哪个模型?

> 我们建议在游乐场中进行实验,以调查哪些模型为您的使用提供了最佳的性价比权衡

英文:

They've updated their docs to answer this question.

TL;DR:
> The chat completions format can be made similar to the completions format by constructing a request using a single user message

> Likewise, the completions API can be used to simulate a chat between a user and an assistant by formatting the input accordingly.

> The difference between these APIs derives mainly from the underlying GPT models that are available in each

Which model should you use?

> We recommend experimenting in the playground to investigate which models provide the best price performance trade-off for your usage

huangapple
  • 本文由 发表于 2023年5月7日 14:29:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定