zhipu.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on 2024-10-31
  4. ---------
  5. @summary: 智普文件抽取服务
  6. ---------
  7. @author: Dzr
  8. """
  9. import base64
  10. from zhipuai import ZhipuAI
  11. class ZhipuFileExtract:
  12. def __init__(self, api_key):
  13. self._client = ZhipuAI(
  14. api_key=api_key,
  15. base_url="https://open.bigmodel.cn/api/paas/v4"
  16. )
  17. def glm_4v_flash(self, image: bytes, text=None):
  18. if text is None:
  19. text = "这是一张字体图片,图片内容是什么?你只需要返回图中内容即可,不要解释,不要返回任何其它内容"
  20. img_base = base64.b64encode(image).decode('utf-8')
  21. response = self._client.chat.completions.create(
  22. model="glm-4v-flash", # 填写需要调用的模型名称
  23. messages=[
  24. {
  25. "role": "user",
  26. "content": [
  27. {
  28. "type": "image_url",
  29. "image_url": {
  30. "url": img_base
  31. }
  32. },
  33. {
  34. "type": "text",
  35. "text": text
  36. }
  37. ]
  38. }
  39. ]
  40. )
  41. return response.choices[0].message.content