2023-05-15 17:05:36 +08:00
|
|
|
import {dayjs} from "element-plus";
|
2023-05-12 18:04:58 +08:00
|
|
|
import axios from "./axios";
|
2023-05-15 01:24:18 +08:00
|
|
|
import {AI_AVATAR, OPEN_AI_KEY} from "./env";
|
2023-05-12 15:53:39 +08:00
|
|
|
|
|
|
|
|
class BaseModel {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
for (const k in props) {
|
|
|
|
|
this[k] = props[k];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ChatGPT extends BaseModel {
|
2023-05-12 18:12:21 +08:00
|
|
|
static base = "https://chatwithai.pages.dev"
|
|
|
|
|
|
2023-05-12 15:53:39 +08:00
|
|
|
constructor() {
|
|
|
|
|
super({
|
2023-05-15 01:24:18 +08:00
|
|
|
avatar: AI_AVATAR, name: 'ChatGPT', id: "gpt-3.5-turbo", desc: "ChatGPT-3.5所基于的模型",
|
2023-05-12 15:53:39 +08:00
|
|
|
});
|
2023-05-15 01:24:18 +08:00
|
|
|
this.apiKey = OPEN_AI_KEY
|
2023-05-15 17:05:36 +08:00
|
|
|
this.headers = {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Accept: "application/json",
|
|
|
|
|
Authorization: `Bearer ${this.apiKey}`
|
|
|
|
|
}
|
2023-05-12 15:53:39 +08:00
|
|
|
}
|
2023-05-12 17:32:54 +08:00
|
|
|
|
2023-05-15 01:24:18 +08:00
|
|
|
setApiKey(key) {
|
|
|
|
|
this.apiKey = key
|
2023-05-15 17:05:36 +08:00
|
|
|
this.headers["Authorization"] = `Bearer ${this.apiKey}`
|
2023-05-15 01:24:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async chat(history) {
|
|
|
|
|
const messages = history.map(e => ({role: e.role, content: e.msg}))
|
|
|
|
|
return await axios.post(ChatGPT.base + "/v1/chat/completions", JSON.stringify({messages, model: this.id}), {
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: 'Bearer ' + this.apiKey, "Content-Type": "application/json", Accept: "application/json",
|
|
|
|
|
},
|
|
|
|
|
}).then(res => res.json()).then(data => data?.choices?.[0]?.message?.content || "key无效或网络波动,请重新尝试");
|
2023-05-12 17:32:54 +08:00
|
|
|
}
|
2023-05-15 01:24:18 +08:00
|
|
|
|
|
|
|
|
async chatStream(history) {
|
|
|
|
|
const messages = history.map(e => ({role: e.role, content: e.msg}))
|
|
|
|
|
return await axios.post(ChatGPT.base + "/v1/chat/completions", JSON.stringify({messages, model: this.id, stream: true}), {
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: 'Bearer ' + this.apiKey, "Content-Type": "application/json", Accept: "application/json",
|
|
|
|
|
},
|
|
|
|
|
}).then(res => res?.body?.getReader());
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 17:05:36 +08:00
|
|
|
async getAccount() {
|
|
|
|
|
const {headers} = this
|
|
|
|
|
const usages = await axios.get(ChatGPT.base + "/v1/dashboard/billing/subscription", {headers}).then(res => res.json());
|
|
|
|
|
const endDate = usages.access_until
|
|
|
|
|
if (endDate) {
|
|
|
|
|
const startDate = new Date(endDate - 90 * 24 * 60 * 60);
|
|
|
|
|
const formattedDate = time => dayjs(time).format("YYYY-MM-DD")
|
|
|
|
|
return axios.get(`${ChatGPT.base}/v1/dashboard/billing/usage?start_date=${formattedDate(startDate * 1000)}&end_date=${formattedDate(endDate * 1000)}`,
|
|
|
|
|
{headers}).then(res => res.json()).then(res => {
|
|
|
|
|
usages.total_usage = res.total_usage
|
|
|
|
|
const names = usages.account_name.split(" ")
|
|
|
|
|
return {
|
|
|
|
|
...usages, username: names.at(-1) + names[0],
|
|
|
|
|
usage: (usages.total_usage / 100)?.toFixed(2),
|
|
|
|
|
total: usages.hard_limit_usd?.toFixed(2)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else return Promise.reject("没有权限或者网络异常,请重新尝试!")
|
|
|
|
|
}
|
2023-05-12 15:53:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ChatGLM extends BaseModel {
|
2023-05-12 18:12:21 +08:00
|
|
|
static base = "https://chatglm.cn/chatglm/backend-api"
|
|
|
|
|
|
2023-05-12 15:53:39 +08:00
|
|
|
constructor() {
|
|
|
|
|
super({
|
2023-05-15 17:05:36 +08:00
|
|
|
avatar: "https://cdn.cunwuyun.cn/chat/chatglm.svg", name: 'ChatGLM', id: "chatglm-6b", desc: "ChatGLM-6B所基于的模型"
|
2023-05-12 15:53:39 +08:00
|
|
|
});
|
|
|
|
|
}
|
2023-05-12 18:12:21 +08:00
|
|
|
|
|
|
|
|
async chat(history, callback) {
|
|
|
|
|
const context = await axios.post(ChatGPT.base + "/v1/stream_context").then(res => res.json());
|
|
|
|
|
return await axios.get(ChatGPT.base + "/v1/stream", {params: context.result})
|
|
|
|
|
}
|
2023-05-12 15:53:39 +08:00
|
|
|
}
|