glm.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from config import load_config
  2. from utils import image_to_base
  3. from zhipuai import ZhipuAI
  4. class Glm:
  5. _instance = None
  6. def __new__(cls):
  7. if not cls._instance:
  8. cls._instance = super(Glm, cls).__new__(cls)
  9. cls._instance._initialized = False
  10. return cls._instance
  11. def __init__(self):
  12. if not self._initialized:
  13. self.cfg = load_config()['glm']
  14. self.client = ZhipuAI(api_key=self.cfg["api_key"])
  15. self._initialized = True
  16. self.model_name = "glm-4-plus"
  17. def text_response(self, query):
  18. """文字问答"""
  19. resonse = self.client.chat.completions.create(
  20. model=self.model_name,
  21. messages=[
  22. {"role": "user", "content": query}
  23. ],
  24. response_format= {
  25. 'type': 'json_object'
  26. }
  27. )
  28. return resonse.choices[0].message
  29. def image_response(self, query):
  30. """单图像问答"""
  31. response = self.client.chat.completions.create(
  32. model="glm-4v-plus-0111",
  33. messages=[
  34. {
  35. "role": "user",
  36. "content": [
  37. {
  38. "type": "text",
  39. "text": query
  40. },
  41. {
  42. "type": "image_url",
  43. "image_url": {"url": "http://h2.appsimg.com/a.appsimg.com/upload/merchandise/pdcvis/613214/2024/1120/88/9b9027dd-95b7-4024-b71e-fb7cbfde16a1.jpg"}
  44. }
  45. ]
  46. }
  47. ]
  48. )
  49. return response.choices[0].message
  50. def multi_epoch_image_response(self, logo_path, image_url, query):
  51. """多轮图像问答"""
  52. image_base = image_to_base(logo_path)
  53. response = self.client.chat.completions.create(
  54. model=self.model_name,
  55. messages=[
  56. {
  57. "content": [
  58. {
  59. "image_url": {"url": image_base},
  60. "type": "image_url"
  61. },
  62. {
  63. "text": "这是李宁的logo",
  64. "type": "text"
  65. }
  66. ],
  67. "role": "user"
  68. },
  69. {
  70. "content": [
  71. {
  72. "image_url": {"url": image_url},
  73. "type": "image_url"
  74. },
  75. {
  76. "text": query,
  77. "type": "text"
  78. }
  79. ],
  80. "role": "user"
  81. }
  82. ],
  83. response_format = {
  84. "type": "json_object"
  85. }
  86. )
  87. return response.choices[0].message
  88. def web_search_in_chat(self, search_prompt, content):
  89. """网络搜索工具"""
  90. tools = [{
  91. "type": "web_search",
  92. "web_search": {
  93. "enable": True,
  94. "search_engine": "search_pro_sogou", # 选择搜索引擎
  95. "search_result": True,
  96. "search_prompt": search_prompt,
  97. }
  98. }]
  99. messages = [{
  100. "role": "user",
  101. "content": content
  102. }]
  103. response = self.client.chat.completions.create(
  104. model=self.model_name,
  105. messages=messages,
  106. tools=tools,
  107. response_format={
  108. "type": "json_object"
  109. }
  110. )
  111. return response.choices[0].message
  112. def set_modelname(self, modelname):
  113. self.model_name = modelname
  114. if __name__ == '__main__':
  115. glm = Glm()
  116. glm.set_modelname("glm-4v-plus-0111")
  117. query = f"""
  118. 你是一个产品图像分析助手,你的任务是判断第二张图像中的产品上是否包含logo,并与第一张logo图像做对比,判断第二张图像中的logo是否与第一张的一致。
  119. 判断思路:
  120. 1. 首先判断第二张图像中是否包含logo。
  121. 2. 如果包含logo的话,再进行判断,是否与第一张的logo一致。
  122. 3. 如果与第一张的logo不一致,请根据你的经验判断图像中的logo是什么品牌,如果不知道返回'未知'
  123. 最终结果返回为以下给出的json格式
  124. 输出结果示例:
  125. {{
  126. "is_contain_logo": false
  127. }}
  128. {{
  129. "is_contain_logo": true,
  130. "is_jugement_logo": true,
  131. "brand_name": "李宁"
  132. }}
  133. """
  134. response = glm.multi_epoch_image_response("./logo/lining.jpg", "http://h2.appsimg.com/a.appsimg.com/upload/merchandise/pdcvis/613214/2024/1120/88/9b9027dd-95b7-4024-b71e-fb7cbfde16a1.jpg", query)
  135. print(response.content)