![](https://crypto4nerd.com/wp-content/uploads/2023/07/1ixNPq8Q2zmn4jMsfLh1APQ-1024x694.jpeg)
In today’s data-driven world, efficient querying and retrieval of information from databases play a vital role in various applications. While traditional database systems have relied on structured query languages and indexing techniques, emerging technologies like Semantic Search and Large Language Models (LLMs) offer new and powerful approaches to data querying. In this blog post, we will explore how we can leverage MongoDB, Vector Search(Semantic Search), and LLMs to enhance data search capabilities.
A bit of MongoDB background:
MongoDB is a popular NoSQL document-oriented database system known for its scalability, flexibility, and high performance. It differs from traditional SQL databases by organizing data into flexible, schema-less documents in BSON format (Binary JSON). This document-centric approach allows MongoDB to handle semi-structured and unstructured data more efficiently. MongoDB’s data model enables seamless scalability and easy integration with modern applications.
Vector Search:
Vector Search is a search methodology that leverages vector representations of data points to perform similarity-based searches. It is particularly effective for searching through high-dimensional data, such as images, audio, and text. Instead of relying on traditional keyword-based search techniques, vector search represents data points as vectors in a multi-dimensional space. These vectors capture the semantic meaning of the data, which can then be searched through to find similar content based on vectors being “near” one another in a high-dimensional space.Rather than searching for exact matches, vector search enables semantic searching. This means that even if the query words aren’t present in the index, but the meanings of the phrases are similar, they will still be considered a match.
A Gist of LLM:
Large Language Models (LLMs) refer to machine learning models capable of producing text that closely resembles human language and comprehending prompts in a natural manner. These models undergo training using extensive datasets comprising books, articles, websites, and other sources. By analyzing statistical patterns within the data, LLMs predict the most probable words or phrases that should follow a given input.
In this article, let’s see how we can use MongoDB Atlas Index for Vector Search and LLM for getting curated answer for the question
First, we establish a connection to the MongoDB server using the pymongo library. This connection allows us to access the desired database and collection that contains the data we want to query.
from pymongo import MongoClienturi="mongodb+srv://<username>:<password>@<host>?retryWrites=true&w=majority"
client = MongoClient(uri)
db_name = "database_name"
collection_name = "collection_name"
collection = client[db_name][collection_name]
Once connected, we can leverage the LangChain framework to perform vector-based similarity search on MongoDB data.
Using LangChain’s MongoDBAtlasVectorSearch , we can create an instance by providing the MongoDB collection, an embeddings model, and an index name. The embeddings model, such as Hugging Face’s sentence-transformers model, allows us to convert textual data into meaningful vector representations. The MongoDBAtlasVectorSearch class builds an index using these vectors, enabling efficient similarity search operations.
To perform a query, we simply pass a search query string to the similarity_search method of the MongoDBAtlasVectorSearch instance. The method returns a list of documents from the MongoDB collection that are most similar to the query, ranked by relevance.
Output:
from langchain.vectorstores import MongoDBAtlasVectorSearch
from langchain.embeddings import HuggingFaceEmbeddingsembeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
index_name = "index_name_from_MongoDB"
docsearch = MongoDBAtlasVectorSearch(embeddings, collection=collection, index_name=index_name)
query = "What did the president say about Ketanji Brown Jackson"
docs = docsearch.similarity_search(query)
print(docs[0].page_content)
Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you're at it, pass the Disclose Act so Americans can know who is funding our elections. Tonight, I'd like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service.
One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court.
And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation's top legal minds, who will continue Justice Breyer's legacy of excellence.
Enhancing the Answer with Large Language Models (LLMs):
While retrieving relevant documents from MongoDB is a powerful capability, we can further enhance the querying process by leveraging Large Language Models (LLMs).
To incorporate LLMs into the querying process, we can utilize the LangChain(LangChain is a framework for developing applications powered by language models) LLM functionality. By defining a prompt template that includes context and a question, we can pass the retrieved documents and the query question to an LLM question-answering chain. The LLM generates a response based on the context and question, providing insightful and relevant information extracted from the queried documents.
Output:
import json
from langchain import PromptTemplate, SagemakerEndpoint
from langchain.llms.sagemaker_endpoint import LLMContentHandler
from langchain.chains.question_answering import load_qa_chainprompt_template = """Use the following pieces of context to answer the question at
the end in a sentence.
{context}
Question: {question}
Answer:"""
PROMPT = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
class ContentHandler(LLMContentHandler):
content_type = "application/json"
accepts = "application/json"
def transform_input(self, prompt, model_kwargs) :
test = {"text_inputs": prompt}
encoded_json = json.dumps(test).encode("utf-8")
return encoded_json
def transform_output(self, output):
response_json = json.loads(output.read().decode("utf-8")).get('generated_texts')
print("response" , response_json)
return "".join(response_json)
content_handler = ContentHandler()
chain = load_qa_chain(llm=SagemakerEndpoint(
endpoint_name='sagemaker_endpoint',
region_name='us-east-1',
model_kwargs={"temperature":1e-10},
content_handler=content_handler),prompt=PROMPT,)
chain({"input_documents": docs, "question": query}, return_only_outputs=True)
response ['One of our nation's top legal minds, who will continue Justice Breyer's legacy']
Conclusion:
In this blog post, we explored the powerful combination of Vector Search, MongoDB, and Large Language Models (LLMs) for efficient data querying. By leveraging the capabilities of MongoDB’s document-oriented database system, we can store and retrieve data in a flexible and scalable manner. The integration of Vector Search with MongoDB, using libraries like langchain, enables us to perform similarity-based searches efficiently.
Additionally, incorporating Large Language Models (LLMs) into the querying process allows for the generation of meaningful responses based on the retrieved data. This fusion of technologies opens up new possibilities for various applications, such as intelligent chatbots, knowledge-based applications, etc.
This blog is written by Mukesh SM our ML engineer