最近又在捣鼓chatgpt
4月份的时候成功申请了chatgpt,后来总是被封就没有怎么用了
这两天花了80元买了https://ai-cn.co/的套餐,有50万字的使用额度,够用几个月了
言归正传,最近又登录了openai发现又可以用了,现在发现是自己用的魔法不够稳定 ,因为ip总在变,有没有办法固定ip,或者想想别的方法
后来发现有云函数这个概念
https://blog.csdn.net/dege2929512534/article/details/125655595
不需要有服务器,只需要编写云函数,可以指定区域,看来这种方案可以解决chatgpt的问题
如何实现以下为php的云函数实现
<?php use RingCentral\Psr7\Response;
/*
To enable the initializer feature (https://help.aliyun.com/document_detail/89029.html)
please implement the initializer function as below:
function initializer($context) {
echo 'initializing' . PHP_EOL;
}
*/
function handler($request, $context): Response{
/*
$body = $request->getBody()->getContents();
$queries = $request->getQueryParams();
$method = $request->getMethod();
$headers = $request->getHeaders();
$path = $request->getAttribute('path');
$requestURI = $request->getAttribute('requestURI');
$clientIP = $request->getAttribute('clientIP');
*/
// set_time_limit(0);
// ChatGPT API endpoint
// $url = 'https://api.openai.com/v1/engines/davinci-codex/completions';
$url = 'https://api.openai.com/v1/chat/completions'; //聊天接口
// $url = 'https://api.openai.com/v1/completions';
// Your API key
$api_key = 'sk-pZNz3r0LpOvSkORNov5JT3BlbkFJc'; //获取到的api key
// Request headers
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key,
);
// Request data
$data = array(
// 'model' => 'text-davinci-003',
'model' => 'gpt-3.5-turbo', //聊天模型
// 'model' => 'text-curie-001',
'temperature' => 0.8,
// 'prompt' => '如何用php使用chatgpt的聊天接口', //聊天不用
'max_tokens' => 3000,
'messages' => [
["role" => "user", "content" => "Hello!"],
["role" => "assistant","content" => "\n\n您好!有什么可以帮助您的今天?"],
["role" => "user", "content" => "写一个php递归函数"],
]
);
// Send request
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
curl_close($ch);
// Print response
return new Response(
200,
array(
'custom_header1' => 'v1',
'custom_header2' => ['v2', 'v3'],
'Content-Type' => 'text/plain;charset=utf-8',
),
$response
);
}
<?php header("Content-Type: application/json; charset=utf-8");
// 直接转发,无权限验证,注意安全问题!!!
$url = 'https://api.openai.com'. str_ireplace('/release', '', $_SERVER ['PHP_SELF']);
$data = file_get_contents('php://input');
$headers = [
'authorization: Bearer 这里要改为 API KEY',
'Content-Type: application/json; charset=utf-8',
'Content-Length:' . strlen($data),
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术
转载请注明:阿里云云函数的使用 出自老鄢博客 | 欢迎分享