forked from simonpcouch/openscapes-25
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.qmd
More file actions
372 lines (269 loc) · 8.24 KB
/
Copy pathindex.qmd
File metadata and controls
372 lines (269 loc) · 8.24 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
---
title: ""
format:
revealjs:
theme: [default, custom.scss]
footer: '<span style="color:#aa5b31;">github.com/simonpcouch/openscapes-25</span>'
editor: source
knitr:
opts_chunk:
echo: true
collapse: true
comment: "#>"
---
## Writing R code with the help of LLMs
<a href="github.com/simonpcouch/openscapes-25"><img src="figures/wall.png" align="right" height="350" alt="Hex wall" /></a>
<br><br><br><br><br><br><br>
_Simon Couch_ - Posit, PBC
Open Source Group, R / LLMs
```{r}
#| label: load-pkgs
#| message: false
#| warning: false
#| eval: false
#| include: false
remotes::install_github("luisdva/hexsession")
library(hexsession)
library(acquaint)
library(btw)
library(chores)
library(ellmer)
library(gander)
library(vitals)
library(hexsession)
```
##
```{r}
#| label: productivity-plot
#| echo: false
#| fig.width: 10
#| fig.height: 6
library(ggplot2)
library(dplyr)
set.seed(123)
data <- tibble(
gain = c(rnorm(100, mean = 1.4, sd = 0.3), rnorm(100, mean = 10, sd = 1.5)),
type = rep(
c("Complex, production software", "Fun little throwaway programs"),
each = 100
)
)
ggplot(data, aes(x = gain, fill = type)) +
geom_density() +
scale_fill_manual(values = c("#aa5b31", "#3b7c98")) +
labs(
title = "Productivity gains from LLMs are bimodal",
x = "Productivity gain",
y = "Density",
fill = "Type"
) +
theme_minimal() +
theme(
legend.position = "bottom",
text = element_text(size = 16)
) +
scale_x_continuous(breaks = c(1, 5, 10), labels = c("1x", "5x", "10x"))
```
. . .
Today, we're talking about the LHS⬅️
# Part 1: Have a chat
## Part 1: Have a chat <a href="https://ellmer.tidyverse.org"><img src="figures/hexes/ellmer.png" align="right" height="400" alt="ellmer website" /></a>
Meet ellmer!🐘
<br>
. . .
::: columns
::: {.column width="50%"}
```{r}
#| label: install-ellmer
#| eval: false
install.packages("ellmer")
```
:::
::: {.column width="50%"}
:::
:::
## Part 1: Have a chat
```{r}
#| label: load-ellmer
#| include: false
library(ellmer)
```
These are the same:
::: columns
::: {.column width="50%"}

