From ff28ce0eff9347b7e4c827438f4c07d54588b310 Mon Sep 17 00:00:00 2001 From: suhassk-hash Date: Sat, 30 Nov 2024 02:26:05 +0530 Subject: [PATCH] Refactoring completely --- .../web/search/brave/BraveApi.java | 13 ++-- .../web/search/brave/BraveClient.java | 19 +++--- .../web/search/brave/BraveSearchDemo.java | 50 --------------- .../search/brave/BraveWebSearchEngine.java | 63 ++++++------------- .../search/brave/BraveWebSearchRequest.java | 23 ++++--- ...ponse.java => BraveWebSearchResponse.java} | 2 +- .../search/brave/BraveWebSearchEngineIT.java | 31 +++++++++ 7 files changed, 80 insertions(+), 121 deletions(-) delete mode 100644 web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveSearchDemo.java rename web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/{BraveResponse.java => BraveWebSearchResponse.java} (94%) create mode 100644 web-search-engines/langchain4j-web-search-engine-brave/src/test/java/dev/langchain4j/web/search/brave/BraveWebSearchEngineIT.java diff --git a/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveApi.java b/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveApi.java index 3b880d78bb9..409857324ea 100644 --- a/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveApi.java +++ b/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveApi.java @@ -5,19 +5,18 @@ import retrofit2.http.Header; import retrofit2.http.Headers; import retrofit2.http.Query; +import retrofit2.http.QueryMap; -public interface BraveApi { +import java.util.Map; + +interface BraveApi { @GET("res/v1/web/search") @Headers({ "Accept: application/json" }) - Call search( + Call search( @Header("X-Subscription-Token") String apiKey, - @Query("q") String query, - @Query("count") Integer count, - @Query("safesearch") String safeSearch, - @Query("resultFilter") String resultFilter, - @Query("freshness") String freshness + @QueryMap Map params ); } diff --git a/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveClient.java b/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveClient.java index f180b60047d..204d76b1dab 100644 --- a/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveClient.java +++ b/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveClient.java @@ -15,8 +15,10 @@ import java.io.IOException; import java.time.Duration; +import java.util.HashMap; +import java.util.Map; -public class BraveClient { +class BraveClient { private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper() .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE) @@ -45,17 +47,14 @@ public BraveClient(String baseUrl, Duration timeout) { this.braveApi = retrofit.create(BraveApi.class); } - public BraveResponse search(BraveWebSearchRequest braveWebSearchRequest) { + public BraveWebSearchResponse search(BraveWebSearchRequest braveWebSearchRequest) { try { - System.out.println(braveWebSearchRequest.getCount()+" "+braveWebSearchRequest.getQuery()+" "+braveWebSearchRequest.getCount()+" "+braveWebSearchRequest.getSafeSearch()+" "+braveWebSearchRequest.getResultFilter()+" "+braveWebSearchRequest.getFreshness()); - Response retrofitResponse = braveApi + Map params=new HashMap<>(braveWebSearchRequest.getOptionalParams()); + params.put("q",braveWebSearchRequest.getQuery()); + Response retrofitResponse = braveApi .search( braveWebSearchRequest.getApiKey(), - braveWebSearchRequest.getQuery(), - braveWebSearchRequest.getCount(), - braveWebSearchRequest.getSafeSearch(), - braveWebSearchRequest.getResultFilter(), - braveWebSearchRequest.getFreshness() + params ) .execute(); @@ -69,7 +68,7 @@ public BraveResponse search(BraveWebSearchRequest braveWebSearchRequest) { } } - private static RuntimeException toException(Response retrofitResponse) throws IOException { + private static RuntimeException toException(Response retrofitResponse) throws IOException { int code = retrofitResponse.code(); String body = retrofitResponse.errorBody().string(); String errorMessage = String.format("status code: %s; body: %s", code, body); diff --git a/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveSearchDemo.java b/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveSearchDemo.java deleted file mode 100644 index 153f4de49a4..00000000000 --- a/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveSearchDemo.java +++ /dev/null @@ -1,50 +0,0 @@ - package dev.langchain4j.web.search.brave; - - - import dev.langchain4j.web.search.WebSearchResults; - - import java.io.UnsupportedEncodingException; - - - - - import java.time.Duration; - - public class BraveSearchDemo { - public static void main(String[] args) throws UnsupportedEncodingException { - // Parameters for the BraveWebSearchEngine - String baseUrl = "https://api.search.brave.com"; - String apiKey = ""; // Replace with your API key - Duration timeout = Duration.ofSeconds(10); - Integer count = 10; - String safeSearch = "off"; - String resultFilter = "web"; - String freshness = ""; - - // Create an instance of BraveWebSearchEngine - BraveWebSearchEngine braveSearchEngine = new BraveWebSearchEngine( - baseUrl, - apiKey, - timeout, - count, - safeSearch, - resultFilter, - freshness - ); - - String query = "Places in hyderabad to visit "; - String encodedQuery = query.replace(" ", "%20"); - System.out.println("Encoded query: " + encodedQuery); - - - // Perform the search - try { - System.out.println("Searching for: " + encodedQuery); - WebSearchResults results = braveSearchEngine.search(query); - System.out.println("Search results: " + results); - } catch (Exception e) { - e.printStackTrace(); - System.out.println("Failed to fetch search results."); - } - } - } diff --git a/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveWebSearchEngine.java b/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveWebSearchEngine.java index 58d010e33e6..56a66f3e324 100644 --- a/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveWebSearchEngine.java +++ b/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveWebSearchEngine.java @@ -5,7 +5,10 @@ import dev.langchain4j.web.search.WebSearchResults; import java.time.Duration; +import java.util.HashMap; +import java.util.Map; +import static dev.langchain4j.internal.Utils.copyIfNotNull; import static dev.langchain4j.internal.Utils.getOrDefault; import static dev.langchain4j.internal.ValidationUtils.ensureNotBlank; @@ -15,10 +18,7 @@ * An implementation of a{@link WebSearchEngine} that uses * Brave Search API for performing web searches. */ -public class BraveWebSearchEngine implements WebSearchEngine { - - private final BraveClient braveClient; - +class BraveWebSearchEngine implements WebSearchEngine { /** * Constructs a new BraveSearchEngine with the specified parameters. * @@ -34,15 +34,9 @@ public class BraveWebSearchEngine implements WebSearchEngine { * - strict: Drops all adult content from search results. * * @param resultFilter Available result filter values are: - * - discussions - * - faq - * - infobox - * - news - * - query * - summarizer - * - videos * - web (set to default as web) - * - locations + * - image * Example result filter param: * result_filter=discussions,videos returns only discussions and videos responses. * Another example where only location results are required, set the result_filter param to result_filter=locations. @@ -60,12 +54,8 @@ public class BraveWebSearchEngine implements WebSearchEngine { private static final String DEFAULT_BASE_URL = "https://api.search.brave.com/"; private final String apiKey; - private final Integer count; - private final String safeSearch; - private final String resultFilter; - private final String freshness; - - + private final BraveClient braveClient; + private final Map optionalParams; /** * This constructor has a Builder method for building Brave Search Client with the default parameters: * baseUrl : It acts as the base URL where the requests are sent - a trailing route /search is added by the retrofit while request is made by user @@ -75,43 +65,26 @@ public class BraveWebSearchEngine implements WebSearchEngine { public BraveWebSearchEngine(String baseUrl, String apiKey, Duration timeout, - Integer count, - String safeSearch, - String resultFilter, - String freshness) { - + Map optionalParams + ) { this.braveClient = BraveClient.builder() .baseUrl(getOrDefault(baseUrl, DEFAULT_BASE_URL)) .timeout(getOrDefault(timeout, ofSeconds(10))) .build(); - + this.optionalParams = getOrDefault(copyIfNotNull(optionalParams), new HashMap<>()); this.apiKey = ensureNotBlank(apiKey, "apiKey"); - this.count = count; - this.safeSearch = safeSearch; - this.resultFilter = resultFilter; - this.freshness = freshness; - } - - @Override - public WebSearchResults search(final String query) { - return search(WebSearchRequest.builder() - .searchTerms(query) - .build()); } @Override public WebSearchResults search(final WebSearchRequest webSearchRequest) { - BraveWebSearchRequest braveWebSearchRequest = BraveWebSearchRequest.builder() - .apiKey(apiKey) - .count(count) - .safeSearch(safeSearch) - .resultFilter(resultFilter) - .freshness(freshness) - .query(webSearchRequest.searchTerms()) - .build(); - BraveResponse response = braveClient.search(braveWebSearchRequest); - System.out.println("Full Response: " + response); - if (response.getWeb() != null && response.getWeb().getResults() != null) { + + BraveWebSearchRequest request=BraveWebSearchRequest.builder() + .apiKey(apiKey) + .query(webSearchRequest.searchTerms()) + .optionalParams(optionalParams) + .build(); + BraveWebSearchResponse response=braveClient.search(request); + if (response.getWeb() != null && response.getWeb().getResults() != null) { response.getWeb().getResults().forEach(result -> { System.out.println("Title: " + result.getTitle()); System.out.println("URL: " + result.getUrl()); diff --git a/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveWebSearchRequest.java b/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveWebSearchRequest.java index a8244693480..265db9ac2bf 100644 --- a/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveWebSearchRequest.java +++ b/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveWebSearchRequest.java @@ -3,15 +3,22 @@ import lombok.Builder; import lombok.Getter; +import java.util.HashMap; +import java.util.Map; + @Getter -@Builder -public class BraveWebSearchRequest { +class BraveWebSearchRequest { - private String query; - private String apiKey; - private Integer count; - private String safeSearch; - private String resultFilter; - private String freshness; + private final String apiKey; + private final String query; + private final Map optionalParams; + @Builder + BraveWebSearchRequest(String apiKey, + String query, + Map optionalParams){ + this.apiKey = apiKey; + this.query = query; + this.optionalParams = new HashMap<>(optionalParams); + } } diff --git a/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveResponse.java b/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveWebSearchResponse.java similarity index 94% rename from web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveResponse.java rename to web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveWebSearchResponse.java index 90f5c0a34e7..a58ed3d01a7 100644 --- a/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveResponse.java +++ b/web-search-engines/langchain4j-web-search-engine-brave/src/main/java/dev/langchain4j/web/search/brave/BraveWebSearchResponse.java @@ -5,7 +5,7 @@ import java.util.List; @Data -public class BraveResponse { +class BraveWebSearchResponse { private Query query; private String type; diff --git a/web-search-engines/langchain4j-web-search-engine-brave/src/test/java/dev/langchain4j/web/search/brave/BraveWebSearchEngineIT.java b/web-search-engines/langchain4j-web-search-engine-brave/src/test/java/dev/langchain4j/web/search/brave/BraveWebSearchEngineIT.java new file mode 100644 index 00000000000..35a060dbe99 --- /dev/null +++ b/web-search-engines/langchain4j-web-search-engine-brave/src/test/java/dev/langchain4j/web/search/brave/BraveWebSearchEngineIT.java @@ -0,0 +1,31 @@ +package dev.langchain4j.web.search.brave; + +import dev.langchain4j.web.search.WebSearchEngine; +import dev.langchain4j.web.search.WebSearchEngineIT; +import dev.langchain4j.web.search.WebSearchRequest; + +import java.time.Duration; +import java.util.HashMap; +import java.util.Map; + +public class BraveWebSearchEngineIT extends WebSearchEngineIT { + + @Override + protected WebSearchEngine searchEngine() { + // Parameters for the BraveWebSearchEngine + String baseUrl = "https://api.search.brave.com"; + String apiKey = ""; // Replace with your API key + Duration timeout = Duration.ofSeconds(10); + Integer count = WebSearchEngineIT.EXPECTED_MAX_RESULTS; + String safeSearch = "off"; + String resultFilter = "web"; + String freshness = ""; + Map params = new HashMap<>(); + params.put("count",count); + params.put("safeSearch",safeSearch); + params.put("resultFilter",resultFilter); + params.put("freshness",freshness); + return new BraveWebSearchEngine(baseUrl,apiKey,timeout,params); + } +} +