Docker項目:ChatGPT API 平臺

自行搭建
復(fù)制下列代碼,并自行修改配置
version: '3'
services:
?web:
? ?image: sengedev/chatgpt:latest
? ?ports:
? ? ?- "5000:5000"
? ?volumes:
? ? ?- ./app:/app
? ?restart: always
? ?environment:
? ? ?API_KEY: YourOpenAIApiKey
? ? ?HOUR_LIMIT: 50
? ? ?MINUTE_LIMIT: 3
? ? ?SECOND_LIMIT: 1
? ? ?ROUTE: chatgpt
字段含義
字段含義volumes持久化配置,文件映射API_KEYAPI密鑰,配置后無需請求頭即可完成調(diào)用,不建議設(shè)置,除非你開啟了IP地址白名單HOUR_LIMIT每小時調(diào)用次數(shù)限制,如果設(shè)置為0則無限制MINUTE_LIMIT每分鐘調(diào)用次數(shù)限制,如果設(shè)置為0則無限制SECOND_LIMIT每秒調(diào)用次數(shù)限制,如果設(shè)置為0則無限制ROUTE例如你的網(wǎng)站是https://api.example.com,路由為route,則請求鏈接為https://api.example.com/route
實例介紹
使用教程
下方是一個示例API地址,請?zhí)鎿Q為您自己的IP/域名。
API鏈接: https://api.example.com/
獲取ChatGPT回答
請求方式:GET
,路由:/chatgpt
請求頭
ApiKey:ChatGPT的API密鑰,不允許為空,除非在docker-compose中聲明了你的密鑰
Model:ChatGPT API模型,默認使用text-davinci-003模型,允許為空
請求數(shù)據(jù)
prompt:向ChatGPT發(fā)送的文本,不允許為空
返回值
成功
{
? ?"code": 200,
? ?"msg": "success",
? ?"data": {
? ? ? ?"response": "ChatGPT回答的內(nèi)容"
? ?}
}
失敗
{
? ?"code": 4xx,
? ?"msg": "failed",
? ?"data": {
? ? ? ?"response": "請求失敗的原因"
? ?}
}
常見返回值
code描述200成功400請求參數(shù)錯誤(API未填寫或填寫錯誤、模型使用錯誤、缺少prompt或prompt為空)401未授權(quán)403禁止訪問404請求路徑不存在500服務(wù)器內(nèi)部錯誤429請求過于頻繁
默認調(diào)用次數(shù)限制(每個IP)
API完全開放使用,無需申請,但是需要自行申請API Key
時間段頻率天無限制小時50次分鐘3次秒1次
示例代碼
將https://api.example.com替換為你的服務(wù)器IP/域名地址
Python
import requests
url = "https://api.example.com/chatgpt"
headers = {
? ?"ApiKey": "YourApiKey",
? ?"Model": "text-davinci-003"
}
params = {
? ?"prompt": "Hello"
}
response = requests.get(url, headers=headers, params=params)
print(response.json()["data"]["response"])
Java
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
public class ChatGPTClient {
? ?public static void main(String[] args) throws IOException {
? ? ? ?OkHttpClient client = new OkHttpClient();
? ? ? ?String apiKey = "YourApiKey";
? ? ? ?String model = "text-davinci-003";
? ? ? ?String prompt = "Hello";
? ? ? ?String url = "https://api.example.com/chatgpt?prompt=" + prompt;
? ? ? ?Request request = new Request.Builder()
? ? ? ? ? ? ? ?.url(url)
? ? ? ? ? ? ? ?.addHeader("ApiKey", apiKey)
? ? ? ? ? ? ? ?.addHeader("Model", model)
? ? ? ? ? ? ? ?.get()
? ? ? ? ? ? ? ?.build();
? ? ? ?Response response = client.newCall(request).execute();
? ? ? ?System.out.println(response.body().string());
? ?}
}
JavaScript
const axios = require('axios');
const url = "https://api.example.com/chatgpt";
const apiKey = "YourApiKey";
const model = "text-davinci-003";
const prompt = "Hello";
axios.get(url, {
? ?headers: {
? ? ? ?ApiKey: apiKey,
? ? ? ?Model: model
? ?},
? ?params: {
? ? ? ?prompt: prompt
? ?}
})
.then(response => {
? ?console.log(response.data.data.response);
})
.catch(error => {
? ?console.log(error.response.data.data.response);
});
curl
curl -H "ApiKey: YourApiKey" -H "Model: text-davinci-003" -X GET "https://api.example.com/chatgpt?prompt=Hello"
GoLang
package main
import (
"fmt"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.example.com/chatgpt?prompt=Hello"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("ApiKey", "YourApiKey")
req.Header.Add("Model", "text-davinci-003")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}
PHP
<?php
$url = "https://api.example.com/chatgpt";
$apiKey = "YourApiKey";
$model = "text-davinci-003";
$prompt = "Hello";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . "?prompt=" . $prompt);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
? ?"ApiKey: " . $apiKey,
? ?"Model: " . $model
));
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response, true);
echo $response["data"]["response"];
?>