Question & Answering (RAG)
Richa Deshpande avatar
Written by Richa Deshpande
Updated over a week ago

Vector Search powers implementing RAG (Retrieval Augmented Generation) in your application. Run the following code to create a question-answering model that uses these documents as context for the LLM.

You can use any LLM to power your AI apps with MongoDB Atlas. Here we give an example with OpenAI. Note that you will need to have credits added in your OpenAI account to continue. This code sample will use <$0.05 credits.

Create an OpenAI Account and API Key: https://platform.openai.com/api-keys

Add credits if you don’t have any: https://platform.openai.com/account/billing/overview

Install libraries:

pip install --upgrade langchain langchain-mongodb langchain-openai pymongo pypdf

Code sample:

import pymongo, pprint
from langchain_community.document_loaders import PyPDFLoader
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_mongodb import MongoDBAtlasVectorSearch
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain.prompts import PromptTemplate
from langchain.text_splitter import RecursiveCharacterTextSplitter
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi

# Instantiate Atlas Vector Search as a retriever
qa_retriever = vector_search.as_retriever(
search_type = "similarity",
search_kwargs = {"k": 10, "score_threshold": 0.75}
)

# Define a basic question-answering prompt template
prompt_template = """

Use the following pieces of context to answer the question at the end.
If you don't know the answer, just say that you don't know, don't try to make up an answer.

{context}

Question: {question}
"""
PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)

OPENAI_API_KEY = <open ai key>

# Create the question-answering model
qa = RetrievalQA.from_chain_type(
llm=OpenAI(openai_api_key=OPENAI_API_KEY)
retriever=qa_retriever,
return_source_documents=True,
chain_type="stuff",
chain_type_kwargs={"prompt": PROMPT},
)

# Prompt the LLM
query = "How can I secure my MongoDB Atlas cluster?"
docs = qa({"query": query})

print(docs["result"])
print("\nSource documents: ")
pprint.pprint(docs["source_documents"])

Tell me what this code does

This example does the following:

  • Instantiates Atlas Vector Search as a retriever to query for similar documents. We include optional parameters to keep only the 10 most relevant documents with a relevance score above 0.75.

  • Defines a LangChain prompt template to instruct the LLM to use these documents as context for your query. LangChain passes these documents to the {context} input variable and your question to the {question} variable.

  • Uses the RetrievalQA chain to create a question-answering model that generates context-aware responses. It specifies the following:

    • OpenAI as the LLM used to generate the response.

    • Atlas Vector Search as the retriever used to augment the data set.

    • The boolean value true to return the source documents used as context.

    • The stuff chain type, which specifies that the relevant documents should be inserted, or "stuffed," into the prompt.

    • The prompt template that you constructed.

  • Prompts the LLM with a sample query about Atlas security recommendations. The generated response might vary.

Did this answer your question?