Spaces:
Running
Running
Commit
•
845e223
1
Parent(s):
02f2c44
Create google_search_agent.py
Browse files- google_search_agent.py +20 -0
google_search_agent.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from langchain_community.utilities import GoogleSerperAPIWrapper
|
3 |
+
|
4 |
+
|
5 |
+
class GoogleSearchAgent:
|
6 |
+
def __init__(self) -> None:
|
7 |
+
self._api = GoogleSerperAPIWrapper()
|
8 |
+
|
9 |
+
def run(self, query: str, source: str | None = None, limit: int = 3) -> pd.DataFrame:
|
10 |
+
if source is not None:
|
11 |
+
query += f" site:{source}"
|
12 |
+
|
13 |
+
self._api.k = limit
|
14 |
+
|
15 |
+
results = self._api.results(query)["organic"]
|
16 |
+
articles = [{"title": article["title"], "url": article["link"], "date": article.get("date", "")} for article in results]
|
17 |
+
|
18 |
+
df = pd.DataFrame(articles)
|
19 |
+
|
20 |
+
return df
|