跳到主要内容

SearchApi

SearchApi 是一个实时 SERP API。你可以使用它在 Google、Google News、Bing、Bing News、百度、Google Scholar 或任何返回自然搜索结果的引擎中执行搜索。

用法

依赖设置

在项目的 pom.xml 中添加以下依赖:

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-web-search-engine-searchapi</artifactId>
<version>1.13.0-beta23</version>
</dependency>

或在项目的 build.gradle 中:

implementation 'dev.langchain4j:langchain4j-web-search-engine-searchapi:1.13.0-beta23'

示例代码

import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiChatModelName;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.web.search.WebSearchTool;
import dev.langchain4j.web.search.searchapi.SearchApiEngine;
import dev.langchain4j.web.search.searchapi.SearchApiWebSearchEngine;

public class SearchApiTool {

interface Assistant {
@SystemMessage({
"You are a web search support agent.",
"If there is any event that has not happened yet",
"You MUST create a web search request with user query and",
"use the web search tool to search the web for organic web results.",
"Include the source link in your final response."
})
String answer(String userMessage);
}

private static final String SEARCHAPI_API_KEY = "YOUR_SEARCHAPI_KEY";
private static final String OPENAI_API_KEY = "YOUR_OPENAI_KEY";

public static void main(String[] args) {
Map<String, Object> optionalParameters = new HashMap<>();
optionalParameters.put("gl", "us");
optionalParameters.put("hl", "en");
optionalParameters.put("google_domain", "google.com");

SearchApiWebSearchEngine searchEngine = SearchApiWebSearchEngine.builder()
.apiKey(SEARCHAPI_API_KEY)
.engine("google")
.optionalParameters(optionalParameters)
.build();
ChatModel chatModel = OpenAiChatModel.builder()
.apiKey(OPENAI_API_KEY)
.modelName(OpenAiChatModelName.GPT_3_5_TURBO)
.logRequests(true)
.build();

WebSearchTool webTool = WebSearchTool.from(searchEngine);

Assistant assistant = AiServices.builder(Assistant.class)
.chatModel(chatModel)
.tools(webTool)
.build();

String answer = assistant.answer("My family is coming to visit me in Madrid next week, list the best tourist activities suitable for the whole family");
System.out.println(answer);
/*
Here are some of the best tourist activities suitable for the whole family in Madrid:

1. **Parque del Retiro** - A beautiful public park where families can enjoy nature and various activities.
2. **Prado Museum** - A renowned art museum that can be fascinating for both adults and children.
3. **Mercado de San Miguel** - A market where you can explore and taste delicious Spanish food.
4. **Royal Palace** - Explore the grandeur of the Royal Palace of Madrid.
5. **Plaza Mayor** and **Puerta del Sol** - Historic squares with a vibrant atmosphere.
6. **Santiago Bernabeu Stadium** - Perfect for sports enthusiasts and soccer fans.
7. **Gran Via** - A famous street for shopping, entertainment, and sightseeing.
8. **National Archaeological Museum** - Discover Spain's rich history through archaeological artifacts.
9. **Templo de Debod** - An ancient Egyptian temple in the heart of Madrid.
*/
}
}

LangChain4j 中可用的引擎

SearchApi 引擎是否可用
Google Web Search
Google News
Bing
Bing News
Baidu

任何返回 organic_results 数组且有机结果包含 titlelinksnippet 的引擎均受此库支持,即使未列出也可使用。