Skip to main content

Ollama

Let’s load the Ollama Embeddings class.

from langchain.embeddings import OllamaEmbeddings
embeddings = OllamaEmbeddings()
text = "This is a test document."

To generate embeddings, you can either query an invidivual text, or you can query a list of texts.

query_result = embeddings.embed_query(text)
query_result[:5]
[-0.09996652603149414,
0.015568195842206478,
0.17670190334320068,
0.16521021723747253,
0.21193109452724457]
doc_result = embeddings.embed_documents([text])
doc_result[0][:5]
[-0.04242777079343796,
0.016536075621843338,
0.10052520781755447,
0.18272875249385834,
0.2079043835401535]

Let’s load the Ollama Embeddings class with smaller model (e.g. llama:7b). Note: See other supported models https://ollama.ai/library

embeddings = OllamaEmbeddings(model="llama2:7b")
text = "This is a test document."
query_result = embeddings.embed_query(text)
query_result[:5]
[-0.09996627271175385,
0.015567859634757042,
0.17670205235481262,
0.16521376371383667,
0.21193283796310425]
doc_result = embeddings.embed_documents([text])
doc_result[0][:5]
[-0.042427532374858856,
0.01653730869293213,
0.10052604228258133,
0.18272635340690613,
0.20790338516235352]