|
| 1 | +#' Chat with GEO metadata using natural language |
| 2 | +#' |
| 3 | +#' Create a [`QueryChat`][querychat::QueryChat] object for exploring GEO |
| 4 | +#' metadata with an LLM. Use `geo_qc()` to create the chat object, `geo_shiny()` |
| 5 | +#' to launch the Shiny app, and `geo_chat()` to start a console chat. |
| 6 | +#' |
| 7 | +#' @inheritParams querychat::querychat |
| 8 | +#' @param data_source A data.frame or a database connection containing GEO |
| 9 | +#' metadata, typically from [geo_meta()] or [geo_search()]. |
| 10 | +#' @inheritDotParams querychat::querychat -extra_instructions -prompt_template |
| 11 | +#' @param instructions Optional single string with additional instructions to |
| 12 | +#' append to the default GEO metadata assistant instructions. |
| 13 | +#' |
| 14 | +#' @return A [`QueryChat`][querychat::QueryChat] object configured with |
| 15 | +#' `data_source`, an LLM client, and GEO-specific instructions. |
| 16 | +#' |
| 17 | +#' @details |
| 18 | +#' `geo_qc()` intentionally does not serialize all rows or build a large data |
| 19 | +#' prompt. Instead, it delegates schema summarization, SQL querying, and |
| 20 | +#' dashboard filtering to [`QueryChat`][querychat::QueryChat]. |
| 21 | +#' |
| 22 | +#' The three exported helpers differ only in how far they take the |
| 23 | +#' [`QueryChat`][querychat::QueryChat] workflow: |
| 24 | +#' |
| 25 | +#' * `geo_qc()` creates and returns the `QueryChat` object. Use it when you want |
| 26 | +#' to inspect the generated prompt, customize the object, embed it in another |
| 27 | +#' Shiny app, or launch the app/console later with `qc$app()` or |
| 28 | +#' `qc$console()`. |
| 29 | +#' * `geo_shiny()` creates the same `QueryChat` object and immediately launches |
| 30 | +#' its Shiny app. Use it for interactive browser-based filtering and |
| 31 | +#' exploration. |
| 32 | +#' * `geo_chat()` creates the same `QueryChat` object and immediately starts |
| 33 | +#' its console chat. Use it for command-line exploration without opening a |
| 34 | +#' Shiny app. |
| 35 | +#' |
| 36 | +#' The default instructions guide the assistant to query and filter GEO |
| 37 | +#' metadata, identify relevant studies, generate reproducible R code when |
| 38 | +#' asked, preserve explicit accession IDs, and explain GEO accession types |
| 39 | +#' (`GSE`, `GSM`, `GPL`, and `GDS`) when useful. |
| 40 | +#' |
| 41 | +#' The first argument is the LLM client. Use `client = NULL` or pass `NULL` as |
| 42 | +#' the first positional argument to let `querychat` choose a client from its |
| 43 | +#' options or environment variables. Additional context such as |
| 44 | +#' `data_description`, `greeting`, `tools`, `categorical_threshold`, and |
| 45 | +#' `cleanup` can be passed through `...` to [querychat::querychat()]. |
| 46 | +#' `prompt_template` is intentionally not forwarded because `geo_qc()` supplies |
| 47 | +#' GEO-specific instructions through `extra_instructions`. |
| 48 | +#' |
| 49 | +#' @examples |
| 50 | +#' if (requireNamespace("querychat", quietly = TRUE) && |
| 51 | +#' requireNamespace("duckdb", quietly = TRUE)) { |
| 52 | +#' records <- data.frame( |
| 53 | +#' Accession = c("GSE1", "GSE2"), |
| 54 | +#' Title = c("human diabetes study", "mouse liver study"), |
| 55 | +#' Type = c("Expression profiling by array", "RNA-seq"), |
| 56 | +#' Samples = c(12L, 8L) |
| 57 | +#' ) |
| 58 | +#' qc <- geo_qc(NULL, records, table_name = "geo_records", cleanup = TRUE) |
| 59 | +#' qc$cleanup() |
| 60 | +#' } |
| 61 | +#' @seealso [geo_meta()], [geo_search()], [QueryChat][querychat::QueryChat], |
| 62 | +#' [ellmer::chat_openai()] |
| 63 | +#' @export |
| 64 | +geo_qc <- function(client, data_source, table_name = NULL, ..., |
| 65 | + instructions = NULL) { |
| 66 | + rlang::check_installed("querychat") |
| 67 | + assert_string(instructions, allow_null = TRUE) |
| 68 | + extra_instructions <- geo_qc_instructions(instructions) |
| 69 | + if (is.null(table_name)) { |
| 70 | + if (is.data.frame(data_source) || |
| 71 | + inherits(data_source, "tbl_sql")) { |
| 72 | + table_name <- deparse1(substitute(data_source)) |
| 73 | + } |
| 74 | + } |
| 75 | + querychat::querychat( |
| 76 | + data_source = data_source, |
| 77 | + table_name = table_name, |
| 78 | + client = client, |
| 79 | + extra_instructions = extra_instructions, |
| 80 | + ..., |
| 81 | + prompt_template = NULL |
| 82 | + ) |
| 83 | +} |
| 84 | + |
| 85 | +#' @export |
| 86 | +#' @rdname geo_qc |
| 87 | +geo_shiny <- function(...) { |
| 88 | + qc <- geo_qc(...) |
| 89 | + qc$app() |
| 90 | +} |
| 91 | + |
| 92 | +#' @export |
| 93 | +#' @rdname geo_qc |
| 94 | +geo_chat <- function(...) { |
| 95 | + qc <- geo_qc(...) |
| 96 | + qc$console() |
| 97 | +} |
| 98 | + |
| 99 | +geo_qc_instructions <- function(instructions = NULL) { |
| 100 | + prompt <- paste( |
| 101 | + "You are a bioinformatics helper for GEO (Gene Expression Omnibus) metadata.", |
| 102 | + "Use the querychat data context and tools to query and filter the metadata.", |
| 103 | + "Help identify relevant studies, samples, platforms, and datasets.", |
| 104 | + "Generate clear R code when the user asks for reproducible filtering or analysis steps.", |
| 105 | + "When generating R code, use backticks around non-syntactic column names such as names containing spaces, punctuation, or slashes.", |
| 106 | + "Explain GEO accession types when useful: GSE for series, GSM for samples, GPL for platforms, and GDS for curated datasets.", |
| 107 | + "When answering from the metadata, prefer explicit accession IDs and avoid guessing beyond the available data.", |
| 108 | + sep = "\n" |
| 109 | + ) |
| 110 | + if (is.null(instructions)) { |
| 111 | + return(prompt) |
| 112 | + } |
| 113 | + paste( |
| 114 | + prompt, |
| 115 | + "Additional instructions:", instructions, |
| 116 | + sep = "\n\n" |
| 117 | + ) |
| 118 | +} |
0 commit comments