Skip to content

Commit 7ad1630

Browse files
authored
set user agent in all embed_* functions (#53)
1 parent ca693e2 commit 7ad1630

File tree

7 files changed

+74
-30
lines changed

7 files changed

+74
-30
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ importFrom(httr2,req_body_json)
5353
importFrom(httr2,req_perform)
5454
importFrom(httr2,req_retry)
5555
importFrom(httr2,req_url_path_append)
56+
importFrom(httr2,req_user_agent)
5657
importFrom(httr2,request)
5758
importFrom(httr2,resp_body_json)
5859
importFrom(methods,is)

R/embed-bedrock.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ embed_bedrock <- function(x, model, profile, api_args = list()) {
5151
"https://bedrock-runtime.",
5252
credentials$region,
5353
".amazonaws.com"
54-
))
54+
)) |>
55+
req_user_agent(ragnar_user_agent())
5556

5657
req <- httr2::req_url_path_append(
5758
req,

R/embed-vertex.R

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@
2424
#' )
2525
#' }
2626
#'@export
27-
embed_google_vertex <- function(x, model, location, project_id, task_type = "RETRIEVAL_QUERY") {
27+
embed_google_vertex <- function(
28+
x,
29+
model,
30+
location,
31+
project_id,
32+
task_type = "RETRIEVAL_QUERY"
33+
) {
2834
if (missing(x) || is.null(x)) {
2935
args <- capture_args()
3036
fn <- partial(quote(ragnar::embed_google_vertex), alist(x = ), args)
@@ -55,6 +61,7 @@ embed_google_vertex <- function(x, model, location, project_id, task_type = "RET
5561

5662
base_req <- vertex_url(location, project_id) |>
5763
httr2::request() |>
64+
req_user_agent(ragnar_user_agent()) |>
5865
httr2::req_headers(!!!credentials, .redact = names(credentials)) |>
5966
httr2::req_url_path_append(
6067
"models",
@@ -65,13 +72,14 @@ embed_google_vertex <- function(x, model, location, project_id, task_type = "RET
6572
json$error$message
6673
})
6774

68-
6975
out <- list()
7076
# The gemini model does not support batches
7177
chunk_size <- if (grepl("gemini", model)) 1 else 20
7278

7379
for (indices in chunk_list(seq_along(x), chunk_size)) {
74-
instances <- lapply(indices, \(i) list(task_type = task_type, content = x[[i]]))
80+
instances <- lapply(indices, \(i) {
81+
list(task_type = task_type, content = x[[i]])
82+
})
7583

7684
resp <- base_req |>
7785
httr2::req_body_json(list(
@@ -95,16 +103,19 @@ embed_google_vertex <- function(x, model, location, project_id, task_type = "RET
95103
}
96104

97105
vertex_url <- function(location, project_id) {
98-
paste(unlist(list(
99-
c("https://", location, "-aiplatform.googleapis.com/v1"),
100-
c("/projects/", project_id),
101-
c("/locations/", location),
102-
"/publishers/google/"
103-
)), collapse="")
106+
paste(
107+
unlist(list(
108+
c("https://", location, "-aiplatform.googleapis.com/v1"),
109+
c("/projects/", project_id),
110+
c("/locations/", location),
111+
"/publishers/google/"
112+
)),
113+
collapse = ""
114+
)
104115
}
105116

106117

107-
google_credentials <- function (error_call = caller_env()) {
118+
google_credentials <- function(error_call = caller_env()) {
108119
scope <- "https://www.googleapis.com/auth/cloud-platform"
109120
if (has_connect_viewer_token(scope = scope)) {
110121
return(function() {
@@ -119,7 +130,6 @@ google_credentials <- function (error_call = caller_env()) {
119130

120131
check_installed("gargle", "for Google authentication")
121132

122-
123133
gargle::with_cred_funs(
124134
funs = list(credentials_app_default = gargle::credentials_app_default),
125135
{
@@ -133,10 +143,13 @@ google_credentials <- function (error_call = caller_env()) {
133143
}
134144

135145
if (is.null(token)) {
136-
cli::cli_abort(c(
137-
"No Google credentials are available.",
138-
i = "Try suppling an API key or configuring Google's application default credentials."
139-
), call = error_call)
146+
cli::cli_abort(
147+
c(
148+
"No Google credentials are available.",
149+
i = "Try suppling an API key or configuring Google's application default credentials."
150+
),
151+
call = error_call
152+
)
140153
}
141154

142155
if (!token$can_refresh()) {
@@ -155,7 +168,7 @@ google_credentials <- function (error_call = caller_env()) {
155168
})
156169
}
157170

158-
has_connect_viewer_token <- function (...) {
171+
has_connect_viewer_token <- function(...) {
159172
if (!is_installed("connectcreds")) {
160173
return(FALSE)
161174
}

R/embed.R

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ embed_ollama <- function(
6666

6767
embeddings <- map2(starts, ends, function(start, end) {
6868
req <- request(base_url) |>
69+
req_user_agent(ragnar_user_agent()) |>
6970
req_url_path_append("/api/embed") |>
7071
req_body_json(list(model = model, input = x[start:end]))
7172

@@ -90,7 +91,7 @@ embed_openai <- function(
9091
base_url = "https://api.openai.com/v1",
9192
api_key = get_envvar("OPENAI_API_KEY"),
9293
dims = NULL,
93-
user = get_ragnar_username(),
94+
user = get_user(),
9495
batch_size = 20L
9596
) {
9697
if (missing(x) || is.null(x)) {
@@ -138,6 +139,7 @@ embed_openai <- function(
138139
data$input <- as.list(text[start:end])
139140

140141
req <- request(base_url) |>
142+
req_user_agent(ragnar_user_agent()) |>
141143
req_url_path_append("/embeddings") |>
142144
req_auth_bearer_token(api_key) |>
143145
req_retry(max_tries = 2L) |>
@@ -184,10 +186,25 @@ get_envvar <- function(name, error_call = caller_env()) {
184186
val
185187
}
186188

187-
get_ragnar_username <- function() {
188-
sprintf("'%s' via ragnar", Sys.info()[["user"]])
189+
get_user <- function() {
190+
sys_info <- Sys.info()
191+
user <- sys_info[["effective_user"]]
192+
if (user != "unknown") {
193+
return(user)
194+
}
195+
user <- sys_info[["user"]]
196+
if (user != "unknown") {
197+
return(user)
198+
}
199+
NULL
200+
}
201+
202+
ragnar_user_agent <- function() {
203+
paste0("r-ragnar/", .package_version)
189204
}
190205

191206
is_testing <- function() {
192207
identical(Sys.getenv("TESTTHAT"), "true")
193208
}
209+
210+
.package_version <- c(read.dcf('DESCRIPTION', 'Version'))

R/utils.R

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#' vec_fill_missing vec_unique vec_slice vec_c list_unchop new_data_frame
1818
#' vec_chop
1919
#' @importFrom httr2 request req_url_path_append req_body_json req_perform
20-
#' resp_body_json req_retry req_auth_bearer_token
20+
#' resp_body_json req_retry req_auth_bearer_token req_user_agent
2121
#' @importFrom DBI dbExecute dbConnect dbExistsTable dbGetQuery dbQuoteString
2222
#' dbWriteTable dbListTables dbReadTable
2323
#' @importFrom glue glue glue_data as_glue
@@ -49,13 +49,17 @@ map_lgl <- function(.x, .f, ...) vapply(X = .x, FUN = .f, FUN.VALUE = TRUE, ...)
4949

5050
map2 <- function(.x, .y, .f, ...) {
5151
out <- .mapply(.f, list(.x, .y), list(...))
52-
if (length(.x) == length(out)) names(out) <- names(.x)
52+
if (length(.x) == length(out)) {
53+
names(out) <- names(.x)
54+
}
5355
out
5456
}
5557

5658
map3 <- function(.x, .y, .z, .f, ...) {
5759
out <- .mapply(.f, list(.x, .y, .z), list(...))
58-
if (length(.x) == length(out)) names(out) <- names(.x)
60+
if (length(.x) == length(out)) {
61+
names(out) <- names(.x)
62+
}
5963
out
6064
}
6165

@@ -84,13 +88,19 @@ imap <- function(.x, .f, ...) {
8488
# is_double2(x, c(NA, NA, NA)) # FALSE
8589
# is_double2(x, 12) # FALSE
8690
is_double2 <- function(x, dim = NULL) {
87-
if (is.null(dim)) return(is_double(x))
91+
if (is.null(dim)) {
92+
return(is_double(x))
93+
}
8894

89-
if (!is.double(x)) return(FALSE)
95+
if (!is.double(x)) {
96+
return(FALSE)
97+
}
9098

9199
actual_size <- base::dim(x)
92100
expected_size <- as.integer(dim)
93-
if (length(actual_size) != length(expected_size)) return(FALSE)
101+
if (length(actual_size) != length(expected_size)) {
102+
return(FALSE)
103+
}
94104

95105
all(expected_size == actual_size, na.rm = TRUE)
96106
}
@@ -136,7 +146,9 @@ partial <- function(.fn, .sig, ...) {
136146

137147
reorder_names <- function(..., last = NULL) {
138148
x <- unique(c(...))
139-
if (!is.null(last)) x <- unique(c(x, last), fromLast = TRUE)
149+
if (!is.null(last)) {
150+
x <- unique(c(x, last), fromLast = TRUE)
151+
}
140152
x
141153
}
142154

man/embed_ollama.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/read_as_markdown.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)