Skip to content

Commit

Permalink
Refactoring completely
Browse files Browse the repository at this point in the history
  • Loading branch information
Suhas-Koheda committed Nov 29, 2024
1 parent 548d979 commit ff28ce0
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<BraveResponse> search(
Call<BraveWebSearchResponse> 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<String,Object> params
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<BraveResponse> retrofitResponse = braveApi
Map<String,Object> params=new HashMap<>(braveWebSearchRequest.getOptionalParams());
params.put("q",braveWebSearchRequest.getQuery());
Response<BraveWebSearchResponse> retrofitResponse = braveApi
.search(
braveWebSearchRequest.getApiKey(),
braveWebSearchRequest.getQuery(),
braveWebSearchRequest.getCount(),
braveWebSearchRequest.getSafeSearch(),
braveWebSearchRequest.getResultFilter(),
braveWebSearchRequest.getFreshness()
params
)
.execute();

Expand All @@ -69,7 +68,7 @@ public BraveResponse search(BraveWebSearchRequest braveWebSearchRequest) {
}
}

private static RuntimeException toException(Response<BraveResponse> retrofitResponse) throws IOException {
private static RuntimeException toException(Response<BraveWebSearchResponse> retrofitResponse) throws IOException {
int code = retrofitResponse.code();
String body = retrofitResponse.errorBody().string();
String errorMessage = String.format("status code: %s; body: %s", code, body);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,10 +18,7 @@
* An implementation of a{@link WebSearchEngine} that uses
* <a href="https://brave.com/search/api//">Brave Search API</a> 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.
*
Expand All @@ -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.
Expand All @@ -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<String,Object> 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
Expand All @@ -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<String, Object> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String,Object> optionalParams;

@Builder
BraveWebSearchRequest(String apiKey,
String query,
Map<String,Object> optionalParams){
this.apiKey = apiKey;
this.query = query;
this.optionalParams = new HashMap<>(optionalParams);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.List;

@Data
public class BraveResponse {
class BraveWebSearchResponse {

private Query query;
private String type;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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);
}
}

0 comments on commit ff28ce0

Please sign in to comment.