如何在OpenAI gpt-3.5-turbo API中保持上下文?

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

How to maintain context with OpenAI gpt-3.5-turbo API?

问题

我认为用户参数正在执行这个任务。但它不起作用。

英文:

I thought the user parameter is doing this job. But it doesn’t work.

https://platform.openai.com/docs/api-reference/chat

如何在OpenAI gpt-3.5-turbo API中保持上下文?

答案1

得分: 7

你需要重新提供之前的回应以保持上下文。(用户参数仅用于OpenAI监控滥用情况)。请记住,这是一个完成型AI,意味着它只能接受输入并提供输出。为了保持上下文,你需要输入上下文。

另外,请注意,新模型“gpt-3.5-turbo”处理信息的方式与Davinci模型有所不同。

Davinci模型的输入方式如下:

// 导入和配置...

const response = await openai.createCompletion({
  model: "text-davinci-003",
  prompt: "Say this is a test",
  temperature: 0,
  max_tokens: 7,
});

而gpt-3.5-turbo模型的输入方式如下:

// 导入和配置...

const response = await openai.createCompletion({
  model: "gpt-3.5-turbo",
  messages: [
    {role: "user", content: "Say this is a test"},
  ],
  temperature: 0,
});

因此,它有些不同。如果你想重新提供上下文,你需要在“messages”中创建一个输入字段,类似于这样...

// 导入和配置...

const message = "<user input>"
const context = "<user previous messages if any, else empty>"

const response = await openai.createCompletion({
  model: "gpt-3.5-turbo",
  messages: [
    {role: "system", content: `${context}`},
    {role: "user", content: `${message}`},
  ],
  temperature: 0,
});

“system”角色用于上下文,这样gpt就知道首要回应用户的输入而不是系统的输入。这也可以用于提前介绍用户提示的字段。

希望这有所帮助。

英文:

You need to refeed your previous responses to maintain context. (The user param is only for OpenAI to monitor abuse). Remember it is a completion AI, meaning that it can only take input and give output. To maintain context, you need to input the context.

Also, keep in mind that the new model, gpt-3.5-turbo processes info differently than the Davinci model.

Davinci input is like this:

//import and configure...

const response = await openai.createCompletion({
  model: &quot;text-davinci-003&quot;,
  prompt: &quot;Say this is a test&quot;,
  temperature: 0,
  max_tokens: 7,
});

while the gpt-3.5-turbo model is like this:

//import and configure...

const response = await openai.createCompletion({
  model: &quot;gpt-3.5-turbo&quot;,
  messages: [
        {role: &quot;user&quot;, content: &quot;Say this is a test&quot;},
      ],
  temperature: 0,
});

So it's a little different. If you want to refeed for context, you need to make an input field in the "messages" - something like this...

//import and configure...

const message = &quot;${&lt;user input&gt;}&quot;
const context = &quot;${&lt;user previous messages if any, else empty&gt;}&quot;

const response = await openai.createCompletion({
  model: &quot;gpt-3.5-turbo&quot;,
  messages: [
        {role: &quot;system&quot;, content: `${context}`},
        {role: &quot;user&quot;, content: `${message}`},
      ],
  temperature: 0,
});

The "system" role is for the context, so gpt knows to respond to the user input primarily and not the system input. That can also be a useful field for prefacing user prompts fyi.

Hope that helps.

huangapple
  • 本文由 发表于 2023年3月12日 11:33:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710916.html
匿名

发表评论

匿名网友

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

确定