使用 Search API
DeepSeek提供了网页搜索接口(Search API),官方说明地址:
https://api-docs.deepseek.com/zh-cn/guides/search
我们可以据此设计一个网页搜索工具,让大模型具备搜索分析网页的能力,例如让AI自动检索某企业的新闻(数据),做出买入还是卖出的建议。
下面的是参考代码,可直接在命令窗口测试执行:
Dim apiKey As String = "sk-a734b83536da45e9aedc3e42605bfb3a" '请替换为自己的APIKey
Dim caller As New LLMCaller("https://api.deepseek.com/chat/completions", "deepseek-flash", apiKey)
'Query参数的描述:
Dim queryPrompt As String = <![CDATA[
搜索词。1-128字符。支持组合多个操作符:
- site: 限制站点。例如:DeepSeek site:github.com。多个站点用空格加 OR 连接。
- -site: 排除站点。例如:DeepSeek -site:example.com。
- - 排除包含指定关键词的网页。
- -"..." 排除包含指定短语的网页。
- "..." 精确匹配指定短语。
- lang: 限制语言。例如:lang:zh,en。
- after: 限制在指定日期之后。例如:after:2025-01-01。
- before: 限制在指定日期之前。例如:before:2026-06-01。
]]>.Value
caller.Messages =
New
JArray()
caller.SystemPrompt =
<![CDATA[
你是一个搜索专家。请严格遵循以下规则:
1. 如果用户提到了具体网站(如GitHub),你必须在query参数后面加上 site: 语法(如 site:github.com)。
2. 如果用户提到了时间范围,请务必加上 after:语法 或(和) before:语法。
]]>.Value '系统提示词
' 1. 定义工具
With caller.AddTool("web_search", "当需要获取实时信息、新闻、最新数据或模型知识库之外的事实性内容时,使用此工具进行网页搜索")
.AddParameter("query", "string", queryPrompt, True)
.AddParameter("count", "integer", "返回条数,1-50,默认10。", False)
.AddParameter("ori_question", "string", "触发搜索的用户原始问题,有助于理解搜索意图。", False)
.AddParameter("need_content", "boolean", "是否在结果中返回网页正文。仅在需要深度阅读长文时开启,默认 false。开启会消耗大量 Token,请慎用", False)
End With
caller.AddTool("get_today_date",
"返回今天日期")
' 2. 实现工具执行逻辑
caller.ToolExecutor =
Function(ttoolName As String, targs As JObject) As String
Select Case ttoolName
Case "web_search"
' 构建请求体
Dim reqBody As New JObject()
reqBody("query") = targs("query").ToString()
' 解析可选参数
If targs.ContainsKey("count") Then
reqBody("count") = targs("count").ToObject(Of Integer)()
End If
If targs.ContainsKey("ori_question") Then
reqBody("ori_question") = targs("ori_question").ToString()
End If
Dim needContent As Boolean = False
If targs.ContainsKey("need_content") Then
needContent = targs("need_content").ToObject(Of Boolean)()
End If
reqBody("need_content") = needContent
' 发起搜索请求
Dim hc As New HttpClient("https://api.deepseek.com/v1/tools/search")
hc.SkipError = True
hc.ContentType = "application/json"
hc.Headers.Add("Authorization", "Bearer " & apiKey) '所有API使用相同的APIKey
hc.Content = reqBody.ToString()
Dim res As String = hc.GetData()
If hc.StatusCode <> 200 Then
Return "{""error"":""搜索请求失败,HTTP状态码:" & hc.StatusCode & """}"
End If
' 解析并清洗结果
Dim searchJo As JObject = JsonConvert.DeserializeObject(res)
Dim results As JArray = searchJo("results")
Dim simplified As New JArray()
For Each item As JObject In results
' 过滤低质结果(rerank_score >= 1.5),也可以考虑根据authority参数筛选权威结果
Dim score As Double = item("rerank_score").ToObject(Of Double)()
If score >= 1.5 Then
Dim entry As New JObject()
entry("title") = item("title") '网页标题
entry("url") = item("url") '网页地址
entry("date") = item("date") '网页发布时间
entry("snippet") = item("snippet") ' 短摘要,通常100~200字符,适用于前端展现
entry("summary") = item("summary") ' 长摘要,通常600-800字符,适合给模型阅读
' 如果 AI 明确要求了 need_content,则附带正文
If needContent Then
entry("content") = item("content")
End If
simplified.Add(entry)
End If
Next
If simplified.Count = 0 Then
Return "未找到相关的优质结果,请尝试调整搜索词或放宽限制。"
End If
Return simplified.ToString()
Case "get_today_date"
Return Date.Now.ToString("yyyy-MM-dd")
Case Else
Return "未知工具:" & ttoolName
End Select
End Function
' 3. 发起提问,让模型自动搜索
caller.UserMessage = "在GitHub搜索最近三个月中关于DeepSeek开源项目的消息,最多返回10条"
If caller.GetResult() Then
MessageBoxA.Show(caller.Result)
Else
MessageBoxA.Show(caller.Error)
End If
底层发生了什么?
当执行这段代码时,AI会判断需要联网搜索,并自动将你的问题转化为高级搜索语法。
AI 接收到“最近三个月”后,发现需要计算时间,于是先调用了 get_today_date
工具获取到了今天的日期(2026-10-22),然后自己在脑海中进行运算,得出了 3 个月前的日期(2026-07-22),最后才生成了这个带有 after:
参数的搜索请求。
它实际向ToolExecutor传递的参数大约是(假定今天是2026年10月22日):
{
"query": "DeepSeek 开源项目 site:github.com after:2026-07-22",
"count": 10,
"ori_question": "在GitHub搜索最近三个月中关于DeepSeek开源项目的消息,最多返回10条",
"need_content": false
}
注意看,AI 自动加上了 site:github.com 和
after:2026-07-22(当前日期前三个月),这就是我们在工具参数描述中教它的高级语法。count: 10 也精准对应了你问题里说的“最多返回10条”。
ToolExecutor 拿到这个请求,去搜索 API 获取摘要,过滤掉低分结果,再交还给 AI 组织语言,最终你就能看到一份精准的近期项目动态总结。
如果AI第一次没有搜索到符合条件的结果,会自动调整搜索词,进行多次搜索,这也是用AI进行搜索的优势之一。
如果你希望了解所有搜索细节,可以在代码的最后面加上一行:
Output.Show(caller.GetRequestBody().ToString())
让AI自动搜索新闻并做出分析判断
有了Search
API,我们就可以通过AI实现一些自动搜索数据(新闻)并做出分析判断的功能。
例如我们要在新浪网搜索关于宁德时代的新闻,并作出买卖决定,只需修改用户问题为:
' 3.
发起提问,让模型自动搜索
caller.UserMessage =
"在新浪网搜索最近一个月关于宁德时代的新闻,分析新闻内容,并做出买入还是卖出的判断"
If
caller.GetResult()
Then
MessageBoxA.Show(caller.Result)
Else
MessageBoxA.Show(caller.Error)
End
If