romainlg commited on
Commit
1ac635b
·
1 Parent(s): 9ad1a41

Upload tool

Browse files
Files changed (4) hide show
  1. app.py +4 -0
  2. postgresql_tool.py +54 -0
  3. requirements.txt +2 -0
  4. tool_config.json +5 -0
app.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from transformers import launch_gradio_demo
2
+ from postgresql_tool import PostgreSQLTool
3
+
4
+ launch_gradio_demo(PostgreSQLTool)
postgresql_tool.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers.tools import Tool
2
+ from psycopg2 import connect
3
+
4
+
5
+ class PostgreSQLTool(Tool):
6
+ name = "postgres_database_tool"
7
+ description = (
8
+ "This tool is used to query a PostgreSQL database with a SQL request. "
9
+ "The tool is already connected to the database. "
10
+ "Example: postgres_tool('SELECT field FROM my_table;')"
11
+ "It takes a SQL request as argument and returns the result of the query. "
12
+ )
13
+
14
+ inputs = ["text"]
15
+ outputs = ["text"]
16
+
17
+ debug = False
18
+
19
+ database = None
20
+ cursor = None
21
+
22
+ def __init__(self, debug: bool = False):
23
+ super().__init__()
24
+ self.debug = debug
25
+
26
+ def connect(
27
+ self, host: str, database: str, user: str, password: str, port: int = 5432
28
+ ):
29
+ # Connect to the database and create a cursor
30
+ self.database = connect(
31
+ database=database, host=host, user=user, password=password, port=port
32
+ )
33
+ self.cursor = self.database.cursor()
34
+
35
+ def disconnect(self):
36
+ # Close the connection to the database
37
+ self.database.close()
38
+
39
+ def __call__(self, query: str):
40
+ if self.debug:
41
+ print(f"[POSTGRESQL_TOOL] Executing: {query}")
42
+
43
+ try:
44
+ # Execute the query
45
+ self.cursor.execute(query)
46
+ except Exception as e:
47
+ if self.debug:
48
+ print(f"[POSTGRESQL_TOOL] Query failed: {e}")
49
+
50
+ # Return the error message
51
+ return "[POSTGRESQL_TOOL] Query failed: " + str(e)
52
+
53
+ # Return the result of the query
54
+ return self.cursor.fetchall()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ transformers
2
+ psycopg2
tool_config.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "description": "This tool is used to query a PostgreSQL database with a SQL request. The tool is already connected to the database. Example: postgres_tool('SELECT field FROM my_table;')It takes a SQL request as argument and returns the result of the query. ",
3
+ "name": "postgres_database_tool",
4
+ "tool_class": "postgresql_tool.PostgreSQLTool"
5
+ }