Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sql agent #152

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions agents/sql/README.md
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions agents/sql/index.yaml
Original file line number Diff line number Diff line change
@@ -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 changes: 43 additions & 0 deletions agents/sql/tools.sh
Original file line number Diff line number Diff line change
@@ -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" "$@")"
2 changes: 1 addition & 1 deletion tools/execute_sql_code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set -e

# @meta require-tools usql

# @env USQL_DSN! The database url, e.g. pgsql://user:pass@host/dbname
# @env USQL_DSN! The database connection url. e.g. pgsql://user:pass@host:port
# @env LLM_OUTPUT=/dev/stdout The output path

ROOT_DIR="${LLM_ROOT_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}"
Expand Down
Loading