Skip to content

Commit

Permalink
feature: 接入coze国内版与国内版
Browse files Browse the repository at this point in the history
  • Loading branch information
Bistutu committed May 25, 2024
1 parent a386704 commit fb47d88
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 5 deletions.
19 changes: 19 additions & 0 deletions components/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,23 @@
</el-col>
</el-row>

<!-- Coze需显示 robot_id -->
<el-row v-show="compute.showRobotId" class="margin-bottom margin-left-2em">
<el-col :span="12" class="lightblue rounded-corner">
<el-tooltip
class="box-item"
effect="dark"
content="如何获取 Bot ID?国内版,请访问:coze.cn/docs/developer_guides/coze_api_overview"
placement="top-start"
>
<span class="popup-text popup-vertical-left">Bot ID<el-icon class="icon-margin"><ChatDotRound/></el-icon></span>
</el-tooltip>
</el-col>
<el-col :span="12">
<el-input v-model="config.robot_id[config.service]" placeholder="请输入机器人ID"/>
</el-col>
</el-row>

<!-- 本地大模型配置 -->
<el-row v-show="compute.showCustom" class="margin-bottom margin-left-2em">
<el-col :span="12" class="lightblue rounded-corner">
Expand Down Expand Up @@ -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)),
})
</script>
Expand Down
3 changes: 3 additions & 0 deletions entrypoints/service/_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -38,4 +39,6 @@ export const _service = {
[services.minimax]: minimax,
[services.jieyue]:common,
[services.graq]: common,
[services.cozecom]: coze,
[services.cozecn]: coze,
}
35 changes: 35 additions & 0 deletions entrypoints/service/coze.ts
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions entrypoints/utils/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("模型尚未配置,请前往设置页配置")
Expand Down
2 changes: 2 additions & 0 deletions entrypoints/utils/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",};
Expand Down
4 changes: 3 additions & 1 deletion entrypoints/utils/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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; // 翻译次数
Expand All @@ -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;
}
}

Expand Down
19 changes: 15 additions & 4 deletions entrypoints/utils/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const services = {
minimax: 'minimax',
jieyue: "jieyue", // 阶跃星辰
graq: 'graq',
cozecom: 'cozecom', // coze 支持机器人不支持模型
cozecn: 'cozecn',
}

export const servicesType = {
Expand All @@ -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([
Expand All @@ -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),
Expand All @@ -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<string, Array<string>>([
[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]],
Expand Down Expand Up @@ -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',
Expand Down
15 changes: 15 additions & 0 deletions entrypoints/utils/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}

0 comments on commit fb47d88

Please sign in to comment.