ERROR : Set Unsafe Header "User Agent" implement ChatGPT

I am doing a basic implementation for my first time. I found this tutorial online. I’ve integrated code with nSo errors, however when I attempt to ask a question I am being returned the following error from the Google console.

xhr.js:162 Refused to set unsafe header “User-Agent”

zone.js:2707

SERVICE.TS

import { Injectable } from ‘@angular/core’;
import { Configuration, OpenAIApi } from ‘openai’;

@Injectable({
providedIn: ‘root’
})
export class OpenaiService {
private openai: OpenAIApi;
configuration = new Configuration({
apiKey: ‘sk-MYKEYHERE’
});

constructor() {
this.openai = new OpenAIApi(this.configuration);
}
generateText(prompt: string):Promise<string | undefined>{
return this.openai.createCompletion({
model: “text-davinci-003”,
prompt: prompt,
max_tokens: 256
}).then(response => {
return response.data.choices[0].text;
}).catch(error=>{
return ‘’;
});
}
}

I think you might be trying to run this code in your browser, you can’t make API calls like that from your browser, as that would break the CORS protection, you would need to run this as a node.js application and then call it from a web page, might be worth taking a look at the examples in the OpenAI documentation here: OpenAI Platform

I have some problem. And I add configuration.baseOptions.headers after initiate the configuration. And it’s work for me!

    const configuration = new Configuration({
      apiKey: "YOURE_OPENAI_KEY",
      organization: "YOURE_OPENAI_OGRANIZATION",
    });
    
    configuration.baseOptions.headers = {
      Authorization: "Bearer " + "YOURE_OPENAI_KEY",
    };
1 Like