## 调用示例 ### 基础与流式 Python 安装 SDK ```bash # 安装最新版本 pip install zai-sdk # 或指定版本 pip install zai-sdk==0.2.0 ``` 验证安装 ```bash import zai print(zai.__version__) ``` 基础调用 ```bash from zai import ZhipuAiClient client = ZhipuAiClient(api_key="") # 填写您自己的 APIKey response = client.chat.completions.create( model="glm-4.6v-flash", # 填写需要调用的模型名称 messages=[ { "content": [ { "type": "image_url", "image_url": { "url": "https://cloudcovert-1305175928.cos.ap-guangzhou.myqcloud.com/%E5%9B%BE%E7%89%87grounding.PNG" } }, { "type": "text", "text": "Where is the second bottle of beer from the right on the table? Provide coordinates in [[xmin,ymin,xmax,ymax]] format" } ], "role": "user" } ], thinking={ "type": "enabled" } ) print(response.choices[0].message) ``` 流式调用 ```bash from zai import ZhipuAiClient client = ZhipuAiClient(api_key="") # 填写您自己的APIKey response = client.chat.completions.create( model="glm-4.6v-flash", # 填写需要调用的模型名称 messages=[ { "content": [ { "type": "image_url", "image_url": { "url": "https://cloudcovert-1305175928.cos.ap-guangzhou.myqcloud.com/%E5%9B%BE%E7%89%87grounding.PNG" } }, { "type": "text", "text": "Where is the second bottle of beer from the right on the table? Provide coordinates in [[xmin,ymin,xmax,ymax]] format" } ], "role": "user" } ], thinking={ "type": "enabled" }, stream=True ) for chunk in response: if chunk.choices[0].delta.reasoning_content: print(chunk.choices[0].delta.reasoning_content, end='', flush=True) if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end='', flush=True) ``` ### 多模态理解 > 不支持同时理解文件、视频和图像。 Python 安装 SDK ```bash # 安装最新版本 pip install zai-sdk # 或指定版本 pip install zai-sdk==0.2.0 ``` 验证安装 ```bash import zai print(zai.__version__) ``` 图片理解 ``` from zai import ZhipuAiClient client = ZhipuAiClient(api_key="your-api-key") # 填写您自己的APIKey response = client.chat.completions.create( model="glm-4.6v-flash", messages=[ { "role": "user", "content": [ { "type": "image_url", "image_url": { "url": "https://cdn.bigmodel.cn/static/logo/register.png" } }, { "type": "image_url", "image_url": { "url": "https://cdn.bigmodel.cn/static/logo/api-key.png" } }, { "type": "text", "text": "What are the pics talk about?" } ] } ], thinking={ "type": "enabled" } ) print(response.choices[0].message) ``` 传入 Base64 图片 ```bash from zai import ZhipuAiClient import base64 client = ZhipuAiClient(api_key="your-api-key") # 填写您自己的APIKey img_path = "your/path/xxx.png" with open(img_path, "rb") as img_file: img_base = base64.b64encode(img_file.read()).decode("utf-8") response = client.chat.completions.create( model="glm-4.6v-flash", messages=[ { "role": "user", "content": [ { "type": "image_url", "image_url": { "url": img_base } }, { "type": "text", "text": "请描述这个图片" } ] } ], thinking={ "type": "enabled" } ) print(response.choices[0].message) ``` 视频理解 ```bash from zai import ZhipuAiClient client = ZhipuAiClient(api_key="your-api-key") # 填写您自己的APIKey response = client.chat.completions.create( model="glm-4.6v-flash", messages=[ { "role": "user", "content": [ { "type": "video_url", "video_url": { "url": "https://cdn.bigmodel.cn/agent-demos/lark/113123.mov" } }, { "type": "text", "text": "What are the video show about?" } ] } ], thinking={ "type": "enabled" } ) print(response.choices[0].message) ``` 文件理解 ```bash from zai import ZhipuAiClient client = ZhipuAiClient(api_key="your-api-key") # 填写您自己的APIKey response = client.chat.completions.create( model="glm-4.6v-flash", messages=[ { "role": "user", "content": [ { "type": "file_url", "file_url": { "url": "https://cdn.bigmodel.cn/static/demo/demo2.txt" } }, { "type": "file_url", "file_url": { "url": "https://cdn.bigmodel.cn/static/demo/demo1.pdf" } }, { "type": "text", "text": "What are the files show about?" } ] } ], thinking={ "type": "enabled" } ) print(response.choices[0].message) ```