diff --git a/components/Main.vue b/components/Main.vue index e8fcd80..9fe43e3 100644 --- a/components/Main.vue +++ b/components/Main.vue @@ -182,6 +182,23 @@ + + + + + Bot ID + + + + + + + @@ -346,6 +363,8 @@ let compute = ref({ showAppIdKey: computed(() => servicesType.isUseAppIdKey(config.value.service)), // 11、判断是否为“双语模式”,控制一些翻译服务的显示 filteredServices: computed(() => options.services.filter((service: any) => !(service.value === 'google' && config.value.style !== 1))), + // 12、判断是否为 coze + showRobotId: computed(() => servicesType.isCoze(config.value.service)), }) diff --git a/entrypoints/service/_service.ts b/entrypoints/service/_service.ts index 7e56023..fe1bc62 100644 --- a/entrypoints/service/_service.ts +++ b/entrypoints/service/_service.ts @@ -14,6 +14,7 @@ import baidu from "@/entrypoints/service/baidu"; import minimax from "@/entrypoints/service/minimax"; import common from "@/entrypoints/service/common"; import deeplx from "@/entrypoints/service/deeplx"; +import coze from "@/entrypoints/service/coze"; export const _service = { @@ -38,4 +39,6 @@ export const _service = { [services.minimax]: minimax, [services.jieyue]:common, [services.graq]: common, + [services.cozecom]: coze, + [services.cozecn]: coze, } diff --git a/entrypoints/service/coze.ts b/entrypoints/service/coze.ts new file mode 100644 index 0000000..656093b --- /dev/null +++ b/entrypoints/service/coze.ts @@ -0,0 +1,35 @@ +import {Config} from "../utils/model"; +import {method, urls} from "../utils/constant"; +import {cozeTemplate} from "@/entrypoints/utils/template"; + +async function coze(config: Config, message: any) { + // 构建请求头 + let headers = new Headers(); + headers.append('Content-Type', 'application/json'); + headers.append('Authorization', `Bearer ${config.token[config.service]}`); + + // 判断是否使用代理 + let url: string = config.proxy[config.service] ? config.proxy[config.service] : urls[config.service]; + + // 发起 fetch 请求 + const resp = await fetch(url, { + method: method.POST, + headers: headers, + body: cozeTemplate(config, message.origin) + }); + + if (resp.ok) { + let result = await resp.json(); + if (result.code === 0 && result.msg === "success") { + console.log(result.messages[0]) + return result.messages[0].content; + } else { + throw new Error(`请求失败: ${result.msg}`); + } + } else { + console.log(resp); + throw new Error(`请求失败: ${resp.status} ${resp.statusText} body: ${await resp.text()}`); + } +} + +export default coze; diff --git a/entrypoints/utils/check.ts b/entrypoints/utils/check.ts index b2dc8de..db6994d 100644 --- a/entrypoints/utils/check.ts +++ b/entrypoints/utils/check.ts @@ -18,6 +18,7 @@ export function checkConfig(config: Config): boolean { // 3、检查模型 model 是否已经选择(如果是 AI 且模型栏为空时返回 false) if (servicesType.isAI(config.service) && + ![services.cozecn,services.cozecom].includes(config.service) && (!config.model[config.service] || (config.model[config.service] === customModelString && config.customModel[config.service] === ""))) { sendErrorMessage("模型尚未配置,请前往设置页配置") diff --git a/entrypoints/utils/constant.ts b/entrypoints/utils/constant.ts index e64f62c..f04c021 100644 --- a/entrypoints/utils/constant.ts +++ b/entrypoints/utils/constant.ts @@ -23,6 +23,8 @@ export const urls: any = { [services.jieyue]:"https://api.stepfun.com/v1/chat/completions", [services.yiyan]:{tokenUrl:"https://aip.baidubce.com/oauth/2.0/token"}, [services.graq]: "https://api.groq.com/openai/v1/chat/completions", + [services.cozecom]: "https://api.coze.com/open_api/v2/chat", + [services.cozecn]: "https://api.coze.cn/open_api/v2/chat", } export const method = {POST: "POST", GET: "GET",}; diff --git a/entrypoints/utils/model.ts b/entrypoints/utils/model.ts index 6106fe7..b6be90e 100644 --- a/entrypoints/utils/model.ts +++ b/entrypoints/utils/model.ts @@ -27,6 +27,7 @@ export class Config { proxy: IMapping; // 代理地址 custom: string; // 本地服务地址 extra: IExtra; // 额外信息(内包信息) + robot_id: IMapping; // 机器人 ID(兼容 coze) system_role: IMapping; user_role: IMapping; count: number; // 翻译次数 @@ -49,9 +50,10 @@ export class Config { this.proxy = {}; this.custom = defaultOption.custom; this.extra = {}; - this.count = 0; + this.robot_id = {}; this.system_role = systemRoleFactory(); this.user_role = userRoleFactory(); + this.count = 0; } } diff --git a/entrypoints/utils/option.ts b/entrypoints/utils/option.ts index ad424dc..c2edde3 100644 --- a/entrypoints/utils/option.ts +++ b/entrypoints/utils/option.ts @@ -21,6 +21,8 @@ export const services = { minimax: 'minimax', jieyue: "jieyue", // 阶跃星辰 graq: 'graq', + cozecom: 'cozecom', // coze 支持机器人不支持模型 + cozecn: 'cozecn', } export const servicesType = { @@ -31,13 +33,13 @@ export const servicesType = { AI: new Set([ services.openai, services.gemini, services.yiyan, services.tongyi, services.zhipu, services.moonshot, services.claude, services.custom, services.infini, services.baichuan, services.deepseek, services.lingyi, - services.minimax, services.jieyue, services.graq + services.minimax, services.jieyue, services.graq, services.cozecom, services.cozecn ]), // 需要 token useToken: new Set([ services.openai, services.gemini, services.tongyi, services.zhipu, services.moonshot, services.claude, services.deepL, services.xiaoniu, services.infini, services.baichuan, services.deepseek, services.lingyi, - services.minimax, services.jieyue, services.graq, services.custom, + services.minimax, services.jieyue, services.graq, services.custom, , services.cozecom, services.cozecn ]), // 需要 model useModel: new Set([ @@ -49,7 +51,7 @@ export const servicesType = { useProxy: new Set([ services.openai, services.gemini, services.claude, services.google, services.deepL, services.moonshot, services.tongyi, services.xiaoniu, services.baichuan, services.deepseek, services.lingyi, services.deepLx, - services.jieyue, services.graq + services.jieyue, services.graq, services.cozecom, services.cozecn ]), isMachine: (service: string) => servicesType.machine.has(service), @@ -60,13 +62,14 @@ export const servicesType = { isCustom: (service: string) => service === services.custom, isUseAkSk: (service: string) => service === services.yiyan, isUseAppIdKey: (service: string) => service === services.baidu, + isCoze: (service: string) => service === services.cozecom || service === services.cozecn, } export const customModelString = "自定义模型" export const models = new Map>([ [services.openai, ["gpt-3.5-turbo", "gpt-4o", "gpt-4", "gpt-4-turbo", customModelString]], [services.gemini, ["gemini-pro", "gemini-1.5-pro", "gemini-1.5-flash", customModelString]], - [services.yiyan, ["ERNIE-Bot 4.0", "ERNIE-Bot", "ERNIE-Speed-8K","ERNIE-Speed-128K"]], // 因文心一言模式不同,暂不支持自定义模型(还需根据model获取最终的url请求参数) + [services.yiyan, ["ERNIE-Bot 4.0", "ERNIE-Bot", "ERNIE-Speed-8K", "ERNIE-Speed-128K"]], // 因文心一言模式不同,暂不支持自定义模型(还需根据model获取最终的url请求参数) [services.tongyi, ["qwen-turbo", "qwen-plus", "qwen-max", "qwen-max-longcontext", customModelString]], [services.zhipu, ["glm-4", "glm-4v", "glm-3-turbo", customModelString]], [services.moonshot, ["moonshot-v1-8k", customModelString]], @@ -218,6 +221,14 @@ export const options = { label: 'OpenAI', model: "gpt-3.5-turbo", }, + { + value: services.cozecom, + label: 'Coze国际版', + }, + { + value: services.cozecn, + label: 'Coze国内版', + }, { value: services.moonshot, label: 'Kimi', diff --git a/entrypoints/utils/template.ts b/entrypoints/utils/template.ts index cc016d1..c317e47 100644 --- a/entrypoints/utils/template.ts +++ b/entrypoints/utils/template.ts @@ -105,3 +105,18 @@ export function minimaxTemplate(config: Config, origin: string) { ] }) } + +export function cozeTemplate(config: Config, origin: string) { + + let system = config.system_role[config.service] || defaultOption.system_role; + let user = (config.user_role[config.service] || defaultOption.user_role) + .replace('{{to}}', config.to).replace('{{origin}}', origin); + + console.log(system + user) + return JSON.stringify({ + bot_id: config.robot_id[config.service], + user: "FluentRead", + query: system + user, + stream: false + }); +}