zhipu.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on 2024-10-31
  4. ---------
  5. @summary: 智普文件抽取服务
  6. ---------
  7. @author: Dzr
  8. """
  9. import base64
  10. import json
  11. import pathlib
  12. from pathlib import Path
  13. from zhipuai import ZhipuAI
  14. class ZhipuFileExtract:
  15. def __init__(self, api_key):
  16. self._client = ZhipuAI(
  17. api_key=api_key,
  18. base_url="https://open.bigmodel.cn/api/paas/v4"
  19. )
  20. def file_extract(self, file):
  21. file_object = self.create(file)
  22. file_content = json.loads(self.get_content(file_object.id))["content"]
  23. self.delete(file_object.id)
  24. print('智普ocr > ', file_content)
  25. return file_content
  26. def create(self, file):
  27. # 格式限制:.PDF .DOCX .DOC .XLS .XLSX .PPT .PPTX .PNG .JPG .JPEG .CSV .PY .TXT .MD .BMP .GIF
  28. # 大小:单个文件50M、总数限制为100个文件
  29. # 支持retrieval、batch、fine-tune、file-extract文件
  30. return self._client.files.create(file=file, purpose="file-extract")
  31. def get_content(self, file_id):
  32. return self._client.files.content(file_id=file_id).content
  33. def delete(self, file_id):
  34. self._client.files.delete(file_id=file_id)
  35. def glm_4v_flash(self, file, text=None):
  36. if isinstance(file, pathlib.Path):
  37. img_bytes = file.read_bytes()
  38. img_name = file.name
  39. else:
  40. img_name, img_bytes, _ = file
  41. if text is None:
  42. text = "这是一张字体图片,图片内容是什么?你只需要返回图中内容即可,不要解释,不要返回任何其它内容"
  43. img_base = base64.b64encode(img_bytes).decode('utf-8')
  44. response = self._client.chat.completions.create(
  45. model="glm-4v-flash", # 填写需要调用的模型名称
  46. messages=[
  47. {
  48. "role": "user",
  49. "content": [
  50. {
  51. "type": "image_url",
  52. "image_url": {
  53. "url": img_base
  54. }
  55. },
  56. {
  57. "type": "text",
  58. "text": text
  59. }
  60. ]
  61. }
  62. ]
  63. )
  64. print(img_name, "--> ", response.choices[0].message)
  65. return response.choices[0].message.content
  66. if __name__ == '__main__':
  67. client = ZhipuFileExtract(
  68. api_key=""
  69. )
  70. ret = client.glm_4v_flash(Path('download.png'), text='这是一张100以内数学计算图片验证码,图片内容计算结果是什么?你只需要返回图中内容计算结果即可,不要解释,不要返回任何其它内容')
  71. print(ret)