How to Generate Ollama Embeddings: The Right Way in JavaScript
Original question: what is the right way to generate ollama embeddings?
The right way to generate Ollama embeddings depends on whether you are using the REST API directly or the official Ollama JavaScript library (ollama-js). For the REST API, use the endpoint with input as the key for the text. For the JavaScript library, use ollama.embed() with input as the key, not prompt. The ollama.embeddings() method with prompt is a legacy or LangChain-specific pattern and is not the canonical approach in the current ollama-js library. This answer covers both approaches, explains the inconsistency, and provides working code examples.
The Full Answer
Ollama provides two primary ways to generate embeddings: through its REST API and through its official JavaScript library (ollama-js). The confusion arises because older documentation, community examples, and LangChain integrations sometimes use prompt as the input key, while the current REST API and ollama-js use input. This guide resolves that confusion and gives you the correct, working syntax for each method.
Approach 1: Using the Ollama REST API Directly
The Ollama REST API is the foundational interface. According to the Ollama API documentation, the endpoint for generating embeddings is /api/embed. The request body uses input as the key for the text you want to embed. Here is the correct structure:
POST /api/embed
{
"model": "mxbai-embed-large",
"input": "Llamas are members of the camelid family"
}
Note that the key is input, not prompt. The response will contain an array of embeddings. For a single input, the response looks like:
{
"model": "mxbai-embed-large",
"embeddings": [[0.123, 0.456, ...]]
}
To generate embeddings for multiple inputs, pass an array of strings:
{
"model": "mxbai-embed-large",
"input": ["First text", "Second text", "Third text"]
}
The response will contain an array of embeddings, one per input, in the same order.
Approach 2: Using the Ollama JavaScript Library (ollama-js)
The official Ollama JavaScript library, ollama-js, wraps the REST API. Its API is designed to mirror the REST API, so it uses input as the key. The method is ollama.embed(), not ollama.embeddings(). The ollama.embeddings() method with prompt is not documented in the current ollama-js library and appears to be a legacy or LangChain-specific pattern.
Here is the correct way to use ollama-js to generate embeddings, based on the source from M.Ali El-Sayed and the official documentation:
- Install the library:
npm install ollama --save
- Import and initialize the client:
import { Ollama } from "ollama";
const ollama = new Ollama({ host: "http://localhost:11434" }); // Replace with your Ollama server URL
- Create an async function to generate embeddings:
async function textToEmbeddingOllama(text) {
try {
const response = await ollama.embed({
model: "mxbai-embed-large", // Replace with your model name
input: text,
truncate: false,
keep_alive: "1.5h",
options: {
embedding: true,
},
});
console.log(response);
return response.embeddings[0];
} catch (error) {
console.error("Error generating embeddings with Ollama:", error);
throw error;
}
}
Explanation of parameters:
model: The name of the embedding model you have pulled (e.g.,mxbai-embed-large,nomic-embed-text,all-minilm).input: The text string (or array of strings) to embed. This is the correct key, notprompt.truncate: A boolean. When set tofalse, the model will not truncate the input if it exceeds the maximum token length. If you set it totrue(or omit it), the input will be truncated. This is useful for long documents.keep_alive: A string specifying how long to keep the model loaded in memory after the request."1.5h"means 1.5 hours. This can speed up subsequent requests.options.embedding: A boolean. Setting this totrueensures the model returns embeddings. Some models may require this flag.
The response object has the following structure:
{
"model": "mxbai-embed-large",
"embeddings": [[0.123, 0.456, ...]],
"total_duration": 123456789,
"load_duration": 123456,
"prompt_eval_count": 10
}
response.embeddings is an array of arrays. For a single input, it contains one array. For multiple inputs, it contains one array per input. The example above returns response.embeddings[0], which is the embedding vector for the single input.
Why the Confusion? prompt vs input
As noted in the Stack Overflow question, the Ollama embedding models documentation shows this syntax:
ollama.embeddings({
model: 'mxbai-embed-large',
prompt: 'Llamas are members of the camelid family',
})
This is not the canonical ollama-js syntax. The official ollama-js documentation states: "The Ollama JavaScript library's API is designed around the Ollama REST API." The REST API uses input, so the library should use input. The prompt key appears in older examples and in LangChain's Ollama integration, which has its own wrapper. If you are using the raw ollama-js library, always use input and the ollama.embed() method.
When to Use Each Approach
- Use the REST API directly if you are not using JavaScript/Node.js, or if you want to make HTTP requests from any language (Python, curl, etc.). It is the most universal.
- Use ollama-js if you are building a Node.js or browser application and want a convenient, promise-based interface. The library handles connection details and provides TypeScript types.
- Avoid
ollama.embeddings()withpromptunless you are working with an older codebase or a specific LangChain version that requires it. For new projects, useollama.embed()withinput.
Common Pitfalls
1. Using prompt Instead of input
This is the most common mistake. If you use prompt with the REST API or ollama-js, the server will likely ignore it or return an error. Always use input.
2. Forgetting to Pull the Model
Before generating embeddings, ensure the model is pulled. Run:
ollama pull mxbai-embed-large
If the model is not present, the API will return an error like model "mxbai-embed-large" not found.
3. Not Handling Multiple Inputs Correctly
When passing multiple inputs as an array, the response contains an array of embeddings. Access them by index. For example:
const response = await ollama.embed({
model: "mxbai-embed-large",
input: ["Text A", "Text B"],
});
const embeddingA = response.embeddings[0];
const embeddingB = response.embeddings[1];
4. Ignoring the truncate Parameter
If your input text is very long (e.g., a whole document), the model may truncate it by default. Set truncate: false to get an error if the input is too long, rather than silently losing data. Alternatively, split your text into chunks before embedding.
5. Not Setting keep_alive for Repeated Calls
If you are generating embeddings for many texts in a loop, setting keep_alive to a longer duration (e.g., "30m" or "1h") keeps the model loaded in memory, reducing latency. Without it, the model may be unloaded after each request, causing slower performance.
6. Confusion with LangChain's OllamaEmbeddings
LangChain has its own OllamaEmbeddings class that uses prompt internally. If you are using LangChain, follow its documentation. But if you are using the raw ollama-js library, do not mix the two. The community on Stack Overflow reports that this confusion is common.
Related Questions
Can I generate embeddings for multiple texts in one API call?
Yes. Both the REST API and ollama-js support passing an array of strings as the input parameter. The response will contain an array of embeddings in the same order. This is more efficient than making separate calls for each text.
What embedding models does Ollama support?
Ollama supports many embedding models, including mxbai-embed-large, nomic-embed-text, all-minilm, snowflake-arctic-embed, and others. You can pull them with ollama pull <model-name>. The model must be designed for embeddings; not all Ollama models support the /api/embed endpoint.
How do I use Ollama embeddings in a Python application?
For Python, use the requests library to call the REST API directly. Install it with pip install requests, then send a POST request to http://localhost:11434/api/embed with a JSON body containing model and input. There is also an unofficial Python client, ollama-python, but the REST API is the most reliable approach.
What is the difference between ollama.embed() and ollama.generate()?
ollama.embed() returns numerical vector representations (embeddings) of the input text, which are used for semantic search, clustering, or as features for other models. ollama.generate() returns generated text (completions) based on a prompt. They serve different purposes: embeddings for understanding, generation for creating.
The #1 AI Newsletter
The most important ai updates, guides, and fixes — one weekly email.
No spam, unsubscribe anytime. Privacy policy
Sources & References
This page was researched from 2 independent sources, combined and verified for completeness.
- 1.what is the right way to generate ollama embeddings?Stack Overflow
- 2.Answer by M.Ali El-Sayed (score 1)Stack Overflow
Related Answers
Keep exploring
Latest error solutions
Skip the manual work
Ready-made AI workflows and automation templates — import and run instead of building from scratch.