阿里云云函数的使用

最近又在捣鼓chatgpt

4月份的时候成功申请了chatgpt,后来总是被封就没有怎么用了

这两天花了80元买了[https://ai-cn.co/](https://ai-cn.co/)的套餐,有50万字的使用额度,够用几个月了

言归正传,最近又登录了openai发现又可以用了,现在发现是自己用的魔法不够稳定 ,因为ip总在变,有没有办法固定ip,或者想想别的方法

后来发现有云函数这个概念

[https://blog.csdn.net/dege2929512534/article/details/125655595](https://blog.csdn.net/dege2929512534/article/details/125655595)

不需要有服务器,只需要编写云函数,可以指定区域,看来这种方案可以解决chatgpt的问题

如何实现以下为php的云函数实现

```
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
);
}

    A+
发布日期:2023年08月02日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: