Skip to content

Commit 5585eea

Browse files
committed
Add search result limit and new line after each context url pair
1 parent e006978 commit 5585eea

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/openagi/actions/tools/searchapi_search.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,19 @@ class SearchApiSearch(BaseAction):
1515
query: str = Field(
1616
..., description="User query of type string used to fetch web search results from a search engine."
1717
)
18-
18+
max_results: Any = Field(
19+
default=10,
20+
description="Total results, an integer, to be executed from the search. Defaults to 10",
21+
)
22+
23+
@field_validator("max_results")
24+
@classmethod
25+
def actions_validator(cls, max_results):
26+
if not max_results or not isinstance(max_results, int):
27+
logging.warning("Max Results set to 10(default).")
28+
max_results = 10
29+
return max_results
30+
1931
def execute(self):
2032
base_url = "https://www.searchapi.io/api/v1/search"
2133
searchapi_api_key = os.environ["SEARCHAPI_API_KEY"]
@@ -39,7 +51,7 @@ def execute(self):
3951
json_response.get("organic_results", []), key=lambda x: x.get("position", 0)
4052
)
4153
meta_data = ""
42-
for info in results:
54+
for info in results[:self.max_results]:
4355
meta_data += f"CONTEXT: {info['title']} \ {info['snippet']}"
44-
meta_data += f"Reference URL: {info['link']}"
56+
meta_data += f"Reference URL: {info['link']}\n"
4557
return meta_data

0 commit comments

Comments
 (0)