From e36af697cd3d8e8f874d6db601aa4352adfc94b7 Mon Sep 17 00:00:00 2001 From: Carlos Antunes Date: Sat, 18 May 2024 16:56:06 -0300 Subject: [PATCH 1/3] adding MySQLSearchTool README --- .../tools/mysql_seach_tool/README.md | 56 +++++++++++++++++++ .../mysql_seach_tool/mysql_search_tool.py | 44 +++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 src/crewai_tools/tools/mysql_seach_tool/README.md create mode 100644 src/crewai_tools/tools/mysql_seach_tool/mysql_search_tool.py diff --git a/src/crewai_tools/tools/mysql_seach_tool/README.md b/src/crewai_tools/tools/mysql_seach_tool/README.md new file mode 100644 index 000000000..b31d7120b --- /dev/null +++ b/src/crewai_tools/tools/mysql_seach_tool/README.md @@ -0,0 +1,56 @@ +# MySQLSearchTool + +## Description +This tool is designed to facilitate semantic searches within MySQL database tables. Leveraging the RAG (Retrieve and Generate) technology, the MySQLSearchTool provides users with an efficient means of querying database table content, specifically tailored for MySQL databases. It simplifies the process of finding relevant data through semantic search queries, making it an invaluable resource for users needing to perform advanced queries on extensive datasets within a MySQL database. + +## Installation +To install the `crewai_tools` package and utilize the MySQLSearchTool, execute the following command in your terminal: + +```shell +pip install 'crewai[tools]' +``` + +## Example +Below is an example showcasing how to use the MySQLSearchTool to conduct a semantic search on a table within a MySQL database: + +```python +from crewai_tools import MySQLSearchTool + +# Initialize the tool with the database URI and the target table name +tool = MySQLSearchTool(db_uri='mysql://user:password@localhost:3306/mydatabase', table_name='employees') + +``` + +## Arguments +The MySQLSearchTool requires the following arguments for its operation: + +- `db_uri`: A string representing the URI of the MySQL database to be queried. This argument is mandatory and must include the necessary authentication details and the location of the database. +- `table_name`: A string specifying the name of the table within the database on which the semantic search will be performed. This argument is mandatory. + +## Custom model and embeddings + +By default, the tool uses OpenAI for both embeddings and summarization. To customize the model, you can use a config dictionary as follows: + +```python +tool = MySQLSearchTool( + config=dict( + llm=dict( + provider="ollama", # or google, openai, anthropic, llama2, ... + config=dict( + model="llama2", + # temperature=0.5, + # top_p=1, + # stream=true, + ), + ), + embedder=dict( + provider="google", + config=dict( + model="models/embedding-001", + task_type="retrieval_document", + # title="Embeddings", + ), + ), + ) +) +``` diff --git a/src/crewai_tools/tools/mysql_seach_tool/mysql_search_tool.py b/src/crewai_tools/tools/mysql_seach_tool/mysql_search_tool.py new file mode 100644 index 000000000..226fb1ddd --- /dev/null +++ b/src/crewai_tools/tools/mysql_seach_tool/mysql_search_tool.py @@ -0,0 +1,44 @@ +from typing import Any, Type + +from embedchain.loaders.postgres import PostgresLoader +from pydantic.v1 import BaseModel, Field + +from ..rag.rag_tool import RagTool + + +class PGSearchToolSchema(BaseModel): + """Input for PGSearchTool.""" + + search_query: str = Field( + ..., + description="Mandatory semantic search query you want to use to search the database's content", + ) + + +class PGSearchTool(RagTool): + name: str = "Search a database's table content" + description: str = "A tool that can be used to semantic search a query from a database table's content." + args_schema: Type[BaseModel] = PGSearchToolSchema + db_uri: str = Field(..., description="Mandatory database URI") + + def __init__(self, table_name: str, **kwargs): + super().__init__(**kwargs) + self.add(table_name) + self.description = f"A tool that can be used to semantic search a query the {table_name} database table's content." + self._generate_description() + + def add( + self, + table_name: str, + **kwargs: Any, + ) -> None: + kwargs["data_type"] = "postgres" + kwargs["loader"] = PostgresLoader(config=dict(url=self.db_uri)) + super().add(f"SELECT * FROM {table_name};", **kwargs) + + def _run( + self, + search_query: str, + **kwargs: Any, + ) -> Any: + return super()._run(query=search_query) From a11cc57345e51eac1e7658a84bb250518eb0b36a Mon Sep 17 00:00:00 2001 From: Carlos Antunes Date: Sat, 18 May 2024 16:58:40 -0300 Subject: [PATCH 2/3] adding MySQLSearcherTool --- .../tools/mysql_seach_tool/mysql_search_tool.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/crewai_tools/tools/mysql_seach_tool/mysql_search_tool.py b/src/crewai_tools/tools/mysql_seach_tool/mysql_search_tool.py index 226fb1ddd..372a02f38 100644 --- a/src/crewai_tools/tools/mysql_seach_tool/mysql_search_tool.py +++ b/src/crewai_tools/tools/mysql_seach_tool/mysql_search_tool.py @@ -1,13 +1,13 @@ from typing import Any, Type -from embedchain.loaders.postgres import PostgresLoader +from embedchain.loaders.mysql import MySQLLoader from pydantic.v1 import BaseModel, Field from ..rag.rag_tool import RagTool -class PGSearchToolSchema(BaseModel): - """Input for PGSearchTool.""" +class MySQLSearchToolSchema(BaseModel): + """Input for MySQLSearchTool.""" search_query: str = Field( ..., @@ -15,10 +15,10 @@ class PGSearchToolSchema(BaseModel): ) -class PGSearchTool(RagTool): +class MySQLSearchTool(RagTool): name: str = "Search a database's table content" description: str = "A tool that can be used to semantic search a query from a database table's content." - args_schema: Type[BaseModel] = PGSearchToolSchema + args_schema: Type[BaseModel] = MySQLSearchToolSchema db_uri: str = Field(..., description="Mandatory database URI") def __init__(self, table_name: str, **kwargs): @@ -32,8 +32,8 @@ class PGSearchTool(RagTool): table_name: str, **kwargs: Any, ) -> None: - kwargs["data_type"] = "postgres" - kwargs["loader"] = PostgresLoader(config=dict(url=self.db_uri)) + kwargs["data_type"] = "mysql" + kwargs["loader"] = MySQLLoader(config=dict(url=self.db_uri)) super().add(f"SELECT * FROM {table_name};", **kwargs) def _run( From 21342fa0f6ceb183dc89376c845ee6c0973ebfb8 Mon Sep 17 00:00:00 2001 From: Carlos Antunes Date: Mon, 15 Jul 2024 13:00:06 -0300 Subject: [PATCH 3/3] adding the proper imports into __init__.py files, and fixing the folder name --- src/crewai_tools/__init__.py | 3 ++- src/crewai_tools/tools/__init__.py | 1 + .../tools/{mysql_seach_tool => mysql_search_tool}/README.md | 0 .../mysql_search_tool.py | 0 4 files changed, 3 insertions(+), 1 deletion(-) rename src/crewai_tools/tools/{mysql_seach_tool => mysql_search_tool}/README.md (100%) rename src/crewai_tools/tools/{mysql_seach_tool => mysql_search_tool}/mysql_search_tool.py (100%) diff --git a/src/crewai_tools/__init__.py b/src/crewai_tools/__init__.py index faac5d37d..4988a2b25 100644 --- a/src/crewai_tools/__init__.py +++ b/src/crewai_tools/__init__.py @@ -23,4 +23,5 @@ from .tools import ( XMLSearchTool, YoutubeChannelSearchTool, YoutubeVideoSearchTool, -) \ No newline at end of file + MySQLSearchTool +) diff --git a/src/crewai_tools/tools/__init__.py b/src/crewai_tools/tools/__init__.py index 648671d97..985537712 100644 --- a/src/crewai_tools/tools/__init__.py +++ b/src/crewai_tools/tools/__init__.py @@ -21,3 +21,4 @@ from .website_search.website_search_tool import WebsiteSearchTool from .xml_search_tool.xml_search_tool import XMLSearchTool from .youtube_channel_search_tool.youtube_channel_search_tool import YoutubeChannelSearchTool from .youtube_video_search_tool.youtube_video_search_tool import YoutubeVideoSearchTool +from .mysql_search_tool.mysql_search_tool import MySQLSearchTool diff --git a/src/crewai_tools/tools/mysql_seach_tool/README.md b/src/crewai_tools/tools/mysql_search_tool/README.md similarity index 100% rename from src/crewai_tools/tools/mysql_seach_tool/README.md rename to src/crewai_tools/tools/mysql_search_tool/README.md diff --git a/src/crewai_tools/tools/mysql_seach_tool/mysql_search_tool.py b/src/crewai_tools/tools/mysql_search_tool/mysql_search_tool.py similarity index 100% rename from src/crewai_tools/tools/mysql_seach_tool/mysql_search_tool.py rename to src/crewai_tools/tools/mysql_search_tool/mysql_search_tool.py