Ollama Embedder

The OllamaEmbedder can be used to embed text data into vectors locally using Ollama.

NoteThe model used for generating embeddings needs to run locally. In this case it is openhermes so you have to install ollama and run ollama pull openhermes in your terminal.

Usage

1from kern.knowledge.knowledge import Knowledge
2from kern.vectordb.pgvector import PgVector
3from kern.knowledge.embedder.ollama import OllamaEmbedder
4
5# Embed sentence in database
6embeddings = OllamaEmbedder(id="openhermes").get_embedding("The quick brown fox jumps over the lazy dog.")
7
8# Print the embeddings and their dimensions
9print(f"Embeddings: {embeddings[:5]}")
10print(f"Dimensions: {len(embeddings)}")
11
12# Use an embedder in a knowledge base
13knowledge = Knowledge(
14 vector_db=PgVector(
15 db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
16 table_name="ollama_embeddings",
17 embedder=OllamaEmbedder(),
18 ),
19 max_results=2,
20)

Params

ParameterTypeDefaultDescription
modelstr"openhermes"The name of the model used for generating embeddings.
dimensionsint4096The dimensionality of the embeddings generated by the model.
hoststr-The host address for the API endpoint.
timeoutAny-The timeout duration for API requests.
optionsAny-Additional options for configuring the API request.
client_kwargsOptional[Dict[str, Any]]-Additional keyword arguments for configuring the API client. Optional.
ollama_clientOptional[OllamaClient]-An instance of the OllamaClient to use for making API requests. Optional.

Developer Resources