Skip to content

Commit

Permalink
Add ohelp
Browse files Browse the repository at this point in the history
  • Loading branch information
hauselin committed Jul 28, 2024
1 parent dc45fb7 commit b506423
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export(generate)
export(image_encode_base64)
export(insert_message)
export(list_models)
export(ohelp)
export(prepend_message)
export(pull)
export(resp_process)
Expand Down
41 changes: 41 additions & 0 deletions R/ollama.R
Original file line number Diff line number Diff line change
Expand Up @@ -568,3 +568,44 @@ embeddings <- function(model, prompt, normalize = TRUE, keep_alive = "5m", endpo



#' Chat with a model in real-time in R console.
#'
#' @param model A character string of the model name such as "llama3". Defaults to "codegemma:7b" which is a decent coding model as of 2024-07-27.
#' @param ... Additional options. No options are currently available at this time.
#'
#' @return Does not return anything. It prints the conversation in the console.
#' @export
#'
#' @examplesIf test_connection()$status_code == 200
#' ohelp(first_prompt = "quit")
#' # regular usage: ohelp()
ohelp <- function(model = "codegemma:7b", ...) {

cat("Say something or type /q to quit or end the conversation.\n\n")

n_messages <- 0
opts <- list(...)
if (length(opts) > 0) {
if (opts$first_prompt == "quit") {
prompt <- "/q"
}
} else {
prompt <- readline()
}

while (prompt != "/q") {
if (n_messages == 0) {
messages <- create_message(prompt, role = 'user')
} else {
messages <- append(messages, create_message(prompt, role = 'user'))
}
n_messages <- n_messages + 1
response <- chat(model, messages = messages, output = 'text', stream = TRUE)
messages <- append_message(response, "assistant", messages)
n_messages <- n_messages + 1
prompt <- readline()
}

cat("Goodbye!\n")

}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ reference:
- pull
- embed
- embeddings
- ohelp
- test_connection
- create_request

Expand Down
2 changes: 1 addition & 1 deletion man/image_encode_base64.Rd

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

25 changes: 25 additions & 0 deletions man/ohelp.Rd

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

0 comments on commit b506423

Please sign in to comment.