Rollama
Rollama
December 6, 2024
Title Communicate with 'Ollama' to Run Large Language Models Locally
Version 0.2.0
Description Wraps the 'Ollama' <https://ollama.com> API, which can be used to
communicate with generative large language models locally.
License GPL (>= 3)
Encoding UTF-8
RoxygenNote 7.3.2
Imports callr, cli, dplyr, httr2, jsonlite, methods, prettyunits,
purrr, rlang, tibble, withr
Suggests base64enc, covr, glue, knitr, rmarkdown, spelling, testthat
(>= 3.0.0)
Depends R (>= 4.1.0)
VignetteBuilder knitr
Config/testthat/edition 3
Language en-US
URL https://jbgruber.github.io/rollama/,
https://github.com/JBGruber/rollama
BugReports https://github.com/JBGruber/rollama/issues
NeedsCompilation no
Author Johannes B. Gruber [aut, cre] (<https://orcid.org/0000-0001-9177-1772>),
Maximilian Weber [aut, ctb] (<https://orcid.org/0000-0002-1174-449X>)
Maintainer Johannes B. Gruber <[email protected]>
Repository CRAN
Date/Publication 2024-12-06 15:20:02 UTC
1
2 check_model_installed
Contents
chat_history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
check_model_installed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
create_model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
embed_text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
list_models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
make_query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
ping_ollama . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
pull_model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
rollama-options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Index 13
Description
Shows and deletes (new_chat) the local prompt and response history to start a new conversation.
Usage
chat_history()
new_chat()
Value
chat_history: tibble with chat history
new_chat: Does not return a value
Description
Check if one or several models are installed on the server
Usage
check_model_installed(
model,
check_only = FALSE,
auto_pull = FALSE,
server = getOption("rollama_server", default = "http://localhost:11434")
)
create_model 3
Arguments
model names of one or several models as character vector.
check_only only return TRUE/FALSE and don’t download models.
auto_pull if FALSE, the default, asks before downloading models.
server URL to one or several Ollama servers (not the API). Defaults to "http://localhost:11434".
Value
invisible TRUE/FALSE
Description
Create a model from a Modelfile
Usage
create_model(model, modelfile, server = NULL)
Arguments
model name of the model to create
modelfile either a path to a model file to be read or the contents of the model file as a
character vector.
server URL to one or several Ollama servers (not the API). Defaults to "http://localhost:11434".
Details
Custom models are the way to save your system message and model parameters in a dedicated
shareable way. If you use show_model(), you can look at the configuration of a model in the
column modelfile. To get more information and a list of valid parameters, check out https://
github.com/ollama/ollama/blob/main/docs/modelfile.md. Most options are also available
through the query and chat functions, yet are not persistent over sessions.
Value
Nothing. Called to create a model on the Ollama server.
Examples
modelfile <- system.file("extdata", "modelfile.txt", package = "rollama")
## Not run: create_model("mario", modelfile)
modelfile <- "FROM llama3.1\nSYSTEM You are mario from Super Mario Bros."
## Not run: create_model("mario", modelfile)
4 embed_text
Description
Generate Embeddings
Usage
embed_text(
text,
model = NULL,
server = NULL,
model_params = NULL,
verbose = getOption("rollama_verbose", default = interactive())
)
Arguments
text text vector to generate embeddings for.
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 pull_model for more details.
server URL to one or several Ollama servers (not the API). Defaults to "http://localhost:11434".
model_params a named list of additional model parameters listed in the documentation for the
Modelfile.
verbose Whether to print status messages to the Console (TRUE/FALSE). The default is to
have status messages in interactive sessions. Can be changed with options(rollama_verbose
= FALSE).
Value
a tibble with embeddings.
Examples
## Not run:
embed_text(c(
"Here is an article about llamas...",
"R is a language and environment for statistical computing and graphics."))
## End(Not run)
list_models 5
Description
Usage
list_models(server = NULL)
Arguments
server URL to one or several Ollama servers (not the API). Defaults to "http://localhost:11434".
Value
Description
make_query generates structured input for a language model, including system prompt, user mes-
sages, and optional examples (assistant answers).
Usage
make_query(
text,
prompt,
template = "{prefix}{text}\n{prompt}\n{suffix}",
system = NULL,
prefix = NULL,
suffix = NULL,
examples = NULL
)
6 make_query
Arguments
Details
The function supports the inclusion of examples, which are dynamically added to the structured
input. Each example follows the same format as the primary user query.
Value
A list of tibbles, one for each input text, containing structured rows for system messages, user
messages, and assistant responses.
Examples
template <- "{prefix}{text}\n\n{prompt}{suffix}"
examples <- tibble::tribble(
~text, ~answer,
"This movie was amazing, with great acting and story.", "positive",
"The film was okay, but not particularly memorable.", "neutral",
"I found this movie boring and poorly made.", "negative"
)
queries <- make_query(
text = c("A stunning visual spectacle.", "Predictable but well-acted."),
prompt = "Classify sentiment as positive, neutral, or negative.",
template = template,
system = "Provide a sentiment classification.",
prefix = "Review: ",
suffix = " Please classify.",
examples = examples
)
print(queries)
if (ping_ollama()) { # only run this example when Ollama is running
query(queries, screen = TRUE, output = "text")
}
ping_ollama 7
Description
Usage
Arguments
server URL to one or several Ollama servers (not the API). Defaults to "http://localhost:11434".
silent suppress warnings and status (only return TRUE/FALSE).
Value
Description
Usage
pull_model(
model = NULL,
server = NULL,
insecure = FALSE,
verbose = getOption("rollama_verbose", default = interactive())
)
Arguments
model name of the model(s). Defaults to "llama3.1" when NULL (except in delete_model).
server URL to one or several Ollama servers (not the API). Defaults to "http://localhost:11434".
insecure allow insecure connections to the library. Only use this if you are pulling from
your own library during development.
verbose Whether to print status messages to the Console (TRUE/FALSE). The default is to
have status messages in interactive sessions. Can be changed with options(rollama_verbose
= FALSE).
destination name of the copied model.
Details
• pull_model(): downloads model
• show_model(): displays information about a local model
• copy_model(): creates a model with another name from an existing model
• delete_model(): deletes local model
Model names: Model names follow a model:tag format, where model can have an optional names-
pace such as example/model. Some examples are orca-mini:3b-q4_1 and llama3.1:70b. The tag is
optional and, if not provided, will default to latest. The tag is used to identify a specific version.
Value
(invisible) a tibble with information about the model (except in delete_model)
Examples
## Not run:
# download a model and save information in an object
model_info <- pull_model("mixtral")
# after you pull, you can get the same information with:
model_info <- show_model("mixtral")
# pulling models from Hugging Face Hub is also possible
pull_model("https://huggingface.co/oxyapi/oxy-1-small-GGUF:Q2_K")
## End(Not run)
Description
Chat with a LLM through Ollama
query 9
Usage
query(
q,
model = NULL,
screen = TRUE,
server = NULL,
images = NULL,
model_params = NULL,
output = c("response", "text", "list", "data.frame", "httr2_response", "httr2_request"),
format = NULL,
template = NULL,
verbose = getOption("rollama_verbose", default = interactive())
)
chat(
q,
model = NULL,
screen = TRUE,
server = NULL,
images = NULL,
model_params = NULL,
template = NULL,
verbose = getOption("rollama_verbose", default = interactive())
)
Arguments
Details
query sends a single question to the API, without knowledge about previous questions (only the
config message is relevant). chat treats new messages as part of the same conversation until
new_chat is called.
To make the output reproducible, you can set a seed with options(rollama_seed = 42). As long
as the seed stays the same, the models will give the same answer, changing the seed leads to a
different answer.
For the output of query, there are a couple of options:
• response: the response of the Ollama server
• text: only the answer as a character vector
• data.frame: a data.frame containing model and response
• list: a list containing the prompt to Ollama and the response
• httr2_response: the response of the Ollama server including HTML headers in the httr2
response format
• httr2_request: httr2_request objects in a list, in case you want to run them with httr2::req_perform(),
httr2::req_perform_sequential(), or httr2::req_perform_parallel() yourself.
Value
list of objects set in output parameter.
Examples
#' # ask a single question
query("why is the sky blue?")
# hold a conversation
chat("why is the sky blue?")
chat("and how do you know that?")
# set custom options for the model at runtime (rather than in create_model())
query("why is the sky blue?",
model_params = list(
num_keep = 5,
query 11
seed = 42,
num_predict = 100,
top_k = 20,
top_p = 0.9,
min_p = 0.0,
tfs_z = 0.5,
typical_p = 0.7,
repeat_last_n = 33,
temperature = 0.8,
repeat_penalty = 1.2,
presence_penalty = 1.5,
frequency_penalty = 1.0,
mirostat = 1,
mirostat_tau = 0.8,
mirostat_eta = 0.6,
penalize_newline = TRUE,
numa = FALSE,
num_ctx = 1024,
num_batch = 2,
num_gpu = 0,
main_gpu = 0,
low_vram = FALSE,
vocab_only = FALSE,
use_mmap = TRUE,
use_mlock = FALSE,
num_thread = 8
))
# this might be interesting if you want to turn off the GPU and load the
# model into the system memory (slower, but most people have more RAM than
# VRAM, which might be interesting for larger models)
query("why is the sky blue?",
model_params = list(num_gpu = 0))
# And if you have multiple Ollama servers in your network, you can send
# requests to them in parallel
if (ping_ollama(c("http://localhost:11434/",
"http://192.168.2.45:11434/"))) { # check if servers are running
query("why is the sky blue?", model = c("llama3.1", "orca-mini"),
server = c("http://localhost:11434/",
"http://192.168.2.45:11434/"))
}
12 rollama-options
Description
The behaviour of rollama can be controlled through options(). Specifically, the options below
can be set.
Details
rollama_server This controls the default server where Ollama is expected to run. It assumes
that you are running Ollama locally in a Docker container.
"http://localhost:11434"
default:
rollama_model The default model is llama3.1, which is a good overall option with rea-
sonable performance and size for most tasks. You can change the model in each function
call or globally with this option.
"llama3.1"
default:
rollama_verbose Whether the package tells users what is going on, e.g., showing a spin-
ner while the models are thinking or showing the download speed while pulling models.
Since this adds some complexity to the code, you might want to disable it when you get
errors (it won’t fix the error, but you get a better error trace).
TRUE
default:
rollama_config The default configuration or system message. If NULL, the system mes-
sage defined in the used model is employed.
None
Examples
default: options(rollama_config = "You make answers understandable to a 5 year old")
Index
chat (query), 8
chat_history, 2
check_model_installed, 2
copy_model (pull_model), 7
create_model, 3
delete_model (pull_model), 7
embed_text, 4
httr2::req_perform(), 10
httr2::req_perform_parallel(), 10
httr2::req_perform_sequential(), 10
list_models, 5
make_query, 5
new_chat, 10
new_chat (chat_history), 2
ping_ollama, 7
pull_model, 4, 7, 9
query, 8
rollama-options, 12
show_model (pull_model), 7
13