Skip to content

Commit

Permalink
code linting
Browse files Browse the repository at this point in the history
  • Loading branch information
JBGruber committed Oct 3, 2024
1 parent 0391b9a commit 7b2d88b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 28 deletions.
6 changes: 4 additions & 2 deletions R/chat.r
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ query <- function(q,
model_params = NULL,
format = NULL,
template = NULL,
verbose = getOption("rollama_verbose", default = interactive())) {
verbose = getOption("rollama_verbose",
default = interactive())) {

if (!is.null(template))
cli::cli_abort(paste(
Expand Down Expand Up @@ -219,7 +220,8 @@ chat <- function(q,
#' @export
chat_history <- function() {
out <- tibble::tibble(
role = c(rep("user", length(the$prompts)), rep("assistant", length(the$responses))),
role = c(rep("user", length(the$prompts)),
rep("assistant", length(the$responses))),
content = unname(c(the$prompts, the$responses)),
time = as.POSIXct(names(c(the$prompts, the$responses)))
)
Expand Down
27 changes: 15 additions & 12 deletions R/embedding.r
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#' Generate Embeddings
#'
#' @param text text vector to generate embeddings for.
#' @param model which model to use. See <https://ollama.com/library> for options.
#' Default is "llama3.1". Set option(rollama_model = "modelname") to change
#' default for the current session. See \link{pull_model} for more details.
#' @param model which model to use. See <https://ollama.com/library> for
#' options. Default is "llama3.1". Set option(rollama_model = "modelname") to
#' change default for the current session. See \link{pull_model} for more
#' details.
#' @param model_params a named list of additional model parameters listed in the
#' [documentation for the
#' Modelfile](https://github.com/ollama/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values).
Expand All @@ -18,18 +19,21 @@
#'
#' @examples
#' \dontrun{
#' embed_text(c("Here is an article about llamas...",
#' "R is a language and environment for statistical computing and graphics."))
#' embed_text(c(
#' "Here is an article about llamas...",
#' "R is a language and environment for statistical computing and graphics."))
#' }
embed_text <- function(text,
model = NULL,
server = NULL,
model_params = NULL,
verbose = getOption("rollama_verbose", default = interactive())) {
verbose = getOption("rollama_verbose",
default = interactive())) {

if (is.null(model)) model <- getOption("rollama_model", default = "llama3.1")
check_model_installed(model)
if (is.null(server)) server <- getOption("rollama_server", default = "http://localhost:11434")
if (is.null(server)) server <- getOption("rollama_server",
default = "http://localhost:11434")

pb <- FALSE
if (verbose) pb <- list(
Expand All @@ -39,11 +43,10 @@ embed_text <- function(text,
)

reqs <- purrr::map(text, function(t) {

req_data <- list(model = model,
prompt = t,
stream = FALSE,
model_params = model_params) |>
list(model = model,
prompt = t,
stream = FALSE,
model_params = model_params) |>
purrr::compact() |>
make_req(server = server,
endpoint = "/api/embeddings",
Expand Down
12 changes: 10 additions & 2 deletions R/lib.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ ping_ollama <- function(server = NULL, silent = FALSE) {
}


build_req <- function(model, msg, server, images, model_params, format, template) {
build_req <- function(model,
msg,
server,
images,
model_params,
format,
template) {

if (is.null(model)) model <- getOption("rollama_model", default = "llama3.1")
if (is.null(server)) server <- getOption("rollama_server",
Expand Down Expand Up @@ -181,7 +187,9 @@ pgrs <- function(resp) {
(as.integer(Sys.time()) - as.integer(the$str_prgs$pb_start))
if (is.numeric(the$str_prgs$speed))
the$str_prgs$speed <- prettyunits::pretty_bytes(the$str_prgs$speed)
} else the$str_prgs$speed <- 1L
} else {
the$str_prgs$speed <- 1L
}

if (!isTRUE(the$str_prgs$pb == the$str_prgs$f)) {
cli::cli_progress_bar(
Expand Down
5 changes: 2 additions & 3 deletions R/rollama-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ the <- new.env()
#' @title rollama Options
#' @name rollama-options
#'
#' @description
#' The behaviour of `rollama` can be controlled through `options()`. Specifically,
#' the options below can be set.
#' @description The behaviour of `rollama` can be controlled through
#' `options()`. Specifically, the options below can be set.
#'
#' @details
#' \describe{
Expand Down
12 changes: 7 additions & 5 deletions man/embed_text.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/rollama-options.Rd

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

2 changes: 0 additions & 2 deletions tests/testthat/test-models.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,3 @@ test_that("model missing", {
expect_error(check_model_installed("NOMODEL"),
"Model NOMODEL not installed.")
})


0 comments on commit 7b2d88b

Please sign in to comment.