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.1
Host: eolink.o.apispace.com
X-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 等语言,我就不一一列举了,有需要的小伙伴欢迎下方留言告知。看到我都会整理出来给大家的。
如何获取平台上的 APIKey
- 需要魔法才能使用的 OpenAI 官方,大家可以自行搜索如何使用魔法。
- 国内不需要魔法就能用渠道 --- APISpace
APISpace的使用步骤:
登录成功后,可以进入顶部菜单的 我的 API ,选择侧边栏的 访问控制 入口,获取到上述示例代码中的 APIKey。
如何在线测试 ChatGPT 3.5 Turbo 的 API
- 注册登录 APISpace
进入聊天机器人 3.5 - Turbo 详情页 ,点击【免费试用】按钮即可领取免费次数
2.进入测试页面
输入请求参数值,点击【发送】按钮
APISpace 简单介绍
APISpace 是 一个专注生产和提供 API 的平台,API 接口类型丰富,比如说天气类、短信类、OCR类、以及物流等等,基本满足多个行业的数据需要,有其他开发需要的小伙伴完全可以先来这里找找。
写在最后
都看到这了,不介意收藏一下啦,整理不易,有疑问欢迎下方评论留言。