View source: R/sentence_embeddings-ollama.R
| embedding_ollama | R Documentation |
embedding_ollama() creates an engine for step_sentence_embeddings() that
gets embeddings from a running Ollama server. No Python
environment or API key is needed, the engine sends HTTP requests to the
server, which is usually running on your own machine.
embedding_ollama(
model,
host = "http://localhost:11434",
text_prefix = NULL,
normalize = FALSE,
batch_size = 64
)
model |
A single string giving the name of the Ollama model, such as
|
host |
A single string giving the address of the Ollama server. Defaults
to |
text_prefix |
A single string added to the start of every text before it
is embedded, or |
normalize |
A single logical. If |
batch_size |
A single positive whole number giving the maximum number of
texts sent in one request. Defaults to |
Embeddings are requested from the /api/embed endpoint. Requests that fail
with a transient error (too many requests, or the server being unavailable)
are retried a few times. Any other failure stops bake() with an error, so
partial results are never returned. Texts longer than the context length of
the model are an error rather than being silently truncated.
Before any text is embedded, the engine checks that the server can be
reached and that model is available on it, and gives an error that says
how to fix it if not. The text is sent to the server at host. For a server
you don't control, keep in mind that this is where your data goes.
The engine stores the model name and the server address, not the model. A
prepped recipe therefore needs the same model to be available on the server
when it is baked, and Ollama model names, such as "all-minilm", can point to
different model versions over time. The engine doesn't pin a version.
Requires the httr2 package.
An object of class sentence_embedding_engine.
The engine needs an Ollama server that is running and has the model.
Install Ollama from https://ollama.com/download. See the Ollama documentation for your system.
Make sure the server is running. The desktop apps start it when you open
them. Otherwise run ollama serve in a terminal and leave it running.
Pull the model once, for example ollama pull all-minilm. Models are
listed at https://ollama.com/library.
You can check which models are on the server with ollama list.
step_sentence_embeddings()
Other Embedding Engines:
embedding_custom(),
embedding_huggingface(),
embedding_model2vec()
# Needs a running Ollama server with the model pulled:
# ollama pull all-minilm
library(recipes)
sample_data <- tibble(
text = c("The cat sat.", "A dog ran.", "Stocks fell sharply today.")
)
rec <- recipe(~text, data = sample_data) |>
step_sentence_embeddings(
text,
engine = embedding_ollama("all-minilm")
)
rec |>
prep() |>
bake(new_data = NULL)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.