Repository navigation
Expand file tree
/
Copy pathtools.py
More file actions
47 lines (35 loc) · 1.35 KB
/
Copy pathtools.py
File metadata and controls
47 lines (35 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""The three tools the agent can call.
The docstrings are not just documentation: the model reads them to decide
when and how to use each tool, so they are written as instructions.
"""
import sqlite3
from langchain_core.tools import tool
import db
_RECOVERABLE = (ValueError, sqlite3.Error, OSError)
@tool
def list_tables() -> str:
"""List every table in the inventory database.
Call this first whenever you are unsure what data is available."""
try:
return ", ".join(db.table_names())
except _RECOVERABLE as exc:
return f"Error: {exc}"
@tool
def describe_table(table_name: str) -> str:
"""Show one table's columns, types, foreign keys and 3 sample rows.
Call this for every table you plan to query, before writing any SQL,
so you use real column names instead of guessing."""
try:
return db.describe_table(table_name)
except _RECOVERABLE as exc:
return f"Error: {exc}"
@tool
def run_query(sql: str) -> str:
"""Run ONE read-only SQLite SELECT statement and return the rows (at most 50).
Only SELECT is allowed; no semicolons, comments or data changes.
If the result is an error message, read it carefully, fix the SQL and try again."""
try:
return db.run_select(sql)
except _RECOVERABLE as exc:
return f"Error: {exc}"
TOOLS = [list_tables, describe_table, run_query]