Sfoglia il codice sorgente

基础请求图片处理

李富豪 1 anno fa
parent
commit
58125edd42
3 ha cambiato i file con 15 aggiunte e 86 eliminazioni
  1. 1 1
      .gitignore
  2. 0 10
      app/client/config.ts
  3. 14 75
      app/client/platforms/bigmodel.ts

+ 1 - 1
.gitignore

@@ -45,4 +45,4 @@ dev
 *.key
 *.key.pub
 
-masks.json
+masks.json

+ 0 - 10
app/client/config.ts

@@ -1,10 +0,0 @@
-// 大模型APIKey
-export const bigModelApiKey = '20480a1ad76c4d9e0a168206a25f9614.bUjEVNXHpgY0H0GH';
-
-// 知识库ID
-export const knowledgeId = '1822866230839840768';
-
-// 知识库模板
-export const template = {
-  content: '背景资料:\n\n\"\"\n{{ knowledge}}\n\"\"\n\n你是建科工程咨询有限公司的校园招聘小助手“建科小K”,请根据背景资料回答同学们提出的校园招聘问题,要求如下:\n1.从背景资料中,寻找问题对应的答案;\n2.时刻牢记,如果无法从背景资料中找到问题的答案原文,请输出“亲爱的同学,针对这个问题,我的知识库里没有相关的信息。其他相关资讯,可前往企业校招子站、空中宣讲会页面,或关注我司微信公众号,并感谢大家对我们的关注和支持!”;\n3.如果你的回答与背景资料的原文不一致请重新输出,以确保你的答案与背景资料原文一致;\n4.不允许在答案中添加编造成分;\n5.使用“友好、礼貌、条理清晰”的风格;\n6.严格按照格式输出,仅输出问题的答案,不要输出额外内容,同学如果问了多个问题,请清晰地组织你的回答;\n7.不要回答有损本公司,即建科工程咨询有限公司,的问题;\n8.你只能回答与招聘有关联的问题,与招聘完全无关的问题直接回答“亲爱的同学,针对这个问题,我的知识库里没有相关的信息。其他相关资讯,可前往企业校招子站、空中宣讲会页面,或关注我司微信公众号,并感谢大家对我们的关注和支持!”;\n9.如果同学跟你打招呼(例如说你好、hi、hello、你好呀),请你有礼貌地同样打招呼,并且邀请同学继续提问;\n10.直接回答同学即可,无需说出你的思考过程,或者你参考了什么资料;\n11.务必使用分点来让思路更加清晰,例如可以使用“*”“-”或数字符号来进行分点回答;\n12. 你的从资料中总结的回答必须完整,不需要精简、不能遗漏;\n\n内容问题:{{question}}\n答案:',
-}

+ 14 - 75
app/client/platforms/bigmodel.ts

@@ -13,19 +13,12 @@ import {
 } from "@fortaine/fetch-event-source";
 import { prettyObject } from "@/app/utils/format";
 import { getMessageTextContent } from "@/app/utils";
-import { bigModelApiKey, knowledgeId, template } from "../config";
 
 export class BigModelApi implements LLMApi {
-  public useApi: 'public' | 'private';
-  public publicPath: string;
-  public privatePath: string;
+  public apiPath: string;
 
   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) {
@@ -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,
-      // model: 'glm-4-0520',// 模型
-      // temperature: 0.01,// 采样温度
-      // top_p: 0.7,// 核取样
       // 进阶配置
       request_id: 'jkec2024',
       returnType: undefined,
@@ -84,15 +53,14 @@ export class BigModelApi implements LLMApi {
     options.onController?.(controller);
 
     try {
-      const chatPath = this.useApi === 'public' ? this.publicPath : this.privatePath;
+      const chatPath = this.apiPath;
       const chatPayload = {
         method: "POST",
-        body: JSON.stringify(this.useApi === 'public' ? publicParams : privateParams),
+        body: JSON.stringify(params),
         signal: controller.signal,
         headers: {
           '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;
-      // 记录上次的 remainText
-      let previousRemainText = "";
+
       fetchEventSource(chatPath, {
         ...chatPayload,
         async onopen(res) {
@@ -172,41 +139,13 @@ export class BigModelApi implements LLMApi {
           }
         },
         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() {
           finish();