Skip to content

Commit

Permalink
and now with llm
Browse files Browse the repository at this point in the history
  • Loading branch information
cboettig committed Jan 2, 2025
1 parent 1367bbc commit 2b73487
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 366 deletions.
12 changes: 6 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ FROM rocker/geospatial:latest
WORKDIR /code

RUN install2.r --error \
bslib \
shiny \
dplyr \
ggplot2 \
readr \
ggExtra \
duckdbfs
shinychat \
tidyverse \
duckdbfs \
markdown

RUN installGithub.r cboettig/mapgl
RUN installGithub.r cboettig/mapgl tidyverse/ellmer

COPY . .

Expand Down
106 changes: 91 additions & 15 deletions app.R
Original file line number Diff line number Diff line change
@@ -1,32 +1,101 @@
library(shiny)
library(bslib)
library(markdown)
library(shinychat)
library(mapgl)
library(tidyverse)
library(duckdbfs)

pmtiles <- "https://data.source.coop/cboettig/us-boundaries/mappinginequality.pmtiles"

# Define the UI
ui <- page_sidebar(

titlePanel("Demo App"),
sidebar = sidebar(

textAreaInput("chat",
"Ask me a question!",
value = "Which state has the highest average social vulnerability?",
width = "100%",
height = 100
),

verbatimTextOutput("sql_code"),
textOutput("explanation"),

input_switch("redlines", "Redlined Areas"),
input_switch("svi", "Social Vulnerability", value = TRUE),
input_switch("richness", "Biodiversity Richness"),
input_switch("rsr", "Biodiversity Range Size Rarity"),
width = 300,
),
titlePanel("Demo App"),

),
# Create a main panel
card(full_screen = TRUE,
maplibreOutput("map")
layout_columns(
card(maplibreOutput("map")),
card(includeMarkdown("## Plot"),
plotOutput("chart1"),
plotOutput("chart2"),
),

)
)
col_widths = c(8,4)
),

tableOutput("table")

)

svi <- "https://data.source.coop/cboettig/social-vulnerability/svi2020_us_tract.parquet" |>
open_dataset(tblname = "svi")

con <- duckdbfs::cached_connection()
schema <- DBI::dbGetQuery(con, "PRAGMA table_info(svi)")
#schema <- svi |> head() |> collect() |> str()

system_prompt = glue::glue('
You are a helpful agent who always replies strictly in JSON-formatted text.
Your task is to translate the users question into a SQL query that will be run
against the "svi" table in a duckdb database. The duckdb database has a
spatial extension which understands PostGIS operations as well.
The table schema is <schema>
The column called "RPL_THEMES" corresponds to the overall "Social vulnerability index" number.
Format your answer as follows:
{
"query": "your raw SQL response goes here",
"explanation": "your explanation of the query"
}
', .open = "<", .close = ">")

# Define the server
server <- function(input, output, session) {
output$map <- renderMaplibre({

m <- maplibre(center=c(-92.9, 41.3), zoom=4)
chat <- ellmer::chat_vllm(
base_url = "https://llm.nrp-nautilus.io/",
model = "llama3",
api_key = Sys.getenv("NRP_API_KEY"),
system_prompt = system_prompt
)

observeEvent(input$chat, {
stream <- chat$chat(input$chat)

chat_append("chat", stream)
response <- jsonlite::fromJSON(stream)

output$sql_code <- renderText({response$query})
output$explanation <- renderText({response$explanation})

df <- DBI::dbGetQuery(con, response$query)
output$table <- renderTable(df, striped = TRUE)

})

output$map <- renderMaplibre({
m <- maplibre(center=c(-92.9, 41.3), zoom=3)

if (input$redlines) {
m <- m |>
Expand All @@ -44,7 +113,7 @@ server <- function(input, output, session) {
tiles = "https://data.source.coop/cboettig/mobi/tiles/red/species-richness-all/{z}/{x}/{y}.png",
maxzoom = 11
) |>
add_raster_layer(id = "richness-layer",
add_raster_layer(id = "richness-layer",
source = "richness")

}
Expand Down Expand Up @@ -72,15 +141,22 @@ server <- function(input, output, session) {
stops = c("lightblue", "darkblue"),
na_color = "lightgrey")
)
}




}
m})


m
})
chart1 <-
svi |>
filter(RPL_THEMES > 0) |>
group_by(COUNTY) |>
summarise(mean_svi = mean(RPL_THEMES)) |>
collect() |>
ggplot(aes(mean_svi)) + geom_density()

output$chart1 <- renderPlot(chart1)
output$chart2 <- renderPlot(chart1)

}

Expand Down
Loading

0 comments on commit 2b73487

Please sign in to comment.