File size: 2,257 Bytes
e0df861
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ec6b81
 
 
e0df861
1ec6b81
 
 
e0df861
 
 
 
1ec6b81
e0df861
1ec6b81
 
 
e0df861
 
 
 
1ec6b81
e0df861
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
## Illustrate/test core app functionality without shiny

library(tidyverse)
library(duckdbfs)
library(mapgl)
library(ellmer)
library(glue)

repo <- "https://data.source.coop/cboettig/social-vulnerability"
pmtiles <- glue("{repo}/svi2020_us_tract.pmtiles")
parquet <- glue("{repo}/svi2020_us_tract.parquet")
svi <- open_dataset(parquet, tblname = "svi") |> filter(RPL_THEMES > 0)

schema <- read_file("schema.yml")
system_prompt <- glue::glue(readr::read_file("system-prompt.md"),
                            .open = "<", .close = ">")



# Or optionally test with cirrus
chat <- ellmer::chat_vllm(
  base_url = "https://llm.cirrus.carlboettiger.info/v1/",
  model = "kosbu/Llama-3.3-70B-Instruct-AWQ",
  api_key = Sys.getenv("CIRRUS_LLM_KEY"),
  system_prompt = system_prompt,
  api_args = list(temperature = 0)
)

# or use the NRP model
chat <- ellmer::chat_vllm(
  base_url = "https://llm.nrp-nautilus.io/",
  model = "llama3",
  api_key = Sys.getenv("NRP_API_KEY"),
  system_prompt = system_prompt,
  api_args = list(temperature = 0)
)


# Test a chat-based response
chat$chat("Which columns describes racial components of social vulnerability?")
## A query-based response
stream <- chat$chat("Which counties in California have the highest average social vulnerability?")
response <- jsonlite::fromJSON(stream)

con <- duckdbfs::cached_connection()
filtered_data <- DBI::dbGetQuery(con, response$query)

filter_column <- function(full_data, filtered_data, id_col) {
  if (nrow(filtered_data) < 1) return(NULL)
  values <- full_data |>
    inner_join(filtered_data, copy = TRUE) |>
    pull(id_col)
  # maplibre syntax for the filter of PMTiles  
  list("in", list("get", id_col), list("literal", values))
}

maplibre(center = c(-102.9, 41.3), zoom = 3) |>
    add_fill_layer(
        id = "svi_layer",
        source = list(type = "vector", url  = paste0("pmtiles://", pmtiles)),
        source_layer = "SVI2000_US_tract",
        filter = filter_column(full_data, filtered_data, "FIPS"),
        fill_opacity = 0.5,
        fill_color = interpolate(column = "RPL_THEMES",
                                values = c(0, 1),
                                stops = c("#e19292c0", "darkblue"),
                                na_color = "lightgrey")
    )