先立个TODO等着准备集成new Bing

This commit is contained in:
2023-06-02 22:42:44 +08:00
parent 32592f3d6d
commit e4c9d2a294
2 changed files with 30 additions and 10 deletions

View File

@@ -1,6 +1,3 @@
export const OPEN_AI_KEY = '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'
//ai头像
export const AI_AVATAR = "https://cdn.cunwuyun.cn/chat/chatGPT.png"
//用户头像 //用户头像
export const USER_AVATAR = "https://avatars.githubusercontent.com/u/20533272?v=4" export const USER_AVATAR = "https://avatars.githubusercontent.com/u/20533272?v=4"

View File

@@ -1,7 +1,6 @@
import {dayjs} from "element-plus"; import {dayjs} from "element-plus";
import {nanoid} from "nanoid"; import {nanoid} from "nanoid";
import axios from "./axios"; import axios from "./axios";
import {AI_AVATAR, OPEN_AI_KEY} from "./env";
import {JSEncrypt} from "jsencrypt"; import {JSEncrypt} from "jsencrypt";
class BaseModel { class BaseModel {
@@ -25,7 +24,7 @@ class BaseModel {
*/ */
export class ChatGPT extends BaseModel { export class ChatGPT extends BaseModel {
static base = "https://chatwithai.pages.dev" static base = "https://chatwithai.pages.dev"
static avatar = AI_AVATAR static avatar = 'https://cdn.cunwuyun.cn/chat/chatGPT.png'
static name = "ChatGPT" static name = "ChatGPT"
static id = "gpt-3.5-turbo" static id = "gpt-3.5-turbo"
static desc = "ChatGPT-3.5所基于的模型" static desc = "ChatGPT-3.5所基于的模型"
@@ -33,7 +32,7 @@ export class ChatGPT extends BaseModel {
constructor(params) { constructor(params) {
const {avatar, name, desc, id} = ChatGPT const {avatar, name, desc, id} = ChatGPT
super({avatar, name, desc, id, ...params}) super({avatar, name, desc, id, ...params})
this.setApiKey(OPEN_AI_KEY) this.setApiKey("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")
} }
async chat(history) { async chat(history) {
@@ -47,7 +46,11 @@ export class ChatGPT extends BaseModel {
async chatStream(history) { async chatStream(history) {
const messages = history.map(e => ({role: e.role, content: e.msg})) 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}), { return await axios.post(ChatGPT.base + "/v1/chat/completions", JSON.stringify({
messages,
model: this.id,
stream: true
}), {
headers: { headers: {
Authorization: 'Bearer ' + this.apiKey, "Content-Type": "application/json", Accept: "application/json", Authorization: 'Bearer ' + this.apiKey, "Content-Type": "application/json", Accept: "application/json",
}, },
@@ -65,7 +68,10 @@ export class ChatGPT extends BaseModel {
usages.total_usage = res.total_usage usages.total_usage = res.total_usage
const names = usages.account_name.split(" ") const names = usages.account_name.split(" ")
return { return {
...usages, username: names.at(-1) + names[0], usage: (usages.total_usage / 100)?.toFixed(2), total: usages.hard_limit_usd?.toFixed(2) ...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("没有权限或者网络异常,请重新尝试!") } else return Promise.reject("没有权限或者网络异常,请重新尝试!")
@@ -128,7 +134,10 @@ export class ChatGLM extends BaseModel {
async getToken() { async getToken() {
if (this.apiKey) return await this.apiKey if (this.apiKey) return await this.apiKey
const encrypted = ChatGLM.encrypt(ChatGLM.publicKey) const encrypted = ChatGLM.encrypt(ChatGLM.publicKey)
return await axios.post(ChatGLM.base + "/passApiToken/createApiToken", JSON.stringify({apiKey: "4e3ceff669c143dfa09e763663aa72cd", encrypted}), { return await axios.post(ChatGLM.base + "/passApiToken/createApiToken", JSON.stringify({
apiKey: "4e3ceff669c143dfa09e763663aa72cd",
encrypted
}), {
headers: this.headers, headers: this.headers,
}).then(res => res.json()).then(data => data?.data); }).then(res => res.json()).then(data => data?.data);
} }
@@ -182,7 +191,10 @@ export class ChatGLM extends BaseModel {
if (usages.code == 200) { if (usages.code == 200) {
const {data} = usages const {data} = usages
return { return {
...data, username: "Kubbo", usage: data.totalSpendAmount?.toFixed(4), total: data.rechargeAmount?.toFixed(4) ...data,
username: "Kubbo",
usage: data.totalSpendAmount?.toFixed(4),
total: data.rechargeAmount?.toFixed(4)
} }
} else return Promise.reject("没有权限或者网络异常,请重新尝试!") } else return Promise.reject("没有权限或者网络异常,请重新尝试!")
} }
@@ -200,3 +212,14 @@ export class ChatGLM extends BaseModel {
}) })
} }
} }
/**
* 集成免费的New Bing搜索,TODO
*/
class NewBing extends BaseModel {
static base = "https://maas.aminer.cn/api/paas"
static avatar = "https://cdn.cunwuyun.cn/chat/chatglm.svg"
static name = "NewBing"
static id = "gpt-4"
static desc = "GPT-4所基于的互联网模型"
}