-
Notifications
You must be signed in to change notification settings - Fork 5
add tool to run queries against data frames #56
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
Draft
simonpcouch
wants to merge
5
commits into
main
Choose a base branch
from
query-52
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+117
−5
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
24e2432
add tool to run queries against data frames
simonpcouch 24add82
delete new snaps
simonpcouch dd1780e
remove outdated TODO
simonpcouch d352184
add duckdb to Imports
simonpcouch 16c166c
`btw_env` -> `.globals` [no ci]
simonpcouch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| #' Perform a SQL query on the data, and return the results as JSON. | ||
| #' | ||
| #' @param query A DuckDB SQL query; must be a SELECT statement. | ||
| #' @param data_frame The name of the data frame. | ||
| #' @return The results of the query as a JSON string. | ||
| btw_tool_env_query_data_frame <- function(query, data_frame) { | ||
| d <- get(data_frame) | ||
| conn <- btw_connection() | ||
|
|
||
| if (!DBI::dbExistsTable(conn, data_frame)) { | ||
| duckdb::duckdb_register(conn, data_frame, d, experimental = FALSE) | ||
| } | ||
|
|
||
| res <- DBI::dbGetQuery(conn, query) | ||
|
|
||
| btw_tool_env_describe_data_frame(res, format = "json", dims = c(Inf, Inf)) | ||
| } | ||
|
|
||
| .btw_add_to_tools( | ||
| name = "btw_tool_env_query_data_frame", | ||
| group = "env", | ||
| tool = function() { | ||
| ellmer::tool( | ||
| btw_tool_env_query_data_frame, | ||
| .name = "btw_tool_env_query_data_frame", | ||
| .description = | ||
| "Run a DuckDB SQL query against a data frame. | ||
| Use this tool instead of btw_tool_env_describe_data_frame to run more | ||
| targeted queries, e.g. calculating statistics on specific columns.", | ||
| query = ellmer::type_string("A DuckDB SQL query, as a string."), | ||
| data_frame = ellmer::type_string("The name of the data frame, as a string.") | ||
| ) | ||
| } | ||
| ) | ||
|
|
||
| btw_connect <- function() { | ||
| # TODO: also check if the connection is active | ||
| if (is.null(.globals$conn)) { | ||
| .globals$conn <- DBI::dbConnect(duckdb::duckdb(), dbdir = ":memory:") | ||
| } | ||
| } | ||
|
|
||
| btw_connection <- function() { | ||
| btw_connect() | ||
|
|
||
| .globals$conn | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # btw_tool_env_query_data_frame() works | ||
|
|
||
| Code | ||
| btw_tool_env_query_data_frame("SELECT mpg FROM mtcars LIMIT 5;", "mtcars") | ||
| Output | ||
| [1] "```json" | ||
| [2] "[\n {\"mpg\":21},\n {\"mpg\":21},\n {\"mpg\":22.8},\n {\"mpg\":21.4},\n {\"mpg\":18.7}\n]" | ||
| [3] "```" | ||
|
|
||
| --- | ||
|
|
||
| Code | ||
| btw_tool_env_query_data_frame("SELECT mpg FROM mtcars LIMIT 5;", "mtcars") | ||
| Output | ||
| [1] "```json" | ||
| [2] "[\n {\"mpg\":21},\n {\"mpg\":21},\n {\"mpg\":22.8},\n {\"mpg\":21.4},\n {\"mpg\":18.7}\n]" | ||
| [3] "```" | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| test_that("btw_tool_env_query_data_frame() works", { | ||
| # can run a simple query | ||
| expect_snapshot( | ||
| btw_tool_env_query_data_frame( | ||
| "SELECT mpg FROM mtcars LIMIT 5;", | ||
| "mtcars" | ||
| ) | ||
| ) | ||
|
|
||
| # can run a query against the same table twice | ||
| expect_snapshot( | ||
| btw_tool_env_query_data_frame( | ||
| "SELECT mpg FROM mtcars LIMIT 5;", | ||
| "mtcars" | ||
| ) | ||
| ) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about adding a registration system for these tables? The usage would be something like
If done before the client is created, the tool description could include the table names. Or we could also have a "list tables" tool to introduce the available tables to the LLM.
The other advantage is that the LLM wouldn't need to provide
data_frame, it would only need to write the SQL code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There'd be friction in needing to register tables for use with btw, but I also like the explicit consent nature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh and the registration function would be a generic, so we could also take existing connections, remote tables,
.csvfiles etc. (eventually, but I'd stick with starting with R data frames via duckdb).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you imagine that the registration is optional or required? I do like the "if it's in my global environment, the model can find it" paradigm that's set with the existing environment and data frame description tools, which makes me lean towards making registration optional. There's also already some existing models of explicit consent happening depending on the UI; i.e. if btw is hooked up to Claude Desktop or Claude Code via acquaint, the first usages of the tools will ask for the user's permission anyway.
I do see the value in the registration system in that
get()doesn't have a lot of uses beyond the global environment and models are otherwise unable to "fetch" data up to this point. The registration system sounds really helpful for those other types of data sources.