-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathprovider-openai.R
358 lines (323 loc) · 9.51 KB
/
provider-openai.R
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
#' @include provider.R
#' @include content.R
#' @include turns.R
#' @include tools-def.R
NULL
#' Chat with an OpenAI model
#'
#' @description
#' [OpenAI](https://openai.com/) provides a number of chat-based models,
#' mostly under the [ChatGPT](https://chat.openai.com/) brand.
#' Note that a ChatGPT Plus membership does not grant access to the API.
#' You will need to sign up for a developer account (and pay for it) at the
#' [developer platform](https://platform.openai.com).
#'
#' @param system_prompt A system prompt to set the behavior of the assistant.
#' @param base_url The base URL to the endpoint; the default uses OpenAI.
#' @param api_key `r api_key_param("OPENAI_API_KEY")`
#' @param model The model to use for the chat. The default, `NULL`, will pick
#' a reasonable default, and tell you about. We strongly recommend explicitly
#' choosing a model for all but the most casual use.
#' @param params Common model parameters, usually created by [params()].
#' @param seed Optional integer seed that ChatGPT uses to try and make output
#' more reproducible.
#' @param api_args Named list of arbitrary extra arguments appended to the body
#' of every chat API call. Combined with the body object generated by ellmer
#' with [modifyList()].
#' @param echo One of the following options:
#' * `none`: don't emit any output (default when running in a function).
#' * `text`: echo text output as it streams in (default when running at
#' the console).
#' * `all`: echo all input and output.
#'
#' Note this only affects the `chat()` method.
#' @family chatbots
#' @export
#' @returns A [Chat] object.
#' @examplesIf has_credentials("openai")
#' chat <- chat_openai()
#' chat$chat("
#' What is the difference between a tibble and a data frame?
#' Answer with a bulleted list
#' ")
#'
#' chat$chat("Tell me three funny jokes about statisticians")
chat_openai <- function(
system_prompt = NULL,
base_url = "https://api.openai.com/v1",
api_key = openai_key(),
model = NULL,
params = NULL,
seed = deprecated(),
api_args = list(),
echo = c("none", "output", "all")
) {
model <- set_default(model, "gpt-4o")
echo <- check_echo(echo)
params <- params %||% params()
if (lifecycle::is_present(seed)) {
lifecycle::deprecate_warn(
when = "0.2.0",
what = "chat_openai(seed)",
with = "chat_openai(params)"
)
params$seed <- seed
}
provider <- ProviderOpenAI(
name = "OpenAI",
base_url = base_url,
model = model,
params = params,
extra_args = api_args,
api_key = api_key
)
Chat$new(provider = provider, system_prompt = system_prompt, echo = echo)
}
chat_openai_test <- function(..., model = "gpt-4o-mini", params = NULL) {
params <- params %||% params()
if (is_testing()) {
params$seed <- params$seed %||% 1014
params$temperature <- params$temperature %||% 0
}
chat_openai(model = model, params = params, ...)
}
ProviderOpenAI <- new_class(
"ProviderOpenAI",
parent = Provider,
properties = list(
api_key = prop_string(),
# no longer used by OpenAI itself; but subclasses still need it
seed = prop_number_whole(allow_null = TRUE)
)
)
openai_key_exists <- function() {
key_exists("OPENAI_API_KEY")
}
openai_key <- function() {
key_get("OPENAI_API_KEY")
}
# https://platform.openai.com/docs/api-reference/chat/create
method(chat_request, ProviderOpenAI) <- function(
provider,
stream = TRUE,
turns = list(),
tools = list(),
type = NULL
) {
req <- request(provider@base_url)
req <- req_url_path_append(req, "/chat/completions")
req <- req_auth_bearer_token(req, provider@api_key)
req <- req_retry(req, max_tries = 2)
req <- ellmer_req_timeout(req, stream)
req <- ellmer_req_user_agent(req)
req <- req_error(req, body = function(resp) {
if (resp_content_type(resp) == "application/json") {
resp_body_json(resp)$error$message
} else if (resp_content_type(resp) == "text/plain") {
resp_body_string(resp)
}
})
messages <- compact(unlist(as_json(provider, turns), recursive = FALSE))
tools <- as_json(provider, unname(tools))
if (!is.null(type)) {
response_format <- list(
type = "json_schema",
json_schema = list(
name = "structured_data",
schema = as_json(provider, type),
strict = TRUE
)
)
} else {
response_format <- NULL
}
params <- chat_params(provider, provider@params)
params$seed <- params$seed %||% provider@seed
body <- compact(list2(
messages = messages,
model = provider@model,
!!!params,
stream = stream,
stream_options = if (stream) list(include_usage = TRUE),
tools = tools,
response_format = response_format
))
body <- utils::modifyList(body, provider@extra_args)
req <- req_body_json(req, body)
req
}
method(chat_params, ProviderOpenAI) <- function(provider, params) {
standardise_params(
params,
c(
temperature = "temperature",
top_p = "top_p",
frequency_penalty = "frequency_penalty",
presence_penalty = "presence_penalty",
seed = "seed",
max_tokens = "max_completion_tokens",
logprobs = "log_probs",
stop = "stop_sequences"
)
)
}
# OpenAI -> ellmer --------------------------------------------------------------
method(stream_parse, ProviderOpenAI) <- function(provider, event) {
if (is.null(event) || identical(event$data, "[DONE]")) {
return(NULL)
}
jsonlite::parse_json(event$data)
}
method(stream_text, ProviderOpenAI) <- function(provider, event) {
if (length(event$choices) == 0) {
NULL
} else {
event$choices[[1]]$delta$content
}
}
method(stream_merge_chunks, ProviderOpenAI) <- function(
provider,
result,
chunk
) {
if (is.null(result)) {
chunk
} else {
merge_dicts(result, chunk)
}
}
method(value_turn, ProviderOpenAI) <- function(
provider,
result,
has_type = FALSE
) {
if (has_name(result$choices[[1]], "delta")) {
# streaming
message <- result$choices[[1]]$delta
} else {
message <- result$choices[[1]]$message
}
if (has_type) {
json <- jsonlite::parse_json(message$content[[1]])
content <- list(ContentJson(json))
} else {
content <- lapply(message$content, as_content)
}
if (has_name(message, "tool_calls")) {
calls <- lapply(message$tool_calls, function(call) {
name <- call$`function`$name
# TODO: record parsing error
args <- jsonlite::parse_json(call$`function`$arguments)
ContentToolRequest(name = name, arguments = args, id = call$id)
})
content <- c(content, calls)
}
tokens <- tokens_log(
provider,
input = result$usage$prompt_tokens,
output = result$usage$completion_tokens
)
Turn(message$role %||% "assistant", content, json = result, tokens = tokens)
}
# ellmer -> OpenAI --------------------------------------------------------------
method(as_json, list(ProviderOpenAI, Turn)) <- function(provider, x) {
if (x@role == "system") {
list(
list(role = "system", content = x@contents[[1]]@text)
)
} else if (x@role == "user") {
# Each tool result needs to go in its own message with role "tool"
is_tool <- map_lgl(x@contents, S7_inherits, ContentToolResult)
content <- as_json(provider, x@contents[!is_tool])
if (length(content) > 0) {
user <- list(list(role = "user", content = content))
} else {
user <- list()
}
tools <- lapply(x@contents[is_tool], function(tool) {
list(
role = "tool",
content = tool_string(tool),
tool_call_id = tool@request@id
)
})
c(user, tools)
} else if (x@role == "assistant") {
# Tool requests come out of content and go into own argument
is_tool <- map_lgl(x@contents, S7_inherits, ContentToolRequest)
content <- as_json(provider, x@contents[!is_tool])
tool_calls <- as_json(provider, x@contents[is_tool])
list(
compact(list(
role = "assistant",
content = content,
tool_calls = tool_calls
))
)
} else {
cli::cli_abort("Unknown role {x@role}", .internal = TRUE)
}
}
method(as_json, list(ProviderOpenAI, ContentText)) <- function(provider, x) {
list(type = "text", text = x@text)
}
method(as_json, list(ProviderOpenAI, ContentImageRemote)) <- function(
provider,
x
) {
list(type = "image_url", image_url = list(url = x@url))
}
method(as_json, list(ProviderOpenAI, ContentImageInline)) <- function(
provider,
x
) {
list(
type = "image_url",
image_url = list(
url = paste0("data:", x@type, ";base64,", x@data)
)
)
}
method(as_json, list(ProviderOpenAI, ContentToolRequest)) <- function(
provider,
x
) {
json_args <- jsonlite::toJSON(x@arguments)
list(
id = x@id,
`function` = list(name = x@name, arguments = json_args),
type = "function"
)
}
method(as_json, list(ProviderOpenAI, ToolDef)) <- function(provider, x) {
list(
type = "function",
"function" = compact(list(
name = x@name,
description = x@description,
strict = TRUE,
parameters = as_json(provider, x@arguments)
))
)
}
method(as_json, list(ProviderOpenAI, TypeObject)) <- function(provider, x) {
if (x@additional_properties) {
cli::cli_abort("{.arg .additional_properties} not supported for OpenAI.")
}
names <- names2(x@properties)
properties <- lapply(x@properties, function(x) {
out <- as_json(provider, x)
if (!x@required) {
out$type <- c(out$type, "null")
}
out
})
names(properties) <- names
list(
type = "object",
description = x@description %||% "",
properties = properties,
required = as.list(names),
additionalProperties = FALSE
)
}