Al-Fathir commited on
Commit
2fccb82
·
1 Parent(s): 24de315

fix sensitive token

Browse files
Files changed (3) hide show
  1. .gitignore +131 -0
  2. agents.py +95 -0
  3. requirements.txt +5 -0
.gitignore ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ target/
76
+
77
+ # Jupyter Notebook
78
+ .ipynb_checkpoints
79
+
80
+ # IPython
81
+ profile_default/
82
+ ipython_config.py
83
+
84
+ # pyenv
85
+ .python-version
86
+
87
+ # celery beat schedule file
88
+ celerybeat-schedule
89
+
90
+ # SageMath parsed files
91
+ *.sage.py
92
+
93
+ # Environments
94
+ .env
95
+ .venv
96
+ env/
97
+ venv/
98
+ ENV/
99
+ env.bak/
100
+ venv.bak/
101
+
102
+ # Spyder project settings
103
+ .spyderproject
104
+ .spyderworkspace
105
+
106
+ # Rope project settings
107
+ .ropeproject
108
+
109
+ # mkdocs documentation
110
+ /site
111
+
112
+ # mypy
113
+ .mypy_cache/
114
+ .dmypy.json
115
+ dmypy.json
116
+
117
+ # Pyre type checker
118
+ .pyre/
119
+
120
+ # pytype static type analyzer
121
+ .pytype/
122
+
123
+ # Cython debug symbols
124
+ cython_debug/
125
+
126
+ # SQL database files
127
+ *.db
128
+ *.sqlite3
129
+
130
+ # VS Code
131
+ .vscode/
agents.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import mysql.connector
2
+ import os
3
+ from dotenv import load_dotenv
4
+ from smolagents import CodeAgent, HfApiModel, tool
5
+ load_dotenv()
6
+ def createConnection(user='root',password='password',host='127.0.0.1',database='sakila'):
7
+ cnx = mysql.connector.connect(user=user, password=password,
8
+ host=host,
9
+ database=database)
10
+
11
+ return cnx
12
+
13
+
14
+ def getAllTable(connection):
15
+ cursor = connection.cursor()
16
+ cursor.execute("SHOW TABLES")
17
+ tables = cursor.fetchall()
18
+ return tables
19
+
20
+
21
+
22
+ def createColumnDesc(connection):
23
+ DESC_TABLE = """"""
24
+ cursor = connection.cursor()
25
+ tables = getAllTable(connection)
26
+ for table in tables:
27
+ cursor.execute(f"DESCRIBE {table[0]}")
28
+ columns = cursor.fetchall()
29
+
30
+ DESC_TABLE += f"*{table[0].upper()}\n"
31
+ for column in columns:
32
+ column_name = column[0]
33
+ column_type = column[1]
34
+
35
+ DESC_TABLE += f"- {column_name} : {column_type}\n"
36
+
37
+ return DESC_TABLE
38
+
39
+
40
+ cnx = createConnection()
41
+ desc = createColumnDesc(cnx)
42
+
43
+ DESCRIPTION = """
44
+ Allows you to perform SQL queries on the table and returns results in various formats. Beware that this tool's output is a string representation of the execution output.
45
+ It can use the following tables:
46
+ """
47
+ DESCRIPTION += desc
48
+
49
+
50
+
51
+ @tool
52
+ def sql_engine(query: str) -> str:
53
+ """
54
+ Performs SQL queries and returns results as a minimal string for LLM processing.
55
+
56
+ Args:
57
+ query: The SQL query to execute. This should be valid SQL.
58
+
59
+ Returns:
60
+ str: Query results in a concise, consistent, and minimal format.
61
+ """
62
+ cursor = cnx.cursor(dictionary=True) # Enable dictionary cursor
63
+ cursor.execute(query)
64
+
65
+ # Get column names
66
+ columns = [desc[0] for desc in cursor.description]
67
+
68
+ # Fetch all rows
69
+ rows = cursor.fetchall()
70
+
71
+ # Return results in a flat format
72
+ if not rows:
73
+ return "No results found"
74
+
75
+ result = []
76
+ result.append("|".join(columns)) # Column headers
77
+ for row in rows:
78
+ result.append("|".join(str(row[col]) for col in columns))
79
+
80
+ return "\n".join(result)
81
+
82
+
83
+
84
+ sql_engine.description = DESCRIPTION
85
+ token = os.getenv('HUGGINGFACE_TOKEN')
86
+
87
+ agent = CodeAgent(
88
+ tools=[sql_engine],
89
+ model=HfApiModel("Qwen/Qwen2.5-Coder-32B-Instruct",token=token),
90
+ additional_authorized_imports=["matplotlib.pyplot"]
91
+ )
92
+
93
+
94
+
95
+
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ mysql-connector-python
2
+ pandas
3
+ smolagents
4
+ huggingface_hub
5
+ python-dotenv