import gradio as gr import pandas as pd from langchain_community.utilities import GoogleSerperAPIWrapper from utils.helpers import map_results from .base import BaseInterfaceWrapper async def serper_search(query: str, api_key: str, max_results: int = 5) -> pd.DataFrame: """ Given a search query, returns the search results from Serper. Args: query (str): The search query. api_key (str): The API key for Serper Search. max_results (int, optional): The number of maximum results to return. Defaults to 5. Returns: dict: The search results from Serper Search Engine. Fields: headers (list): The headers of the columns in the search results. Contains "title", "link", "body". data (list): The data in the search results. Each row contains the title, link, and body of the search result. """ serper = GoogleSerperAPIWrapper( k=max_results, serper_api_key=api_key, ) results = await serper.aresults(query) results = results["organic"] mapping = { "title": "title", "link": "link", "snippet": "body", } results = await map_results(results, mapping) return results class SerperInterfaceWrapper(BaseInterfaceWrapper): def __init__(self): super().__init__( fn=serper_search, inputs=[ gr.Textbox(label="Search Query"), gr.Textbox(label="API Key", type="password"), gr.Slider( minimum=1, maximum=10, step=1, value=5, label="Number of Results" ), ], title="Serper Search", description="Search the web using Serper Search Engine.", )