question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Refused to set unsafe header "User-Agent"

See original GitHub issue

Getting this error when trying to run the following code: Refused to set unsafe header "User-Agent"

const configuration = new Configuration({
    apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createCompletion("code-davinci-001", {
  prompt: filePart,
  temperature: 0.1,
  max_tokens: 2000,
  top_p: 1,
  frequency_penalty: 0,
  presence_penalty: 0.5,
});

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

6reactions
AmanKishorecommented, May 21, 2022

@rickstylz01 Here’s the workaround I used:

      const requestOptions = {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer ' + String(openAIKey)
        },
        body: JSON.stringify({
          'prompt': prompt,
          'temperature': 0.1,
          'max_tokens': Math.floor(fileLength/2),
          'top_p': 1,
          'frequency_penalty': 0,
          'presence_penalty': 0.5,
          'stop': ["\"\"\""],
        })
      };
      fetch('https://api.openai.com/v1/engines/code-davinci-001/completions', requestOptions)
          .then(response => response.json())
          .then(data => {
            # Do something with data
        }).catch(err => {
          console.log("Ran out of tokens for today! Try tomorrow!");
        });
    }
1reaction
schnerdcommented, Oct 20, 2022

Please do not use this library in the browser, it is not secure – people will be able to steal your API key. See README.md:

Important note: this library is meant for server-side usage only, as using it in client-side browser code will expose your secret API key. See here for more details.

You should route your requests through a backend server that you control.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ajax request: Refused to set unsafe header
Therefore I need to post a request to their endpoint with the Referer and the User-Agent properly set. This call should return an...
Read more >
Refused to set unsafe header "User-Agent" · Issue #982
Description Every time an api call is made i get the error: Refused to set unsafe header "User-Agent" I'm using the API package...
Read more >
Refused to get unsafe header
The message describes a failed attempt to access header data from a network request. We discovered this error while working on a prototype...
Read more >
Refused to set unsafe header "User-Agent": connection.js
This toolkit predates the requirement that some headers be rejected if a script tries to set them, and most, if not all, browsers...
Read more >
Refused to set unsafe header "User-Agent" Error with Dall- ...
Trying the Dall-E api, give the Refused to set unsafe header “User-Agent” Error. Why would Open AI set this so strict, to only...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found