Skip to content

Commit

Permalink
Query assistance for specific table, closes #8
Browse files Browse the repository at this point in the history
Also added breadcrumbs to the assistant page, and added the schema in a details/summary.
  • Loading branch information
simonw committed Apr 16, 2024
1 parent cb71eae commit d39c403
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
40 changes: 26 additions & 14 deletions datasette_query_assistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,26 @@
""".strip()

SCHEMA_SQL = """
select group_concat(sql, '; ') from sqlite_master;
select group_concat(sql, ';
') from sqlite_master where type != 'trigger'
"""
SCHEMA_SQL_SPECIFIC = """
select group_concat(sql, ';
') from sqlite_master where tbl_name in (PARAMS) and type != 'trigger'
"""


async def get_schema(db, table=None):
if table:

def _related(conn):
return get_related_tables(conn, table)

tables = await db.execute_fn(_related)
sql = SCHEMA_SQL_SPECIFIC.replace("PARAMS", ",".join("?" for _ in tables))
return (await db.execute(sql, tuple(tables))).first()[0]
else:
return (await db.execute(SCHEMA_SQL)).first()[0]


async def has_permission(datasette, actor, database):
Expand Down Expand Up @@ -97,12 +115,13 @@ async def assistant(request, datasette):
if request.method == "POST":
post_vars = await request.post_vars()
question = (post_vars.get("question") or "").strip()
table = post_vars.get("table") or None
if not question:
datasette.add_message(request, "Question is required", datasette.ERROR)
return Response.redirect(request.path)
return Response.redirect(request.full_path)

# Here we go
schema = (await db.execute(SCHEMA_SQL)).first()[0]
schema = await get_schema(db, table)

client = AsyncAnthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

Expand All @@ -115,19 +134,12 @@ async def assistant(request, datasette):

# Figure out tables
table = request.args.get("table")
tables = []
if not table:
tables = await db.table_names()
else:
# Get tables related to this one
def act(conn):
return get_related_tables(conn, table)

tables = await db.execute_fn(act)

schema = await get_schema(db, table)
return Response.html(
await datasette.render_template(
"query_assistant.html", {"tables": tables}, request=request
"query_assistant.html",
{"schema": schema, "database": database, "table": table},
request=request,
)
)

Expand Down
13 changes: 10 additions & 3 deletions datasette_query_assistant/templates/query_assistant.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{% extends "base.html" %}

{% block title %}Query assistant{% endblock %}
{% block title %}Query assistant for {% if table %}{{ table }}{% else %}{{ database }}{% endif %}{% endblock %}

{% block crumbs %}
{{ crumbs.nav(request=request, database=database, table=table) }}
{% endblock %}

{% block content %}

<h1>Query assistant</h1>
<h1>Query assistant for {% if table %}{{ table }}{% else %}{{ database }}{% endif %}</h1>

<form action="" method="POST">
<p>
Expand All @@ -13,10 +17,13 @@ <h1>Query assistant</h1>
<p><textarea style="width: 80%; height: 3em;" name="question" id="id_question"></textarea>
<p>
<input type="hidden" name="csrftoken" value="{{ csrftoken() }}">
<input type="hidden" name="table" value="{{ table }}">
<input type="submit" value="Submit">
</p>

Tables: {{ tables }}
<details><summary>Schema that will be passed to the model</summary>
<pre>{{ schema }}</pre>
</details>

<script>
document.querySelector('textarea[name=question]').focus();
Expand Down

0 comments on commit d39c403

Please sign in to comment.