File size: 9,751 Bytes
14124c0
 
 
 
 
5a7aca2
14124c0
c2fd183
 
 
14124c0
c2fd183
c399c9b
c2fd183
 
 
 
5a7aca2
7446924
 
14124c0
c399c9b
 
 
 
 
14124c0
 
 
 
 
 
 
c2fd183
 
7446924
 
 
c2fd183
7446924
c2fd183
 
 
 
 
 
 
 
338ca35
c2fd183
 
 
 
14124c0
 
 
 
c399c9b
 
c2fd183
c399c9b
c2fd183
c399c9b
 
 
 
c2fd183
c399c9b
c2fd183
14124c0
d2d3b8d
c399c9b
 
14124c0
c2fd183
 
 
 
 
d2d3b8d
 
 
f942fde
d2d3b8d
c2fd183
 
 
 
 
 
 
 
 
 
7446924
c2fd183
 
 
 
 
 
 
14124c0
 
c2fd183
 
14124c0
 
 
 
 
 
c2fd183
 
 
14124c0
a6ad9de
 
 
5a7aca2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6ad9de
14124c0
 
5a7aca2
 
c2fd183
f942fde
c2fd183
7446924
f942fde
c2fd183
 
5a7aca2
 
 
f942fde
 
c399c9b
f942fde
 
14124c0
5a7aca2
 
 
14124c0
a6ad9de
5a7aca2
 
f942fde
 
5a7aca2
 
a6ad9de
5a7aca2
 
 
 
 
 
 
 
14124c0
5a7aca2
 
c399c9b
32deb59
c399c9b
5a7aca2
14124c0
f942fde
5a7aca2
f942fde
 
 
 
 
 
 
 
 
5a7aca2
f942fde
 
 
 
 
 
 
14124c0
5a7aca2
c2fd183
 
14124c0
c2fd183
 
14124c0
c2fd183
 
 
14124c0
c2fd183
 
14124c0
c2fd183
 
14124c0
 
c2fd183
 
 
 
5a7aca2
7446924
c2fd183
5a7aca2
 
 
 
c2fd183
 
 
 
7446924
5a7aca2
7446924
 
c2fd183
 
 
7446924
 
 
32deb59
 
 
 
 
7446924
 
 
 
 
 
 
 
 
c2fd183
 
 
7446924
c2fd183
32deb59
338ca35
32deb59
c2fd183
7446924
c2fd183
f942fde
c2fd183
 
 
 
 
 
 
7446924
 
c2fd183
 
 
 
 
 
7446924
32deb59
c2fd183
 
 
7446924
 
 
 
 
c2fd183
 
 
14124c0
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
library(shiny)
library(bslib)
library(htmltools)
library(fontawesome)
library(bsicons)
library(bench)
library(glue)
library(sf)
library(duckdb.agent)
library(duckdbfs)
library(dplyr)
library(ellmer)
library(mapgl)
library(digest)
library(stringr)
library(shinybusy)

source("utils.R")

duckdbfs::load_spatial()

css <-
  HTML(paste0("<link rel='stylesheet' type='text/css' ",
              "href='https://demos.creative-tim.com/",
              "material-dashboard/assets/css/",
              "material-dashboard.min.css?v=3.2.0'>"))


