Docker项目:ChatGPT API 平台

时间 : 2023-03-08 17:06:06 来源 : 哔哩哔哩

Docker项目:ChatGPT API 平台

自行搭建

复制下列代码,并自行修改配置

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密钥,配置后无需请求头即可完成调用,不建议设置,除非你开启了IP地址白名单HOUR_LIMIT每小时调用次数限制,如果设置为0则无限制MINUTE_LIMIT每分钟调用次数限制,如果设置为0则无限制SECOND_LIMIT每秒调用次数限制,如果设置为0则无限制ROUTE例如你的网站是https://api.example.com,路由为route,则请求链接为https://api.example.com/route

实例介绍

使用教程

下方是一个示例API地址,请替换为您自己的IP/域名。

API链接: https://api.example.com/

获取ChatGPT回答

请求方式:GET,路由:/chatgpt

请求头

ApiKey:ChatGPT的API密钥,不允许为空,除非在docker-compose中声明了你的密钥

Model:ChatGPT API模型,默认使用text-davinci-003模型,允许为空

请求数据

prompt:向ChatGPT发送的文本,不允许为空

返回值

成功

{    "code": 200,    "msg": "success",    "data": {        "response": "ChatGPT回答的内容"    }}

失败

{    "code": 4xx,    "msg": "failed",    "data": {        "response": "请求失败的原因"    }}

常见返回值

code描述200成功400请求参数错误(API未填写或填写错误、模型使用错误、缺少prompt或prompt为空)401未授权403禁止访问404请求路径不存在500服务器内部错误429请求过于频繁

默认调用次数限制(每个IP)

API完全开放使用,无需申请,但是需要自行申请API Key

时间段频率天无限制小时50次分钟3次秒1次

示例代码

将https://api.example.com替换为你的服务器IP/域名地址

Python

import requestsurl = "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 mainimport ( "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"];?>

标签:

相关阅读

X 关闭

X 关闭

热门文章