Skip to content

Commit

Permalink
Add model options
Browse files Browse the repository at this point in the history
  • Loading branch information
hauselin committed May 14, 2024
1 parent 929a05a commit 8ffc27c
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 3 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
Imports:
crayon,
glue,
httr2,
jsonlite,
Expand Down
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YEAR: 2024
COPYRIGHT HOLDER: ollamar authors
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

export(append_message)
export(chat)
export(check_option_valid)
export(check_options)
export(create_message)
export(create_request)
export(delete)
Expand All @@ -13,5 +15,9 @@ export(list_models)
export(prepend_message)
export(pull)
export(resp_process)
export(search_options)
export(test_connection)
export(validate_options)
importFrom(crayon,green)
importFrom(crayon,red)
importFrom(glue,glue)
48 changes: 45 additions & 3 deletions R/model_options.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# https://github.com/ollama/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values
#' Model options
model_options <- list(
mirostat = list(
description = "Enable Mirostat sampling for controlling perplexity.",
Expand Down Expand Up @@ -54,12 +54,35 @@ model_options <- list(
)
)




#' Check if an option is valid.
#'
#' @param opt An option (character) to check.
#'
#' @return Returns TRUE if the option is valid, FALSE otherwise.
#' @export
#'
#' @examples
#' check_option_valid("mirostat")
#' check_option_valid("invalid_option")
check_option_valid <- function(opt) {
return(opt %in% names(model_options))
}



#' Check if a vector of options are valid.
#'
#' @param opts A vector of options to check.
#'
#' @return Returns a list with two elements: valid_options and invalid_options.
#' @export
#'
#' @examples
#' check_options(c("mirostat", "invalid_option"))
#' check_options(c("mirostat", "num_predict"))
check_options <- function(opts = NULL) {
if (is.null(opts)) {
return(names(model_options))
Expand All @@ -77,6 +100,20 @@ check_options <- function(opts = NULL) {
}


#' Search for options based on a query.
#'
#' @param query A query (character) to search for in the options.
#'
#' @importFrom crayon green
#' @importFrom crayon red
#'
#' @return Returns a list of matching options.
#' @export
#'
#' @examples
#' search_options("learning rate")
#' search_options("tokens")
#' search_options("invalid query")
search_options <- function(query) {
matching_options <- list()
option_names <- names(model_options)
Expand All @@ -92,9 +129,9 @@ search_options <- function(query) {
}
}
if (length(matching_options) == 0) {
message("No matching options found")
cat(crayon::red("No matching options found\n"))
} else {
message(paste0("Matching options: ", paste(names(matching_options), collapse = ", ")))
cat(crayon::green(paste0("Matching options: ", paste(names(matching_options), collapse = ", "))), "\n")
}
return(matching_options)
}
Expand All @@ -108,7 +145,12 @@ search_options <- function(query) {
#'
#' @param ... Additional options or parameters provided to the API call
#'
#' @importFrom crayon green
#' @importFrom crayon red
#'
#' @return TRUE if all additional options are valid, FALSE otherwise
#' @export
#'
#' @examples
#' validate_options(mirostat = 1, mirostat_eta = 0.2, num_ctx = 1024)
#' validate_options(mirostat = 1, mirostat_eta = 0.2, invalid_opt = 1024)
Expand Down
21 changes: 21 additions & 0 deletions man/check_option_valid.Rd

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

21 changes: 21 additions & 0 deletions man/check_options.Rd

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

16 changes: 16 additions & 0 deletions man/model_options.Rd

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

22 changes: 22 additions & 0 deletions man/search_options.Rd

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

21 changes: 21 additions & 0 deletions man/validate_options.Rd

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

0 comments on commit 8ffc27c

Please sign in to comment.