# Define the UI
ui <- page_sidebar(
  fillable = FALSE, # do not squeeze to vertical screen space
  tags$head(css),
  titlePanel("Demo App"),
  shinybusy::add_busy_spinner(),
  
  markdown("
  Select a desired area with the draw tools on the map, using the search bar if desired. 
  Then hit **Set Area of Interest** to select.
  Then, enter your query in the text box below the map to count occurrences of your specified taxonomic group.
  Use the airplane button to send your query.  The computation may take a few minutes depending on the size and scale of
  the search.
"),

p("
Scroll to zoom, ctrl+click to pitch and rotate. Hitting the area button with no selection to include the entire map.
"),

  layout_columns(
    card(maplibreOutput("map", , height = "600px")),
    div(actionButton("get_features", "Set Area Of Interest", icon = icon("object-group"),
                   class = "btn-primary align-bottom")),
    col_widths = c(11,1)
    ),


  card(
    layout_columns(
      textInput("chat",
        label = NULL,
        "show all bird occurrences at zoom level 6",
        width = "100%"),

      div(
      actionButton("user_msg", "", icon = icon("paper-plane"),
                   class = "btn-primary btn-sm align-bottom"),
      class = "align-text-bottom"),
      col_widths = c(11, 1),
      fill = FALSE
    ),
  ),
  
  textOutput("agent"),

  sidebar = sidebar(
    card(fill = TRUE,
    card_header("Selected area:"),
    verbatimTextOutput("feature_output")
    ),

    selectInput(
    "select",
    "Select an LLM:", 
    list("Llama3.3-cirrus" = "Llama3.3-cirrus")
  ),
    card(fill = TRUE,
      card_header(fa("robot"),  textOutput("model", inline = TRUE)),
      accordion(
        open = TRUE,
        accordion_panel(
          HTML("<span, class='text-info'>Show SQL query</span>"),
          icon = fa("terminal"),
          verbatimTextOutput("sql_code")
        ),
        accordion_panel(
          title = HTML("<span, class='text-info'>Explain query</span>"),
          icon = fa("user", prefer_type = "solid"),
          textOutput("explanation")
        )
      )
    ),
   
   
    card(
      card_header(bs_icon("github"), "Source code:"),
      a(href = "https://github.com/boettiger-lab/biodiversity-justice",
        "https://github.com/boettiger-lab/biodiversity-justice"))
  ),

  theme = bs_theme(version = "5")
)


duckdb_secrets(Sys.getenv("MINIO_KEY"),
            Sys.getenv("MINIO_SECRET"),
            "minio.carlboettiger.info")




    # system prompt generation is slow, do only once??

    system_prompt = create_prompt(additional_instructions =
    "Note that the columns h1, h2, h3, through h11 contains a geohash representing a H3 hexagon index.
    Higher numbers indicate higher zoom resolution (smaller hexes)
    Always aggregate results to count the number of rows matching
    the query to the desired hexagon. Always name the count column 'count'.
    Remember to group by hexagon level to aggregate! 

    Always rename the chosen hexagon column as 'h3id' in your final answer.
    Only select the h3id and count in your final answer. 

    Examples: 
  user: 'show all bird occurrences at zoom level 6'

  your reply: 

    {
    'query': 'CREATE OR REPLACE VIEW bird_occurrences_h6 AS SELECT gbif.h6 AS h3id, COUNT(*) AS count FROM gbif WHERE gbif.class = 'Aves' GROUP BY gbif.h6',
    'table_name': 'bird_occurrences_h6',
    'explanation': 'This query creates a view that shows the count of bird occurrences at zoom level 6. It selects the h6 column as the hexagon id, counts the number of rows for each hexagon, and groups the results by the h6 column.'
    }

    Refer to the full table by its table name as given above.
    Be sure to list column names 
    Be sure to generate fully valid SQL. Check your SQL for possible errors.
    

    Do not use the 'scientificname' column! Instead, filter specific species using the
    binomial name as the 'species' column.

    IMPORTANT: return raw JSON only, do not decorate your reply with markdown code syntax.
    ")




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

  # first we draw the map with geosearch and draw controls.
  output$map <- renderMaplibre({
    m <- maplibre(center = c(-110, 38), zoom = 2, pitch = 0, maxZoom = 12) |>
      add_draw_control() |>
      add_geocoder_control()

    m
  })

  # React to user's polygon
  observeEvent(input$get_features, {
    bounds <- ""
    aoi_info <- NULL

    drawn_features <- get_drawn_features(mapboxgl_proxy("map"))
    if(nrow(drawn_features) > 0) {

      aoi <- as_dataset.sf(drawn_features)
      h3_aoi <- get_h3_aoi(aoi)
      subset <- h3_aoi |> distinct(h0) |> pull(h0)


      print(h3_aoi)

      urls <- paste0("https://minio.carlboettiger.info/public-gbif/hex/h0=", subset, "/part0.parquet")
      gbif <- open_dataset(urls, tblname = "gbif")
      # would be better to spatial join
      bounds <- st_bbox(drawn_features)

   #   timer <- bench::bench_time({
   #   xmin <- bounds[1]; ymin <- bounds[2]; xmax <- bounds[3]; ymax <- bounds[4]
   #   open_dataset(urls, tblname = "gbif") |> 
   #     #filter(between(decimallongitude, xmin, xmax), between(decimallatitude, ymin, ymax)) |> 
   #     mutate(geom = st_geomfromwkb(geom)) |> spatial_join(aoi) |>
   #     as_view("gbif_aoi")
   #   })
   #   print(timer)

      output$feature_output <- renderPrint(print(bounds))
    }



  observeEvent(input$user_msg, {

    model <- reactive(input$select)()

    if (grepl("cirrus", model)) {
      agent <- 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)
      )
    } else {
      agent <- ellmer::chat_vllm( # NRP models have too small a context window for useful interaction
        base_url = "https://llm.nrp-nautilus.io/",
        model = model,
        api_key = Sys.getenv("NRP_API_KEY"),
        system_prompt = system_prompt,
        api_args = list(temperature = 0)
      )
    }


    print("Agent thinking...")
    stream <- agent$chat(input$chat)

    # Parse response
    response <- jsonlite::fromJSON(stream)

    if ("query" %in% names(response)) {
      output$sql_code <- renderText(str_wrap(response$query, width = 60))
      output$explanation <- renderText(response$explanation)

    # clear agent memory
    agent$set_turns(NULL)

    } else {
      output$agent <- renderText(response$agent)
    }

      # cache the query
      query_id <- digest::digest(paste(response$query, bounds, collapse=""))
      data_url <- glue::glue("https://minio.carlboettiger.info/public-data/cache/{query_id}.h3j")
      
      # use tempfile as cache.  we could use database tempdir
      cache_parquet <- tempfile(glue("{query_id}"), fileext = ".parquet")




      # compute if not yet in cache
      status <- httr::status_code(httr::HEAD(data_url))
      if(status == 404) {
        print("Computing...")
        time <- bench::bench_time({
          agent_query(stream) |>
          hex_join(h3_aoi) |>
          mutate(log_count = log(count)) |>
          write_dataset(cache_parquet)
        })
        print(time)
      }
      cached_data <- open_dataset(cache_parquet)

      # so we can scale color and height to max value
      biggest <-
        cached_data |> 
        summarise(max = max(log_count)) |>
        pull(max) |>
        first()

      # so we can zoom to the selected data (choose random point)
      aoi_info <- cached_data |>
        head(1) |> 
        mutate(zoom = h3_get_resolution(h3id),
               lat = h3_cell_to_lat(h3id),
               lng = h3_cell_to_lng(h3id)) |>
        collect()
                  

      # draw on map
      h3j <- glue("s3://public-data/cache/{query_id}.h3j")
      cached_data |> to_h3j(h3j)

      # adjust v-scale based on zoom:
      vscale <- 10000 / aoi_info$zoom

      # override previous map with drawn map
      # we should use set_h3j_source and set_layer on maplibre_proxy instead.
      output$map <- renderMaplibre({
          m <- maplibre(center=c(-110, 38), zoom = 1, pitch = 0, maxZoom = 12) |>
          add_h3j_source("h3j_source",
                        url = data_url) |>
          add_fill_extrusion_layer(
            id = "h3j_layer",
            source = "h3j_source",
            tooltip = "count",
            fill_extrusion_color = interpolate(
              column = "log_count",
              values = c(0, biggest),
              stops = c("#430254", "#f83c70")
            ),
            fill_extrusion_height = list(
              "interpolate",
              list("linear"),
              list("zoom"),
              0, 0, biggest,
              list("*", vscale, list("get", "log_count"))
            ),
            fill_extrusion_opacity = 0.7
          )
        if (!is.null(aoi_info)) {
          m <- m |> fly_to(c(aoi_info$lng, aoi_info$lat), zoom = (aoi_info$zoom - 1))
        }

        m
      }) # close renderMaplibre
    }) # close observeEvent->get_features
  }) # close observeEvent->user_msg
}

# Run the app
shinyApp(ui = ui, server = server)