Spaces:
Sleeping
Sleeping
Add GoogleSearchTool for querying Google via Gemini API
Browse files- tools/__init__.py +0 -0
- tools/google_search.py +62 -0
tools/__init__.py
ADDED
|
File without changes
|
tools/google_search.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Optional
|
| 2 |
+
from langchain_core.callbacks import CallbackManagerForToolRun
|
| 3 |
+
from langchain_core.tools import BaseTool
|
| 4 |
+
from langchain_core.tools.base import ArgsSchema
|
| 5 |
+
from pydantic import BaseModel, Field
|
| 6 |
+
from google.genai.types import Tool, GoogleSearch, GenerateContentConfig, Content, Part
|
| 7 |
+
from google import genai
|
| 8 |
+
|
| 9 |
+
class GoogleSearchInput(BaseModel):
|
| 10 |
+
query: str = Field(description="The query to search for")
|
| 11 |
+
|
| 12 |
+
class GoogleSearchTool(BaseTool):
|
| 13 |
+
name: str = "google_search"
|
| 14 |
+
description: str = "Useful for searching Google to find information about current events, data, or public information."
|
| 15 |
+
args_schema: Optional[ArgsSchema] = GoogleSearchInput
|
| 16 |
+
client: Any = None
|
| 17 |
+
model_id: str = "gemini-2.0-flash"
|
| 18 |
+
return_direct: bool = False
|
| 19 |
+
google_search_tool: Any = None
|
| 20 |
+
|
| 21 |
+
def __init__(self, api_key: Optional[str] = None, **kwargs):
|
| 22 |
+
super().__init__(**kwargs)
|
| 23 |
+
self.client = genai.Client(api_key=api_key)
|
| 24 |
+
self.google_search_tool = Tool(google_search=GoogleSearch())
|
| 25 |
+
|
| 26 |
+
def _run(self, query: str, run_manager: Optional[CallbackManagerForToolRun]=None) -> str:
|
| 27 |
+
"""Run the Google search with the query."""
|
| 28 |
+
try:
|
| 29 |
+
response = self.client.models.generate_content(
|
| 30 |
+
model=self.model_id,
|
| 31 |
+
contents=Content(
|
| 32 |
+
parts=[Part.from_text(text=f"Search for information about: {query}")],
|
| 33 |
+
role="user"
|
| 34 |
+
),
|
| 35 |
+
config=GenerateContentConfig(
|
| 36 |
+
tools=[self.google_search_tool],
|
| 37 |
+
response_modalities=["TEXT"]
|
| 38 |
+
)
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Extract search results
|
| 42 |
+
return response.text
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return f"Error performing Google search: {str(e)}"
|
| 45 |
+
|
| 46 |
+
async def _arun(self, query: str, run_manager: Optional[CallbackManagerForToolRun]=None) -> str:
|
| 47 |
+
"""Run the Google search with the query asynchronously."""
|
| 48 |
+
return self._run(query, run_manager=run_manager.get_sync())
|
| 49 |
+
|
| 50 |
+
# Example usage
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
from dotenv import load_dotenv
|
| 53 |
+
|
| 54 |
+
# Load API key from environment variables
|
| 55 |
+
load_dotenv()
|
| 56 |
+
|
| 57 |
+
# Create the Google Search runnable
|
| 58 |
+
google_search = GoogleSearchTool()
|
| 59 |
+
|
| 60 |
+
# Run a search query
|
| 61 |
+
result = google_search.invoke({"query": "What is the latest news about AI?"})
|
| 62 |
+
print(result)
|