ChatGPT 镜像API使用文档

发布于 2023-06-03  480 次阅读


对于第一次使用 ChatGPT API 的开发者来说,没有什么比直接跑个例子更容易上手的了。

让 GPT 给回复hi。我们会分别用7种方式请求接口,跟着教程操作,你也能马上体验 AI 的强大。

在以下代码中你只需要把content-type对于的Bearer sk-xxxxxxxxxx替换为自己的key即可使用

Python

import requests

url = "https://gha.tianli0.top/v1/chat/completions"

payload = {
    "model": "gpt-3.5-turbo",
    "messages": [
        {
            "role": "user",
            "content": "say 你好”"
        }
    ]
}
headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer sk-xxxxxxxxxx",
    "content-type": "application/json"
}

response = requests.request("POST", url, json=payload, headers=headers)

print(response.text)

js(fetch)

const options = {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Authorization: 'Bearer sk-xxxxxxxxxx',
    'content-type': 'application/json'
  },
  body: '{"model":"gpt-3.5-turbo","messages":[{"role":"user","content":"say 你好”"}]}'
};

fetch('https://gha.tianli0.top/v1/chat/completions', options)
  .then(response => response.json())
  .then(response => console.log(response))
  .catch(err => console.error(err));

js(jquery)

const settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://gha.tianli0.top/v1/chat/completions",
  "method": "POST",
  "headers": {
    "Content-Type": "application/json",
    "Authorization": "Bearer sk-xxxxxxxxxx",
    "content-type": "application/json"
  },
  "processData": false,
  "data": "{\r\n    \"model\": \"gpt-3.5-turbo\",\r\n    \"messages\": [\r\n        {\r\n            \"role\": \"user\",\r\n            \"content\": \"say 你好”\"\r\n        }\r\n    ]\r\n}"
};

$.ajax(settings).done(function (response) {
  console.log(response);
});

Kotlin

val client = OkHttpClient()

val mediaType = MediaType.parse("application/json")
val body = RequestBody.create(mediaType, "{\r\n    \"model\": \"gpt-3.5-turbo\",\r\n    \"messages\": [\r\n        {\r\n            \"role\": \"user\",\r\n            \"content\": \"say 你好”\"\r\n        }\r\n    ]\r\n}")
val request = Request.Builder()
  .url("https://gha.tianli0.top/v1/chat/completions")
  .post(body)
  .addHeader("Content-Type", "application/json")
  .addHeader("Authorization", "Bearer sk-xxxxxxxxxx")
  .addHeader("content-type", "application/json")
  .build()

val response = client.newCall(request).execute()

C

CURL *hnd = curl_easy_init();

curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "https://gha.tianli0.top/v1/chat/completions");

struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "Authorization: Bearer sk-xxxxxxxxxx");
headers = curl_slist_append(headers, "content-type: application/json");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);

curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{\r\n    \"model\": \"gpt-3.5-turbo\",\r\n    \"messages\": [\r\n        {\r\n            \"role\": \"user\",\r\n            \"content\": \"say 你好”\"\r\n        }\r\n    ]\r\n}");

CURLcode ret = curl_easy_perform(hnd);

go

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
)

func main() {

	url := "https://gha.tianli0.top/v1/chat/completions"

	payload := strings.NewReader("{\r\n    \"model\": \"gpt-3.5-turbo\",\r\n    \"messages\": [\r\n        {\r\n            \"role\": \"user\",\r\n            \"content\": \"say 你好”\"\r\n        }\r\n    ]\r\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Authorization", "Bearer sk-xxxxxxxxxx")
	req.Header.Add("content-type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}

java(okhttp)

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\r\n    \"model\": \"gpt-3.5-turbo\",\r\n    \"messages\": [\r\n        {\r\n            \"role\": \"user\",\r\n            \"content\": \"say 你好”\"\r\n        }\r\n    ]\r\n}");
Request request = new Request.Builder()
  .url("https://gha.tianli0.top/v1/chat/completions")
  .post(body)
  .addHeader("Content-Type", "application/json")
  .addHeader("Authorization", "Bearer sk-xxxxxxxxxx")
  .addHeader("content-type", "application/json")
  .build();

Response response = client.newCall(request).execute();

如果一切成功,恭喜!你完成了第一次和 AI 的对话。是时候开始实现你脑海中改变世界的那个想法了。

基本信息

  • 接口状态:开发完成
  • 接口URL:POSThttps://gha.tianli0.top/v1/chat/completions
  • Content-Type:application/json
  • 认证方式:无需认证

请求参数

Header 请求参数

参数名参数值是否必填参数类型描述说明
Content-Typeapplication/jsonString默认
AuthorizationBearer sk-xxxxxxxxxxString注意替换为自己的key

Body 请求参数

{
    "model": "gpt-3.5-turbo",
    "messages": [
        {
            "role": "user",
            "content": "say 你好”"
        }
    ]
}

Body 请求参数

参数名参数值是否必填参数类型描述说明
modelgpt-3.5-turboString
messagesArray
messages.roleuserString
messages.contentsay hiString

响应示例

{
	"id": "chatcmpl-7NDn5Oalbq8S0JEoRsOPwcVJIaYIA",
	"object": "chat.completion",
	"created": 1685768911,
	"model": "gpt-3.5-turbo-0301",
	"usage": {
		"prompt_tokens": 13,
		"completion_tokens": 2,
		"total_tokens": 15
	},
	"choices": [
		{
			"message": {
				"role": "assistant",
				"content": "Hello!"
			},
			"finish_reason": "stop",
			"index": 0
		}
	]
}

支持的接口

  • /v1/chat/completions
  • /v1/completions
  • /v1/edits
  • /v1/embeddings
  • /v1/images/generations

支持的模型

  • gpt-4
  • gpt-4-0314
  • gpt-3.5-turbo-0301
  • gpt-3.5-turbo
  • text-embedding-ada-002( 向量生成接口 )
  • text-davinci-edit-001code-davinci-edit-001( 文本编辑接口 )
  • text-davinci-003text-davinci-002text-curie-001text-babbage-001text-ada-001( 内容补全接口 )

其他文档请参考介绍 - ChatGPT中文文档 (chatgptcn.readthedocs.io)

  • alipay_img
  • wechat_img
是个憨憨
最后更新于 2023-08-06