NodeJS 使用CURL时如何接收ChatGPT多行回复?

dtcbnfnu  于 2023-01-04  发布在  Node.js
关注(0)|答案(1)|浏览(427)

下面的代码可以工作。问题是当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
gwbalxhn

gwbalxhn1#

completion.data.choices[0].text使用\n响应多行
您需要使用多行curl调用来调用匹配的API。
我测试了名为Natural language to Stripe API

server.js的OpenAI示例

const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");

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

const configuration = new Configuration({
    apiKey: "*********** your 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-003",
        prompt: prompt,
        temperature: 0,
        max_tokens: 100,
        top_p: 1.0,
        frequency_penalty: 0.0,
        presence_penalty: 0.0,
        stop: ["\"\"\""],
    });

    console.log(completion.data.choices[0].text)
    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": "\"\"\"\nUtil exposes the following:\n\nutil.stripe() -> authenticates & returns the stripe module; usable as stripe.Charge.create etc\n\"\"\"\nimport util\n\"\"\"\nCreate a Stripe token using the users credit card: 5555-4444-3333-2222, expiration date 12 / 28, cvc 521\n\"\"\""}' http://localhost:8080/chat

终端响应

$ curl -X POST -H "Content-Type: application/json" -d '{"prompt": "\"\"\"\nUtil exposes the following:\n\nutil.stripe() -> authenticates & returns the stripe module; usable as stripe.Charge.create etc\n\"\"\"\nimport util\n\"\"\"\nCreate a Stripe token using the users credit card: 5555-4444-3333-2222, expiration date 12 / 28, cvc 521\n\"\"\""}' http://localhost:8080/chat
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   449  100   159  100   290     53     98  0:00:03  0:00:02  0:00:01   152

token = stripe.Token.create(
    card={
        "number": "5555-4444-3333-2222",
        "exp_month": 12,
        "exp_year": 28,
        "cvc": 521
    },
)

服务器端响应

$ node server.js
Server listening on port 8080

token = stripe.Token.create(
    card={
        "number": "5555-4444-3333-2222",
        "exp_month": 12,
        "exp_year": 28,
        "cvc": 521
    },
)

相关问题