首先感谢论坛的各位大佬。用到了你们无私的项目。始皇, aurora ,newapi
升级三级后,能拿到refresh token了。有了这个东西,我就可以定时的更新accesstoken了。
前些时间薅了个坡县的oracle服务器,正好可以将大佬们的aurora newapi给部署上
newapi:
image: calciumion/new-api:latest
container_name: newapi
restart: always
networks: [nasnet]
volumes:
- ./newapi:/data
ports:
- "3000:3000"
environment:
- TZ=Asia/Shanghai
aurora:
image: ghcr.io/aurora-develop/aurora:latest
container_name: aurora
restart: always
networks: [nasnet]
ports:
- "3001:8080"
environment:
- TZ=Asia/Shanghai
- FREE_ACCOUNTS=false
newapi上添加上渠道后 大概是这样的
代理就是上面aurora的地址了 密钥的话就是咱们的accesstoken
然后有个这个accesstoken是只有10天有效的,但是我已经是尊贵的3级用户了拿到refresh token就能自动更新了。
这里的话就自己糊了一个青龙的定时任务了。
/*
cron: 0 1 1,9,17,25 * *
const $ = new Env("ONE API更新");
*/
const axios = require('axios');
const notify = require('./sendNotify');
const qlapi = require('./tol_qlapi');
const headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
};
async function updateOneApi(oneUrl, oneToken, refresh) {
let resp = await axios.post('https://token.oaifree.com/api/auth/refresh', `refresh_token=${refresh}`, { headers });
if (!resp.data.access_token) {
console.log('获取access_token失败', resp.data);
return '获取access_token失败';
}
let accessToken = resp.data.access_token
resp = await axios.get(`${oneUrl}/api/channel/search?keyword=openai`, { headers: { 'Authorization': `Bearer ${oneToken}` } });
if (!resp.data.data || !resp.data.data.length) {
console.log('获取channel失败', resp.data);
return '获取channel失败';
}
let data = resp.data.data[0];
data.key = accessToken;
resp = await axios.put(`${oneUrl}/api/channel/`, data, { headers: { 'Authorization': `Bearer ${oneToken}` } });
if (resp.data.data && resp.data.success && resp.data.data.key) {
console.log('更新channel成功, key:', resp.data.data.key);
return '更新channel成功';
}
console.log('更新channel失败', resp.data);
return '更新channel失败';
}
!(async () => {
// const oneInfos = { data: [{ value: 'https://new.baidu.pp.ua&oneToken&refresh' }] };
const oneInfos = await qlapi.getQLEnvs('ONEAPI_INFO');
if (!oneInfos || !oneInfos.data || !oneInfos.data.length) {
console.log('未获取到GPT变量 ONEAPI_INFO');
return;
}
let oneUrl = oneInfos.data[0].value.split('&')[0];
let oneToken = oneInfos.data[0].value.split('&')[1];
let refresh = oneInfos.data[0].value.split('&')[2];
let msg = await updateOneApi(oneUrl, oneToken, refresh);
await notify.sendNotify(`ONE API 同步`, msg)
})();
大概长这样 青龙环境变量里加这个ONEAPI_INFO 值的话看自己的newapi地址和token就是 'https://new.baidu.pp.ua&oneToken&refresh
tol_qlapi sendNotify这两个都不重要
然后就可以愉快的给机器人对接上啦
public boolean call(BigMsg bigMsg) {
String prompt = bigMsg.getContent().replace("@" + Account.getNick(), "").trim();
String toTxt;
if ("清空".equals(prompt)) {
messages.clear();
toTxt = "上下文已清空";
} else {
if (messages.size() >= 20) messages.clear();
if (messages.isEmpty()) {
messages.add(new JSONObject().fluentPut("role", "system").fluentPut("content", "你是一个乐于助人的助手,并且不管我的问题是什么语言你都以中文回答我的问题。"));
}
messages.add(new JSONObject().fluentPut("role", "user").fluentPut("content", prompt));
JSONObject reqObj = new JSONObject()
.fluentPut("frequency_penalty", 0)
.fluentPut("model", "gpt-3.5-turbo")//gpt-3.5-turbo
.fluentPut("presence_penalty", 0)
.fluentPut("stream", false)
.fluentPut("temperature", 1)
.fluentPut("top_p", 1)
.fluentPut("messages", messages);
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer sk-666666");
String resp = jRequest("https://new.baidu.pp.ua/v1/chat/completions", "POST", headers, null, reqObj.toJSONString());
if (resp.startsWith("响应-")) {
toTxt = resp;
} else {
if (resp.startsWith("data:")) resp = resp.replaceFirst("data:", "");
Leg.d(TAG, "resp: " + resp);
JSONObject root = JSONObject.parseObject(resp);
toTxt = root.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content");
messages.add(new JSONObject().fluentPut("role", "assistant").fluentPut("content", toTxt));
}
}
bigMsg.setToTxt(toTxt);
MMSendMsg.sendTextMsg(bigMsg);
return true;
}