英文:
Open AI API: Only getting array of metadata, no response
问题
这是您提供的PHP代码以及CURL请求返回的数据。如果您有关于这些代码或返回数据的特定问题,请随时提出,我将尽力提供帮助。
英文:
I am experimenting with Open AI API using a PHP curl request. The returned data from the API is only an array of metadata, no response to the prompt. I have read the open ai guidelines and searched for an answer to this problem but haven't found anything - I guess I have made a simple mistake somewhere.
Code:
$Url = "https://api.openai.com/v1/completions";
$OpenAIModel = "text-davinci-003"; //"model-id-0";
$Headers = [
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer ' . $ApiKey . ''
];
$Messages = "What is th capital of France and how many people live there?";
//$Messages = array("role" => "user", "content" => "What is th capital of France and how many people live there?");
//var_dump($Headers);
//define data
$Data = array(
'model' => $OpenAIModel,
'prompt' => $Messages,
//'messages' => $Messages,
'max_tokens' => 7,
'temperature' => 0.5,
'top_p' => 1,
'best_of' => 2,
'frequency_penalty' => 0.0,
'presence_penalty' => 0.0,
//'stop' => '["\n"]',
);
/*<note>Initiate CURL request and specifies settings. */
$Ch = curl_init();
curl_setopt($Ch, CURLOPT_URL, $Url);
curl_setopt($Ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($Ch, CURLOPT_HTTPHEADER, $Headers);
curl_setopt($Ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($Ch, CURLOPT_POST, 1);
curl_setopt($Ch, CURLOPT_POSTFIELDS, json_encode($Data));
$Response = curl_exec($Ch); /*<note>Execute curl request. */
Here is the data returned by the curl request/API.
array (size=37)
'url' => string 'https://api.openai.com/v1/completions' (length=37)
'content_type' => string 'application/json' (length=16)
'http_code' => int 200
'header_size' => int 739
'request_size' => int 207
'filetime' => int -1
'ssl_verify_result' => int 0
'redirect_count' => int 0
'total_time' => float 2.649047
'namelookup_time' => float 0.031485
'connect_time' => float 0.04659
'pretransfer_time' => float 0.141851
'size_upload' => float 198
'size_download' => float 292
'speed_download' => float 110
'speed_upload' => float 74
'download_content_length' => float 292
'upload_content_length' => float 198
'starttransfer_time' => float 0.141891
'redirect_time' => float 0
'redirect_url' => string '' (length=0)
'primary_ip' => string '104.18.7.192' (length=12)
'certinfo' =>
array (size=0)
empty
'primary_port' => int 443
'local_ip' => string '192.168.1.136' (length=13)
'local_port' => int 54910
'http_version' => int 3
'protocol' => int 2
'ssl_verifyresult' => int 0
'scheme' => string 'HTTPS' (length=5)
'appconnect_time_us' => int 141043
'connect_time_us' => int 46590
'namelookup_time_us' => int 31485
'pretransfer_time_us' => int 141851
'redirect_time_us' => int 0
'starttransfer_time_us' => int 141891
'total_time_us' => int 2649047
答案1
得分: 1
在代码中发生了一个错误。在下面(不包括在上述代码中)有一个包含错误处理功能的部分:
$Response = curl_getinfo($Ch);
这覆盖了存储在以下位置的$Response:
$Response = curl_exec($Ch);
curl_exec 返回来自API的实际数据响应(以及其他元数据)
curl_getinfo 返回有关最后一次传输的信息,但不返回数据本身。
英文:
There was a mistake in the code. Further down (not included in the above code) there was an error handling function that included:
$Response = curl_getinfo($Ch);
This overwrote the $Response that was stored in:
$Response = curl_exec($Ch);
curl_exec return the actual data response from the API (amongst other metadata)
curl_getinfo returns information about the last transfer but not the returned data itself
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论