Skip to content

Instantly share code, notes, and snippets.

@david-diviny-nousgroup
Created November 17, 2024 23:24
Show Gist options
  • Save david-diviny-nousgroup/a24cc60abbcfaba24446acd1df236ac8 to your computer and use it in GitHub Desktop.
Save david-diviny-nousgroup/a24cc60abbcfaba24446acd1df236ac8 to your computer and use it in GitHub Desktop.
Generate a Mermaid.js diagram using LLM with elmer package to explain code
library(elmer)
library(tidyverse)
library(DiagrammeR)
library(glue)
code <- "starwars %>%
group_by(species) %>%
summarise(
n = n(),
mass = mean(mass, na.rm = TRUE)
) %>%
filter(
n > 1,
mass > 50
)"
chat <- chat_openai(
model = "gpt-4o",
system_prompt = "You are a friendly but terse assistant.",
echo = TRUE,
api_args = list(temperature = 0)
)
response <- chat$chat(glue("Generate mermaid.js code explaining this R code:
{code}"))
mermaid_code <- str_extract(response, "(?s)(?<=```mermaid\\n).*?(?=```)")
DiagrammeR(mermaid_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment