From 8f8a89892a70535ec4775d7d6dd40974961d6dcb Mon Sep 17 00:00:00 2001 From: Hause Lin Date: Sun, 28 Apr 2024 19:26:41 -0400 Subject: [PATCH] Use examplesIf --- .Rbuildignore | 2 ++ DESCRIPTION | 4 ++-- NAMESPACE | 1 + R/ollama.R | 34 ++++++++++++++-------------------- R/test_connection.R | 2 -- R/utils.R | 4 +--- cran-comments.md | 5 +++++ man/chat.Rd | 4 ++-- man/create_request.Rd | 7 ++++++- man/embeddings.Rd | 6 +++--- man/generate.Rd | 5 ++--- man/list_models.Rd | 4 ++-- man/pull.Rd | 5 ++--- man/resp_process.Rd | 4 ++-- man/test_connection.Rd | 2 -- revdep/.gitignore | 7 +++++++ revdep/README.md | 24 ++++++++++++++++++++++++ revdep/cran.md | 7 +++++++ revdep/failures.md | 1 + revdep/problems.md | 1 + 20 files changed, 84 insertions(+), 45 deletions(-) create mode 100644 cran-comments.md create mode 100644 revdep/.gitignore create mode 100644 revdep/README.md create mode 100644 revdep/cran.md create mode 100644 revdep/failures.md create mode 100644 revdep/problems.md diff --git a/.Rbuildignore b/.Rbuildignore index 04580b3..cb78b87 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -6,3 +6,5 @@ ^docs$ ^pkgdown$ ^\.github$ +^revdep$ +^cran-comments\.md$ diff --git a/DESCRIPTION b/DESCRIPTION index a212148..dfde709 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: ollamar -Title: Ollama Language Models +Title: Ollama Language Models in R Version: 0.0.0.9000 Authors@R: - person("Hause", "Lin", , "hauselin@gmail.com", role = c("aut", "cre"), + person("Hause", "Lin", , "hauselin@gmail.com", role = c("aut", "cre", "cph"), comment = c(ORCID = "0000-0003-4590-7039")) Description: Provides an interface to run local language models in R with Ollama. License: MIT + file LICENSE diff --git a/NAMESPACE b/NAMESPACE index 89bf380..901eb30 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export(chat) +export(create_request) export(delete) export(embeddings) export(generate) diff --git a/R/ollama.R b/R/ollama.R index 5b3ea54..5930971 100644 --- a/R/ollama.R +++ b/R/ollama.R @@ -9,11 +9,17 @@ package_config <- list( #' Create a httr2 request object. #' -#' Creates a httr2 request object with the base URL, headers and endpoint. Used by other functions in the package and not intended to be used directly. +#' Creates a httr2 request object with base URL, headers and endpoint. Used by other functions in the package and not intended to be used directly. #' #' @param endpoint The endpoint to create the request #' #' @return A httr2 request object. +#' @export +#' +#' @examples +#' create_request("/api/tags") +#' create_request("/api/chat") +#' create_request("/api/embeddings") create_request <- function(endpoint) { url <- package_config$baseurls[1] url <- httr2::url_parse(url) @@ -36,14 +42,12 @@ create_request <- function(endpoint) { #' @return A httr2 response object, json list, raw or data frame. Default is "df". #' @export #' -#' @examples -#' \dontrun{ +#' @examplesIf test_connection()$status_code == 200 #' list_models() # returns dataframe/tibble by default #' list_models("df") #' list_models("resp") #' list_models("jsonlist") #' list_models("raw") -#' } list_models <- function(output = c("df", "resp", "jsonlist", "raw"), endpoint = "/api/tags") { req <- create_request(endpoint) @@ -70,8 +74,7 @@ list_models <- function(output = c("df", "resp", "jsonlist", "raw"), endpoint = #' @return A httr2 response object, json list, raw or data frame. #' @export #' -#' @examples -#' \dontrun{ +#' @examplesIf test_connection()$status_code == 200 #' # one message #' messages <- list( #' list(role = "user", content = "How are you doing?") @@ -89,7 +92,6 @@ list_models <- function(output = c("df", "resp", "jsonlist", "raw"), endpoint = #' list(role = "user", content = "List all the previous messages.") #' ) #' chat("llama3", messages, stream = TRUE) -#' } chat <- function(model, messages, stream = FALSE, output = c("resp", "jsonlist", "raw", "df"), endpoint = "/api/chat") { req <- create_request(endpoint) @@ -186,12 +188,9 @@ chat <- function(model, messages, stream = FALSE, output = c("resp", "jsonlist", #' @return A httr2 response object. #' @export #' -#' @examples -#' \dontrun{ +#' @examplesIf test_connection()$status_code == 200 #' pull("llama3") -#' pull("llama3", stream = FALSE) -#" pull("all-minilm") -#' } +#" pull("all-minilm", stream = FALSE) pull <- function(model, stream = TRUE, endpoint = "/api/pull") { req <- create_request(endpoint) req <- httr2::req_method(req, "POST") @@ -285,10 +284,8 @@ normalize <- function(x) { #' @return A numeric vector of the embedding. #' @export #' -#' @examples -#' \dontrun{ -#' get_embeddings("gemma:latest", "The quick brown fox jumps over the lazy dog.") -#' } +#' @examplesIf test_connection()$status_code == 200 +#' embeddings("nomic-embed-text:latest", "The quick brown fox jumps over the lazy dog.") embeddings <- function(model, prompt, normalize = TRUE, endpoint = "/api/embeddings") { req <- create_request(endpoint) req <- httr2::req_method(req, "POST") @@ -327,13 +324,10 @@ embeddings <- function(model, prompt, normalize = TRUE, endpoint = "/api/embeddi #' @return A response in the format specified in the output parameter. #' @export #' -#' @examples -#' \dontrun{ +#' @examplesIf test_connection()$status_code == 200 #' generate("llama3", "The sky is...", stream = FALSE, output = "df") #' generate("llama3", "The sky is...", stream = TRUE, output = "df") -#' generate("llama3", "The sky is...", stream = FALSE, output = "resp") #' generate("llama3", "The sky is...", stream = FALSE, output = "jsonlist") -#' } generate <- function(model, prompt, stream = FALSE, output = c("resp", "jsonlist", "raw", "df"), endpoint = "/api/generate") { req <- create_request(endpoint) diff --git a/R/test_connection.R b/R/test_connection.R index a968f9e..31200e6 100644 --- a/R/test_connection.R +++ b/R/test_connection.R @@ -6,11 +6,9 @@ #' @export #' #' @examples -#' \dontrun{ #' test_connection() #' test_connection("http://localhost:11434") #' test_connection("http://127.0.0.1:11434") -#' } test_connection <- function(url = "http://localhost:11434") { req <- httr2::request(url) req <- httr2::req_method(req, "GET") diff --git a/R/utils.R b/R/utils.R index 9e66e58..37104d5 100644 --- a/R/utils.R +++ b/R/utils.R @@ -6,14 +6,12 @@ #' @return A data frame, json list, raw or httr2 response object. #' @export #' -#' @examples -#' \dontrun{ +#' @examplesIf test_connection()$status_code == 200 #' resp <- list_models("resp") #' resp_process(resp, "df") # parse response to dataframe/tibble #' resp_process(resp, "jsonlist") # parse response to list #' resp_process(resp, "raw") # parse response to raw string #' resp_process(resp, "resp") # return input response object -#' } resp_process <- function(resp, output = c("df", "jsonlist", "raw", "resp")) { if (is.null(resp) || resp$status_code != 200) { diff --git a/cran-comments.md b/cran-comments.md new file mode 100644 index 0000000..858617d --- /dev/null +++ b/cran-comments.md @@ -0,0 +1,5 @@ +## R CMD check results + +0 errors | 0 warnings | 1 note + +* This is a new release. diff --git a/man/chat.Rd b/man/chat.Rd index 9fac045..f819da4 100644 --- a/man/chat.Rd +++ b/man/chat.Rd @@ -30,7 +30,7 @@ A httr2 response object, json list, raw or data frame. Chat with Ollama models } \examples{ -\dontrun{ +\dontshow{if (test_connection()$status_code == 200) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} # one message messages <- list( list(role = "user", content = "How are you doing?") @@ -48,5 +48,5 @@ messages <- list( list(role = "user", content = "List all the previous messages.") ) chat("llama3", messages, stream = TRUE) -} +\dontshow{\}) # examplesIf} } diff --git a/man/create_request.Rd b/man/create_request.Rd index 5bd4dc0..1edc817 100644 --- a/man/create_request.Rd +++ b/man/create_request.Rd @@ -13,5 +13,10 @@ create_request(endpoint) A httr2 request object. } \description{ -Creates a httr2 request object with the base URL, headers and endpoint. Used by other functions in the package and not intended to be used directly. +Creates a httr2 request object with base URL, headers and endpoint. Used by other functions in the package and not intended to be used directly. +} +\examples{ +create_request("/api/tags") +create_request("/api/chat") +create_request("/api/embeddings") } diff --git a/man/embeddings.Rd b/man/embeddings.Rd index 6292de6..c100112 100644 --- a/man/embeddings.Rd +++ b/man/embeddings.Rd @@ -22,7 +22,7 @@ A numeric vector of the embedding. Get vector embedding for a prompt } \examples{ -\dontrun{ -get_embeddings("gemma:latest", "The quick brown fox jumps over the lazy dog.") -} +\dontshow{if (test_connection()$status_code == 200) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +embeddings("nomic-embed-text:latest", "The quick brown fox jumps over the lazy dog.") +\dontshow{\}) # examplesIf} } diff --git a/man/generate.Rd b/man/generate.Rd index d5dc9a1..ab90f60 100644 --- a/man/generate.Rd +++ b/man/generate.Rd @@ -30,10 +30,9 @@ A response in the format specified in the output parameter. Generate a response for a given prompt with a provided model. } \examples{ -\dontrun{ +\dontshow{if (test_connection()$status_code == 200) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} generate("llama3", "The sky is...", stream = FALSE, output = "df") generate("llama3", "The sky is...", stream = TRUE, output = "df") -generate("llama3", "The sky is...", stream = FALSE, output = "resp") generate("llama3", "The sky is...", stream = FALSE, output = "jsonlist") -} +\dontshow{\}) # examplesIf} } diff --git a/man/list_models.Rd b/man/list_models.Rd index 28632fb..db47a04 100644 --- a/man/list_models.Rd +++ b/man/list_models.Rd @@ -21,11 +21,11 @@ A httr2 response object, json list, raw or data frame. Default is "df". Get available local models } \examples{ -\dontrun{ +\dontshow{if (test_connection()$status_code == 200) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} list_models() # returns dataframe/tibble by default list_models("df") list_models("resp") list_models("jsonlist") list_models("raw") -} +\dontshow{\}) # examplesIf} } diff --git a/man/pull.Rd b/man/pull.Rd index 73d57ab..06df3af 100644 --- a/man/pull.Rd +++ b/man/pull.Rd @@ -20,8 +20,7 @@ A httr2 response object. See https://ollama.com/library for a list of available models. Use the list_models() function to get the list of models already downloaded/installed on your machine. } \examples{ -\dontrun{ +\dontshow{if (test_connection()$status_code == 200) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} pull("llama3") -pull("llama3", stream = FALSE) -} +\dontshow{\}) # examplesIf} } diff --git a/man/resp_process.Rd b/man/resp_process.Rd index 268a375..aff06e0 100644 --- a/man/resp_process.Rd +++ b/man/resp_process.Rd @@ -18,11 +18,11 @@ A data frame, json list, raw or httr2 response object. Process httr2 response object. } \examples{ -\dontrun{ +\dontshow{if (test_connection()$status_code == 200) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} resp <- list_models("resp") resp_process(resp, "df") # parse response to dataframe/tibble resp_process(resp, "jsonlist") # parse response to list resp_process(resp, "raw") # parse response to raw string resp_process(resp, "resp") # return input response object -} +\dontshow{\}) # examplesIf} } diff --git a/man/test_connection.Rd b/man/test_connection.Rd index 3808f7b..da21522 100644 --- a/man/test_connection.Rd +++ b/man/test_connection.Rd @@ -16,9 +16,7 @@ A httr2 response object. Test connection to Ollama server } \examples{ -\dontrun{ test_connection() test_connection("http://localhost:11434") test_connection("http://127.0.0.1:11434") } -} diff --git a/revdep/.gitignore b/revdep/.gitignore new file mode 100644 index 0000000..111ab32 --- /dev/null +++ b/revdep/.gitignore @@ -0,0 +1,7 @@ +checks +library +checks.noindex +library.noindex +cloud.noindex +data.sqlite +*.html diff --git a/revdep/README.md b/revdep/README.md new file mode 100644 index 0000000..d2962d9 --- /dev/null +++ b/revdep/README.md @@ -0,0 +1,24 @@ +# Platform + +|field |value | +|:--------|:------------------------------------------------------------------------------------------| +|version |R version 4.3.2 (2023-10-31) | +|os |macOS Ventura 13.0 | +|system |aarch64, darwin20 | +|ui |RStudio | +|language |(EN) | +|collate |en_US.UTF-8 | +|ctype |en_US.UTF-8 | +|tz |America/New_York | +|date |2024-04-28 | +|rstudio |2023.12.1+402 Ocean Storm (desktop) | +|pandoc |3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown) | + +# Dependencies + +|package |old |new |Δ | +|:-------|:---|:----------|:--| +|ollamar |NA |0.0.0.9000 |* | + +# Revdeps + diff --git a/revdep/cran.md b/revdep/cran.md new file mode 100644 index 0000000..33114b6 --- /dev/null +++ b/revdep/cran.md @@ -0,0 +1,7 @@ +## revdepcheck results + +We checked 0 reverse dependencies, comparing R CMD check results across CRAN and dev versions of this package. + + * We saw 0 new problems + * We failed to check 0 packages + diff --git a/revdep/failures.md b/revdep/failures.md new file mode 100644 index 0000000..9a20736 --- /dev/null +++ b/revdep/failures.md @@ -0,0 +1 @@ +*Wow, no problems at all. :)* \ No newline at end of file diff --git a/revdep/problems.md b/revdep/problems.md new file mode 100644 index 0000000..9a20736 --- /dev/null +++ b/revdep/problems.md @@ -0,0 +1 @@ +*Wow, no problems at all. :)* \ No newline at end of file