MongoDB Atlas is a developer data platform providing a suite of cloud database and data services that accelerate and simplify how you build with data.
MongoDB offers a forever free Atlas cluster in the public cloud which can be done upon creating an account here . You can also follow the quickstart here to get your free Atlas cluster!
You can create and get an API Key on the API Keys Page :
We will be using the Patronus SDK in addition to LLamaIndex. LlamaIndex is a data framework that provides an interface for ingesting and indexing datasets, which integrates well with MongoDB’s Atlas data store.
Text
pip install patronus llama_index
Text
from llama_index.node_parser import SimpleNodeParser
from llama_index.storage.docstore import MongoDocumentStore
nodes = SimpleNodeParser () .get_nodes_from_documents ( YOUR_DOCUMENTS )
# You can get this from your test deployment in the MongoDB interface
MONGODB_URI ="mongodb+srv://<username>:<password>@<cluster_identifier>.mongodb.net/?retryWrites=true&w=majority"
docstore = MongoDocumentStore.from_uri (uri = MONGODB_URI )
docstore.add_documents(nodes )
Below is an overview of an example system:
We will have to first load a vector store:
Text
from llama_index.storage.docstore import MongoDocumentStore
from llama_index.storage.index_store import MongoIndexStore
from llama_index.storage.storage_context import StorageContext
from llama_index import GPTVectorStoreIndex
docstore = MongoDocumentStore.from_uri (uri = MONGODB_URI, db_name = "db_docstore" )
storage_context = StorageContext.from_defaults (
docstore = MongoDocumentStore.from_uri (uri = MONGODB_URI, db_name = "db_docstore" ) ,
index_store = MongoIndexStore.from_uri (uri = MONGODB_URI, db_name = "db_docstore" ) ,
)
vector_index = GPTVectorStoreIndex ( nodes, storage_context=storage_context )
Now we can get responses:
Text
for INPUT in YOUR_INPUTS:
vector_response = vector_index.as_query_engine () .query ( user_query )
llm_output = vector_response.response
source_text = [x.text for x in vector_response.source_nodes]
Now let's use the SOTA Lynx hallucination detection evaluator from Patronus:
Text
PATRONUS_API_KEY = "PREVIOUS_API_KEY"
headers = {
"Content-Type" : "application/json",
"X-API-KEY" : PATRONUS_API_KEY,
}
data = {
"criteria" : [
{ "criterion_id" : "lynx"}
],
"input" : sample["model_input"],
"retrieved_context" : sample["retrieved_context"],
"output" : sample["model_output"],
"explain" : True
}
response = requests.post ( "<https://api.patronus.ai/v1/evaluate>" , headers=headers, json=data )
response.raise_for_status ()
results = response.json () ["results"]
Now you have a functional RAG system! 🚀