# 安装最新版本 pip install zai-sdk # 或指定版本 pip install zai-sdk==0.0.1
import zai print(zai.__version__)
from zai import ZhipuAiClient client = ZhipuAiClient(api_key="your-api-key") response = client.web_search.web_search( search_engine="search_pro", search_query="搜索2025年4月的财经新闻", count=15, # 返回结果的条数,范围1-50,默认10 search_domain_filter="www.sohu.com", # 只访问指定域名的内容 search_recency_filter="noLimit", # 搜索指定日期范围内的内容 content_size="high" # 控制网页摘要的字数,默认medium ) print(response)
MCP Server 配置指南
{ "mcpServers": { "zhipu-web-search-sse": { "url": "https://open.bigmodel.cn/api/mcp/web_search/sse?Authorization=YOUR API Key" } } }
from zai import ZhipuAiClient client = ZhipuAiClient(api_key="your-api-key") # 定义工具参数 tools = [{ "type": "web_search", "web_search": { "enable": "True", "search_engine": "search_pro", "search_result": "True", "search_prompt": "你是一位财经分析师。请用简洁的语言总结网络搜索{search_result}中的关键信息,按重要性排序并引用来源日期。今天的日期是2025年4月11日。", "count": "5", "search_domain_filter": "www.sohu.com", "search_recency_filter": "noLimit", "content_size": "high" } }] # 定义用户消息 messages = [{ "role": "user", "content": "2025年4月的重要财经事件、政策变化和市场数据" }] # 调用API获取响应 response = client.chat.completions.create( model="glm-4-air", # 模型标识符 messages=messages, # 用户消息 tools=tools # 工具参数 ) # 打印响应结果 print(response)
风险评估报告生成示例
from zai import ZhipuAiClient client = ZhipuAiClient(api_key="your-api-key") generate = client.assistant.conversation( assistant_id="659e54b1b8006379b4b2abd6", conversation_id=None, model="glm-4-assistant", messages=[ { "role": "user", "content": [{ "type": "text", "text": "请对2025年第一季度中东地缘政治冲突对全球能源市场的影响进行全面分析。结合原油价格波动数据、主要产油国政策调整以及欧洲能源替代方案。生成基于时间线的风险评估报告,突出期货市场的实时反应和关键机构响应(如IEA建议)。" }] } ], stream=True, attachments=None, metadata=None ) for resp in generate: print(resp)
Was this page helpful?