agent.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from agent import Glm, Prompt
  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 = Prompt.EXTRACT_INFO_FROM_TITLE + f"""
  17. 请根据上述逻辑,分析以下商品标题,并输出结果:
  18. 商品标题:{title}
  19. 给定的引流品牌:{brandname}"""
  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 = Prompt.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. "is_similarity": true
  45. }}
  46. ```
  47. - 如果既不是授权生产的商品,也不是相似产品则输出:
  48. ```json
  49. {{
  50. "in_list": false,
  51. "is_similarity": false
  52. }}
  53. ```
  54. """
  55. response = self.glm.text_response(prompt)
  56. response = response.content.replace(' ', '').replace('\n', '').replace('\t', '').replace(":", ": ")
  57. return response
  58. def image_logo_judgement(self, logo_path, image_url):
  59. """判断图像中是否有指定品牌的logo"""
  60. self.glm.set_modelname("glm-4v-plus-0111")
  61. prompt = Prompt.IMAGE_LOGO_JUDGEMENT
  62. response = self.glm.multi_epoch_image_response(logo_path, image_url, prompt)
  63. response = response.content
  64. return response
  65. if __name__ == "__main__":
  66. agent = Agent()
  67. agent.brand_key_word_judgement("【防泼水】荷叶风衣连帽加绒外套防风外套保暖户外运动服女外套", "李宁")