Shrijeeth-Suresh commited on
Commit
9cc4f9f
·
1 Parent(s): 4a216fe

feat: add Brave search engine integration with API interface

Browse files
Files changed (2) hide show
  1. register_interfaces.py +3 -1
  2. search_engines/brave.py +56 -0
register_interfaces.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from search_engines.duckduckgo import DuckDuckGoInterfaceWrapper
2
  from search_engines.searxng import SearxNGInterfaceWrapper
3
  from search_engines.serpapi import SerpAPIInterfaceWrapper
@@ -9,8 +10,9 @@ def register_all_interfaces(wrapper):
9
  """
10
  Register all search engine interfaces with the provided AppWrapper instance.
11
  """
 
12
  wrapper.register_interface(DuckDuckGoInterfaceWrapper(), "DuckDuckGo Search")
13
- wrapper.register_interface(TavilyInterfaceWrapper(), "Tavily Search")
14
  wrapper.register_interface(SearxNGInterfaceWrapper(), "SearxNG Search")
15
  wrapper.register_interface(SerpAPIInterfaceWrapper(), "SerpAPI Search")
16
  wrapper.register_interface(SerperInterfaceWrapper(), "Serper Search")
 
 
1
+ from search_engines.brave import BraveInterfaceWrapper
2
  from search_engines.duckduckgo import DuckDuckGoInterfaceWrapper
3
  from search_engines.searxng import SearxNGInterfaceWrapper
4
  from search_engines.serpapi import SerpAPIInterfaceWrapper
 
10
  """
11
  Register all search engine interfaces with the provided AppWrapper instance.
12
  """
13
+ wrapper.register_interface(BraveInterfaceWrapper(), "Brave Search")
14
  wrapper.register_interface(DuckDuckGoInterfaceWrapper(), "DuckDuckGo Search")
 
15
  wrapper.register_interface(SearxNGInterfaceWrapper(), "SearxNG Search")
16
  wrapper.register_interface(SerpAPIInterfaceWrapper(), "SerpAPI Search")
17
  wrapper.register_interface(SerperInterfaceWrapper(), "Serper Search")
18
+ wrapper.register_interface(TavilyInterfaceWrapper(), "Tavily Search")
search_engines/brave.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ import gradio as gr
4
+ import pandas as pd
5
+ from langchain_community.tools import BraveSearch
6
+
7
+ from utils.helpers import map_results
8
+
9
+ from .base import BaseInterfaceWrapper
10
+
11
+
12
+ async def brave_search(query: str, api_key: str, max_results: int = 5) -> pd.DataFrame:
13
+ """
14
+ Given a search query, returns the search results from Brave.
15
+
16
+ Args:
17
+ query (str): The search query.
18
+ api_key (str): The API key for Brave Search.
19
+ max_results (int, optional): The number of maximum results to return. Defaults to 5.
20
+
21
+ Returns:
22
+ dict: The search results from Brave Search Engine. Fields:
23
+ headers (list): The headers of the columns in the search results. Contains "title", "link", "body".
24
+ data (list): The data in the search results. Each row contains the title, link, and body of the search result.
25
+ """
26
+ brave = BraveSearch.from_api_key(
27
+ api_key=api_key,
28
+ search_kwargs={
29
+ "max_results": max_results,
30
+ },
31
+ )
32
+ results = await brave.arun(query)
33
+ results = json.loads(results)
34
+ mapping = {
35
+ "title": "title",
36
+ "link": "link",
37
+ "snippet": "body",
38
+ }
39
+ results = await map_results(results, mapping)
40
+ return results
41
+
42
+
43
+ class BraveInterfaceWrapper(BaseInterfaceWrapper):
44
+ def __init__(self):
45
+ super().__init__(
46
+ fn=brave_search,
47
+ inputs=[
48
+ gr.Textbox(label="Search Query"),
49
+ gr.Textbox(label="API Key", type="password"),
50
+ gr.Slider(
51
+ minimum=1, maximum=10, step=1, value=5, label="Number of Results"
52
+ ),
53
+ ],
54
+ title="Brave Search",
55
+ description="Search the web using Brave Search Engine.",
56
+ )