ChatGPT多个APIkey如何自主切换

发布时间 2023-09-28 08:43:06作者: 麒麟速排

chatgpt目前账户大多数为18美金,那么用户在调用api时候出现金额不足要手动更换chatgpt 当前方法为了解决多账户自主切换api使用。

创建一个名为$arr的数组,它包含几个key。我们使用foreach循环,每次将数组中的一个字符串作为密钥请求cURL。如果cURL的返回值不是null,我们将退出循环。否则,将数组的下一个字符串替换到密钥。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$arr array("key1""key2""key3");
$result = null;
  
foreach ($arr as $key) {
                $curl = curl_init();
                curl_setopt_array($curl, [
                    CURLOPT_URL => 'https://api.openai.com/v1/completions',
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_POST => true,
                    CURLOPT_HTTPHEADER => [
                        'Content-Type: application/json',
                        'Authorization: Bearer ' $key
                    ],
                    CURLOPT_POSTFIELDS => json_encode([
                        'prompt' => $msg_after,
                        'model' => 'text-davinci-003',
                        'max_tokens' => 4000,
                        'temperature' => 0.5
                    ])
                ]);
             
                // 请求结束
                $domain = curl_exec($curl);
                curl_close($curl);
             
                if ($domain != null) {
                    break;
                }
            }

9335游戏网-中文游戏门户站