local dkjson = require("dkjson")
-- 配置区
local API_KEY = "你的密钥"
local ENGINE = "gpt-4o"
-- 请求访问GPT
function gpt_request(content)
-- 创建HTTP客户Duan实例
local client = httpclient.new()
-- 设置超时时间
client:set_timeout(3000,1000*60)
-- 设置请求头
client:request_header("Content-Type", "application/json")
client:request_header("Authorization", "Bearer " .. API_KEY)
-- 发起POST请求
local url = "https://api.openai.com/v1/chat/completions"
local body = {
model = ENGINE,
messages = {
{
role = "system",
content = "You are a helpful assistant."
},
{
role = "user",
content = content
}
}
}
local success = client:post(url, dkjson.encode(body))
-- 检查请求是否成功
if success then
local body = dkjson.decode(client:response())
local status_code = client:status()
-- 返回GPT的发送内容
return body.choices[1].message.content
else
return "请求失败"
end
end
-- 获取请求参数
local content = dkjson.decode(request:body()).content
-- 发送给浏览器
response:send(gpt_request(content))