Преглед изворни кода

增加网页搜索的功能

yangzeyu пре 11 месеци
родитељ
комит
2de974deff
4 измењених фајлова са 48 додато и 9 уклоњено
  1. 2 2
      agent/__init__.py
  2. 4 4
      agent/agent.py
  3. 11 2
      agent/config.py
  4. 31 1
      agent/glm.py

+ 2 - 2
agent/__init__.py

@@ -1,9 +1,9 @@
 from agent.glm import Glm
-from agent.config import KeyWordPrompt
+from agent.config import Prompt
 from agent.agent import Agent
 
 __all__ = [
     "Glm",
-    "KeyWordPrompt",
+    "Prompt",
     "Agent"
 ]

+ 4 - 4
agent/agent.py

@@ -1,4 +1,4 @@
-from agent import Glm, KeyWordPrompt
+from agent import Glm, Prompt
 class Agent:
     
     _instance = None
@@ -16,7 +16,7 @@ class Agent:
     def brand_key_word_judgement(self, brandname, title):
         """判断是否为关键词引流"""
         self.glm.set_modelname("glm-4-plus")
-        prompt = KeyWordPrompt.EXTRACT_INFO_FROM_TITLE + f"""
+        prompt = Prompt.EXTRACT_INFO_FROM_TITLE + f"""
             请根据上述逻辑,分析以下商品标题,并输出结果:
             商品标题:{brandname}
             给定的引流品牌:{title}"""
@@ -31,7 +31,7 @@ class Agent:
             license_list_str += f"{product}\n"
             
         self.glm.set_modelname("glm-4-plus")
-        prompt = KeyWordPrompt.LICENSE_LIST_FILTER + f"""
+        prompt = Prompt.LICENSE_LIST_FILTER + f"""
         请根据上述逻辑,分析以下商品是否为授权生产的,并输出结果:
         商品标题: {title}
         已生产的产品清单:
@@ -59,7 +59,7 @@ class Agent:
     def image_logo_judgement(self, logo_path, image_url):
         """判断图像中是否有指定品牌的logo"""
         self.glm.set_modelname("glm-4v-plus-0111")
-        prompt = KeyWordPrompt.IMAGE_LOGO_JUDGEMENT
+        prompt = Prompt.IMAGE_LOGO_JUDGEMENT
         response = self.glm.multi_epoch_image_response(logo_path, image_url, prompt)
         response = response.content
         

+ 11 - 2
agent/config.py

@@ -1,4 +1,11 @@
-class KeyWordPrompt:
+class Prompt:
+    # 判断标题中是否包含商标名称
+    JUDGE_TITLE_INCLUDE_BRAND = f"""
+        请根据给定的商品标题判断是否包含品牌信息
+    
+    """
+    
+    # 判断关键词引流
     EXTRACT_INFO_FROM_TITLE = f"""
          你是一个电商数据分析助手,负责从商品标题中提取实际售卖品牌和产品款式等信息,并根据给定的引流品牌判断是否涉嫌关键词引流。关键词引流的定义是:商品标题中包含给定的引流品牌名称,但实际销售的是另一个品牌的产品。请根据以下步骤进行分析:
 
@@ -26,6 +33,7 @@ class KeyWordPrompt:
         ```
     """
     
+    # 判断是否为授权商品
     LICENSE_LIST_FILTER = f"""
         你是一个产品过滤助手,你的任务是根据输入的商品名称和提供的商品清单,判断输入的商品名称是否为清单中的产品。如果是,输出True并返回匹配的产品信息;如果不是,输出False。
         判断思路:
@@ -39,6 +47,7 @@ class KeyWordPrompt:
         
     """
     
+    # 判断图像中是否包含LOGO,并判断是否为指定产品的logo
     IMAGE_LOGO_JUDGEMENT = f"""
         你是一个产品图像分析助手,你的任务是判断第二张图像中的产品上是否包含logo,并与第一张logo图像做对比,判断第二张图像中的logo是否与第一张的一致。
         判断思路:
@@ -63,4 +72,4 @@ class KeyWordPrompt:
     
     
 if __name__ == "__main__":
-    print(KeyWordPrompt.EXTRACT_INFO_FROM_TITLE)
+    print(Prompt.EXTRACT_INFO_FROM_TITLE)

+ 31 - 1
agent/glm.py

@@ -87,12 +87,42 @@ class Glm:
                 }
             ],
             response_format = {
-                "type": "joson_object"
+                "type": "json_object"
             }
         )
         
         return response.choices[0].message
     
+    def web_search_in_chat(self, search_prompt, content):
+        """网络搜索工具"""
+        tools = [{
+            "type": "web_search",
+            "web_search": {
+                "enable": True,
+                "search_engine": "search_pro_sogou", # 选择搜索引擎
+                "search_result": True,
+                "search_prompt": search_prompt,
+            }
+        }]
+        
+        messages = [{
+            "role": "user",
+            "content": content
+        }]
+        
+        response = self.client.chat.completions.create(
+            model=self.model_name,
+            messages=messages,
+            tools=tools,
+            response_format={
+                "type": "json_object"
+            }
+        )
+        
+        return response.choices[0].message
+        
+        
+    
     def set_modelname(self, modelname):
         self.model_name = modelname