ChatGPT 微调接口 PHP 示例:修改和使用 ChatGPT 模型
以下是一个简单的 ChatGPT 修改微调接口的 PHP 示例代码:
<?php
function chatGptApiCall($message) {
$data = array(
'messages' => array(
array('role' => 'system', 'content' => 'You are a helpful assistant.'),
array('role' => 'user', 'content' => $message),
),
);
$data_string = json_encode($data);
$ch = curl_init('https://api.openai.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer YOUR_API_KEY', // 替换为你的 ChatGPT API 密钥
));
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
$message = $_POST['message']; // 假设从表单中获取用户输入的消息
$response = chatGptApiCall($message);
if (isset($response['choices'][0]['message']['content'])) {
$reply = $response['choices'][0]['message']['content'];
} else {
$reply = 'Sorry, I am unable to generate a response at the moment.';
}
echo $reply;
?>
在上面的代码中,我们通过 chatGptApiCall 函数向 ChatGPT API 发送请求,并传递用户的消息。函数将返回一个包含生成的回复的关联数组。然后,我们从该数组中提取回复并将其发送回客户端。
请注意,你需要将 YOUR_API_KEY 替换为你的 ChatGPT API 密钥。此外,你还需要适当地处理错误和边界情况,这里的示例代码只是一个简单的演示。
原文地址: https://www.cveoy.top/t/topic/lWKl 著作权归作者所有。请勿转载和采集!