jiyuhang 3 月之前
父節點
當前提交
b670f40ef9
共有 4 個文件被更改,包括 37 次插入6 次删除
  1. 2 2
      intent_description_template.py
  2. 4 3
      patch_intent_cls.py
  3. 2 1
      remote_model.py
  4. 29 0
      tem.py

+ 2 - 2
intent_description_template.py

@@ -10,8 +10,8 @@ template = {
          "水厂今天产水浊度、电导率、电导、温度是多少?",
          "查询水厂今天的外供水电导率、外供水PH、进水氨氮、进水COD、进水总氮、进水水温、进水总磷等产水水质"],
     "2":["查询当前、今天、昨天或过去某个时间段的水厂电耗、能耗等信息,如累计用电量等",
-         "查询水厂电量或能耗",
-         "水厂台设备用电量最高?什么设备用电量最高?",],
+         "查询水厂电量或能耗",
+         "水厂台设备用电量最高?什么设备用电量最高?",],
     "4":["查询当前、今天、昨天或过去某个时间段的水厂用水量和进水产水流量相关的数据,如总进水量(进水总量)、总出水量(出水总量)、总进水流量或总出水流量。",
          "查询水厂进出水量和流量",
          "今天水厂进水量是多少?",

+ 4 - 3
patch_intent_cls.py

@@ -1,3 +1,4 @@
+
 import os
 script_dir = os.path.dirname(os.path.abspath(__file__))
 import sys
@@ -33,8 +34,8 @@ class IntentRecognizer:
         database_path = os.path.join(script_dir, "intent_index.faiss")
         if not os.path.exists(database_path):
             # embeddings = self.model.encode(self.template_meta_list)['dense_vecs'].astype(np.float32)  # 选取密集向量,变为float32
-            #faiss.normalize_L2(embeddings)  # L2归一化
-            # 调用远程embedding模型,one by one 地处理
+            #faiss.normalize_L2(embeddings)  # L2归一化,本地模型需要归一化
+            # 调用远程embedding模型,one by one 地处理,远程模型通过配置参数进行归一化
             embeddings = [self.model.encode([temp], normalize=True)[0] for temp in self.template_meta_list]
             for _ in  embeddings:
                 if _ is None:
@@ -64,7 +65,7 @@ class IntentRecognizer:
         # 下面使用远程模型代替本地模型
         # 要求query_embedding是一个二维矩阵,形状为(1, 1024)
         query_embedding = np.array(self.model.encode([query], normalize=True), dtype=np.float32)
-        faiss.normalize_L2(query_embedding)
+        # faiss.normalize_L2(query_embedding)
         distances, indices = self.database_index.search(query_embedding, top_k)
         group_query = [(query, self.template_meta_list[indices[0][i]]) for i in range(top_k)]
         # 调用远程reranker模型

+ 2 - 1
remote_model.py

@@ -1,3 +1,4 @@
+# version: 2025.12.04
 import requests
 from typing import List, Tuple, Optional
 import os
@@ -74,7 +75,7 @@ class RemoteBGEModel:
         )
 
     def compute_score(self, pairs: List[Tuple[str, str]]):
-        """调用远程bge-reranker计算相关性"""
+        """调用远程bge-reranker计算相关性, 并按照原位置输出分数"""
         # 类型检查
         if not isinstance(pairs, list):
             raise TypeError("Pairs must be list",pairs)

+ 29 - 0
tem.py

@@ -0,0 +1,29 @@
+import requests
+import time
+
+def access_remote_api(url: str,headers:dict, data: dict, max_retries=1):
+    """调用bge-m3,embedding"""
+    # 类型检查
+    time.sleep(0.08)  # 方式频繁调用接口
+    for attempt in range(max_retries):
+        try:
+            response = requests.post(url=url, headers=headers, json=data)
+            if response.status_code == 200:
+                return response.json()
+        except Exception as e:
+            print('请求embedding模型失败', e)
+            time.sleep(1)
+            return None
+    return None
+
+if '__main__' == __name__:
+    headers = {"Content-Type": "application/json",
+               "JWT-TOKEN": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJJRCI6NywiVXNlcm5hbWUiOiJhZG1pbiIsIkRlcCI6IjEzNSIsImV4cCI6MTc3NjExOTExNCwiaXNzIjoiZ2luLWJsb2cifQ.0HTtzHZjyd2mHo8VCy8icYROxmntRMuQhyoZsAYRL_M"}
+    data = [{"deviceId":"1","deviceItems":"ns=3;s=2#RO_CSDD_O","deviceName":"外供水PH","project_id":1420},{"deviceId":"1","deviceItems":"ns=3;s=2#RO_CSDD_O","deviceName":"外供水PH","project_id":1420}]
+    resp = access_remote_api(url='http://120.55.44.4:8900/api/v1/jinke-cloud/device/current-data?time=1739859286292',
+                      headers=headers,
+                      data=data)
+    print(resp)
+
+
+