44# ' Shiny sessions in the R process.
55# '
66# ' @param df A data frame.
7- # ' @param tbl_name A string containing a valid table name for the data frame,
7+ # ' @param table_name A string containing a valid table name for the data frame,
88# ' that will appear in SQL queries. Ensure that it begins with a letter, and
99# ' contains only letters, numbers, and underscores. By default, querychat will
1010# ' try to infer a table name using the name of the `df` argument.
1111# ' @param greeting A string in Markdown format, containing the initial message
1212# ' to display to the user upon first loading the chatbot. If not provided, the
1313# ' LLM will be invoked at the start of the conversation to generate one.
14- # ' @param data_description A string in plain text or Markdown format, containing
15- # ' a description of the data frame or any additional context that might be
16- # ' helpful in understanding the data. This will be included in the system
17- # ' prompt for the chat model. If a `system_prompt` argument is provided, the
18- # ' `data_description` argument will be ignored.
19- # ' @param extra_instructions A string in plain text or Markdown format, containing
20- # ' any additional instructions for the chat model. These will be appended at
21- # ' the end of the system prompt. If a `system_prompt` argument is provided,
22- # ' the `extra_instructions` argument will be ignored.
23- # ' @param create_chat_func A function that takes a system prompt and returns a
24- # ' chat object. The default uses `ellmer::chat_openai()`.
14+ # ' @param ... Additional arguments passed to the `querychat_system_prompt()`
15+ # ' function, such as `categorical_threshold`, and `prompt_path`. If a
16+ # ' `system_prompt` argument is provided, the `...` arguments will be silently
17+ # ' ignored.
18+ # ' @inheritParams querychat_system_prompt
2519# ' @param system_prompt A string containing the system prompt for the chat model.
2620# ' The default uses `querychat_system_prompt()` to generate a generic prompt,
2721# ' which you can enhance via the `data_description` and `extra_instructions`
2822# ' arguments.
29- # '
23+ # ' @param create_chat_func A function that takes a system prompt and returns a
24+ # ' chat object. The default uses `ellmer::chat_openai()`.
3025# ' @returns An object that can be passed to `querychat_server()` as the
3126# ' `querychat_config` argument. By convention, this object should be named
3227# ' `querychat_config`.
3328# '
3429# ' @export
3530querychat_init <- function (
3631 df ,
37- tbl_name = deparse(substitute(df )),
32+ ... ,
33+ table_name = deparse(substitute(df )),
3834 greeting = NULL ,
3935 data_description = NULL ,
4036 extra_instructions = NULL ,
41- create_chat_func = purrr :: partial(ellmer :: chat_openai , model = " gpt-4o" ),
4237 system_prompt = querychat_system_prompt(
4338 df ,
44- tbl_name ,
39+ table_name ,
40+ # By default, pass through any params supplied to querychat_init()
41+ ... ,
4542 data_description = data_description ,
4643 extra_instructions = extra_instructions
47- )
44+ ),
45+ create_chat_func = purrr :: partial(ellmer :: chat_openai , model = " gpt-4o" )
4846) {
49- is_tbl_name_ok <- is.character(tbl_name ) &&
50- length(tbl_name ) == 1 &&
51- grepl(" ^[a-zA-Z][a-zA-Z0-9_]*$" , tbl_name , perl = TRUE )
52- if (! is_tbl_name_ok ) {
53- if (missing(tbl_name )) {
47+ is_table_name_ok <- is.character(table_name ) &&
48+ length(table_name ) == 1 &&
49+ grepl(" ^[a-zA-Z][a-zA-Z0-9_]*$" , table_name , perl = TRUE )
50+ if (! is_table_name_ok ) {
51+ if (missing(table_name )) {
5452 rlang :: abort(
55- " Unable to infer table name from `df` argument. Please specify `tbl_name ` argument explicitly."
53+ " Unable to infer table name from `df` argument. Please specify `table_name ` argument explicitly."
5654 )
5755 } else {
5856 rlang :: abort(
59- " `tbl_name ` argument must be a string containing a valid table name."
57+ " `table_name ` argument must be a string containing a valid table name."
6058 )
6159 }
6260 }
6361
6462 force(df )
65- force(system_prompt )
63+ force(system_prompt ) # Have default `...` params evaluated
6664 force(create_chat_func )
6765
6866 # TODO: Provide nicer looking errors here
6967 stopifnot(
7068 " df must be a data frame" = is.data.frame(df ),
71- " tbl_name must be a string" = is.character(tbl_name ),
69+ " table_name must be a string" = is.character(table_name ),
7270 " system_prompt must be a string" = is.character(system_prompt ),
7371 " create_chat_func must be a function" = is.function(create_chat_func )
7472 )
7573
74+ if (" table_name" %in% names(attributes(system_prompt ))) {
75+ # If available, be sure to use the `table_name` argument to `querychat_init()`
76+ # matches the one supplied to the system prompt
77+ if (table_name != attr(system_prompt , " table_name" )) {
78+ rlang :: abort(
79+ " `querychat_init(table_name=)` must match system prompt `table_name` supplied to `querychat_system_prompt()`."
80+ )
81+ }
82+ }
7683 if (! is.null(greeting )) {
7784 greeting <- paste(collapse = " \n " , greeting )
7885 } else {
@@ -83,7 +90,7 @@ querychat_init <- function(
8390 }
8491
8592 conn <- DBI :: dbConnect(duckdb :: duckdb(), dbdir = " :memory:" )
86- duckdb :: duckdb_register(conn , tbl_name , df , experimental = FALSE )
93+ duckdb :: duckdb_register(conn , table_name , df , experimental = FALSE )
8794 shiny :: onStop(function () DBI :: dbDisconnect(conn ))
8895
8996 structure(
0 commit comments