Skip to content

Commit 4878f6f

Browse files
committed
Add search api engine integration class
Add documentation and example action invoke Add steps to perform searches on various engines using SearchApi
1 parent 8d41ccd commit 4878f6f

File tree

5 files changed

+83
-8
lines changed

5 files changed

+83
-8
lines changed

dev/Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
```bash
4141
export AZURE_OPENAI_API_KEY="<your key>" # required AZURE OPENAI USAGE
4242
export SERPER_API_KEY="<your key>" # required for Google Serper API
43+
export SEARCHAPI_API_KEY="<your key>" # required to perform web searches on any search engines.
4344
python usecases/ProfAgent.py
4445
```
4546

docs/components/action/tools.md

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,35 @@ admin = Admin(
2525
)
2626
```
2727

28-
### 2. Serper Search Tool
28+
### 2. SearchApi Search
29+
30+
[SearchApi.io](https://searchapi.io/) provides a real-time API to access search results from Google (default), Google Scholar, Bing, Baidu, and other search engines. Any existing or upcoming SERP engine that returns `organic_results` is supported. The default web search engine is `google`, but it can be changed to `bing`, `baidu`, `google_news`, `bing_news`, `google_scholar`, `google_patents`, and others.
31+
32+
#### Setup API
33+
34+
```python
35+
import os
36+
37+
os.environ['SEARCHAPI_API_KEY'] = "<replace-with-your-api-key>"
38+
os.environ['SEARCHAPI_ENGINE'] = "bing" # defaults to google.
39+
```
40+
41+
Get your API key by creating an account or logging into your account on [SearchApi](https://searchapi.io/).
42+
43+
```python
44+
from openagi.actions.tools.searchapi_search import SearchApiSearch
45+
from openagi.agent import Admin
46+
from openagi.llms.openai import OpenAIModel
47+
from openagi.planner.task_decomposer import TaskPlanner
48+
49+
admin = Admin(
50+
llm = llm,
51+
actions=[SearchApiSearch],
52+
planner=TaskPlanner(),
53+
)
54+
```
55+
56+
### 3. Serper Search Tool
2957

3058
Serper is a low-cost Google Search API that can be used to add answer box, knowledge graph, and organic results data from Google Search. This tool is mainly helps user to query the Google results with less throughput and latency.&#x20;
3159

@@ -52,7 +80,7 @@ admin = Admin(
5280
)
5381
```
5482

55-
### 3. Google Serp API Search
83+
### 4. Google Serp API Search
5684

5785
Serp API is yet another solution to integrate search data. SERP stands for _Search Engine Results Page_. It refers to the page displayed by a search engine in response to a user's query.
5886

@@ -79,7 +107,7 @@ admin = Admin(
79107
)
80108
```
81109

82-
### 4. Github Search Tool
110+
### 5. Github Search Tool
83111

84112
The Github SearchTool is used for retrieving information from Github repositories using natural language queries. This tool provides functionality for querying Github repositories for various information, such as code changes, commits, active pull requests, issues, etc., using natural language input. It is designed to be used as part of a larger AI-driven agent system.
85113

@@ -106,7 +134,7 @@ admin = Admin(
106134
)
107135
```
108136

109-
### 5. YouTube Search Tool
137+
### 6. YouTube Search Tool
110138

111139
The YouTube Search tool allows users to search for videos on YouTube using natural language queries. This tool retrieves relevant video content based on user-defined search parameters, making it easier to find specific videos or topics of interest.
112140

@@ -132,7 +160,7 @@ admin = Admin(
132160
)
133161
```
134162

135-
### 6. Tavily QA Search Tool
163+
### 7. Tavily QA Search Tool
136164

137165
The Tavily QA Search tool is designed to provide answers to user queries by fetching data from various online sources. This tool enhances the capability of the agent to retrieve precise information and answer questions effectively.
138166

@@ -166,7 +194,7 @@ admin = Admin(
166194
)
167195
```
168196

169-
### 7. Exa Search Tool
197+
### 8. Exa Search Tool
170198

171199
The Exa Search tool allows users to query the Exa API to retrieve relevant responses based on user-defined questions. This tool is particularly useful for extracting information and insights from various data sources using natural language queries.
172200

@@ -202,7 +230,7 @@ admin = Admin(
202230
)
203231
```
204232

205-
### 8. Unstructured PDF Loader Tool
233+
### 9. Unstructured PDF Loader Tool
206234

207235
The Unstructured PDF Loader tool is designed to extract content, including metadata, from PDF files. It utilizes the Unstructured library to partition the PDF and chunk the content based on titles. This tool is useful for processing large volumes of PDF documents and making their contents accessible for further analysis.
208236

docs/use-cases/jobsearch-agent.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ It utilize various tools for internet search and document comparison to fulfill
66

77
First, we need to import the necessary modules. Each module serves a specific purpose in our script. We utilize various tools for internet search and document comparison to fulfill the agent's task. Here’s what each import does:
88

9-
* `GoogleSerpAPISearch` and `DuckDuckGoSearch` are tools for performing web searches.
9+
* `SearchApiSearch`, `GoogleSerpAPISearch` and `DuckDuckGoSearch` are tools for performing web searches.
1010
* `Admin` manages the overall execution of tasks.
1111
* `AzureChatOpenAIModel` is used to configure the large language model from Azure.
1212
* `Memory` is for maintaining context during the agent's operations.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import logging
2+
import os
3+
import requests
4+
from urllib.parse import urlencode
5+
from typing import Any
6+
7+
from pydantic import Field
8+
9+
from openagi.actions.base import BaseAction
10+
from openagi.exception import OpenAGIException
11+
12+
13+
class SearchApiSearch(BaseAction):
14+
"""SearchApi.io provides a real-time API to access search results from Google (default), Google Scholar, Bing, Baidu, and other search engines."""
15+
query: str = Field(
16+
..., description="User query of type string used to fetch web search results from a search engine."
17+
)
18+
19+
def execute(self):
20+
base_url = "https://www.searchapi.io/api/v1/search"
21+
searchapi_api_key = os.environ["SEARCHAPI_API_KEY"]
22+
engine = os.environ.get("SEARCHAPI_ENGINE") or "google"
23+
search_dict = {
24+
"q": self.query,
25+
"engine": engine,
26+
"api_key": searchapi_api_key
27+
}
28+
logging.debug(f"{search_dict=}")
29+
url = f"{base_url}?{urlencode(search_dict)}"
30+
response = requests.request("GET", url)
31+
json_response = response.json()
32+
33+
if not json_response:
34+
raise OpenAGIException(f"Unable to generate result for the query {self.query}")
35+
36+
logging.debug(json_response)
37+
38+
organic_results = json_response.get("organic_results", [])
39+
40+
meta_data = ""
41+
for organic_result in organic_results:
42+
meta_data += f"CONTEXT: {organic_result['title']} \ {organic_result['snippet']}"
43+
meta_data += f"Reference URL: {organic_result['link']}\n"
44+
return meta_data

src/openagi/utils/tool_list.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from openagi.actions.tools import (
33
ddg_search,
44
document_loader,
5+
searchapi_search,
56
serp_search,
67
serper_search,
78
webloader,
@@ -14,6 +15,7 @@
1415
modules = [
1516
document_loader,
1617
ddg_search,
18+
searchapi_search,
1719
serp_search,
1820
serper_search,
1921
webloader,

0 commit comments

Comments
 (0)