-
Notifications
You must be signed in to change notification settings - Fork 75
tools deprecation and new format support #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e2a9adf
tools deprecation and new format support
lucifertrj a3c313f
Add tool for arxiv (#88)
AdvH039 81994ee
enable advanced=True for Google search
lucifertrj a36b1ed
remote action changes
lucifertrj 10639d4
add ConfigurableAction in base
lucifertrj 55e23d4
yahoo tool added
lucifertrj aa4eb81
pubmed tool added
lucifertrj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,57 @@ | ||
| from openagi.actions.base import ConfigurableAction | ||
| from pydantic import Field | ||
| from openagi.exception import OpenAGIException | ||
| from typing import ClassVar, Dict, Any | ||
|
|
||
| try: | ||
| import arxiv | ||
| except ImportError: | ||
| raise OpenAGIException("Install arxiv with cmd `pip install arxiv`") | ||
|
|
||
|
|
||
| class ArxivSearch(ConfigurableAction): | ||
| """ | ||
| Arxiv Search is a tool used to search articles in Physics, Mathematics, Computer Science, Quantitative Biology, Quantitative Finance, and Statistics | ||
| """ | ||
| query: str = Field(..., description="User query or question") | ||
| max_results: int = Field(10, description="Total results, in int, to be executed from the search. Defaults to 10.") | ||
|
|
||
| def execute(self): | ||
| search = arxiv.Search( | ||
| query = self.query, | ||
| max_results = self.max_results, | ||
| ) | ||
| client = arxiv.Client() | ||
| results = client.results(search) | ||
| meta_data = "" | ||
| for result in results: | ||
| meta_data += f"title : {result.title}\n " | ||
| meta_data += f"summary : {result.summary}\n " | ||
| meta_data += f"published : {result.published}\n " | ||
| meta_data += f"authors : {result.authors}\n " | ||
| meta_data += f"pdf_url : {result.pdf_url}\n " | ||
| meta_data += f"entry_id : {result.entry_id}\n\n " | ||
| return meta_data.strip() | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,39 +1,64 @@ | ||
| from openagi.actions.base import BaseAction | ||
| from typing import Any | ||
| from openagi.actions.base import ConfigurableAction | ||
| from langchain_community.document_loaders import TextLoader | ||
| from langchain_community.document_loaders.csv_loader import CSVLoader | ||
| from langchain_community.document_loaders.pdf import PyPDFLoader | ||
| from pydantic import Field | ||
|
|
||
| class TextLoaderTool(ConfigurableAction): | ||
| """Load content from a text file. | ||
|
|
||
| This action loads and processes content from .txt files, combining | ||
| metadata and content into a single context string. | ||
| """ | ||
|
|
||
| def execute(self) -> str: | ||
| file_path: str = self.get_config('filename') | ||
| loader = TextLoader(file_path=file_path) | ||
| documents = loader.load() | ||
|
|
||
| if not documents: | ||
| return "" | ||
|
|
||
| page_content = documents[0].page_content | ||
| source = documents[0].metadata["source"] | ||
| return f"{source} {page_content}" | ||
|
|
||
| class DocumentLoader(BaseAction): | ||
| """Use this Action to extract content from documents""" | ||
| class PDFLoaderTool(ConfigurableAction): | ||
| """Load content from a PDF file. | ||
|
|
||
| This action loads and processes content from .pdf files, combining | ||
| metadata and content into a single context string. | ||
| """ | ||
|
|
||
| def execute(self) -> str: | ||
| file_path: str = self.get_config('filename') | ||
| loader = PyPDFLoader(file_path=file_path) | ||
| documents = loader.load() | ||
|
|
||
| if not documents: | ||
| return "" | ||
|
|
||
| page_content = documents[0].page_content | ||
| source = documents[0].metadata["source"] | ||
| return f"{source} {page_content}" | ||
|
|
||
| file_path: str = Field( | ||
| default_factory=str, | ||
| description="File from which content is extracted", | ||
| ) | ||
|
|
||
| def text_loader(self): | ||
| loader = TextLoader(file_path=self.file_path) | ||
| data = loader.load() | ||
| page_content = data[0].page_content | ||
| meta_data = data[0].metadata["source"] | ||
| context = meta_data + " " + page_content | ||
| return context | ||
|
|
||
| def csv_loader(self): | ||
| content = "" | ||
| loader = CSVLoader(file_path=self.file_path) | ||
| data = loader.load() | ||
|
|
||
| for i in range(len(data)): | ||
| row_content = data[i].page_content | ||
| row_no = data[i].metadata["row"] | ||
| content += "row_no" + " " + str(row_no) + ": " + str(row_content) | ||
| return content | ||
|
|
||
| def execute(self): | ||
| if self.file_path.endswith(".txt"): | ||
| context = self.text_loader() | ||
| elif self.file_path.endswith(".csv"): | ||
| context = self.csv_loader() | ||
| return context | ||
| class CSVLoaderTool(ConfigurableAction): | ||
| """Load content from a CSV file. | ||
|
|
||
| This action loads and processes content from .csv files, combining | ||
| row numbers and content into a formatted string representation. | ||
| """ | ||
|
|
||
| def execute(self) -> str: | ||
| file_path: str = self.get_config('filename') | ||
| loader = CSVLoader(file_path=file_path) | ||
| documents = loader.load() | ||
|
|
||
| content_parts = [] | ||
| for idx, doc in enumerate(documents): | ||
| row_content = doc.page_content | ||
| row_number = doc.metadata["row"] | ||
| content_parts.append(f"row_no {row_number}: {row_content}") | ||
|
|
||
| return "".join(content_parts) |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,30 +1,63 @@ | ||
| from openagi.actions.base import BaseAction | ||
| import os | ||
| from openagi.actions.base import ConfigurableAction | ||
| from pydantic import Field | ||
| from openagi.exception import OpenAGIException | ||
| import os | ||
| import warnings | ||
|
|
||
| try: | ||
| from exa_py import Exa | ||
| from exa_py import Exa | ||
| except ImportError: | ||
| raise OpenAGIException("Install Exa Py with cmd `pip install exa_py`") | ||
| raise OpenAGIException("Install Exa Py with cmd `pip install exa_py`") | ||
|
|
||
| class ExaSearch(BaseAction): | ||
| """ | ||
| Exa Search is a tool used when user needs to ask the question in terms of query to get response | ||
| class ExaSearch(ConfigurableAction): | ||
| """Exa Search tool for querying and retrieving information. | ||
|
|
||
| This action uses the Exa API to perform searches and retrieve relevant content | ||
| based on user queries. Requires an API key to be configured before use. | ||
| """ | ||
| query: str = Field(..., description="User query or question ") | ||
|
|
||
| def execute(self): | ||
| api_key = os.environ["EXA_API_KEY"] | ||
| query: str = Field(..., description="User query or question") | ||
|
|
||
| def __init__(self, **data): | ||
| super().__init__(**data) | ||
| self._check_deprecated_usage() | ||
|
|
||
| def _check_deprecated_usage(self): | ||
| if 'EXA_API_KEY' in os.environ and not self.get_config('api_key'): | ||
| warnings.warn( | ||
| "Using environment variables for API keys is deprecated and will be removed in a future version. " | ||
| "Please use ExaSearch.set_config(api_key='your_key') instead of setting environment variables.", | ||
| DeprecationWarning, | ||
| stacklevel=2 | ||
| ) | ||
| self.set_config(api_key=os.environ['EXA_API_KEY']) | ||
|
|
||
|
|
||
| def execute(self) -> str: | ||
| api_key: str = self.get_config('api_key') | ||
| if not api_key: | ||
| if 'EXA_API_KEY' in os.environ: | ||
| api_key = os.environ['EXA_API_KEY'] | ||
| warnings.warn( | ||
| "Using environment variables for API keys is deprecated and will be removed in a future version. " | ||
| "Please use ExaSearch.set_config(api_key='your_key') instead of setting environment variables.", | ||
| DeprecationWarning, | ||
| stacklevel=2 | ||
| ) | ||
| else: | ||
| raise OpenAGIException("API KEY NOT FOUND. Use ExaSearch.set_config(api_key='your_key') to set the API key.") | ||
|
|
||
| exa = Exa(api_key=api_key) | ||
| results = exa.search_and_contents( | ||
| self.query, | ||
| text={"max_characters": 512}, | ||
| ) | ||
|
|
||
| exa = Exa(api_key = api_key) | ||
| results = exa.search_and_contents(self.query, | ||
| text={"max_characters": 512}, | ||
| ) | ||
| content = "" | ||
| for idx in results.results: | ||
| content += idx.text.strip() | ||
| content_parts = [] | ||
| for result in results.results: | ||
| content_parts.append(result.text.strip()) | ||
|
|
||
| content = content.replace("<|endoftext|>","") | ||
| content = content.replace("NaN","") | ||
| return content | ||
| content = "".join(content_parts) | ||
| return ( | ||
| content.replace("<|endoftext|>", "") | ||
| .replace("NaN", "") | ||
| ) |
This file contains hidden or 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,38 @@ | ||
| from openagi.actions.base import ConfigurableAction | ||
| from pydantic import Field | ||
| from openagi.exception import OpenAGIException | ||
| import logging | ||
|
|
||
| try: | ||
| from googlesearch import search | ||
| except ImportError: | ||
| raise OpenAGIException("Install googlesearch-python with cmd `pip install googlesearch-python`") | ||
|
|
||
| class GoogleSearchTool(ConfigurableAction): | ||
| """ | ||
| Google Search is a tool used for scraping the Google search engine. Extract information from Google search results. | ||
| """ | ||
| query: str = Field(..., description="User query or question ") | ||
|
|
||
| max_results: int = Field( | ||
| default=10, | ||
| description="Total results, in int, to be executed from the search. Defaults to 10. The limit should be 10 and not execeed more than 10", | ||
| ) | ||
|
|
||
| lang: str = Field( | ||
| default="en", | ||
| description = "specify the langauge for your search results." | ||
| ) | ||
|
|
||
| def execute(self): | ||
| if self.max_results > 15: | ||
| logging.info("Over threshold value... Limiting the Max results to 15") | ||
| self.max_results = 15 | ||
|
|
||
| context = "" | ||
| search_results = search(self.query,num_results=self.max_results,lang=self.lang,advanced=True) | ||
| for info in search_results: | ||
| context += f"Title: {info.title}. Description: {info.description}. URL: {info.url}" | ||
|
|
||
| return context | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we use this tool will it work or explicitly we will have to run the install command? @tarun-aiplanet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we will have to run install command