agent.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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):
  23. """判断是否为未授权商品"""
  24. license_list_str = ""
  25. for product in license_list:
  26. license_list_str += f"{product}\n"
  27. self.glm.set_modelname("glm-4-plus")
  28. prompt = KeyWordPrompt.LICENSE_LIST_FILTER + f"""
  29. 请根据上述逻辑,分析以下商品是否为授权生产的,并输出结果:
  30. 商品标题: {title}
  31. 已生产的产品清单:
  32. {license_list_str}
  33. 输出结果:
  34. - 如果找到匹配项则输出:
  35. ```json
  36. {{
  37. "in_list": true,"产品名称": 匹配到的产品名称
  38. }}
  39. ```
  40. - 如果没找到匹配项则输出:
  41. ```json
  42. {{
  43. "in_list": false,
  44. }}
  45. ```
  46. """
  47. response = self.glm.text_response(prompt)
  48. response = response.content.replace(' ', '').replace('\n', '').replace('\t', '').replace(":", ": ")
  49. return response
  50. def image_logo_judgement(self, logo_path, image_url):
  51. """判断图像中是否有指定品牌的logo"""
  52. self.glm.set_modelname("glm-4v-plus-0111")
  53. prompt = KeyWordPrompt.IMAGE_LOGO_JUDGEMENT
  54. response = self.glm.multi_epoch_image_response(logo_path, image_url, prompt)
  55. response = response.content
  56. return response
  57. if __name__ == "__main__":
  58. agent = Agent()
  59. agent.brand_key_word_judgement("【防泼水】荷叶风衣连帽加绒外套防风外套保暖户外运动服女外套", "李宁")