-2

The code below works. The problem is when ChatGPT replies only 1 line is rendered to the terminal and the rest of the text is cut off. I am not familiar with curl commands. How do I update the code so that multi-line replies are rendered?

EDIT: I tried console.log() the response inside the express post request believing that CURL was the problem. It appears CURL is not the problem and ChatGPT simply cuts off mid reply

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
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
William
  • 4,422
  • 17
  • 55
  • 108
  • I'm unfamiliar with this api, but does the `choices` array have more than one element? – Konrad Jan 01 '23 at 19:12
  • The code looks almost identical to the code from this page https://www.codingthesmartway.com/how-to-use-chatgpt-with-react/ . (apparently the only difference is the addition of `console.log(completion.data.choices[0].text) // still cuts off`) (If you aren't the author this code you should give proper attribution, otherwise it's plagiarism. Ref. https://stackoverflow.com/help/referencing – Rubén Jan 11 '23 at 16:50

1 Answers1

0

The completion.data.choices[0].text response a multiple lines with \n

You needs to call matched API with multiple lines curl call.

I tested OpenAI example , called Natural language to Stripe API enter image description here server.js

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 command

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

Response in terminal

$ 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
    },
)

Server side response

$ 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
    },
)
Bench Vue
  • 5,257
  • 2
  • 10
  • 14
  • 1
    OP is directly accessing ChatGPT's internal requests using cURL. They're not using an official API. – Cerbrus Jan 09 '23 at 12:01