赞
踩
(1)要注册OpenAI账户(ps:注册教程自行百度或谷歌,上网工具如找不到可私聊我)。
(2)完成注册后,访问 https://platform.openai.com/account/api-keys,点右上角登录OpenAI控制台。在控制台中,您可以创建一个API密钥,以便访问OpenAI API。在"API"选项卡下,单击"Create new API key"按钮创建一个新的API密钥。记住保存您的API密钥,因为您稍后需要使用它。
在PHP中调用OpenAI API,需要使用cURL库向OpenAI API发送HTTP请求。为了简化代码,以下是一个简单的PHP函数,用于发送HTTP请求并返回OpenAI API的响应结果。
function callOpenAPI($url, $parameters, $apiKey) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($parameters)); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-Type: application/json", "Authorization: Bearer " . $apiKey )); $response = curl_exec($ch); curl_close($ch); return json_decode($response, true); }
这个函数接受三个参数,分别是API接口地址、API请求参数和您的OpenAI API密钥。该函数使用cURL库向OpenAI API发送HTTP POST请求,并将API请求参数作为JSON字符串发送。函数返回OpenAI API的响应结果,以JSON格式解码后返回。
// 调用DAVINCI API接口
$response = callOpenAPI(
"https://api.openai.com/v1/engines/davinci/completions",
array(
"prompt" => "Hello, I'm a PHP developer. Can you please tell me more about OpenAI?",
"max_tokens" => 50,
"temperature" => 0.5,
"stop" => ["\n"]
),
"YOUR_API_KEY"
);
// 打印API响应结果
print_r($response);
(1)GPT-3 API
地址:https://api.openai.com/v1/engines/davinci-codex/completions
参数:
(2)DALL-E API
地址:https://api.openai.com/v1/images/generations
参数:
(3)Codex API
地址:https://api.openai.com/v1/engines/davinci-codex/completions
参数:
(4)Translation API
地址:https://api.openai.com/v1/translations
参数:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。