i faced same issue on this api with same test case …so i need your help and how can i implement that?
i use this api https://api.openai.com/v1/completions
this is my code
import React, { useState } from ‘react’;
import { Container, Col, Button, Row, Form, FormControl, Spinner } from ‘react-bootstrap’;
import { Configuration } from “openai”;
const PARAMS = {
model: “text-davinci-003”,
temperature: 0.7,
max_tokens: 256,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
}
const configuration = new Configuration({
apiKey: process.env.REACT_APP_URL
})
const App = () => {
// const [state, setState] = useState(JSON.parse(localStorage.getItem(‘question’)))
const [questionType, setQuestionType] = useState(‘general’);
const [cbResponse, setcbResponse] = useState(‘’);
const [userInput, setUserInput] = useState(‘’);
const [isLoading, setIsLoading] = useState(false);
const getInstructions = (qt, input) => {
let prompt;
switch (qt) {
case ‘general’:
prompt = input;
break;
case ‘healthCare’:
prompt = if the question healthCare is realated to -answer it :${input},else say:can not answer it.
;
break;
case ‘weather’:
prompt = if the question is weather realated to -answer it :${input},else say:can not answer it.
;
break;
case ‘sports’:
prompt = if the question is sports realated to -answer it :${input},else say:can not answer it.
;
break;
case ‘entertainment’:
prompt = if the question is entertainment realated to -answer it :${input},else say:can not answer it.
;
break;
default:
prompt = input
}
return prompt
}
const handleSendData = async (e) => {
e.preventDefault()
setIsLoading(true)
const prompt = getInstructions(questionType, userInput)
const endpoint = `https://api.openai.com/v1/completions`;
const body = { ...PARAMS, prompt };
const response = await fetch(endpoint, {
method: 'POST',
headers: {
"content-type": "application/json;charset=utf-8",
Authorization: `Bearer ${configuration.apiKey}`,
},
body: JSON.stringify(body)
})
const data = await response.json()
setcbResponse(data.choices[0].text)
// console.log("data", data.choices[0].text)
var ans = [{
question: userInput,
ans: data?.choices[0].text
}];
const state = JSON.parse(localStorage.getItem('question'))
// console.log("state", state);
if (state === null) {
localStorage.setItem('question', JSON.stringify([...ans]));
} else {
localStorage.setItem('question', JSON.stringify([...state, ...ans]));
}
setIsLoading(false)
}
return (
<Container>
<Row className="pt-4">
{['healthCare', 'general', 'weather', 'sports', 'entertainment'].map(el => {
return (
<Col key={el}>
<Button variant="primary" onClick={() => setQuestionType(el)}>{el}</Button>
</Col>
)
})}
</Row>
<h3 className='my-3'> Question Type: <b>{questionType}</b></h3>
<Form onSubmit={handleSendData}>
<FormControl type="text"
value={userInput}
onChange={e => setUserInput(e.target.value)}
/>
<Button variant='info' type="submit" className='mt-3'>Submit</Button>
</Form>
<div className="mt-4">
{
isLoading ?
<Spinner />
: cbResponse ? cbResponse : "no questions asked"
}
</div>
</Container>
)
}
export default App
New to this myself but I think a problem you’ve got there is the completions api. https://api.openai.com/v1/completions
Both the completions api and the chat api take a string array as the message inputs. If you send 5 strings to the chat api it reads it as a conversation 5 turns long and responds once to it all. If you send 5 to the completions api It seems to take each one as the beginning of 5 separate requests and returns 5 separate responses.
I think you need to be using https://api.openai.com/v1/chat/completions
400 error is a bad request. I’d start checking the JSON request is formatted properly.