json not valid message showing with curl and php
I am working with php and curl and right now i am trying to use "openapi" via curl,But i am getting following error message We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON.Here is my current code, How can i use valid json ? Where i am wrong ?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n \"model\": \"text-davinci-003\",\n \"prompt\": \"I am a highly intelligent question answering bot. If you ask me a question that is rooted in truth, I will give you the answer. If you ask me a question that is nonsense, trickery, or has no clear answer, I will respond with \"Unknown\".nnQ: What is human life expectancy in the United States?nA: Human life expectancy in the United States is 78 years.nnQ: Who was president of the United States in 1955?nA: Dwight D. Eisenhower was president of the United States in 1955.nnQ: Which party did he belong to?nA: He belonged to the Republican Party.nnQ: What is the square root of banana?nA: UnknownnnQ: How does a telescope work?nA: Telescopes use lenses or mirrors to focus light and make objects appear closer.nnQ: Where were the 1992 Olympics held?nA: The 1992 Olympics were held in Barcelona, Spain.nnQ: How many squigs are in a bonk?nA: UnknownnnQ: Where is the Valley of Kings?nA:\",\n \"temperature\": 0,\n \"max_tokens\": 100,\n \"top_p\": 1,\n \"frequency_penalty\": 0.0,\n \"presence_penalty\": 0.0,\n \"stop\": [\"n\"]\n}");
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer xxxxx';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
echo "<pre>";print_R($result);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}`
curl_close($ch);
Comments
Post a Comment