agent.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from agent import Glm, KeyWordPrompt
  2. class Agent:
  3. _instance = None
  4. def __new__(cls):
  5. if not cls._instance:
  6. cls._instance = super(Agent, cls).__new__(cls)
  7. cls._instance._initialized = False
  8. return cls._instance
  9. def __init__(self):
  10. if not self._initialized:
  11. self.glm = Glm()
  12. self._initialized = True
  13. def brand_key_word_judgement(self, brandname, title):
  14. """判断是否为关键词引流"""
  15. self.glm.set_modelname("glm-4-plus")
  16. prompt = KeyWordPrompt.EXTRACT_INFO_FROM_TITLE + f"""
  17. 请根据上述逻辑,分析以下商品标题,并输出结果:
  18. 商品标题:{brandname}
  19. 给定的引流品牌:{title}"""
  20. response = self.glm.text_response(prompt)
  21. return response.content
  22. def license_product_judgement(self, title, license_list_str):
  23. """判断是否为未授权商品"""
  24. self.glm.set_modelname("glm-4-plus")
  25. prompt = KeyWordPrompt.LICENSE_LIST_FILTER + f"""
  26. 请根据上述逻辑,分析以下商品是否为授权生产的,并输出结果:
  27. 商品标题: {title}
  28. 已生产的产品清单:
  29. {license_list_str}
  30. 输出结果:
  31. - 如果找到匹配项则输出:
  32. ```json
  33. {{
  34. "in_list": true,"产品名称": 匹配到的产品名称
  35. }}
  36. ```
  37. - 如果没找到匹配项则输出:
  38. ```json
  39. {{
  40. "in_list": false,
  41. }}
  42. ```
  43. """
  44. response = self.glm.text_response(prompt)
  45. response = response.content.replace(' ', '').replace('\n', '').replace('\t', '').replace(":", ": ")
  46. return response
  47. def image_logo_judgement(self, logo_path, image_url):
  48. """判断图像中是否有指定品牌的logo"""
  49. self.glm.set_modelname("glm-4v-plus-0111")
  50. prompt = KeyWordPrompt.IMAGE_LOGO_JUDGEMENT
  51. response = self.glm.multi_epoch_image_response(logo_path, image_url, prompt)
  52. response = response.content
  53. return response
  54. if __name__ == "__main__":
  55. agent = Agent()
  56. agent.brand_key_word_judgement("【防泼水】荷叶风衣连帽加绒外套防风外套保暖户外运动服女外套", "李宁")