0

I have a site that is returning a server error 500 when requesting data from OpenAi api. I'm using the text-curie-001. I'm a newbie with this type of thing so I apologise in advance if I've got something totally wrong.

You can see in the console how the error shows: https://original-story-maker.vercel.app/

I'm using the free version of OpenAi if that makes any difference.

Here is the code that requests from api.

import { Configuration, OpenAIApi } from "openai";

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

export default async function (req, res) {
  const completion = await openai.createCompletion("text-curie-001", {
    // prompt: generatePrompt(req.body.animal),
    prompt: "Write a story about " + req.body.employee + " wearing a " + req.body.wearing,
    temperature: 0.9,
    max_tokens: 720,
    top_p: 1.0,
    frequency_penalty: 0.0,
    presence_penalty: 0.0
  });
  res.status(200).json({ result: completion.data.choices[0].text });
}

Vercel error log

    : [WritableState],
      _events: [Object: null prototype],
      _eventsCount: 3,
      _maxListeners: undefined,
      _options: [Object],
      _ended: true,
      _ending: true,
      _redirectCount: 0,
      _redirects: [],
      _requestBodyLength: 142,
      _requestBodyBuffers: [],
      _onNativeResponse: [Function (anonymous)],
      _currentRequest: [Circular *1],
      _currentUrl: 'https://api.openai.com/v1/engines/text-curie-001/completions',
      [Symbol(kCapture)]: false
    },
    [Symbol(kCapture)]: false,
    [Symbol(kNeedDrain)]: false,
    [Symbol(corked)]: 0,
    [Symbol(kOutHeaders)]: [Object: null prototype] {
      accept: [Array],
      'content-type': [Array],
      'user-agent': [Array],
      authorization: [Array],
      'content-length': [Array],
      host: [Array]
    }
  },
  response: {
    status: 401,
    statusText: 'Unauthorized',
    headers: {
      date: 'Tue, 21 Jun 2022 10:33:02 GMT',
      'content-type': 'application/json; charset=utf-8',
      'content-length': '280',
      connection: 'close',
      vary: 'Origin',
      'x-request-id': '5534283309335fe6e2bf1c0ed0c68c1d',
      'strict-transport-security': 'max-age=15724800; includeSubDomains'
    },
    config: {
      transitional: [Object],
      adapter: [Function: httpAdapter],
      transformRequest: [Array],
      transformResponse: [Array],
      timeout: 0,
      xsrfCookieName: 'XSRF-TOKEN',
      xsrfHeaderName: 'X-XSRF-TOKEN',
      maxContentLength: -1,
      maxBodyLength: -1,
      validateStatus: [Function: validateStatus],
      headers: [Object],
      method: 'post',
      data: '{"prompt":"Write a story about jack wearing a wallet","temperature":0.9,"max_tokens":720,"top_p":1,"frequency_penalty":0,"presence_penalty":0}',
      url: 'https://api.openai.com/v1/engines/text-curie-001/completions'
    },
    request: <ref *1> ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 7,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      destroyed: false,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: false,
      maxRequestsOnConnectionReached: false,
      _defaultKeepAlive: true,
      useChunkedEncodingByDefault: true,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      _contentLength: null,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      _closed: false,
      socket: [TLSSocket],
      _header: 'POST /v1/engines/text-curie-001/completions HTTP/1.1\r\n' +
        'Accept: application/json, text/plain, */*\r\n' +
        'Content-Type: application/json\r\n' +
        'User-Agent: OpenAI/NodeJS/2.0.0\r\n' +
        'Authorization: Bearer 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\r\n' +
        'Content-Length: 142\r\n' +
        'Host: api.openai.com\r\n' +
        'Connection: close\r\n' +
        '\r\n',
      _keepAliveTimeout: 0,
      _onPendingData: [Function: nop],
      agent: [Agent],
      socketPath: undefined,
      method: 'POST',
      maxHeaderSize: undefined,
      insecureHTTPParser: undefined,
      path: '/v1/engines/text-curie-001/completions',
      _ended: true,
      res: [IncomingMessage],
      aborted: false,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      reusedSocket: false,
      host: 'api.openai.com',
      protocol: 'https:',
      _redirectable: [Writable],
      [Symbol(kCapture)]: false,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype]
    },
    data: { error: [Object] }
  },
  isAxiosError: true,
  toJSON: [Function: toJSON]
}
END RequestId: 427a64bf-859c-4f86-b3ca-33e5017aa24d
REPORT RequestId: 427a64bf-859c-4f86-b3ca-33e5017aa24d  Duration: 697.32 ms Billed Duration: 698 ms Memory Size: 1024 MB    Max Memory Used: 45 MB  
RequestId: 427a64bf-859c-4f86-b3ca-33e5017aa24d Error: Runtime exited with error: exit status 1
Runtime.ExitError

Here are screenshots from dev tools. enter image description here

enter image description here

Sambuxc
  • 425
  • 2
  • 11
  • 26
  • Please include the server-side code which handles the requests to /api/generate since that is where the error comes from. – Molda Jun 10 '22 at 12:23
  • Good idea I have added it no @Molda . You got me thinking, that the API key is here as a variable however the file that holds my API key is not on vercel. In vercel I have added an environment variable. So logical tells me I need to link to vercel environment variable. I now search for that! – Sambuxc Jun 10 '22 at 13:35
  • 1
    You should also place the `... await openai.creat....` into a try/catch block and handle any error caused by it properly. – Molda Jun 11 '22 at 15:17
  • What error is being thrown by the API route in Vercel? You can check that in [Vercel Functions Logs](https://vercel.com/docs/concepts/deployments/logs#function-logs). – juliomalves Jun 13 '22 at 18:01
  • You're getting a 401 Unauthorized response from the OpenAI request, which indicates the API key you're using might not be correct. Are you correctly setting the `OPENAI_API_KEY` environment variable in Vercel? If you log its value in the API route, do you get the expected output? – juliomalves Jun 25 '22 at 11:37

0 Answers0