改造请求headers,并优化初始请求

This commit is contained in:
aixianling
2023-05-17 09:47:05 +08:00
parent 2f9dfabdde
commit 31c75dd885
2 changed files with 22 additions and 17 deletions

View File

@@ -4,7 +4,7 @@
<el-form-item label="语言模型">
<el-row class="flexWrap">
<ai-model v-for="m in models" :model="m" small :class="{active:settings.model.id==m.id}"
@click="settings.model=m"/>
@click="settings.model=new m()"/>
</el-row>
</el-form-item>
<el-form-item label="流式输出">
@@ -50,7 +50,7 @@ export default {
}
},
computed: {
models: () => Object.values(models).map((model => new model())),
models: () => Object.values(models),
account: v => v.settings.account || {usage: 0, total: 0}
},
methods: {

View File

@@ -7,25 +7,28 @@ class BaseModel {
for (const k in props) {
this[k] = props[k];
}
this.headers = {
"Content-Type": "application/json",
Accept: "application/json",
}
}
setApiKey(key) {
this.apiKey = key
this.headers = {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${key}`
}
this.headers.Authorization = `Bearer ${key}`
}
}
export class ChatGPT extends BaseModel {
static base = "https://chatwithai.pages.dev"
static avatar = AI_AVATAR
static name = "ChatGPT"
static id = "gpt-3.5-turbo"
static desc = "ChatGPT-3.5所基于的模型"
constructor() {
super({
avatar: AI_AVATAR, name: 'ChatGPT', id: "gpt-3.5-turbo", desc: "ChatGPT-3.5所基于的模型",
});
constructor(params) {
const {avatar, name, desc, id} = ChatGPT
super({avatar, name, desc, id, ...params})
this.setApiKey(OPEN_AI_KEY)
}
@@ -73,18 +76,20 @@ export class ChatGPT extends BaseModel {
*/
export class ChatGLM extends BaseModel {
static base = "https://maas.aminer.cn/api/paas"
static avatar = "https://cdn.cunwuyun.cn/chat/chatglm.svg"
static name = "ChatGLM"
static id = "chatglm-130b"
static desc = "ChatGLM-130B所基于的模型"
constructor() {
super({
avatar: "https://cdn.cunwuyun.cn/chat/chatglm.svg",
name: 'ChatGLM', id: "chatglm-130b", desc: "ChatGLM-130B所基于的模型"
});
constructor(params) {
const {avatar, name, desc, id} = ChatGLM
super({avatar, name, desc, id, ...params})
this.getToken().then(e => this.setApiKey(e))
}
getToken() {
const encrypted = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMZXxmDh2Rs1lh3Ymud1eVBjds/9SfjczHJFpNe9+0FsUffILVMTBcTqmdPZxjC6M1Ad2EHaHMWXZuc0fIc4Lh8CAwEAAQ=="
return axios.post(ChatGLM.base + "/passApiToken/createApiToken", {apiKey: "4e3ceff669c143dfa09e763663aa72cd", encrypted}, {
return axios.post(ChatGLM.base + "/passApiToken/createApiToken", JSON.stringify({apiKey: "4e3ceff669c143dfa09e763663aa72cd", encrypted}), {
headers: this.headers,
}).then(res => res.json()).then(data => data?.token || "key无效或网络波动,请重新尝试");
}