史上最全最详细的多语言调用 ChatGPT 3.5 Turbo 的 API 教程(持续更新中!!!)
ChatGPT-3.5 Turbo 模型是 ChatGPT 所使用的模型,现 OpenAI 已正式开放 ChatGPT 的 API 能力供广大开发者使用,它可以提供超高准确性、可靠性和可扩展性,让机器学习和自然语言处理的开发者以极低的成本获取精准的结果。
那么我们如何通过API 的方式调用 GPT 3.5 呢?下面给大家整理多种语言的调用方式以及接入示例代码,有需要赶紧收藏起来。
调用代码示例
Java 示例
OkHttpClient client = new OkHttpClient().newBuilder().build();MediaType mediaType = MediaType.parse("application/json");RequestBody body = RequestBody.create(mediaType, "{"system":"你是一个小助手","message":["user:我是孙悟空","assistant:你好,悟空","user:今天师傅有没有被抓走?"],"temperature":"0.9"}");Request request = new Request.Builder()
.url("https://eolink.o.apispace.com/chatgpt-turbo/create")
.method("POST",body)
.addHeader("X-APISpace-Token", 平台上的 APIKey)
.addHeader("Authorization-Type","apikey")
.addHeader("Content-Type","")
.build();Response response = client.newCall(request).execute();PHP / pecl_http 示例
<?php
$client = new http\Client;$request = new http\Client\Request;$body = new http\Message\Body;$body->append("{"system":"你是一个小助手","message":["user:我是孙悟空","assistant:你好,悟空","user:今天师傅有没有被抓走?"],"temperature":"0.9"}");$request->setRequestUrl("eolink.o.apispace.com/chatgpt-turbo/create");$request->setRequestMethod("POST");$request->setBody($body);$request->setHeaders(array(
"X-APISpace-Token" => 平台上的 APIKey,
"Authorization-Type" => "apikey",
"Content-Type" => ""));$client->enqueue($request)->send();$response = $client->getResponse();echo $response->getBody();PHP / cURL
<?php
$curl = curl_init();curl_setopt_array($curl, array(
CURLOPT_URL => "https://eolink.o.apispace.com/chatgpt-turbo/create",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{"system":"你是一个小助手","message":["user:我是孙悟空","assistant:你好,悟空","user:今天师傅有没有被抓走?"],"temperature":"0.9"}",
CURLOPT_HTTPHEADER => array(
"X-APISpace-Token: 平台上的 APIKey",
"Authorization-Type:apikey",
"Content-Type:application/json"
),));$response = curl_exec($curl);$err = curl_error($curl);curl_close($curl);if ($err) {
echo "cURL Error #:" . $err;} else {
echo $response;}Python / http.client(Python 3)
import http.client
conn = http.client.HTTPSConnection("eolink.o.apispace.com")payload = {"system":"你是一个小助手","message":["user:我是孙悟空","assistant:你好,悟空","user:今天师傅有没有被抓走?"],"temperature":"0.9"}headers = {
"X-APISpace-Token": 平台上的 APIKey,
"Authorization-Type":"apikey",
"Content-Type":""}conn.request("POST","/chatgpt-turbo/create", payload, headers)res = conn.getresponse()data = res.read()print(data.decode("utf-8"))Python / Requests
import requests
url = "https://eolink.o.apispace.com/chatgpt-turbo/create"payload = {"system":"你是一个小助手","message":["user:我是孙悟空","assistant:你好,悟空","user:今天师傅有没有被抓走?"],"temperature":"0.9"}headers = {
"X-APISpace-Token":平台上的 APIKey,
"Authorization-Type":"apikey",
"Content-Type":""}response=requests.request("POST", url, data=json.dumps(payload), headers=headers)print(response.text)HTTP
POST /chatgpt-turbo/create HTTP/1.1Host: eolink.o.apispace.comX-APISpace-Token:平台上的 APIKey
Authorization-Type:apikey
Content-Type:application/json{"system":"你是一个小助手","message":["user:我是孙悟空","assistant:你好,悟空","user:今天师傅有没有被抓走?"],"temperature":"0.9"}JavaScript / Jquery AJAX
var data = "{"system":"你是一个小助手","message":["user:我是孙悟空","assistant:你好,悟空","user:今天师傅有没有被抓走?"],"temperature":"0.9"}"$.ajax({
"url":"https://eolink.o.apispace.com/chatgpt-turbo/create",
"method": "POST",
"headers": {
"X-APISpace-Token":平台上的 APIKey,
"Authorization-Type":"apikey",
"Content-Type":""
},
"data": data,
"crossDomain": true})
.done(function(response){})
.fail(function(jqXHR){})NodeJS / Request
var request = require("request");var requestInfo={
method: "POST",
url: "https://eolink.o.apispace.com/chatgpt-turbo/create",
headers: {
"X-APISpace-Token":平台上的 APIKey,
"Authorization-Type":"apikey",
"Content-Type":""
},
body: "{"system":"你是一个小助手","message":["user:我是孙悟空","assistant:你好,悟空","user:今天师傅有没有被抓走?"],"temperature":"0.9"}"};request(requestInfo, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);});微信小程序
var data = {"system":"你是一个小助手","message":["user:我是孙悟空","assistant:你好,悟空","user:今天师傅有没有被抓走?"],"temperature":"0.9"} wx.request({
"url":"https://eolink.o.apispace.com/chatgpt-turbo/create",
"method": "POST",
"header": {
"X-APISpace-Token": 平台上的 APIKey,
"Authorization-Type":"apikey",
"Content-Type":""
},
"data": data,
"success": (response)=> {
console.log(response.data)
}})还有如 GO、Ruby(Net:Http)、Shell / Httpie 等语言,我就不一一列举了,有需要的小伙伴欢迎下方留言告知。看到我都会整理出来给大家的。
闪电发卡ChatGPT产品推荐:
ChatGPT独享账号:https://www.chatgptzh.com/post/86.html
ChatGPT Plus共享账号:https://www.chatgptzh.com/post/319.html
ChatGPT Plus独享账号(购买充值代充订阅):https://www.chatgptzh.com/post/306.html
ChatGPT APIKey购买充值(直连+转发):https://www.chatgptzh.com/post/305.html
ChatGPT Plus国内镜像逆向版:https://www.chatgptzh.com/post/312.html
ChatGPT国内版(AIChat):https://www.chatgptzh.com/post/318.html
