-
-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EXASearchTool class and integrate with AI agents
- Introduced a new `EXASearchTool` class to interact with the EXA Search API, allowing AI agents to perform searches and retrieve results. - Implemented methods for running queries and parsing results into a readable format. - Created a `SearchAgent` to utilize the `EXASearchTool` for searching information about the 'AI Agents Framework'. - Added a task definition for the `SearchAgent` to demonstrate the tool's functionality. - Updated documentation to include the new `tools_class` and provide a comprehensive guide on using class-based tools with AI agents.
- Loading branch information
1 parent
9b30d1a
commit 2a14508
Showing
9 changed files
with
584 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from praisonaiagents import Agent, Task, PraisonAIAgents | ||
import os | ||
import requests | ||
from typing import Any, Dict, List, Optional | ||
from pydantic import BaseModel, Field | ||
|
||
class EXASearchTool(BaseModel): | ||
"""Wrapper for EXA Search API.""" | ||
search_url: str = "https://api.exa.ai/search" | ||
headers: Dict = { | ||
"accept": "application/json", | ||
"content-type": "application/json", | ||
} | ||
max_results: Optional[int] = None | ||
|
||
def run(self, query: str) -> str: | ||
"""Run query through EXA and return concatenated results. | ||
Args: | ||
query (str): The search query to use | ||
Returns: | ||
str: The concatenated search results | ||
""" | ||
payload = { | ||
"query": query, | ||
"type": "magic", | ||
} | ||
|
||
headers = self.headers.copy() | ||
headers["x-api-key"] = os.environ['EXA_API_KEY'] | ||
|
||
response = requests.post(self.search_url, json=payload, headers=headers) | ||
results = response.json() | ||
|
||
if 'results' in results: | ||
return self._parse_results(results['results']) | ||
return "" | ||
|
||
def results(self, query: str, max_results: Optional[int] = None) -> List[Dict[str, Any]]: | ||
"""Run query through EXA and return metadata. | ||
Args: | ||
query (str): The search query to use | ||
max_results (Optional[int]): Maximum number of results to return | ||
Returns: | ||
List[Dict[str, Any]]: List of result dictionaries | ||
""" | ||
payload = { | ||
"query": query, | ||
"type": "magic", | ||
} | ||
|
||
headers = self.headers.copy() | ||
headers["x-api-key"] = os.environ['EXA_API_KEY'] | ||
|
||
response = requests.post(self.search_url, json=payload, headers=headers) | ||
results = response.json() | ||
|
||
if 'results' in results: | ||
return results['results'][:max_results] if max_results else results['results'] | ||
return [] | ||
|
||
def _parse_results(self, results: List[Dict[str, Any]]) -> str: | ||
"""Parse results into a readable string format. | ||
Args: | ||
results (List[Dict[str, Any]]): List of result dictionaries | ||
Returns: | ||
str: Formatted string of results | ||
""" | ||
strings = [] | ||
for result in results: | ||
try: | ||
strings.append('\n'.join([ | ||
f"Title: {result['title']}", | ||
f"Score: {result['score']}", | ||
f"Url: {result['url']}", | ||
f"ID: {result['id']}", | ||
"---" | ||
])) | ||
except KeyError: | ||
continue | ||
|
||
content = '\n'.join(strings) | ||
return f"\nSearch results: {content}\n" | ||
|
||
# Create an agent with the tool | ||
agent = Agent( | ||
name="SearchAgent", | ||
role="Research Assistant", | ||
goal="Search for information about 'AI Agents Framework'", | ||
backstory="I am an AI assistant that can search GitHub.", | ||
tools=[EXASearchTool], | ||
self_reflect=False | ||
) | ||
|
||
# Create tasks to demonstrate both tools | ||
task = Task( | ||
name="search_task", | ||
description="Search for information about 'AI Agents Framework'", | ||
expected_output="Information about AI Agents Framework", | ||
agent=agent | ||
) | ||
|
||
# Create and start the workflow | ||
agents = PraisonAIAgents( | ||
agents=[agent], | ||
tasks=[task], | ||
verbose=True | ||
) | ||
|
||
agents.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from praisonaiagents import Agent, Task, PraisonAIAgents | ||
import os | ||
import requests | ||
from typing import Any, Dict, List, Optional | ||
from pydantic import BaseModel, Field | ||
|
||
class EXASearchTool(BaseModel): | ||
"""Wrapper for EXA Search API.""" | ||
search_url: str = "https://api.exa.ai/search" | ||
headers: Dict = { | ||
"accept": "application/json", | ||
"content-type": "application/json", | ||
} | ||
max_results: Optional[int] = None | ||
|
||
def run(self, query: str) -> str: | ||
"""Run query through EXA and return concatenated results. | ||
Args: | ||
query (str): The search query to use | ||
Returns: | ||
str: The concatenated search results | ||
""" | ||
payload = { | ||
"query": query, | ||
"type": "magic", | ||
} | ||
|
||
headers = self.headers.copy() | ||
headers["x-api-key"] = os.environ['EXA_API_KEY'] | ||
|
||
response = requests.post(self.search_url, json=payload, headers=headers) | ||
results = response.json() | ||
|
||
if 'results' in results: | ||
return self._parse_results(results['results']) | ||
return "" | ||
|
||
def results(self, query: str, max_results: Optional[int] = None) -> List[Dict[str, Any]]: | ||
"""Run query through EXA and return metadata. | ||
Args: | ||
query (str): The search query to use | ||
max_results (Optional[int]): Maximum number of results to return | ||
Returns: | ||
List[Dict[str, Any]]: List of result dictionaries | ||
""" | ||
payload = { | ||
"query": query, | ||
"type": "magic", | ||
} | ||
|
||
headers = self.headers.copy() | ||
headers["x-api-key"] = os.environ['EXA_API_KEY'] | ||
|
||
response = requests.post(self.search_url, json=payload, headers=headers) | ||
results = response.json() | ||
|
||
if 'results' in results: | ||
return results['results'][:max_results] if max_results else results['results'] | ||
return [] | ||
|
||
def _parse_results(self, results: List[Dict[str, Any]]) -> str: | ||
"""Parse results into a readable string format. | ||
Args: | ||
results (List[Dict[str, Any]]): List of result dictionaries | ||
Returns: | ||
str: Formatted string of results | ||
""" | ||
strings = [] | ||
for result in results: | ||
try: | ||
strings.append('\n'.join([ | ||
f"Title: {result['title']}", | ||
f"Score: {result['score']}", | ||
f"Url: {result['url']}", | ||
f"ID: {result['id']}", | ||
"---" | ||
])) | ||
except KeyError: | ||
continue | ||
|
||
content = '\n'.join(strings) | ||
return f"\nSearch results: {content}\n" | ||
|
||
# Create an agent with the tool | ||
agent = Agent( | ||
name="SearchAgent", | ||
role="Research Assistant", | ||
goal="Search for information about 'AI Agents Framework'", | ||
backstory="I am an AI assistant that can search GitHub.", | ||
tools=[EXASearchTool], | ||
self_reflect=False | ||
) | ||
|
||
# Create tasks to demonstrate both tools | ||
task = Task( | ||
name="search_task", | ||
description="Search for information about 'AI Agents Framework'", | ||
expected_output="Information about AI Agents Framework", | ||
agent=agent | ||
) | ||
|
||
# Create and start the workflow | ||
agents = PraisonAIAgents( | ||
agents=[agent], | ||
tasks=[task], | ||
verbose=True | ||
) | ||
|
||
agents.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.