All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.langchain4j.adaptiverag.WebSearchTool Maven / Gradle / Ivy

There is a newer version: 1.0-beta5
Show newest version
package dev.langchain4j.adaptiverag;

import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.WebSearchContentRetriever;
import dev.langchain4j.rag.query.Query;
import dev.langchain4j.web.search.WebSearchEngine;
import dev.langchain4j.web.search.tavily.TavilyWebSearchEngine;
import dev.langchain4j.rag.content.Content;
import lombok.Value;

import java.util.List;
import java.util.function.Function;

@Value( staticConstructor = "of" )
public class WebSearchTool implements Function> {
    String tavilyApiKey;

    @Override
    public List apply(String query) {
        WebSearchEngine webSearchEngine = TavilyWebSearchEngine.builder()
                .apiKey(tavilyApiKey) // get a free key: https://app.tavily.com/sign-in
                .build();

        ContentRetriever webSearchContentRetriever = WebSearchContentRetriever.builder()
                .webSearchEngine(webSearchEngine)
                .maxResults(3)
                .build();

        return webSearchContentRetriever.retrieve( new Query( query ) );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy