This will help you get started with Ollama embedding models using LangChain. For detailed documentation on OllamaEmbeddings
features and configuration options, please refer to the API reference.
First, follow these instructions to set up and run a local Ollama instance:
brew install ollama
and start with brew services start ollama
ollama pull <name-of-model>
ollama pull llama3
On Mac, the models will be download to
~/.ollama/models
On Linux (or WSL), the models will be stored at
/usr/share/ollama/.ollama/models
ollama pull vicuna:13b-v1.5-16k-q4_0
(View the various tags for the Vicuna
model in this instance)ollama list
ollama run <name-of-model>
ollama help
in the terminal to see available commands.To enable automated tracing of your model calls, set your LangSmith API key:
InstallationThe LangChain Ollama integration lives in the langchain-ollama
package:
%pip install -qU langchain-ollama
Note: you may need to restart the kernel to use updated packages.
Instantiation
Now we can instantiate our model object and generate embeddings:
from langchain_ollama import OllamaEmbeddings
embeddings = OllamaEmbeddings(
model="llama3",
)
Indexing and Retrieval
Embedding models are often used in retrieval-augmented generation (RAG) flows, both as part of indexing data as well as later retrieving it. For more detailed instructions, please see our RAG tutorials.
Below, see how to index and retrieve data using the embeddings
object we initialized above. In this example, we will index and retrieve a sample document in the InMemoryVectorStore
.
from langchain_core.vectorstores import InMemoryVectorStore
text = "LangChain is the framework for building context-aware reasoning applications"
vectorstore = InMemoryVectorStore.from_texts(
[text],
embedding=embeddings,
)
retriever = vectorstore.as_retriever()
retrieved_documents = retriever.invoke("What is LangChain?")
print(retrieved_documents[0].page_content)
LangChain is the framework for building context-aware reasoning applications
Direct Usage
Under the hood, the vectorstore and retriever implementations are calling embeddings.embed_documents(...)
and embeddings.embed_query(...)
to create embeddings for the text(s) used in from_texts
and retrieval invoke
operations, respectively.
You can directly call these methods to get embeddings for your own use cases.
Embed single textsYou can embed single texts or documents with embed_query
:
single_vector = embeddings.embed_query(text)
print(str(single_vector)[:100])
[-0.0039849705, 0.023019705, -0.001768838, -0.0058736936, 0.00040999008, 0.017861595, -0.011274585,
Embed multiple texts
You can embed multiple texts with embed_documents
:
text2 = (
"LangGraph is a library for building stateful, multi-actor applications with LLMs"
)
two_vectors = embeddings.embed_documents([text, text2])
for vector in two_vectors:
print(str(vector)[:100])
[-0.0039849705, 0.023019705, -0.001768838, -0.0058736936, 0.00040999008, 0.017861595, -0.011274585,
[-0.0066985516, 0.009878328, 0.008019467, -0.009384944, -0.029560851, 0.025744654, 0.004872892, -0.0
API Reference
For detailed documentation on OllamaEmbeddings
features and configuration options, please refer to the API reference.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4