-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
ENG-1392: SQL tool in Agents #400
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b104e8b
SQL Tool feature
thiago-aixplain a7c1d41
Function tests for sql tool
thiago-aixplain 83de744
Enable multiple tables and constraint size of SQLite file
thiago-aixplain dbaf03d
Enable more than one table
thiago-aixplain 563fc15
Constrain the size of the database
thiago-aixplain db6e4ac
Enable commit on SQL tool
thiago-aixplain 0894ea1
Merge branch 'development' into ENG-1392-sqllite
thiago-aixplain 4af59cd
Improving SQL tool functional test
thiago-aixplain 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
__author__ = "aiXplain" | ||
|
||
""" | ||
Copyright 2024 The aiXplain SDK authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
Author: Lucas Pavanelli and Thiago Castro Ferreira | ||
Date: May 16th 2024 | ||
Description: | ||
Agentification Class | ||
""" | ||
import os | ||
import validators | ||
from typing import Text, Optional, Dict, List, Union | ||
|
||
from aixplain.modules.agent.tool import Tool | ||
|
||
|
||
class SQLTool(Tool): | ||
"""Tool to execute SQL commands in an SQLite database. | ||
|
||
Attributes: | ||
description (Text): description of the tool | ||
database (Text): database name | ||
schema (Text): database schema description | ||
tables (Optional[Union[List[Text], Text]]): table names to work with (optional) | ||
""" | ||
|
||
def __init__( | ||
self, | ||
description: Text, | ||
database: Text, | ||
schema: Text, | ||
tables: Optional[Union[List[Text], Text]] = None, | ||
**additional_info, | ||
) -> None: | ||
"""Tool to execute SQL query commands in an SQLite database. | ||
|
||
Args: | ||
description (Text): description of the tool | ||
database (Text): database name | ||
schema (Text): database schema description | ||
tables (Optional[Union[List[Text], Text]]): table names to work with (optional) | ||
""" | ||
super().__init__("", description, **additional_info) | ||
self.database = database | ||
self.schema = schema | ||
self.tables = tables if isinstance(tables, list) else [tables] if tables else None | ||
|
||
def to_dict(self) -> Dict[str, Text]: | ||
return { | ||
"description": self.description, | ||
"parameters": [ | ||
{"name": "database", "value": self.database}, | ||
{"name": "schema", "value": self.schema}, | ||
{"name": "tables", "value": ",".join(self.tables) if self.tables is not None else None}, | ||
], | ||
"type": "sql", | ||
} | ||
|
||
def validate(self): | ||
from aixplain.factories.file_factory import FileFactory | ||
|
||
assert self.description and self.description.strip() != "", "SQL Tool Error: Description is required" | ||
assert self.database and self.database.strip() != "", "SQL Tool Error: Database is required" | ||
if not ( | ||
str(self.database).startswith("s3://") | ||
or str(self.database).startswith("http://") | ||
or str(self.database).startswith("https://") | ||
or validators.url(self.database) | ||
): | ||
if not os.path.exists(self.database): | ||
raise Exception(f"SQL Tool Error: Database '{self.database}' does not exist") | ||
if not self.database.endswith(".db"): | ||
raise Exception(f"SQL Tool Error: Database '{self.database}' must have .db extension") | ||
self.database = FileFactory.upload(local_path=self.database, is_temp=True) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from aixplain.factories import AgentFactory | ||
from aixplain.modules.agent.tool.sql_tool import SQLTool | ||
|
||
|
||
def test_create_sql_tool(mocker): | ||
tool = AgentFactory.create_sql_tool(description="Test", database="test.db", schema="test", tables=["test", "test2"]) | ||
assert isinstance(tool, SQLTool) | ||
assert tool.description == "Test" | ||
assert tool.database == "test.db" | ||
assert tool.schema == "test" | ||
assert tool.tables == ["test", "test2"] | ||
|
||
tool_dict = tool.to_dict() | ||
assert tool_dict["description"] == "Test" | ||
assert tool_dict["parameters"] == [ | ||
{"name": "database", "value": "test.db"}, | ||
{"name": "schema", "value": "test"}, | ||
{"name": "tables", "value": "test,test2"}, | ||
] | ||
|
||
mocker.patch("aixplain.factories.file_factory.FileFactory.upload", return_value="s3://test.db") | ||
mocker.patch("os.path.exists", return_value=True) | ||
tool.validate() | ||
assert tool.database == "s3://test.db" |
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.
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.
This should have tables as well:
tables: Optional[List[Text]]