Ollama

Ollama offers out-of-the-box embedding API which allows you to generate embeddings for your documents. Chroma provides a convenient wrapper around Ollama’s embedding API.

Ollama Embedding Models

While you can use any of the ollama models including LLMs to generate embeddings. We generally recommend using specialized models like nomic-embed-text for text embeddings. The latter models are specifically trained for embeddings and are more efficient for this purpose (e.g. the dimensions of the output embeddings are much smaller than those from LLMs e.g. 1024 - nomic-embed-text vs 4096 - llama3)

Models:

ModelPullOllama Registry Link
nomic-embed-textollama pull nomic-embed-textnomic-embed-text
mxbai-embed-largeollama pull mxbai-embed-largemxbai-embed-large
snowflake-arctic-embedollama pull snowflake-arctic-embedsnowflake-arctic-embed
all-minilm-l6-v2ollama pull chroma/all-minilm-l6-v2-f32all-minilm-l6-v2-f32

Basic Usage

First let’s run a local docker container with Ollama. We’ll pull nomic-embed-text model:

  1. docker run -d --rm -v ./ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
  2. docker exec -it ollama ollama run nomic-embed-text # press Ctrl+D to exit after model downloads successfully
  3. # test it
  4. curl http://localhost:11434/api/embeddings -d '{"model": "nomic-embed-text","prompt": "Here is an article about llamas..."}'

Ollama Docs

For more information on Ollama, visit the Ollama GitHub repository.

Using the CLI

If you have or prefer to use the Ollama CLI, you can use the following command to get a model:

  1. ollama pull nomic-embed-text

Now let’s configure our OllamaEmbeddingFunction Embedding (python) function with the default Ollama endpoint:

Python

  1. import chromadb
  2. from chromadb.utils.embedding_functions import OllamaEmbeddingFunction
  3. client = chromadb.PersistentClient(path="ollama")
  4. # create EF with custom endpoint
  5. ef = OllamaEmbeddingFunction(
  6. model_name="nomic-embed-text",
  7. url="http://localhost:11434/api/embeddings",
  8. )
  9. print(ef(["Here is an article about llamas..."]))

JavaScript

For JS users, you can use the OllamaEmbeddingFunction class to create embeddings:

  1. const {OllamaEmbeddingFunction} = require('chromadb');
  2. const embedder = new OllamaEmbeddingFunction({
  3. url: "http://localhost:11434/api/embeddings",
  4. model: "nomic-embed-text"
  5. })
  6. // use directly
  7. const embeddings = embedder.generate(["Here is an article about llamas..."])

Golang

For Golang you can use the chroma-go client’s OllamaEmbeddingFunction embedding function to generate embeddings for your documents:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. ollama "github.com/amikos-tech/chroma-go/ollama"
  6. )
  7. func main() {
  8. documents := []string{
  9. "Document 1 content here",
  10. "Document 2 content here",
  11. }
  12. // the `/api/embeddings` endpoint is automatically appended to the base URL
  13. ef, err := ollama.NewOllamaEmbeddingFunction(ollama.WithBaseURL("http://127.0.0.1:11434"), ollama.WithModel("nomic-embed-text"))
  14. if err != nil {
  15. fmt.Printf("Error creating Ollama embedding function: %s \n", err)
  16. }
  17. resp, err := ef.EmbedDocuments(context.Background(), documents)
  18. if err != nil {
  19. fmt.Printf("Error embedding documents: %s \n", err)
  20. }
  21. fmt.Printf("Embedding response: %v \n", resp)
  22. }

Golang Client

You can install the Golang client by running the following command:

  1. go get github.com/amikos-tech/chroma-go

For more information visit https://go-client.chromadb.dev/

May 9, 2024