对话中的网络搜索
中文已实测✓exp-2843
对话中的网络搜索允许 Completions API 调用搜索引擎,将实时网络检索结果与 GLM 的生成能力相结合,提供最新且可验证的答案。
API文档: 对话中的网络搜索 示例:财经分析摘要
Python Java 响应示例 安装 SDK # 安装最新版本 pip install zai-sdk # 或指定版本 pip install zai-sdk== 0.2.3 验证安装 import zai print (zai. __version__ ) 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) 安装 SDK Maven < dependency > < groupId > ai.z.openapi </ groupId > < artifactId > zai-sdk </ artifactId > < version > 0.3.5 </ version > </ dependency > Gradle (Groovy) implementation 'ai.z.openapi:zai-sdk:0.3.5' import ai.z.openapi.ZhipuAiClient; import ai.z.openapi.service.chat.ChatService; import ai.z.openapi.service.model.ChatCompletionCreateParams; import ai.z.openapi.service.model.ChatCompletionResponse; import ai.z.openapi.service.model.ChatMessage; import ai.z.openapi.service.model.ChatMessageRole; import ai.z.openapi.service.model.ChatTool; import ai.z.openapi.service.model.ChatToolType; import ai.z.openapi.service.model.WebSearch; import java.util.ArrayList; import java.util.List; public static void main ( String [] args) { // 创建客户端 ZhipuAiClient client = ZhipuAiClient . builder (). ofZHIPU (). build ();; ChatService chatService = client . chat (); // 定义用户消息 List < ChatMessage > messages = new ArrayList <>(); ChatMessage userMessage = new ChatMessage ( ChatMessageRole . USER . value (), "2025年4月的重要财经事件、政策变化和市场数据" ); messages . add (userMessage); // 定义工具参数 List < ChatTool > tools = new ArrayList <>(); ChatTool webSearchTool = new ChatTool (); webSearchTool . setType ( ChatToolType . WEB_SEARCH . value ()); WebSearch webSearch = WebSearch . builder () . enable ( true ) . searchEngine ( "search_pro" ) . searchResult ( true ) . searchPrompt ( "你是一位财经分析师。请用简洁的语言总结网络搜索{search_result}中的关键信息,按重要性排序并引用来源日期。今天的日期是2025年4月11日。" ) . count ( 5 ) . searchDomainFilter ( "www.sohu.com" ) . searchRecencyFilter ( "noLimit" ) . contentSize ( "high" ) . build (); webSearchTool . setWebSearch (webSearch); tools . add (webSearchTool); // 调用API获取响应 ChatCompletionCreateParams request = ChatCompletionCreateParams . builder () . model ( "glm-4-air" ) // 模型标识符 . messages (messages) // 用户消息 . tools (tools) // 工具参数 . toolChoice ( "auto" ) // 自动选择工具 . stream ( false ) // 非流式响应 . build (); ChatCompletionResponse response = chatService . createChatCompletion (request); // 打印响应结果 System . out . println (response); } { "choices" : [ { "finish_reason" : "stop" , "index" : 0 , "message" : { "content" : "根据你提供的文档,以下是2025年4月的重要财经事件,按重要性排序: \n\n 1. **G20财长和央行行长会议** - 日期待定。G20会议将讨论全球经济复苏、金融稳定和可持续发展等关键议题。这将对全球经济政策协调和金融市场情绪产生深远影响。[来源:ref_1] \n\n 2. **多国和地区制造业PMI初值发布** - 包括法国、德国、欧元区和英国。这些数据将揭示各自制造业部门的活动状况,为投资者提供关键洞察。[来源:ref_1]" , "role" : "assistant" } } ], "created" : 1748311718 , "id" : "20250527100811da2f8f7243f94b02" , "model" : "glm-4-air" , "request_id" : "20250527100811da2f8f7243f94b02" , "usage" : { "completion_tokens" : 868 , "prompt_tokens" : 4199 , "total_tokens" : 5067 } }
复制后粘贴到 AI 对话框发送,本次对话全程生效
出处:智谱 GLM 官方提示词教程 · docs.bigmodel.cn/cn/guide/tools/web-search · 可信度 S · 标签:中文 / 提示词工程 / 防幻觉 / 少样本 / 检索增强 怎么用(对话型)复制后粘贴到对话框发送,本次对话全程生效。想长期用:支持自定义指令的产品(设置→个性化)可设为默认。