一、引言为什么选择智谱AI多模态能力2025年以来多模态大模型已从“能看懂图”进化到“能理解复杂视觉逻辑”的阶段。智谱AI的GLM-4V-Plus和CogView-3-Plus系列模型在视觉理解和图像生成两个方向上都达到了国际一流水准。相比其他多模态方案智谱API具有三大核心优势视觉推理能力不仅能识别物体还能理解图表、公式、文档布局等复杂视觉逻辑国产自主可控符合国内数据安全合规要求API响应速度有保障成本与效果平衡相比GPT-4V在同等效果下价格更具竞争力本文将系统介绍智谱AI多模态API的调用方案涵盖图像理解、视频分析、文档识别等核心场景并提供可直接上线的代码实践。二、智谱AI多模态API全景截至2026年5月智谱AI提供的多模态相关能力如下模型能力类型输入输出适用场景GLM-4V-Plus视觉理解图像/视频帧 文本文本描述图像问答、物体检测、场景理解GLM-4V-Flash视觉理解轻量图像 文本文本描述快速响应、成本敏感场景CogView-3-Plus图像生成文本描述图像文生图、设计辅助GLM-4V-Doc文档理解PDF/图片文档结构化文本OCR、表格识别、文档问答VideoGLM视频理解视频文件文本描述视频摘要、动作识别三、环境准备与基础配置3.1 获取API Key登录 智谱AI开放平台进入“API密钥”页面创建新的API Key记录密钥并妥善保管3.2 安装SDKbash# 安装官方SDK pip install zhipuai # 或使用 requests 直接调用 pip install requests pillow3.3 基础初始化pythonfrom zhipuai import ZhipuAI # 初始化客户端 client ZhipuAI( api_keyyour-api-key-here # 替换为你的实际API Key ) # 测试连接 response client.models.list() print(✅ 智谱API连接成功)四、核心实践一图像理解GLM-4V-Plus4.1 基础图像问答pythonimport base64 def image_understanding(image_path, question): 对图片进行多模态理解问答 # 读取并编码图片 with open(image_path, rb) as f: image_data base64.b64encode(f.read()).decode(utf-8) # 调用GLM-4V-Plus response client.chat.completions.create( modelglm-4v-plus, # 使用最新plus版本 messages[ { role: user, content: [ { type: image_url, image_url: { url: fdata:image/jpeg;base64,{image_data} } }, { type: text, text: question } ] } ], temperature0.7, # 控制创造性 top_p0.9, max_tokens500 ) return response.choices[0].message.content # 使用示例 result image_understanding(photo.jpg, 请详细描述这张图片中的内容和场景) print(result)4.2 批量图像分析pythonfrom concurrent.futures import ThreadPoolExecutor, as_completed import time from typing import List, Dict class BatchImageAnalyzer: 批量图像分析器支持并发请求 def __init__(self, api_key: str, max_workers: int 5): self.client ZhipuAI(api_keyapi_key) self.max_workers max_workers def analyze_single(self, image_path: str, question: str) - Dict: 分析单张图片 try: with open(image_path, rb) as f: image_data base64.b64encode(f.read()).decode(utf-8) start_time time.time() response self.client.chat.completions.create( modelglm-4v-plus, messages[ { role: user, content: [ {type: image_url, image_url: {url: fdata:image/jpeg;base64,{image_data}}}, {type: text, text: question} ] } ], temperature0.3, max_tokens300 ) elapsed time.time() - start_time return { image: image_path, success: True, result: response.choices[0].message.content, time_ms: int(elapsed * 1000) } except Exception as e: return { image: image_path, success: False, error: str(e) } def batch_analyze(self, image_paths: List[str], question: str) - List[Dict]: 批量分析多张图片 results [] with ThreadPoolExecutor(max_workersself.max_workers) as executor: futures { executor.submit(self.analyze_single, path, question): path for path in image_paths } for future in as_completed(futures): result future.result() results.append(result) status ✅ if result[success] else ❌ print(f{status} {result[image]}) return results # 使用示例 analyzer BatchImageAnalyzer(api_keyyour-api-key, max_workers3) results analyzer.batch_analyze( image_paths[img1.jpg, img2.jpg, img3.png], question这张图片中主要有哪些物体请用中文回答。 ) # 统计结果 success_count sum(1 for r in results if r[success]) print(f\n批量分析完成: 成功 {success_count}/{len(results)})4.3 流式输出适合长文本场景pythondef stream_image_understanding(image_path, question): 流式接收模型响应适合长文本生成场景 with open(image_path, rb) as f: image_data base64.b64encode(f.read()).decode(utf-8) response client.chat.completions.create( modelglm-4v-plus, messages[ { role: user, content: [ {type: image_url, image_url: {url: fdata:image/jpeg;base64,{image_data}}}, {type: text, text: question} ] } ], streamTrue # 启用流式输出 ) # 逐步输出 for chunk in response: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end, flushTrue) return # 内容已在循环中打印 # 使用示例 stream_image_understanding(diagram.png, 请详细解释这张流程图中的业务逻辑)五、核心实践二文档智能识别GLM-4V-Doc智谱AI专门针对文档场景优化了GLM-4V-Doc模型能够高质量识别复杂的文档布局、表格和公式。5.1 文档OCR与结构化提取pythondef document_ocr(document_path, extraction_typefull): 文档OCR与结构化信息提取 :param extraction_type: full(完整), text_only(仅文字), table_only(仅表格) with open(document_path, rb) as f: doc_data base64.b64encode(f.read()).decode(utf-8) prompts { full: 请完整提取文档中的所有文字、表格和结构信息保持原有格式层次。, text_only: 请仅提取文档中的纯文本内容忽略表格和图片。, table_only: 请提取文档中的所有表格以Markdown表格格式输出。 } response client.chat.completions.create( modelglm-4v-doc, # 专门针对文档优化 messages[ { role: user, content: [ {type: image_url, image_url: {url: fdata:image/jpeg;base64,{doc_data}}}, {type: text, text: prompts.get(extraction_type, prompts[full])} ] } ], temperature0.1, # 文档识别需要低温度保证准确性 max_tokens4000 ) return response.choices[0].message.content # 使用示例 result document_ocr(contract.pdf, extraction_typefull) print(result)5.2 表格专项识别pythondef extract_table_from_document(document_path, table_index0): 从文档中提取特定表格转换为结构化数据 with open(document_path, rb) as f: doc_data base64.b64encode(f.read()).decode(utf-8) response client.chat.completions.create( modelglm-4v-doc, messages[ { role: user, content: [ {type: image_url, image_url: {url: fdata:image/jpeg;base64,{doc_data}}}, {type: text, text: f请提取文档中的第{table_index 1}个表格以JSON数组格式输出每个元素是一个字典键为列名。} ] } ], temperature0.1, response_format{type: json_object} # 要求JSON格式输出 ) import json try: # 尝试解析JSON table_data json.loads(response.choices[0].message.content) return table_data except json.JSONDecodeError: # 如果返回不是纯JSON返回原始文本 return response.choices[0].message.content # 使用示例 table extract_table_from_document(financial_report.pdf, table_index0) print(table)六、核心实践三视频理解VideoGLMVideoGLM 支持对视频进行智能分析适用于监控摘要、视频内容审核等场景。6.1 视频片段分析pythondef analyze_video(video_path, query, max_frames20): 分析视频内容 :param video_path: 视频文件路径 :param query: 分析问题 :param max_frames: 最多采样帧数 import cv2 # 从视频中均匀采样帧 cap cv2.VideoCapture(video_path) total_frames int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) frame_indices np.linspace(0, total_frames - 1, max_frames, dtypeint) frames_base64 [] for idx in frame_indices: cap.set(cv2.CAP_PROP_POS_FRAMES, idx) ret, frame cap.read() if ret: _, buffer cv2.imencode(.jpg, frame) frame_base64 base64.b64encode(buffer).decode(utf-8) frames_base64.append(frame_base64) cap.release() # 构建多帧输入消息 content [] for frame_b64 in frames_base64: content.append({ type: image_url, image_url: {url: fdata:image/jpeg;base64,{frame_b64}} }) content.append({type: text, text: query}) response client.chat.completions.create( modelglm-4v-plus, # 当前使用plus进行视频分析 messages[{role: user, content: content}], temperature0.5, max_tokens1000 ) return response.choices[0].message.content # 使用示例 description analyze_video( security_footage.mp4, 请描述这段视频中发生了什么事情重点关注人员活动。 ) print(description)6.2 视频结构化分析封装pythonfrom dataclasses import dataclass from typing import Optional, List dataclass class VideoAnalysisResult: 视频分析结果数据结构 scene_description: str objects_detected: List[str] actions: List[str] key_timestamps: Optional[List[float]] None class VideoAnalyzer: 视频分析器提供结构化输出 def __init__(self, client): self.client client def analyze(self, video_path: str) - VideoAnalysisResult: 对视频进行全面结构化分析 # 统一prompt要求结构化输出 structured_prompt 请对视频进行结构化分析并以JSON格式返回包含以下字段 1. scene_description: 整体场景描述字符串 2. objects_detected: 检测到的物体列表数组 3. actions: 观察到的主要动作数组 4. key_timestamps: 关键事件发生的时间点数组秒数如果无法确定则返回null 请确保输出是合法的JSON格式。 response_text analyze_video(video_path, structured_prompt) # 解析JSON响应 import json try: data json.loads(response_text) return VideoAnalysisResult( scene_descriptiondata.get(scene_description, ), objects_detecteddata.get(objects_detected, []), actionsdata.get(actions, []), key_timestampsdata.get(key_timestamps) ) except json.JSONDecodeError: # 降级处理 return VideoAnalysisResult( scene_descriptionresponse_text, objects_detected[], actions[] )七、核心实践四图像生成CogView-3-Plus7.1 文生图基础pythondef generate_image(prompt: str, aspect_ratio: str 16:9, quality: str HD): 使用CogView-3-Plus生成图像 :param prompt: 文本描述 :param aspect_ratio: 宽高比 16:9, 4:3, 1:1, 9:16 :param quality: 质量 HD(高清), SD(标清) response client.images.generations( modelcogview-3-plus, promptprompt, aspect_ratioaspect_ratio, qualityquality ) # 返回生成的图片URL image_url response.data[0].url print(f✅ 图片生成成功: {image_url}) return image_url # 使用示例 url generate_image( 一只可爱的橘猫坐在咖啡店的窗边阳光洒在它的毛发上电影感4k, aspect_ratio16:9 ) # 下载生成的图片 import requests img_data requests.get(url).content with open(generated_cat.jpg, wb) as f: f.write(img_data)7.2 带参考图的图像生成pythondef generate_with_reference(prompt: str, reference_image_path: str, similarity: float 0.7): 基于参考图生成新图像图生图 :param similarity: 与参考图的相似度 0-1越高越相似 with open(reference_image_path, rb) as f: ref_data base64.b64encode(f.read()).decode(utf-8) response client.images.generations( modelcogview-3-plus, promptprompt, image_referencefdata:image/jpeg;base64,{ref_data}, similaritysimilarity ) return response.data[0].url # 使用示例 result_url generate_with_reference( prompt将这张照片中的场景转换为夜晚有星空和月光, reference_image_pathdaytime_photo.jpg, similarity0.6 )八、进阶实践多模态智能体编排将视觉理解、文档识别和图像生成串联起来构建一个完整的智能工作流。pythonclass MultimodalAgent: 多模态智能体能够串联视觉理解、文档处理和图像生成 def __init__(self, client): self.client client def analyze_and_visualize(self, source_image_path: str, analysis_question: str, generation_prompt_template: str): 先分析图片内容然后基于分析结果生成新图像 示例识别产品照片中的缺陷 - 生成标注了缺陷位置的示意图 # Step 1: 分析原图 with open(source_image_path, rb) as f: image_data base64.b64encode(f.read()).decode(utf-8) analysis_response self.client.chat.completions.create( modelglm-4v-plus, messages[ { role: user, content: [ {type: image_url, image_url: {url: fdata:image/jpeg;base64,{image_data}}}, {type: text, text: analysis_question} ] } ], temperature0.3 ) analysis_result analysis_response.choices[0].message.content print(f 分析结果: {analysis_result}) # Step 2: 基于分析结果构造生成prompt generation_prompt generation_prompt_template.format(analysisanalysis_result) # Step 3: 生成新图像 gen_response self.client.images.generations( modelcogview-3-plus, promptgeneration_prompt, aspect_ratio16:9 ) return { analysis: analysis_result, generated_image_url: gen_response.data[0].url } # 使用示例 agent MultimodalAgent(client) result agent.analyze_and_visualize( source_image_pathfactory_line.jpg, analysis_question请识别这张生产线上可能存在的异常或缺陷位置, generation_prompt_template请生成一张示意图标注出以下位置的缺陷: {analysis} ) print(f生成图片地址: {result[generated_image_url]})九、性能优化与成本控制9.1 成本估算参考2026年价格模型计费方式单价参考GLM-4V-Plus按调用次数/Token¥0.02/次GLM-4V-Flash按调用次数/Token¥0.005/次CogView-3-Plus按生成张数¥0.1/张9.2 降低成本的最佳实践pythonclass CostOptimizedClient: 成本优化版客户端 def __init__(self, client): self.client client self.call_count 0 self.cache {} # 简单缓存 def analyze_with_cache(self, image_path: str, question: str): 带缓存的图像分析相同图片相同问题直接返回缓存 import hashlib # 生成缓存键 with open(image_path, rb) as f: img_hash hashlib.md5(f.read()).hexdigest() cache_key f{img_hash}_{question} if cache_key in self.cache: print(✅ 命中缓存节省API调用) return self.cache[cache_key] # 未命中则调用API result image_understanding(image_path, question) self.cache[cache_key] result self.call_count 1 return result def use_flash_for_simple(self, image_path: str, question: str, complexity: str simple): 简单问题使用更便宜的Flash模型 with open(image_path, rb) as f: image_data base64.b64encode(f.read()).decode(utf-8) model glm-4v-flash if complexity simple else glm-4v-plus response self.client.chat.completions.create( modelmodel, messages[ { role: user, content: [ {type: image_url, image_url: {url: fdata:image/jpeg;base64,{image_data}}}, {type: text, text: question} ] } ] ) return response.choices[0].message.content十、避坑指南与最佳实践10.1 常见问题及解决方案问题现象可能原因解决方案返回内容为空图片格式不支持转换为 JPG/PNG 格式超时错误图片过大压缩图片到 1MB 以内频繁限流并发请求过高增加请求间隔使用指数退避重试文档识别乱码PDF 非标准扫描件先转换为高质量图片再识别10.2 重试机制实现pythonfrom tenacity import retry, stop_after_attempt, wait_exponential retry( stopstop_after_attempt(3), waitwait_exponential(multiplier1, min2, max10) ) def robust_api_call(func, *args, **kwargs): 带自动重试的API调用 return func(*args, **kwargs) # 使用示例 result robust_api_call(image_understanding, photo.jpg, 描述这张图片)十一、总结智谱AI的多模态API已构建起从图像理解、文档识别到视频分析和图像生成的完整能力栈。本文核心要点场景推荐模型关键参数日常图像问答GLM-4V-Plustemperature0.7文档结构化提取GLM-4V-Doctemperature0.1, response_formatjson视频内容分析GLM-4V-Plus 帧采样max_frames20文生图CogView-3-Plusaspect_ratio16:9成本敏感场景GLM-4V-Flash简单问题优先使用未来趋势随着智谱API的持续迭代多模态能力将从“识别”走向“推理”从“单图”走向“长视频”从“被动回答”走向“主动交互”。掌握这些API的最佳实践将为你的应用带来真正的智能升级。
智谱AI API多模态识别方案:从基础调用到生产级实践
一、引言为什么选择智谱AI多模态能力2025年以来多模态大模型已从“能看懂图”进化到“能理解复杂视觉逻辑”的阶段。智谱AI的GLM-4V-Plus和CogView-3-Plus系列模型在视觉理解和图像生成两个方向上都达到了国际一流水准。相比其他多模态方案智谱API具有三大核心优势视觉推理能力不仅能识别物体还能理解图表、公式、文档布局等复杂视觉逻辑国产自主可控符合国内数据安全合规要求API响应速度有保障成本与效果平衡相比GPT-4V在同等效果下价格更具竞争力本文将系统介绍智谱AI多模态API的调用方案涵盖图像理解、视频分析、文档识别等核心场景并提供可直接上线的代码实践。二、智谱AI多模态API全景截至2026年5月智谱AI提供的多模态相关能力如下模型能力类型输入输出适用场景GLM-4V-Plus视觉理解图像/视频帧 文本文本描述图像问答、物体检测、场景理解GLM-4V-Flash视觉理解轻量图像 文本文本描述快速响应、成本敏感场景CogView-3-Plus图像生成文本描述图像文生图、设计辅助GLM-4V-Doc文档理解PDF/图片文档结构化文本OCR、表格识别、文档问答VideoGLM视频理解视频文件文本描述视频摘要、动作识别三、环境准备与基础配置3.1 获取API Key登录 智谱AI开放平台进入“API密钥”页面创建新的API Key记录密钥并妥善保管3.2 安装SDKbash# 安装官方SDK pip install zhipuai # 或使用 requests 直接调用 pip install requests pillow3.3 基础初始化pythonfrom zhipuai import ZhipuAI # 初始化客户端 client ZhipuAI( api_keyyour-api-key-here # 替换为你的实际API Key ) # 测试连接 response client.models.list() print(✅ 智谱API连接成功)四、核心实践一图像理解GLM-4V-Plus4.1 基础图像问答pythonimport base64 def image_understanding(image_path, question): 对图片进行多模态理解问答 # 读取并编码图片 with open(image_path, rb) as f: image_data base64.b64encode(f.read()).decode(utf-8) # 调用GLM-4V-Plus response client.chat.completions.create( modelglm-4v-plus, # 使用最新plus版本 messages[ { role: user, content: [ { type: image_url, image_url: { url: fdata:image/jpeg;base64,{image_data} } }, { type: text, text: question } ] } ], temperature0.7, # 控制创造性 top_p0.9, max_tokens500 ) return response.choices[0].message.content # 使用示例 result image_understanding(photo.jpg, 请详细描述这张图片中的内容和场景) print(result)4.2 批量图像分析pythonfrom concurrent.futures import ThreadPoolExecutor, as_completed import time from typing import List, Dict class BatchImageAnalyzer: 批量图像分析器支持并发请求 def __init__(self, api_key: str, max_workers: int 5): self.client ZhipuAI(api_keyapi_key) self.max_workers max_workers def analyze_single(self, image_path: str, question: str) - Dict: 分析单张图片 try: with open(image_path, rb) as f: image_data base64.b64encode(f.read()).decode(utf-8) start_time time.time() response self.client.chat.completions.create( modelglm-4v-plus, messages[ { role: user, content: [ {type: image_url, image_url: {url: fdata:image/jpeg;base64,{image_data}}}, {type: text, text: question} ] } ], temperature0.3, max_tokens300 ) elapsed time.time() - start_time return { image: image_path, success: True, result: response.choices[0].message.content, time_ms: int(elapsed * 1000) } except Exception as e: return { image: image_path, success: False, error: str(e) } def batch_analyze(self, image_paths: List[str], question: str) - List[Dict]: 批量分析多张图片 results [] with ThreadPoolExecutor(max_workersself.max_workers) as executor: futures { executor.submit(self.analyze_single, path, question): path for path in image_paths } for future in as_completed(futures): result future.result() results.append(result) status ✅ if result[success] else ❌ print(f{status} {result[image]}) return results # 使用示例 analyzer BatchImageAnalyzer(api_keyyour-api-key, max_workers3) results analyzer.batch_analyze( image_paths[img1.jpg, img2.jpg, img3.png], question这张图片中主要有哪些物体请用中文回答。 ) # 统计结果 success_count sum(1 for r in results if r[success]) print(f\n批量分析完成: 成功 {success_count}/{len(results)})4.3 流式输出适合长文本场景pythondef stream_image_understanding(image_path, question): 流式接收模型响应适合长文本生成场景 with open(image_path, rb) as f: image_data base64.b64encode(f.read()).decode(utf-8) response client.chat.completions.create( modelglm-4v-plus, messages[ { role: user, content: [ {type: image_url, image_url: {url: fdata:image/jpeg;base64,{image_data}}}, {type: text, text: question} ] } ], streamTrue # 启用流式输出 ) # 逐步输出 for chunk in response: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end, flushTrue) return # 内容已在循环中打印 # 使用示例 stream_image_understanding(diagram.png, 请详细解释这张流程图中的业务逻辑)五、核心实践二文档智能识别GLM-4V-Doc智谱AI专门针对文档场景优化了GLM-4V-Doc模型能够高质量识别复杂的文档布局、表格和公式。5.1 文档OCR与结构化提取pythondef document_ocr(document_path, extraction_typefull): 文档OCR与结构化信息提取 :param extraction_type: full(完整), text_only(仅文字), table_only(仅表格) with open(document_path, rb) as f: doc_data base64.b64encode(f.read()).decode(utf-8) prompts { full: 请完整提取文档中的所有文字、表格和结构信息保持原有格式层次。, text_only: 请仅提取文档中的纯文本内容忽略表格和图片。, table_only: 请提取文档中的所有表格以Markdown表格格式输出。 } response client.chat.completions.create( modelglm-4v-doc, # 专门针对文档优化 messages[ { role: user, content: [ {type: image_url, image_url: {url: fdata:image/jpeg;base64,{doc_data}}}, {type: text, text: prompts.get(extraction_type, prompts[full])} ] } ], temperature0.1, # 文档识别需要低温度保证准确性 max_tokens4000 ) return response.choices[0].message.content # 使用示例 result document_ocr(contract.pdf, extraction_typefull) print(result)5.2 表格专项识别pythondef extract_table_from_document(document_path, table_index0): 从文档中提取特定表格转换为结构化数据 with open(document_path, rb) as f: doc_data base64.b64encode(f.read()).decode(utf-8) response client.chat.completions.create( modelglm-4v-doc, messages[ { role: user, content: [ {type: image_url, image_url: {url: fdata:image/jpeg;base64,{doc_data}}}, {type: text, text: f请提取文档中的第{table_index 1}个表格以JSON数组格式输出每个元素是一个字典键为列名。} ] } ], temperature0.1, response_format{type: json_object} # 要求JSON格式输出 ) import json try: # 尝试解析JSON table_data json.loads(response.choices[0].message.content) return table_data except json.JSONDecodeError: # 如果返回不是纯JSON返回原始文本 return response.choices[0].message.content # 使用示例 table extract_table_from_document(financial_report.pdf, table_index0) print(table)六、核心实践三视频理解VideoGLMVideoGLM 支持对视频进行智能分析适用于监控摘要、视频内容审核等场景。6.1 视频片段分析pythondef analyze_video(video_path, query, max_frames20): 分析视频内容 :param video_path: 视频文件路径 :param query: 分析问题 :param max_frames: 最多采样帧数 import cv2 # 从视频中均匀采样帧 cap cv2.VideoCapture(video_path) total_frames int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) frame_indices np.linspace(0, total_frames - 1, max_frames, dtypeint) frames_base64 [] for idx in frame_indices: cap.set(cv2.CAP_PROP_POS_FRAMES, idx) ret, frame cap.read() if ret: _, buffer cv2.imencode(.jpg, frame) frame_base64 base64.b64encode(buffer).decode(utf-8) frames_base64.append(frame_base64) cap.release() # 构建多帧输入消息 content [] for frame_b64 in frames_base64: content.append({ type: image_url, image_url: {url: fdata:image/jpeg;base64,{frame_b64}} }) content.append({type: text, text: query}) response client.chat.completions.create( modelglm-4v-plus, # 当前使用plus进行视频分析 messages[{role: user, content: content}], temperature0.5, max_tokens1000 ) return response.choices[0].message.content # 使用示例 description analyze_video( security_footage.mp4, 请描述这段视频中发生了什么事情重点关注人员活动。 ) print(description)6.2 视频结构化分析封装pythonfrom dataclasses import dataclass from typing import Optional, List dataclass class VideoAnalysisResult: 视频分析结果数据结构 scene_description: str objects_detected: List[str] actions: List[str] key_timestamps: Optional[List[float]] None class VideoAnalyzer: 视频分析器提供结构化输出 def __init__(self, client): self.client client def analyze(self, video_path: str) - VideoAnalysisResult: 对视频进行全面结构化分析 # 统一prompt要求结构化输出 structured_prompt 请对视频进行结构化分析并以JSON格式返回包含以下字段 1. scene_description: 整体场景描述字符串 2. objects_detected: 检测到的物体列表数组 3. actions: 观察到的主要动作数组 4. key_timestamps: 关键事件发生的时间点数组秒数如果无法确定则返回null 请确保输出是合法的JSON格式。 response_text analyze_video(video_path, structured_prompt) # 解析JSON响应 import json try: data json.loads(response_text) return VideoAnalysisResult( scene_descriptiondata.get(scene_description, ), objects_detecteddata.get(objects_detected, []), actionsdata.get(actions, []), key_timestampsdata.get(key_timestamps) ) except json.JSONDecodeError: # 降级处理 return VideoAnalysisResult( scene_descriptionresponse_text, objects_detected[], actions[] )七、核心实践四图像生成CogView-3-Plus7.1 文生图基础pythondef generate_image(prompt: str, aspect_ratio: str 16:9, quality: str HD): 使用CogView-3-Plus生成图像 :param prompt: 文本描述 :param aspect_ratio: 宽高比 16:9, 4:3, 1:1, 9:16 :param quality: 质量 HD(高清), SD(标清) response client.images.generations( modelcogview-3-plus, promptprompt, aspect_ratioaspect_ratio, qualityquality ) # 返回生成的图片URL image_url response.data[0].url print(f✅ 图片生成成功: {image_url}) return image_url # 使用示例 url generate_image( 一只可爱的橘猫坐在咖啡店的窗边阳光洒在它的毛发上电影感4k, aspect_ratio16:9 ) # 下载生成的图片 import requests img_data requests.get(url).content with open(generated_cat.jpg, wb) as f: f.write(img_data)7.2 带参考图的图像生成pythondef generate_with_reference(prompt: str, reference_image_path: str, similarity: float 0.7): 基于参考图生成新图像图生图 :param similarity: 与参考图的相似度 0-1越高越相似 with open(reference_image_path, rb) as f: ref_data base64.b64encode(f.read()).decode(utf-8) response client.images.generations( modelcogview-3-plus, promptprompt, image_referencefdata:image/jpeg;base64,{ref_data}, similaritysimilarity ) return response.data[0].url # 使用示例 result_url generate_with_reference( prompt将这张照片中的场景转换为夜晚有星空和月光, reference_image_pathdaytime_photo.jpg, similarity0.6 )八、进阶实践多模态智能体编排将视觉理解、文档识别和图像生成串联起来构建一个完整的智能工作流。pythonclass MultimodalAgent: 多模态智能体能够串联视觉理解、文档处理和图像生成 def __init__(self, client): self.client client def analyze_and_visualize(self, source_image_path: str, analysis_question: str, generation_prompt_template: str): 先分析图片内容然后基于分析结果生成新图像 示例识别产品照片中的缺陷 - 生成标注了缺陷位置的示意图 # Step 1: 分析原图 with open(source_image_path, rb) as f: image_data base64.b64encode(f.read()).decode(utf-8) analysis_response self.client.chat.completions.create( modelglm-4v-plus, messages[ { role: user, content: [ {type: image_url, image_url: {url: fdata:image/jpeg;base64,{image_data}}}, {type: text, text: analysis_question} ] } ], temperature0.3 ) analysis_result analysis_response.choices[0].message.content print(f 分析结果: {analysis_result}) # Step 2: 基于分析结果构造生成prompt generation_prompt generation_prompt_template.format(analysisanalysis_result) # Step 3: 生成新图像 gen_response self.client.images.generations( modelcogview-3-plus, promptgeneration_prompt, aspect_ratio16:9 ) return { analysis: analysis_result, generated_image_url: gen_response.data[0].url } # 使用示例 agent MultimodalAgent(client) result agent.analyze_and_visualize( source_image_pathfactory_line.jpg, analysis_question请识别这张生产线上可能存在的异常或缺陷位置, generation_prompt_template请生成一张示意图标注出以下位置的缺陷: {analysis} ) print(f生成图片地址: {result[generated_image_url]})九、性能优化与成本控制9.1 成本估算参考2026年价格模型计费方式单价参考GLM-4V-Plus按调用次数/Token¥0.02/次GLM-4V-Flash按调用次数/Token¥0.005/次CogView-3-Plus按生成张数¥0.1/张9.2 降低成本的最佳实践pythonclass CostOptimizedClient: 成本优化版客户端 def __init__(self, client): self.client client self.call_count 0 self.cache {} # 简单缓存 def analyze_with_cache(self, image_path: str, question: str): 带缓存的图像分析相同图片相同问题直接返回缓存 import hashlib # 生成缓存键 with open(image_path, rb) as f: img_hash hashlib.md5(f.read()).hexdigest() cache_key f{img_hash}_{question} if cache_key in self.cache: print(✅ 命中缓存节省API调用) return self.cache[cache_key] # 未命中则调用API result image_understanding(image_path, question) self.cache[cache_key] result self.call_count 1 return result def use_flash_for_simple(self, image_path: str, question: str, complexity: str simple): 简单问题使用更便宜的Flash模型 with open(image_path, rb) as f: image_data base64.b64encode(f.read()).decode(utf-8) model glm-4v-flash if complexity simple else glm-4v-plus response self.client.chat.completions.create( modelmodel, messages[ { role: user, content: [ {type: image_url, image_url: {url: fdata:image/jpeg;base64,{image_data}}}, {type: text, text: question} ] } ] ) return response.choices[0].message.content十、避坑指南与最佳实践10.1 常见问题及解决方案问题现象可能原因解决方案返回内容为空图片格式不支持转换为 JPG/PNG 格式超时错误图片过大压缩图片到 1MB 以内频繁限流并发请求过高增加请求间隔使用指数退避重试文档识别乱码PDF 非标准扫描件先转换为高质量图片再识别10.2 重试机制实现pythonfrom tenacity import retry, stop_after_attempt, wait_exponential retry( stopstop_after_attempt(3), waitwait_exponential(multiplier1, min2, max10) ) def robust_api_call(func, *args, **kwargs): 带自动重试的API调用 return func(*args, **kwargs) # 使用示例 result robust_api_call(image_understanding, photo.jpg, 描述这张图片)十一、总结智谱AI的多模态API已构建起从图像理解、文档识别到视频分析和图像生成的完整能力栈。本文核心要点场景推荐模型关键参数日常图像问答GLM-4V-Plustemperature0.7文档结构化提取GLM-4V-Doctemperature0.1, response_formatjson视频内容分析GLM-4V-Plus 帧采样max_frames20文生图CogView-3-Plusaspect_ratio16:9成本敏感场景GLM-4V-Flash简单问题优先使用未来趋势随着智谱API的持续迭代多模态能力将从“识别”走向“推理”从“单图”走向“长视频”从“被动回答”走向“主动交互”。掌握这些API的最佳实践将为你的应用带来真正的智能升级。