gpt4 book ai didi

api - OpenAI ChatGPT (gpt-3.5-turbo) API 错误 : "InvalidRequestError: Unrecognized request argument supplied: messages"

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

重要

有相同问题的 friend :请引用@Rok Benko 的回答。 gpt-3.5 introductory guide刚刚更新。这是他们报告的代码,它运行良好:

import openai

openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
{"role": "user", "content": "Where was it played?"}
]
)

在提出这个问题时,文档中的代码改为报告 GPT-3 Completions 端点:

    openai.Completion.create()

问题

我目前正在尝试使用 OpenAI 的最新模型:gpt-3.5-turbo。我正在关注一个非常 basic tutorial .

我正在使用 Google Collab 笔记本工作。我必须对提示列表中的每个提示发出请求,为简单起见,它看起来像这样:

prompts = ['What are your functionalities?', 'what is the best name for an ice-cream shop?', 'who won the premier league last year?']

我为此定义了一个函数:

import openai

# Load your API key from an environment variable or secret management service
openai.api_key = 'my_API'

def get_response(prompts: list, model = "gpt-3.5-turbo"):
responses = []


restart_sequence = "\n"

for item in prompts:

response = openai.Completion.create(
model=model,
messages=[{"role": "user", "content": prompt}],
temperature=0,
max_tokens=20,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)

responses.append(response['choices'][0]['message']['content'])

return responses

但是,当我调用 responses = get_response(prompts=prompts[0:3]) 时,出现以下错误:

InvalidRequestError: Unrecognized request argument supplied: messages

有什么建议吗?

编辑:

messages 参数替换为 prompt 会导致以下错误:

InvalidRequestError: [{'role': 'user', 'content': 'What are your functionalities?'}] is valid under each of {'type': 'array', 'minItems': 1, 'items': {'oneOf': [{'type': 'integer'}, {'type': 'object', 'properties': {'buffer': {'type': 'string', 'description': 'A serialized numpy buffer'}, 'shape': {'type': 'array', 'items': {'type': 'integer'}, 'description': 'Array shape'}, 'dtype': {'type': 'string', 'description': 'Stringified dtype'}, 'token': {'type': 'string'}}}]}, 'example': '[1, 1313, 451, {"buffer": "abcdefgh", "shape": [1024], "dtype": "float16"}]'}, {'type': 'array', 'minItems': 1, 'maxItems': 2048, 'items': {'oneOf': [{'type': 'string'}, {'type': 'object', 'properties': {'buffer': {'type': 'string', 'description': 'A serialized numpy buffer'}, 'shape': {'type': 'array', 'items': {'type': 'integer'}, 'description': 'Array shape'}, 'dtype': {'type': 'string', 'description': 'Stringified dtype'}, 'token': {'type': 'string'}}}], 'default': '', 'example': 'This is a test.', 'nullable': False}} - 'prompt'

最佳答案

您使用了错误的函数来完成。

python

ChatGPT Completions endpoint:

你需要用到这个↓。

completion = openai.ChatCompletion.create()

GPT-3 Completions endpoint:

completion = openai.Completion.create()

工作示例

如果您运行 test.py,OpenAI API 将返回以下完成信息:

Hello there! How can I assist you today?

测试.py

import openai
import os

openai.api_key = os.getenv('OPENAI_API_KEY')

completion = openai.ChatCompletion.create(
model = 'gpt-3.5-turbo',
messages = [
{'role': 'user', 'content': 'Hello!'}
],
temperature = 0
)

print(completion['choices'][0]['message']['content'])

节点

ChatGPT Completions endpoint:

你需要用到这个↓。

const completion = await openai.createChatCompletion()

GPT-3 Completions endpoint:

const completion = await openai.createCompletion()

工作示例

如果您运行 test.js,OpenAI API 将返回以下完成信息:

Hello there! How can I assist you today?

test.js

const { Configuration, OpenAIApi } = require('openai');

const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});

const openai = new OpenAIApi(configuration);

async function getCompletionFromOpenAI() {
const completion = await openai.createChatCompletion({
model: 'gpt-3.5-turbo',
messages: [
{ role: 'user', content: 'Hello!' }
],
temperature: 0,
});

console.log(completion.data.choices[0].message.content);
}

getCompletionFromOpenAI();

关于api - OpenAI ChatGPT (gpt-3.5-turbo) API 错误 : "InvalidRequestError: Unrecognized request argument supplied: messages",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75617865/

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