|
@@ -13,19 +13,12 @@ import {
|
|
|
} from "@fortaine/fetch-event-source";
|
|
} from "@fortaine/fetch-event-source";
|
|
|
import { prettyObject } from "@/app/utils/format";
|
|
import { prettyObject } from "@/app/utils/format";
|
|
|
import { getMessageTextContent } from "@/app/utils";
|
|
import { getMessageTextContent } from "@/app/utils";
|
|
|
-import { bigModelApiKey, knowledgeId, template } from "../config";
|
|
|
|
|
|
|
|
|
|
export class BigModelApi implements LLMApi {
|
|
export class BigModelApi implements LLMApi {
|
|
|
- public useApi: 'public' | 'private';
|
|
|
|
|
- public publicPath: string;
|
|
|
|
|
- public privatePath: string;
|
|
|
|
|
|
|
+ public apiPath: string;
|
|
|
|
|
|
|
|
constructor() {
|
|
constructor() {
|
|
|
- this.useApi = 'private';
|
|
|
|
|
- this.publicPath = 'https://open.bigmodel.cn/api/paas/v4/chat/completions';
|
|
|
|
|
- // 配置私有请求地址
|
|
|
|
|
- // this.privatePath = 'https://open.bigmodel.cn/api/llm-application/open/model-api/1828613766624038913/sse-invoke'; // 建科校招助手
|
|
|
|
|
- this.privatePath = 'https://open.bigmodel.cn/api/llm-application/open/model-api/1830829847853891585/sse-invoke'; // 欢迎使用建科校招助手2👏
|
|
|
|
|
|
|
+ this.apiPath = 'http://192.168.3.42:8091/bigmodel/api/model-api/1833684324797624321/sse-invoke';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async chat(options: ChatOptions) {
|
|
async chat(options: ChatOptions) {
|
|
@@ -45,33 +38,9 @@ export class BigModelApi implements LLMApi {
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 开放大模型参数
|
|
|
|
|
- const publicParams: any = {
|
|
|
|
|
- messages: userMessages,
|
|
|
|
|
- stream: true,// 流式回复
|
|
|
|
|
- model: 'glm-4-0520',// 模型
|
|
|
|
|
- temperature: 0.01,// 采样温度
|
|
|
|
|
- top_p: 0.7,// 核取样
|
|
|
|
|
- // 进阶配置
|
|
|
|
|
- tools: [
|
|
|
|
|
- {
|
|
|
|
|
- type: 'retrieval', // 工具类型为检索
|
|
|
|
|
- retrieval: {
|
|
|
|
|
- // 知识库ID
|
|
|
|
|
- knowledge_id: knowledgeId,
|
|
|
|
|
- // 知识库模板
|
|
|
|
|
- prompt_template: template.content,
|
|
|
|
|
- },
|
|
|
|
|
- },
|
|
|
|
|
- ],
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- // 私有大模型参数
|
|
|
|
|
- const privateParams: any = {
|
|
|
|
|
|
|
+ // 大模型参数
|
|
|
|
|
+ const params: any = {
|
|
|
prompt: userMessages,
|
|
prompt: userMessages,
|
|
|
- // model: 'glm-4-0520',// 模型
|
|
|
|
|
- // temperature: 0.01,// 采样温度
|
|
|
|
|
- // top_p: 0.7,// 核取样
|
|
|
|
|
// 进阶配置
|
|
// 进阶配置
|
|
|
request_id: 'jkec2024',
|
|
request_id: 'jkec2024',
|
|
|
returnType: undefined,
|
|
returnType: undefined,
|
|
@@ -84,15 +53,14 @@ export class BigModelApi implements LLMApi {
|
|
|
options.onController?.(controller);
|
|
options.onController?.(controller);
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- const chatPath = this.useApi === 'public' ? this.publicPath : this.privatePath;
|
|
|
|
|
|
|
+ const chatPath = this.apiPath;
|
|
|
const chatPayload = {
|
|
const chatPayload = {
|
|
|
method: "POST",
|
|
method: "POST",
|
|
|
- body: JSON.stringify(this.useApi === 'public' ? publicParams : privateParams),
|
|
|
|
|
|
|
+ body: JSON.stringify(params),
|
|
|
signal: controller.signal,
|
|
signal: controller.signal,
|
|
|
headers: {
|
|
headers: {
|
|
|
'Content-Type': 'application/json',
|
|
'Content-Type': 'application/json',
|
|
|
- // APIKey
|
|
|
|
|
- Authorization: bigModelApiKey
|
|
|
|
|
|
|
+ Authorization: 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImI5NTk2OWJhLTg0NTQtNDA2ZS04MzRhLTI5NTU0NzFhMjViMSJ9.BOcqxX8EBYgNcHmUbgL91RoHm96Il2CoLuzgHHoP_h5b1Bv_KzE7cGBdfgy_Y5XVleWHdjlM-FPL9-EVwE765w',
|
|
|
},
|
|
},
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -133,8 +101,7 @@ export class BigModelApi implements LLMApi {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
controller.signal.onabort = finish;
|
|
controller.signal.onabort = finish;
|
|
|
- // 记录上次的 remainText
|
|
|
|
|
- let previousRemainText = "";
|
|
|
|
|
|
|
+
|
|
|
fetchEventSource(chatPath, {
|
|
fetchEventSource(chatPath, {
|
|
|
...chatPayload,
|
|
...chatPayload,
|
|
|
async onopen(res) {
|
|
async onopen(res) {
|
|
@@ -172,41 +139,13 @@ export class BigModelApi implements LLMApi {
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
onmessage: (msg) => {
|
|
onmessage: (msg) => {
|
|
|
- const handlePublicMessage = () => {
|
|
|
|
|
- if (msg.data === "[DONE]" || finished) {
|
|
|
|
|
- return finish();
|
|
|
|
|
- }
|
|
|
|
|
- const text = msg.data;
|
|
|
|
|
- try {
|
|
|
|
|
- const json = JSON.parse(text);
|
|
|
|
|
- const choices = json.choices as Array<{ delta: { content: string } }>;
|
|
|
|
|
- const delta = choices[0]?.delta?.content;
|
|
|
|
|
- if (delta) {
|
|
|
|
|
- remainText += delta;
|
|
|
|
|
- }
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
- console.error("[Request] parse error", text, msg);
|
|
|
|
|
- }
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- const handlePrivateMessage = () => {
|
|
|
|
|
- if (msg.event === 'finish') {
|
|
|
|
|
- return finish();
|
|
|
|
|
- }
|
|
|
|
|
- // 获取当前的数据
|
|
|
|
|
- const currentData = msg.data;
|
|
|
|
|
- // 计算新增的字符
|
|
|
|
|
- const newChars = currentData.substring(previousRemainText.length);
|
|
|
|
|
- remainText += newChars;
|
|
|
|
|
- // 更新 previousRemainText
|
|
|
|
|
- previousRemainText = currentData;
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- if (this.useApi === 'public') {
|
|
|
|
|
- handlePublicMessage();
|
|
|
|
|
- } else {
|
|
|
|
|
- handlePrivateMessage();
|
|
|
|
|
|
|
+ const info = JSON.parse(msg.data);
|
|
|
|
|
+ if (info.event === 'finish') {
|
|
|
|
|
+ return finish();
|
|
|
}
|
|
}
|
|
|
|
|
+ // 获取当前的数据
|
|
|
|
|
+ const currentData = info.data;
|
|
|
|
|
+ remainText += currentData;
|
|
|
},
|
|
},
|
|
|
async onclose() {
|
|
async onclose() {
|
|
|
finish();
|
|
finish();
|