feat: add sql agent (#152)

This commit is contained in:
sigoden
2024-12-30 19:03:40 +08:00
committed by GitHub
parent f81a81711b
commit 0a88f22df5
4 changed files with 64 additions and 1 deletions
+7
View File
@@ -0,0 +1,7 @@
# SQL
An AI agent that helps you manage a SQL database.
> The tool script uses [usql](https://github.com/xo/usql) to interact with SQL, it supports all mainstream databases.
![image](https://github.com/user-attachments/assets/28bc1118-5f87-4571-a1c9-6c8cec4636d5)
+13
View File
@@ -0,0 +1,13 @@
name: Sql
description: An AI agent that helps you manage a SQL database
version: 0.1.0
instructions: |
You are an AI agent that manages a SQL database.
Available tools:
{{__tools__}}
variables:
- name: dsn
description: The database connection url. e.g. pgsql://user:pass@host:port
conversation_starters:
- What you can do?
+43
View File
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
set -e
ROOT_DIR="${LLM_ROOT_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)}"
# @meta require-tools usql
# @env LLM_AGENT_VAR_DSN! The database connection url. e.g. pgsql://user:pass@host:port
# @cmd Execute a SELECT query
# @option --query! SELECT SQL query to execute
read_query() {
if ! grep -qi '^select' <<<"$argc_query"; then
echo "error: only SELECT query is allowed" >&2
exit 1
fi
_run_sql "$argc_query"
}
# @cmd Execute an SQL query
# @option --query! SQL query to execute
write_query() {
"$ROOT_DIR/utils/guard_operation.sh" "Execute SQL?"
_run_sql "$argc_query"
}
# @cmd List all tables
list_tables() {
_run_sql "\dt+"
}
# @cmd Get the schema information for a specific table
# @option --table-name! Name of the table to describe
describe_table() {
_run_sql "\d $argc_table_name"
}
_run_sql() {
usql "$LLM_AGENT_VAR_DSN" -c "$1" >> "$LLM_OUTPUT"
}
# See more details at https://github.com/sigoden/argc
eval "$(argc --argc-eval "$0" "$@")"