Skip to content

Commit

Permalink
Use examplesIf
Browse files Browse the repository at this point in the history
  • Loading branch information
hauselin committed Apr 28, 2024
1 parent e07fa3c commit 8f8a898
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 45 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
^docs$
^pkgdown$
^\.github$
^revdep$
^cran-comments\.md$
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre"),
person("Hause", "Lin", , "[email protected]", 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
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(chat)
export(create_request)
export(delete)
export(embeddings)
export(generate)
Expand Down
34 changes: 14 additions & 20 deletions R/ollama.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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?")
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions R/test_connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 1 addition & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## R CMD check results

0 errors | 0 warnings | 1 note

* This is a new release.
4 changes: 2 additions & 2 deletions man/chat.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/create_request.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/embeddings.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions man/generate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/list_models.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions man/pull.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/resp_process.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions man/test_connection.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions revdep/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
checks
library
checks.noindex
library.noindex
cloud.noindex
data.sqlite
*.html
24 changes: 24 additions & 0 deletions revdep/README.md
Original file line number Diff line number Diff line change
@@ -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

7 changes: 7 additions & 0 deletions revdep/cran.md
Original file line number Diff line number Diff line change
@@ -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

1 change: 1 addition & 0 deletions revdep/failures.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*Wow, no problems at all. :)*
1 change: 1 addition & 0 deletions revdep/problems.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*Wow, no problems at all. :)*

0 comments on commit 8f8a898

Please sign in to comment.