:::
::: {.column width="50%"}
<br>
```{r}
#| label: chat-example
#| eval: false
library(ellmer)
ch <- chat_github(
model = "gpt-4o"
)
ch$chat("hey!")
#> "Hey there!😊 What can I
#> help you with today?"
```
:::
:::
## Part 1: Have a chat
Your turn! Create a chat object and say "hey!"
* `chat_github()` might "just work"
* If not, set up `chat_anthropic()` using instructions linked below
```{r 1-have-a-chat}
#| echo: false
countdown::countdown(minutes = 3, id = "have-a-chat")
```
<!--
Interactively:
* Chat a couple times
* Extract the turns information
-->
# Part 2: The system prompt
## Part 2: The system prompt
* An "invisible message" at the start of your chat
* Use it to influence behavior, give knowledge, define output format, etc
. . .
```{r}
#| label: system-prompt-example
#| eval: false
ch <- chat_anthropic(
system_prompt =
"Try and tie any response back to the Openscapes organization."
)
ch$chat("What's 2+2?")
```
## Part 2: The system prompt
Your turn: adjust the system prompt to the model so that, when supplied a question like "What's 2+2?", the model returns _only_ the answer as a _word_ (rather than a digit), no punctuation or exposition.
<br>
```{r}
#| label: system-prompt-exercise
#| eval: false
ch$chat("What's 2+2?")
#> four
```
```{r 2-system-prompt}
#| echo: false
countdown::countdown(minutes = 3, id = "system-prompt")
```
## Part 2: The system prompt
You can get a [lot of mileage](https://github.com/simonpcouch/chores/blob/main/inst/prompts/roxygen-prefix.md) out of the system prompt:
```{r}
#| label: complex-system-prompt
#| eval: false
ch <- chat_anthropic(
system_prompt = paste0(readLines(
"https://raw.githubusercontent.com/simonpcouch/chores/refs/heads/main/inst/prompts/roxygen-prefix.md"
), collapse = "\n")
)
```
## Part 2: The system prompt
What if that wasn't super difficult to interface with?
{{< video figures/chore-roxygen.mov >}}
## Part 2: The system prompt <a href="https://simonpcouch.github.io/chores/"><img src="figures/hexes/chores.png" align="right" height="200" alt="chores website" /></a>
<br>
```r
install.packages("chores")
```
* Attach system prompts to a key command in your IDE
* Select some code, press the command, and watch code stream in
* Supports common R package development actions by default
# Intermission: tokens
## Intermission: tokens
OpenAI and Anthropic have two main ways they make money from their models:
1) Subscription plans (like chatgpt.com)
2) API usage (like from ellmer)
## Intermission: tokens
API usage is "pay-as-you-go" by _token_:
- Words, parts of words, or individual characters
- "hello" → 1 token
- "unconventional" → 3 tokens: `un|con|ventional`
## Intermission: tokens
Here's the pricing _per million tokens_ for some common models:
<br>
```{r}
#| label: pricing-table
#| echo: false
pricing <-
tibble::tribble(
~Name, ~Input, ~Output,
"GPT 4o", "$3.75", "$15.00",
"GPT 4o-mini", "$0.15", "$0.60",
"Claude 4 Sonnet", "$3.00", "$15.00"
)
knitr::kable(pricing)
```
## Intermission: tokens
To put that into context, the source code for these slides so far is 650 tokens.
If I input them to GPT 4o:
$$
650 \text{ tokens} \times \frac{\$3.75 }{1,000,000~\text{tokens}} = \$0.00244
$$
# Part 3: Hallucination and code assistance
## Part 3: Hallucination and code assistance
```{r}
#| label: stackoverflow-data
#| eval: false
library(ggplot2)
library(modeldata)
stackoverflow
#> # A tibble: 5,594 × 21
#> Country Salary YearsCodedJob OpenSource Hobby CompanySizeNumber Remote
#> <fct> <dbl> <int> <dbl> <dbl> <dbl> <fct>
#> 1 United Kingdom 1 e5 20 0 1 5000 Remote
#> 2 United States 1.3 e5 20 1 1 1000 Remote
#> 3 United States 1.75e5 16 0 1 10000 Not r…
#> # ℹ 5,590 more rows
#> # ℹ 14 more variables: CareerSatisfaction <int>, Data_scientist <dbl>, …
```
If I type “plot salary vs experience”, what information does the model need access to complete that request?
## Part 3: Hallucination and code assistance
If I type “plot salary vs experience”, what information does the model need access to?
:::incremental
* The name of the relevant data frame
* My preferred plotting library
* The names of the relevant columns
:::
. . .
The first two can be inferred from the source code, but the third requires access to your R session.
## Part 3: Hallucination and code assistance <a href="https://simonpcouch.github.io/gander/"><img src="figures/hexes/gander.png" align="right" height="200" alt="gander website" /></a>
<br>
```r
install.packages("gander")
```
* An R coding assistant written in R
* Automatically incorporates relevant context from your R sessions
# Appendix A: Providers
## Appendix A: Providers
A "provider" is a service that hosts models on an API.
In ellmer, each provider has its own `chat_*()` function, like `chat_github()` or `chat_anthropic()`
. . .
* `chat_github()` serves some popular models, like GPT-4o, for "free"
- "free" in the sense of "we're going to use all of the data you send us"
- heavily rate-limited; you'll need to pay for even modest usage
## Appendix A: Providers
* `chat_openai()`
- traditionally, more consumer-focused
- weaker privacy guarantees
## Appendix A: Providers
* `chat_anthropic()` serves Claude Sonnet
- traditionally more developer/enterprised-focused
- stronger privacy guarantees
- subsidizes credits via Claude for Education
## Appendix A: Providers
You can be your own "provider", too:
* `chat_ollama()` uses a model that runs on your laptop
- much less powerful than the Big Ones
- "free" in the usual sense
## Appendix A: Providers
Many organizations have private deployments of models set up for internal, secure use. ellmer supports the common ones.
Ask around to see if this is the case at NOAA/NASA!
## Learn more
<br>
<span style="font-size:130%">
<center>[github.com/simonpcouch/openscapes-25]{style="color:#c46938;"}</center>
</span>
<br>
<a href="github.com/simonpcouch/openscapes-25"><img src="figures/wall.png" align="right" height="350" alt="Hex wall" /></a>