R/Medicine Workshop
2025-06-11
.Renviron“What is the capitol of France?”
"Paris."
“What is its most famous landmark?”
"The Eiffel Tower."
curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-4.1",
    "messages": [
        {"role": "system", "content": "You are a terse assistant."},
        {"role": "user", "content": "What is the capitol of France?"}
    ]
}'curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-4.1",
    "messages": [
      {"role": "system", "content": "You are a terse assistant."},
      {"role": "user", "content": "What is the capitol of France?"},
      {"role": "assistant", "content": "Paris."},
      {"role": "user", "content": "What is its most famous landmark?"}
    ]
}'un|con|ventionalellmer::chat() automatically streams output to the console by defaultlive_console(chat) or live_browser(chat)client$chat(prompt) returns a stringclient$stream(prompt) returns a streaming output objectclient$chat_async(prompt) and client$stream_async(prompt)Open and run 01-basics.R.
If it errors, now is the time to debug; open troubleshoot.R and run it. Otherwise:
live_browser(client) to open a browser-based chat clientsystem_prompt and see how it affects the outputrole (“system”, “user”, “assistant”) and a content string{shinychat} package
https://github.com/posit-dev/shinychat
03-shiny-chat-app.R for an exampleui.Chat for data privacy reasons, so instead…Another way to think of it:
 LLM
 LLM with system prompt
 LLM with system prompt and tool calling
02-tools-weather.R, skim the code, and run it.02-tools-quiz.R.Goal: Extract ingredient list from recipe and return in a structured format.
Example user input:
In a large bowl, cream together 1 cup of softened unsalted butter and ½ cup of white sugar until smooth. Beat in 1 egg and 1 teaspoon of vanilla extract. Gradually stir in 2 cups of all-purpose flour until the dough forms. Finally, fold in 1 cup of semisweet chocolate chips. Drop spoonfuls of dough onto an ungreased baking sheet and bake at 350°F (175°C) for 10-12 minutes, or until the edges are lightly browned. Let the cookies cool on the baking sheet for a few minutes before transferring to a wire rack to cool completely. Enjoy!The user input contains a recipe. Extract a list of ingredients and return it in JSON format.
Assistant response:
The user input contains a recipe. Extract a list of ingredients and return it in JSON format. It should be an array of objects, where each object has keys `ingredient`, `quantity`, and `unit`. Put each object on one line of output.
Assistant response:
[
    {"ingredient": "unsalted butter", "quantity": 1, "unit": "cup"},
    {"ingredient": "white sugar", "quantity": 1/2, "unit": "cup"},
    {"ingredient": "egg", "quantity": 1, "unit": "large"},
    {"ingredient": "vanilla extract", "quantity": 1, "unit": "teaspoon"},
    {"ingredient": "all-purpose flour", "quantity": 2, "unit": "cups"},
    {"ingredient": "semisweet chocolate chips", "quantity": 1, "unit": "cup"}
]The user input contains a recipe. Extract a list of ingredients and return it in JSON format.
Example Output:
```json
[
    { "ingredient": "Flour", "quantity": 1, "unit": "cup" },
    { "ingredient": "Vegetable oil", "quantity": 0.5, "unit": "tsp" },
    { "ingredient": "Onion", "quantity": 1, "unit": null },
]
```
Assistant response:
[
    { "ingredient": "Unsalted butter", "quantity": 1, "unit": "cup" },
    { "ingredient": "White sugar", "quantity": 0.5, "unit": "cup" },
    { "ingredient": "Egg", "quantity": 1, "unit": null },
    { "ingredient": "Vanilla extract", "quantity": 1, "unit": "teaspoon" },
    { "ingredient": "All-purpose flour", "quantity": 2, "unit": "cups" },
    { "ingredient": "Semisweet chocolate chips", "quantity": 1, "unit": "cup" }
]LLMs are good at generating unstructured output, but with a little effort, you can get structured output as well.
/```json\n(.*?)\n```/)set_result(object), where its implementation sets some variable. (Works great for ellmer.)Open 04-structured.R, skim the code, and run it.
05-vision.R, skim the code, and run it.https://jcheng5.github.io/rmedicine-2025