callOpenAI3(): void {
if (this.state.isDone) {
return; // Stop fetching data if we're done
}
const CancelToken = axios.CancelToken;
const source = CancelToken.source();
this.source = source;
const API_KEY = 'xyz';
const ENGINE = 'text-davinci-003';
axios.post(
`https://api.openai.com/v1/engines/${ENGINE}/completions`,
{
prompt: this.state.prompt,
max_tokens: 1,
// suffix:stop,
stream: true,
// model: "text-davinci-003",
n: 1,
// stop: '\n'
},
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${API_KEY}`
},
responseType: 'text',
cancelToken: source.token,
}
)
.then(response => {
console.log('response', response)
if (response.data && typeof response.data.choices === 'object') {
// Check if data is valid
const token = response.data.choices[0].text.trim();
if (token) {
const newResponse = [...this.state.response, token];
this.setState({ response: newResponse }, () => {
// Scroll to the bottom of the output div to show the latest
response
const outputDiv = document.getElementById('output');
outputDiv.scrollTop = outputDiv.scrollHeight;
// Check if the response contains the stop sequence
if (newResponse[newResponse.length - 1] ===
this.state.stopSequence) {
// Stop fetching data if the stop sequence is found
this.setState({ isDone: true });
} else {
// Continue fetching data if the stop sequence is not found
this.callOpenAI3();
}
if (response.data.choices[0].finish_reason === 'stop') {
this.setState({ isDone: true });
}
});
}
} else {
console.error('Response data is not valid');
}
})
.catch(error => {
console.error(error);
});
}
this code is not give the proper data . I want continues streaming the data from api. print the on the consol. this code is not give the proper data . I want continues streaming the data from api. print the on the consol. this code is not give the proper data . I want continues streaming the data from api. print the on the consol.