SpringBoot 整合 LangChain4j 与 Tavily 实现联网搜索
要实现联网搜索功能,获取 API Key 是关键的第一步。建议按以下步骤操作:访问 Tavily 官网注册账号,进入 API Playground 填写申请信息,返回 Overview 页面即可查看生成的 API 密钥。

获得 API 密钥后,即可在项目中完成相应配置。可以通过 max-results 参数来控制最多搜索结果数量。
langchain4j:
open-ai:
chat-model:
base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
api-key: ${QWEN_API_KEY}
model-name: qwen-plus
log-responses: true
log-requests: true
strict-tools: true
streaming-chat-model:
base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
api-key: ${QWEN_API_KEY}
model-name: qwen-plus
log-responses: true
log-requests: true
strict-tools: true
embedding-model:
base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
api-key: ${EMBEDDING_API_KEY}
model-name: text-embedding-v4
log-requests: true
log-responses: true
max-segments-per-batch: 10
web-search-engine:
tavily:
api-key: ${TAVILY_API_KEY}
# 从环境变量读取 API Key
max-results: 3
# 最多返回 3 条搜索结果
随后新建一个 WebSearchConfig 类来管理配置。LangChain4j 提供了 WebSearchEngine 来处理联网搜索的需求,记得引入对应的依赖。
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-web-search-engine-tavily</artifactId>
<version>1.0.0-beta3</version>
</dependency>
网络搜索引擎可以作为 RAG 管道中的 ContentRetriever 使用,也可以作为 @Tool 调用。我们在配置类中使用 @Value 注解将配置注入。
@Value("${langchain4j.web-search-engine.tavily.api-key}")
private String tavilyApiKey;
@Value("${langchain4j.web-search-engine.tavily.max-results:3}")
private Integer maxResults;
接着完成 WebSearchEngine Bean 的创建,集中管理 Tavily 搜索引擎配置和统一 API 密钥等参数。
@Bean
public WebSearchEngine webSearchEngine() {
return TavilyWebSearchEngine.builder()
.apiKey(tavilyApiKey)
.build();
}
接下来是 ContentRetriever 内容检索器接口,核心作用是从数据源(如网络)中,根据查询语义精准召回相关内容片段,为大模型提供上下文支持。这里专门通过 WebSearchEngine 从网络检索相关内容,返回给大模型辅助回答。
@Bean
public ContentRetriever webContentRetriever(WebSearchEngine webSearchEngine) {
return WebSearchContentRetriever.builder()
.webSearchEngine(webSearchEngine)
.maxResults(maxResults)
.build();
}
完整配置代码如下:
package com.example.kui.config;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.WebSearchContentRetriever;
import dev.langchain4j.web.search.WebSearchEngine;
import dev.langchain4j.web.search.tavily.TavilyWebSearchEngine;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WebSearchConfig {
@Value("${langchain4j.web-search-engine.tavily.api-key}")
private String tavilyApiKey;
@Value("${langchain4j.web-search-engine.tavily.max-results:3}")
private Integer maxResults;
@Bean
public WebSearchEngine webSearchEngine() {
return TavilyWebSearchEngine.builder()
.apiKey(tavilyApiKey)
.build();
}
@Bean
public ContentRetriever webContentRetriever(WebSearchEngine webSearchEngine) {
return WebSearchContentRetriever.builder()
.webSearchEngine(webSearchEngine)
.maxResults(maxResults)
.build();
}
}
我们可以创建一个专门用于网络检索的 Agent 接口,命名为 WebSearchAgent。借助 @AiService 注解配置好联网搜索的 ContentRetriever,名称应该与类名相同。
@AiService(
wiringMode = EXPLICIT,
chatModel = "openAiChatModel",
streamingChatModel = "openAiStreamingChatModel",
chatMemory = "chatMemory",
chatMemoryProvider = "chatMemoryProvider",
contentRetriever = "webContentRetriever"
)
public interface WebSearchAgent {
@SystemMessage("根据检索结果回答")
String chat(@UserMessage String message);
}
此时我们只需要调用 WebSearchAgent 的 chat 方法就能得到网络搜索结果了。
@PostMapping("/search")
public String webSearch(@RequestBody String userMessage) {
return webSearchAgent.chat(userMessage);
}
实际运行后,查看日志输出是否符合预期。



