diff --git a/src/utils/models.js b/src/utils/models.js index f3ad7c1..7c7d2c9 100644 --- a/src/utils/models.js +++ b/src/utils/models.js @@ -8,6 +8,15 @@ class BaseModel { this[k] = props[k]; } } + + setApiKey(key) { + this.apiKey = key + this.headers = { + "Content-Type": "application/json", + Accept: "application/json", + Authorization: `Bearer ${key}` + } + } } export class ChatGPT extends BaseModel { @@ -20,15 +29,6 @@ export class ChatGPT extends BaseModel { this.setApiKey(OPEN_AI_KEY) } - setApiKey(key) { - this.apiKey = key - this.headers = { - "Content-Type": "application/json", - Accept: "application/json", - Authorization: `Bearer ${key}` - } - } - 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}), { @@ -55,7 +55,7 @@ export class ChatGPT extends BaseModel { const startDate = new Date(endDate - 90 * 24 * 60 * 60); const formattedDate = time => dayjs(time).format("YYYY-MM-DD") return await 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 => { + {headers}).then(res => res.json()).then(res => { usages.total_usage = res.total_usage const names = usages.account_name.split(" ") return { @@ -79,23 +79,13 @@ export class ChatGLM extends BaseModel { avatar: "https://cdn.cunwuyun.cn/chat/chatglm.svg", name: 'ChatGLM', id: "chatglm-130b", desc: "ChatGLM-130B所基于的模型" }); - this.getToken().then(this.setApiKey) - } - - setApiKey(key) { - this.apiKey = key - this.headers = { - "Content-Type": "application/json", - Accept: "application/json", - Authorization: `Bearer ${key}` - } + this.getToken().then(e => this.setApiKey(e)) } getToken() { const encrypted = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMZXxmDh2Rs1lh3Ymud1eVBjds/9SfjczHJFpNe9+0FsUffILVMTBcTqmdPZxjC6M1Ad2EHaHMWXZuc0fIc4Lh8CAwEAAQ==" - return axios.post(ChatGLM.base + "/passApiToken/createApiToken", null, { + return axios.post(ChatGLM.base + "/passApiToken/createApiToken", {apiKey: "4e3ceff669c143dfa09e763663aa72cd", encrypted}, { headers: this.headers, - params: {apiKey:"4e3ceff669c143dfa09e763663aa72cd",encrypted} }).then(res => res.json()).then(data => data?.token || "key无效或网络波动,请重新尝试"); }