gpt4 book ai didi

node.js - node.js 上的 gpt-3.5-turbo post 请求问题

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

我正在尝试获取 json 格式的响应当我向 api 发送 post 请求时,我的结果只是:{“成功”:正确},我的提示对象是这样的:{"prompt": "神经性厌食症限制型"}这是我的代码:

const express = require("express");
require("dotenv").config();
const { Configuration, OpenAIApi } = require("openai");
const app = express();
app.use(express.json());
const configuration = new Configuration({
apiKey: process.env.OPEN_AI_KEY,
});
const openai = new OpenAIApi(configuration);

app.post("/try", async (req, res) => {
try {
const { prompt } = req.body;
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content: "you are a checklist provider",
},
{
role: "user",
content: `which symptom is should check about ${prompt}`,
},
],
max_tokens: 1000,
temperature: 0,
top_p: 1.0,
frequency_penalty: 0.0,
presence_penalty: 0.0,
});
return res.status(200).json({
success: true,
data: response.data.choices[0].text,
});
} catch (error) {
return res.status(400).json({
success: false,
error: error.response
? error.response.data
: "There is a problem on server bro :(",
});
}
});

const port = process.env.PORT || 3001;
app.listen(port, () => console.log("server running"));

最佳答案

这应该可以修复

const express = require("express");
require("dotenv").config();
const { Configuration, OpenAIApi } = require("openai");
const app = express();
app.use(express.json());
const configuration = new Configuration({
apiKey: process.env.OPEN_AI_KEY,
});
const openai = new OpenAIApi(configuration);

app.post("/try", async (req, res) => {
try {
const { prompt } = req.body;
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content: "you are a checklist provider",
},
{
role: "user",
content: `which symptom is should check about ${prompt}`,
},
],
max_tokens: 1000,
temperature: 0,
top_p: 1.0,
frequency_penalty: 0.0,
presence_penalty: 0.0,
});
return res.status(200).json({
success: true,
data: response.data.choices[0].content, // its should be content here
});
} catch (error) {
return res.status(400).json({
success: false,
error: error.response
? error.response.data
: "There is a problem on server bro :(",
});
}
});

const port = process.env.PORT || 3001;
app.listen(port, () => console.log("server running"));

关于node.js - node.js 上的 gpt-3.5-turbo post 请求问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76229590/

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