gpt4 book ai didi

lua - 当 key 正确时,OpenAI API 返回 "unauthorized"错误

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

我正在用 Lua 编写一个 Discord 机器人,我认为以某种方式实现 OpenAI 的 API 会很有趣,而且我一切正常,除了我不断收到 401 错误。这是我的部分代码

coroutine.wrap(function()
local s,e = pcall(function()
local Headers = {
["Authorization"] = "Bearer "..key,
["Content-Type"] = "application/json",
}

local Body = json.encode({
model = "text-davinci-002",
prompt = "Human: ".. table.concat(Args, " ") .. "\n\nAI:",
temperature = 0.9,
max_tokens = 47, --150
top_p = 1,
frequency_penalty = 0.0,
presence_penalty = 0.6,
stop = {" Human:", " AI:"}
})

res,body = coro.request("POST", link, Headers, Body, 5000)

if res == nil then
Message:reply("didnt return anything")
return
end

if res.code < 200 or res.code >= 300 then
Message:reply("Failed to send request: " .. res.reason); return --Always ends up here "Failed to send request: Unauthorized"
end

Message:reply("Request sent successfully!")
end)
end)()

“ key ”是我从 website 获得的 API key .我觉得这个错误很简单而且很愚蠢,但无论如何我都被卡住了

最佳答案

这是很好的代码,尽管我会在您验证代码之前对类型进行一些检查。

这背后的另一个原因是,某些域可能需要代理而不是直接连接。

coroutine.resume(coroutine.create(function()
local headers = {
Authorization = "Bearer " .. key,
["Content-Type"] = "application/json",
}
local body = json.encode({
model = "text-davinci-002",
prompt = "Human: " .. table.concat(Args, " ") .. "\n\nAI:",
temperature = 0.9,
max_tokens = 47, --150
top_p = 1,
frequency_penalty = 0.0,
presence_penalty = 0.6,
stop = { " Human:", " AI:" },
})
local success, http_result, http_body = pcall(coro.request, "POST", link, headers, body, 5e3)
if success ~= true then
return error(http_result, 0)
elseif type(http_result) == "table" and type(http_result.code) == "number" and http_result.code < 200 or http_result.code >= 300 then
return Message:reply("Failed to send request: " .. type(http_result.reason) == "string" and http_result.reason or "No reason provided.")
end
return Message:reply("Request sent successfully!")
end))

关于lua - 当 key 正确时,OpenAI API 返回 "unauthorized"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73872748/

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