Ollama is an open-source project that makes it easy to run large language models(LLM) in a local environment. This tool allows users to download and run various LLMs with simple commands, enabling developers to experiment with and use AI models directly on their computers. Ollama is a tool with a user-friendly interface and fast performance, making AI development and experimentation more accessible and efficient.
Now that you have downloaded the model, let's load the model you downloaded and proceed with the embedding.
First, define Query and Documents
# Query
q = "Please tell me more about LangChain."
# Documents for Text Embedding
docs = [
"Hi, nice to meet you.",
"LangChain simplifies the process of building applications with large language models.",
"The LangChain English tutorial is structured based on LangChain's official documentation, cookbook, and various practical examples to help users utilize LangChain more easily and effectively.",
"LangChain simplifies the process of building applications with large-scale language models.",
"Retrieval-Augmented Generation (RAG) is an effective technique for improving AI responses.",
]
Next, let's load the embedding model downloaded with Ollama using Langchain.
The OllamaEmbeddings class in langchain_community/embeddings.py will be removed in langchain-community version 1.0.0.
# Load Embedding Model : Legacy
from langchain_community.embeddings import OllamaEmbeddings
# Serving and load the desired embedding model.
ollama_embeddings = OllamaEmbeddings(
model="nomic-embed-text", # model=<model-name>
)
So, in this tutorial, we used the OllamaEmbeddings class from langchain-ollama.
# Load Embedding Model : Latest
from langchain_ollama import OllamaEmbeddings
# Serving and load the desired embedding model.
ollama_embeddings = OllamaEmbeddings(
model="nomic-embed-text", # model=<model-name>
)
Let's use the loaded model to embed the Query and Documents.
Let's use the vector values of the query and documents obtained earlier to calculate the similarity.
In this tutorial, we will use cosine similarity to calculate the similarity between the Query and the Documents.
Using the Sklearn library in Python, you can easily calculate cosine similarity.
from sklearn.metrics.pairwise import cosine_similarity
# Calculate Cosine Similarity
similarity = cosine_similarity([embedded_query], embedded_docs)
# Sorting by Similarity in descending order
sorted_idx = similarity.argsort()[0][::-1]
# Output Result
print("[Query] Tell me about LangChain.\n====================================")
for i, idx in enumerate(sorted_idx):
print(f"[{i}] similarity: {similarity[0][idx]:.3f} | {docs[idx]}")
print()
[Query] Tell me about LangChain.
====================================
[0] similarity: 0.775 | The LangChain English tutorial is structured based on LangChain's official documentation, cookbook, and various practical examples to help users utilize LangChain more easily and effectively.
[1] similarity: 0.748 | LangChain simplifies the process of building applications with large language models.
[2] similarity: 0.745 | LangChain simplifies the process of building applications with large-scale language models.
[3] similarity: 0.399 | Retrieval-Augmented Generation (RAG) is an effective technique for improving AI responses.
[4] similarity: 0.398 | Hi, nice to meet you.