下面的代码可以工作。问题是当ChatGPT回复时,只有1行呈现给终端,其余文本被截断。我不熟悉curl命令。我如何更新代码,以便呈现多行回复?
编辑:我尝试console.log()快速发布请求中的响应,认为CURL是问题所在。结果显示CURL不是问题所在,ChatGPT只是在回复中间切断
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: "my-api-key",
});
const openai = new OpenAIApi(configuration);
// Set up the server
const app = express();
app.use(bodyParser.json());
app.use(cors())
// Set up the ChatGPT endpoint
app.post("/chat", async (req, res) => {
// Get the prompt from the request
const { prompt } = req.body;
// Generate a response with ChatGPT
const completion = await openai.createCompletion({
model: "text-davinci-002",
prompt: prompt,
});
console.log(completion.data.choices[0].text) // still cuts off
res.send(completion.data.choices[0].text);
});
// Start the server
const port = 8080;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
curl
curl -X POST -H "Content-Type: application/json" -d '{"prompt":"Hello, how are you doing today?"}' http://localhost:8080/chat
1条答案
按热度按时间gwbalxhn1#
completion.data.choices[0].text
使用\n
响应多行您需要使用多行curl调用来调用匹配的API。
我测试了名为
Natural language to Stripe API
server.js的OpenAI示例
curl
命令终端响应
服务器端响应