Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(google_serper_api): migrate to new tool mode implementation #5446

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 56 additions & 23 deletions src/backend/base/langflow/components/tools/google_serper_api.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,73 @@
from langchain_community.utilities.google_serper import GoogleSerperAPIWrapper

from langflow.base.langchain_utilities.model import LCToolComponent
from langflow.field_typing import Tool
from langflow.inputs import IntInput, MultilineInput, SecretStrInput
from langflow.schema import Data
from langflow.custom import Component
from langflow.io import IntInput, MultilineInput, Output, SecretStrInput
from langflow.schema import DataFrame
from langflow.schema.message import Message


class GoogleSerperAPIComponent(LCToolComponent):
class GoogleSerperAPIComponent(Component):
display_name = "Google Serper API"
description = "Call the Serper.dev Google Search API."
name = "GoogleSerperAPI"
icon = "Google"

inputs = [
SecretStrInput(name="serper_api_key", display_name="Serper API Key", required=True),
SecretStrInput(
name="serper_api_key",
display_name="Serper API Key",
required=True,
),
MultilineInput(
name="input_value",
display_name="Input",
tool_mode=True,
),
IntInput(
name="k",
display_name="Number of results",
value=4,
required=True,
),
]

outputs = [
Output(
display_name="Results",
name="results",
type_=DataFrame,
method="search_serper",
),
IntInput(name="k", display_name="Number of results", value=4, required=True),
]

def run_model(self) -> Data | list[Data]:
wrapper = self._build_wrapper()
results = wrapper.results(query=self.input_value)
list_results = results.get("organic", [])
data = [Data(data=result, text=result["snippet"]) for result in list_results]
self.status = data
return data

def build_tool(self) -> Tool:
wrapper = self._build_wrapper()
return Tool(
name="google_search",
description="Search Google for recent results.",
func=wrapper.run,
)
def search_serper(self) -> DataFrame:
try:
wrapper = self._build_wrapper()
results = wrapper.results(query=self.input_value)
list_results = results.get("organic", [])

# Convert results to DataFrame using list comprehension
df_data = [
{
"title": result.get("title", ""),
"link": result.get("link", ""),
"snippet": result.get("snippet", ""),
}
for result in list_results
]

return DataFrame(df_data)
except (ValueError, KeyError, ConnectionError) as e:
error_message = f"Error occurred while searching: {e!s}"
self.status = error_message
return DataFrame({"error": [error_message]})

def text_search_serper(self) -> Message:
search_results = self.search_serper()
text_result = search_results.to_string(index=False) if not search_results.empty else "No results found."
return Message(text=text_result)

def _build_wrapper(self):
return GoogleSerperAPIWrapper(serper_api_key=self.serper_api_key, k=self.k)

def build(self):
return self.search_serper
Loading