Hm no that didn't fix it. I tried with axios and now I don't get the "refused to set unsafe header "User-agent", but I get an auth error in the browser console (POST https://api.openai.com/v1/completions 401)
import './App.css';
import {Configuration, OpenAIApi} from "openai"
import {useState} from "react"
import logo from './logo.svg';
import axios from 'axios';
function App() {
const [text, setText] = useState("" );
const [summarizedText, setSummarizedText] = useState("")
const [loading, setLoading] = useState(false)
const configuration = new Configuration({
apiKey: process.env.REACT_APP_OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration)
const ButtonSubmit = (event) => {
setLoading(true);
event.preventDefault();
const data = {
model: "text-davinci-003",
prompt: generatePrompt(text),
temperature: 0.6,
max_tokens: 100,
};
axios.post('https://api.openai.com/v1/completions', data, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.REACT_APP_OPENAI_API_KEY}`,
},
})
.then((res) => {
if (res.status === 200) {
setLoading(false);
setSummarizedText(res?.data?.choices[0]?.text);
}
})
.catch((err) => {
console.log(err, "An error occurred!")
});
};
function generatePrompt(text) {
return `Summarize this ${text}`
}