forked from TransformerOptimus/SuperAGI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
44 additions
and
48 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 |
---|---|---|
@@ -1,90 +1,89 @@ | ||
import json | ||
import evadb | ||
from typing import Type, Optional | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from superagi.helper.google_search import GoogleSearchWrap | ||
from superagi.helper.token_counter import TokenCounter | ||
from superagi.llms.base_llm import BaseLlm | ||
from superagi.tools.base_tool import BaseTool | ||
|
||
|
||
|
||
class DbCreateConnectionSchema(BaseModel): | ||
db_engine: str = Field( | ||
..., | ||
description = "The name of underlying database engine to connect to", | ||
description="The name of underlying database engine to connect to", | ||
) | ||
username: str = Field( | ||
..., | ||
description = "The username for connecting to the database", | ||
description="The username for connecting to the database", | ||
) | ||
host: str = Field( | ||
..., | ||
description = "The hostname for connecting to the database", | ||
description="The hostname for connecting to the database", | ||
) | ||
port: str = Field( | ||
..., | ||
description = "The port for connecting to the database", | ||
description="The port for connecting to the database", | ||
) | ||
database_name: str = Field( | ||
..., | ||
description = "The name of the database in the underlying database to connect to", | ||
description="The database name in the underlying engine to connect to", | ||
) | ||
|
||
|
||
class DbCreateConnectionTool(BaseTool): | ||
""" | ||
Creates a connection entry in EvaDB for the underlying database engine to use it to execute future queries on the underlying database. | ||
Register a connection entry within EvaDB, enabling the underlying database engine to execute future queries effectively. | ||
Attributes: | ||
name : The name of the tool. | ||
description : The description of the tool. | ||
args_schema : The args schema. | ||
""" | ||
|
||
llm: Optional[BaseLlm] = None | ||
name = "DbCreateConnectionTool" | ||
description = ( | ||
""" | ||
A tool for creating connection to the required database in the corresponding database engine using the credentials provided by the user. | ||
description = """ | ||
A tool for establishing connections to the designated database within the corresponding db engine, utilizing user-provided credentials. | ||
""" | ||
) | ||
args_schema: Type[DbCreateConnectionSchema] = DbCreateConnectionSchema | ||
|
||
class Config: | ||
arbitrary_types_allowed = True | ||
|
||
def _execute(self, db_engine: str, username: str, host: str, port: str, database_name: str) -> tuple: | ||
def _execute( | ||
self, db_engine: str, username: str, host: str, port: str, database_name: str | ||
) -> tuple: | ||
""" | ||
Execute the EvaDB create connection tool. | ||
Args: | ||
db_engine: The name of underlying database engine to connect to | ||
db_engine: The name of db engine to connect to eg. postgres, sqlite | ||
username: The username for connecting to the database | ||
password: The password for connecting to the database | ||
host: The hostname for connecting to the database | ||
port: The port for connecting to the database | ||
database_name: The name of the database in the underlying database to connect to | ||
database_name: The database name in the underlying engine to connect to | ||
Returns: | ||
The response of create connection query | ||
""" | ||
# print("####^^^^####") | ||
cursor = evadb.connect().cursor() | ||
db_connection_name = db_engine + '__' + database_name | ||
create_db_query = """ | ||
CREATE DATABASE IF NOT EXISTS {connection_name} WITH ENGINE = '{db_engine}', PARAMETERS = {{ | ||
db_connection_name = db_engine + "__" + database_name | ||
|
||
# todo: discuss a better way to get the password from user | ||
password = "" | ||
create_db_query = f""" | ||
CREATE DATABASE IF NOT EXISTS {db_connection_name} | ||
WITH ENGINE = "{db_engine}", | ||
PARAMETERS = {{ | ||
"user": "{username}", | ||
"host": "{hostname}", | ||
"host": "{host}", | ||
"port": "{port}", | ||
"database": "{db_name}", | ||
"database": "{database_name}", | ||
"password": "{password}" | ||
}}; | ||
""".format(connection_name = db_connection_name, db_engine = db_engine, username = username, | ||
hostname = host, port = port, db_name = database_name, password = "ada@Gatech") | ||
print("create_db_query", create_db_query) | ||
#cursor.query(f"DROP DATABASE IF EXISTS {db_connection_name}").df() | ||
ret = cursor.query(create_db_query).df() | ||
""" | ||
print(f"DbCreateConnectionTool running query {create_db_query}") | ||
cursor.query(create_db_query).df() | ||
cursor.close() | ||
return "Sucessfully created the desired connection to underlying databse engine with name {connection}. This connection can now be used to query requested database.".format(connection = db_connection_name) | ||
return f"Sucessfully created the desired connection to underlying database engine with name {db_connection_name}. This connection can now be used to query requested database." |
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