基本功能已完成

This commit is contained in:
2023-05-15 01:24:18 +08:00
parent 65b4d57675
commit b99f837c72
7 changed files with 75 additions and 57 deletions

View File

@@ -1,5 +1,5 @@
import axios from "./axios";
import {AI_AVATAR} from "./env";
import {AI_AVATAR, OPEN_AI_KEY} from "./env";
class BaseModel {
constructor(props) {
@@ -14,16 +14,33 @@ export class ChatGPT extends BaseModel {
constructor() {
super({
avatar: AI_AVATAR,
name: 'ChatGPT',
id: "gpt-3.5-turbo",
desc: "ChatGPT-3.5所基于的模型"
avatar: AI_AVATAR, name: 'ChatGPT', id: "gpt-3.5-turbo", desc: "ChatGPT-3.5所基于的模型",
});
this.apiKey = OPEN_AI_KEY
}
async chat(history, callback) {
return await axios.post(ChatGPT.base + "/v1/chat/completions")
setApiKey(key) {
this.apiKey = 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}), {
headers: {
Authorization: 'Bearer ' + this.apiKey, "Content-Type": "application/json", Accept: "application/json",
},
}).then(res => res.json()).then(data => data?.choices?.[0]?.message?.content || "key无效或网络波动,请重新尝试");
}
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());
}
}
export class ChatGLM extends BaseModel {
@@ -31,10 +48,7 @@ export class ChatGLM extends BaseModel {
constructor() {
super({
avatar: AI_AVATAR,
name: 'ChatGLM',
id: "chatglm-6b",
desc: "ChatGLM-6B所基于的模型"
avatar: AI_AVATAR, name: 'ChatGLM', id: "chatglm-6b", desc: "ChatGLM-6B所基于的模型"
});
}