GPT国内直接访问方式

人工智能 0 6482
小小草
小小草 2023年5月24日 12:33 发表

GPT国内直接访问方式

为了便于国内开发者不需要借助其他工具,直接访问GPT API,在此我们进行了接口封装,大家可以按照此接口规范直接调用,无需使用梯子。

接下来我们将介绍如何具体使用。如果你需要通过流式访问,请参考接口文档对接:GPT国内访问方式之流式访问

方式一

1、国内接口地址

http://api.zszhan.cn/ai/chat/v5

2、请求方式

post

3、请求参数

字段字段中文释义是否必须
token您申请的token必须
question对话内容必须
maxTokens对话最大token数量选填
user对话标识(用于对话状态保持,需唯一)必须

4、报文格式:json

5、请求报文示例

{
"question": "你好",
"user": "1231231231231",
"token": "sk-xxxx",
"maxTokens": "20"

}

6、响应示例

{
"code": 200,
"data": "Hello! How can I assist you today?"
}

方式二

1、国内接口地址

http://api.zszhan.cn/ai/chat/v4

2、请求方式

post

3、请求参数

字段字段中文释义是否必须
token您申请的token必须
question对话内容必须
maxTokens对话最大token数量选填
user对话标识(用于对话状态保持,需唯一)选填

4、请求示例

4.1通过jquery.ajax进行调用示例

function test(){
$.ajax({
url: 'http://localhost:8083/ai/chat/v4',
type: 'post',//get/post
dataType: 'json',//html/text/json/script/xml等
data: {
"question": "你好",
"user":"1231231231231",
"token":"12312312",
"maxTokens":"123123"
},
async: true,//是否异步
success(response) {//回调函数,成功取得数据后的处理
if(response != null){
console.log(response)
}
},
error(err) {//回调函数,发生错误
console.log(err.status)
}
})
}

4.2通过java调用

private void requestChatGPT(AiChatLog curAiChatLog) throws Exception {

String response = null;
JSONObject jsonObject = null;
Map<String, Object> map = new HashMap<>();

try {
Map<String, String> chatMap = new HashMap<String, String>();
chatMap.put("question", URLEncoder.encode("你好", "utf-8"));
chatMap.put("token", "sk-xxxxx");
chatMap.put("maxToken", "450");
chatMap.put("user", "abvbbadfad1213123");
response = HttpClientUtil.doPostV2("http://api.zszhan.cn/ai/chat/v4", chatMap);
} catch (Exception e) {
log.error("调用chatGPT接口出现异常!");
} finally {
}
}

HttpClientUtil逻辑如下

public static String doPostV2(String url, Map<String, String> param) throws Exception {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);

httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
// httpPost.setHeader("Accept", "application/json");

// 创建参数列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
// log.info("返回内容为:" + resultString);
} catch (Exception e) {
log.error(e.getMessage());
resultString = "";
throw e;
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
log.error(e.getMessage());
}
}

return resultString;
}

5、响应示例

{"code":200,"data":"你好!"}

ChatGPT  额度查询接口

1、请求地址:http://api.zszhan.cn/ai/chat/quota

2、请求方式

post

3、请求参数

字段 字段中文释义 是否必须
apiKey 您申请的token 必须

5、请求示例:
{
"apiKey": "sk-XXXX"
}

5、响应示例

{
"code": "200",
"subscription": {
"object": "billing_subscription",
"has_payment_method": true,
"canceled": false,
"canceled_at": null,
"delinquent": null,
"access_until": 1690848000,
"soft_limit": 1600000,
"hard_limit": 2000000,
"system_hard_limit": 2000000,
"soft_limit_usd": 96,
"hard_limit_usd": 120,
"system_hard_limit_usd": 120
},
"billingUsage": {
"object": "list",
...
}
}

字段说明:

code:返回的状态码,200表示成功。
subscription:订阅信息对象。
object:对象类型,这里为billing_subscription。
has_payment_method:是否有支付方式。
canceled:订阅是否已取消。
canceled_at:订阅取消时间。
delinquent:是否逾期未付款。
access_until:订阅有效期截止时间。
soft_limit:软限制,即每月可使用的API请求次数上限。
hard_limit:硬限制,即每月可使用的API请求次数上限。
system_hard_limit:系统硬限制,即每月可使用的API请求次数上限。
soft_limit_usd:软限制对应的美元价格。
hard_limit_usd:硬限制对应的美元价格。
system_hard_limit_usd:系统硬限制对应的美元价格。
plan:订阅计划信息。
title:计划名称。
id:计划ID。
account_name:账户名称。
po_number:采购订单号。
billing_email:账单邮件地址。
tax_ids:税务ID。
billing_address:账单地址。
business_address:商业地址。
billingUsage:账单使用情况对象。
object:对象类型,这里为list。
daily_costs:每日使用情况列表。
timestamp:时间戳,表示当天的日期。
line_items:使用明细列表。
name:使用项目名称。
cost:使用费用。
total_usage:总使用量。

微信交流群


如群失效可添加个人微信





点赞 0 收藏(0)    分享
相关标签: ChatGPT
问题没解决?让chatGPT帮你作答 智能助手
0 个评论
  • 消灭零评论