Skip to content

Commit 4d8e8b6

Browse files
author
ajosh0504
committed
Updating README
1 parent ddf02d0 commit 4d8e8b6

File tree

47 files changed

+733
-783
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+733
-783
lines changed

README.md

+19-152
Large diffs are not rendered by default.

apps/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Javascript and Python apps and demos showcasing how to use MongoDB in GenAI applications.

apps/graph_rag_demo/data_insert.py

+33-20
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,66 @@
1-
from langchain_core.documents import Document
2-
from langchain_community.graphs.graph_document import GraphDocument, Node, Relationship
3-
from pymongo import MongoClient
4-
from dotenv import load_dotenv
5-
from pprint import pprint
61
import os
7-
import json
8-
from nodes_relationships import nodes,links
2+
3+
from dotenv import load_dotenv
4+
from nodes_relationships import links, nodes
5+
from pymongo import MongoClient
6+
97

108
def build_lookup_map():
119
quick_lookup = {}
1210
for key in links.keys():
1311
relationship = links[key]
1412
source_node = relationship.source
15-
lookup_key = str(source_node.id)+":"+str(source_node.type)
16-
lookup_content = quick_lookup.get(lookup_key,"empty")
13+
lookup_key = str(source_node.id) + ":" + str(source_node.type)
14+
lookup_content = quick_lookup.get(lookup_key, "empty")
1715
if lookup_content != "empty":
1816
quick_lookup.get(lookup_key).append(relationship)
1917
else:
2018
quick_lookup[lookup_key] = [relationship]
2119
return quick_lookup
2220

21+
2322
def create_mongo_documents():
2423
mongo_documents = []
2524
quick_lookup = build_lookup_map()
2625
for key in nodes.keys():
2726
node = nodes[key]
28-
id = str(node.id)+":"+str(node.type)
27+
id = str(node.id) + ":" + str(node.type)
2928
type = node.type
30-
rel = quick_lookup.get(id,None)
29+
rel = quick_lookup.get(id, None)
3130
relationships = set()
3231
targets = {}
33-
if rel!=None:
32+
if rel is not None:
3433
for relationship in rel:
35-
target_id = str(relationship.target.id)+":"+str(relationship.target.type)
34+
target_id = (
35+
str(relationship.target.id) + ":" + str(relationship.target.type)
36+
)
3637
relationships.add(target_id)
37-
target_type = targets.get(target_id,None)
38-
if target_type != None:
38+
target_type = targets.get(target_id, None)
39+
if target_type is not None:
3940
targets[target_id].append(relationship.type)
4041
else:
4142
targets[target_id] = [relationship.type]
42-
mongo_documents.append({"_id":id,"type":type,"relationships":list(relationships),"targets":targets})
43+
mongo_documents.append(
44+
{
45+
"_id": id,
46+
"type": type,
47+
"relationships": list(relationships),
48+
"targets": targets,
49+
}
50+
)
4351
else:
44-
mongo_documents.append({"_id":id,"type":type,"relationships":[],"targets":{}})
52+
mongo_documents.append(
53+
{"_id": id, "type": type, "relationships": [], "targets": {}}
54+
)
4555
return mongo_documents
4656

57+
4758
def mongo_insert():
4859
mongo_documents = create_mongo_documents()
4960
try:
5061
uri = os.getenv("ATLAS_CONNECTION_STRING")
5162
print(uri)
52-
client = MongoClient(uri)
63+
client = MongoClient(uri, appname="devrel.showcase.graph_rag_app")
5364
database = client["langchain_db"]
5465
collection = database["nodes_relationships"]
5566
for doc in mongo_documents:
@@ -58,8 +69,10 @@ def mongo_insert():
5869
print(e)
5970
finally:
6071
client.close()
61-
if __name__=="__main__":
72+
73+
74+
if __name__ == "__main__":
6275
load_dotenv()
6376
print("Inserting Documents")
6477
mongo_insert()
65-
print("Successfully Inserted Documents")
78+
print("Successfully Inserted Documents")

apps/local-rag-pdf/rag_module.py

+42-25
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
from langchain_core.globals import set_verbose, set_debug
2-
from langchain_ollama import ChatOllama, OllamaEmbeddings
1+
import logging
2+
from typing import Optional
3+
4+
import yaml
35
from langchain.schema.output_parser import StrOutputParser
4-
from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearch
5-
from pymongo import MongoClient
6-
from langchain_community.document_loaders import PyPDFLoader
7-
from langchain.text_splitter import RecursiveCharacterTextSplitter
86
from langchain.schema.runnable import RunnablePassthrough
7+
from langchain.text_splitter import RecursiveCharacterTextSplitter
8+
from langchain_community.document_loaders import PyPDFLoader
99
from langchain_community.vectorstores.utils import filter_complex_metadata
10+
from langchain_core.globals import set_debug, set_verbose
1011
from langchain_core.prompts import ChatPromptTemplate
11-
import logging
12-
import yaml
13-
12+
from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearch
13+
from langchain_ollama import ChatOllama, OllamaEmbeddings
14+
from pymongo import MongoClient
1415

1516
# Enable verbose debugging
1617
set_debug(True)
@@ -20,11 +21,13 @@
2021
logging.basicConfig(level=logging.INFO)
2122
logger = logging.getLogger(__name__)
2223

24+
2325
def load_config(config_file: str = "config.yaml"):
2426
"""Load configuration from a YAML file."""
25-
with open(config_file, "r") as file:
27+
with open(config_file) as file:
2628
return yaml.safe_load(file)
2729

30+
2831
class ChatPDF:
2932
"""A class designed for PDF ingestion and question answering using RAG with detailed debugging logs."""
3033

@@ -40,29 +43,33 @@ def __init__(self, config_file: str = "config.yaml"):
4043
mongo_connection_str = config["mongo_connection_str"]
4144
database_name = config["database_name"]
4245
collection_name = config["collection_name"]
43-
46+
4447
self.model = ChatOllama(model=llm_model)
4548
self.embeddings = OllamaEmbeddings(model=embedding_model)
46-
self.text_splitter = RecursiveCharacterTextSplitter(chunk_size=1024, chunk_overlap=100)
49+
self.text_splitter = RecursiveCharacterTextSplitter(
50+
chunk_size=1024, chunk_overlap=100
51+
)
4752
self.prompt = ChatPromptTemplate.from_template(
4853
"""
4954
You are a helpful assistant answering questions based on the uploaded document and the conversation.
50-
55+
5156
Conversation History:
5257
{conversation_history}
53-
58+
5459
Context from Documents:
5560
{context}
56-
61+
5762
Question:
5863
{question}
59-
64+
6065
Provide a concise, accurate answer (preferably within three sentences), ensuring it directly addresses the question.
6166
"""
6267
)
63-
68+
6469
# Setup MongoDB connection
65-
self.client = MongoClient(mongo_connection_str)
70+
self.client = MongoClient(
71+
mongo_connection_str, appname="devrel.showcase.local_rag_pdf_app"
72+
)
6673
self.collection = self.client[database_name][collection_name]
6774

6875
# Verbose connection check
@@ -74,7 +81,7 @@ def __init__(self, config_file: str = "config.yaml"):
7481
collection=self.collection,
7582
embedding=self.embeddings,
7683
index_name="vector_index",
77-
relevance_score_fn="cosine"
84+
relevance_score_fn="cosine",
7885
)
7986

8087
# Create vector search index on the collection
@@ -107,7 +114,13 @@ def upload_and_index_pdf(self, pdf_file_path: str):
107114
self.vector_store.add_documents(documents=chunks)
108115
logger.info("Document embeddings stored successfully in MongoDB Atlas.")
109116

110-
def query_with_context(self, query: str, conversation_history: list = None, k: int = 5, score_threshold: float = 0.2):
117+
def query_with_context(
118+
self,
119+
query: str,
120+
conversation_history: Optional[list] = None,
121+
k: int = 5,
122+
score_threshold: float = 0.2,
123+
):
111124
"""
112125
Answer a query using the RAG pipeline with verbose debugging and conversation history.
113126
@@ -132,7 +145,9 @@ def query_with_context(self, query: str, conversation_history: list = None, k: i
132145
# Generate and log query embeddings
133146
query_embedding = self.embeddings.embed_query(query)
134147
logger.info(f"User Query: {query}")
135-
logger.debug(f"Query Embedding (sample values): {query_embedding[:10]}... [Total Length: {len(query_embedding)}]")
148+
logger.debug(
149+
f"Query Embedding (sample values): {query_embedding[:10]}... [Total Length: {len(query_embedding)}]"
150+
)
136151

137152
logger.info(f"Retrieving context for query: {query}")
138153
retrieved_docs = self.retriever.invoke(query)
@@ -147,17 +162,19 @@ def query_with_context(self, query: str, conversation_history: list = None, k: i
147162

148163
# Format the input for the LLM, including conversation history
149164
formatted_input = {
150-
"conversation_history": "\n".join(conversation_history) if conversation_history else "",
165+
"conversation_history": (
166+
"\n".join(conversation_history) if conversation_history else ""
167+
),
151168
"context": "\n\n".join(doc.page_content for doc in retrieved_docs),
152169
"question": query,
153170
}
154171

155172
# Build the RAG chain
156173
chain = (
157174
RunnablePassthrough() # Passes the input as-is
158-
| self.prompt # Formats the input for the LLM
159-
| self.model # Queries the LLM
160-
| StrOutputParser() # Parses the LLM's output
175+
| self.prompt # Formats the input for the LLM
176+
| self.model # Queries the LLM
177+
| StrOutputParser() # Parses the LLM's output
161178
)
162179

163180
logger.info("Generating response using the LLM.")

misc/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Miscellaneous collection of guides, code snippets etc.
File renamed without changes.
File renamed without changes.
File renamed without changes.

notebooks/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Step-by-step Jupyter Notebook examples on how to use MongoDB as a vector database, data store, memory provider etc. in AI applications.
2+
3+
The easiest way to run these notebooks is in Google Colab by clicking on the [Open In Colab](https://colab.research.google.com/assets/colab-badge.svg) button at the top of the notebook.
4+
5+
Some notebooks also have written articles/tutorials associated with them. To read the article, click the [View Article](https://img.shields.io/badge/View%20Article-blue) at the top of the notebook.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Jupyter Notebooks that cover advanced techniques such as vector quantization, parent document retrieval, hybrid search etc. that can help improve and/or optimize LLM applications.

notebooks/mongodb-specific/geospatialqueries_vectorsearch_spritzes.ipynb notebooks/advanced_techniques/geospatialqueries_vectorsearch_spritzes.ipynb

+3-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,9 @@
435435
"connection_string = getpass.getpass(\n",
436436
" prompt=\"Enter connection string WITH USER + PASS here\"\n",
437437
")\n",
438-
"client = MongoClient(connection_string)\n",
438+
"client = MongoClient(\n",
439+
" connection_string, appname=\"devrel.showcase.geospatial_vector_search\"\n",
440+
")\n",
439441
"\n",
440442
"# name your database and collection anything you want since it will be created when you enter your data\n",
441443
"database = client[\"spritz_summer\"]\n",

notebooks/techniques/retrieval_strategies_mongodb_llamaindex.ipynb notebooks/advanced_techniques/retrieval_strategies_mongodb_llamaindex.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
"source": [
111111
"MONGODB_URI = getpass.getpass(\"Enter your MongoDB URI: \")\n",
112112
"mongodb_client = MongoClient(\n",
113-
" MONGODB_URI, appname=\"devrel.content.retrieval_strategies_llamaindex\"\n",
113+
" MONGODB_URI, appname=\"devrel.showcase.retrieval_strategies_llamaindex\"\n",
114114
")"
115115
]
116116
},

notebooks/techniques/retrieval_strategies_mongodb_llamaindex_togetherai.ipynb notebooks/advanced_techniques/retrieval_strategies_mongodb_llamaindex_togetherai.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
"source": [
118118
"MONGODB_URI = getpass.getpass(\"Enter your MongoDB URI: \")\n",
119119
"mongodb_client = MongoClient(\n",
120-
" MONGODB_URI, appname=\"devrel.content.retrieval_strategies_llamaindex\"\n",
120+
" MONGODB_URI, appname=\"devrel.showcase.retrieval_strategies_llamaindex\"\n",
121121
")"
122122
]
123123
},

notebooks/agents/MongoDB_Haystack_self_reflecting_Cooking_agent.ipynb

+4-1
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,10 @@
15711571
"doc[\"date\"] = datetime.datetime.now()\n",
15721572
"\n",
15731573
"# Insert JSON reciepe into MongoDB\n",
1574-
"mongo_client = MongoClient(os.environ[\"MONGO_CONNECTION_STRING\"])\n",
1574+
"mongo_client = MongoClient(\n",
1575+
" os.environ[\"MONGO_CONNECTION_STRING\"],\n",
1576+
" appname=\"devrel.showcase.haystack_cooking_agent\",\n",
1577+
")\n",
15751578
"db = mongo_client[\"ai_shop\"]\n",
15761579
"collection = db[\"reciepes\"]\n",
15771580
"collection.insert_one(doc)"

notebooks/agents/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Jupyter Notebooks showing how to build agentic AI applications using MongoDB's integrations with CrewAI, Haystack, LangGraph, LlamaIndex etc.

notebooks/agents/how_to_build_ai_agent_claude_3_5_sonnet_llamaindex_mongodb.ipynb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,9 @@
10271027
"def get_mongo_client(mongo_uri):\n",
10281028
" \"\"\"Establish and validate connection to the MongoDB.\"\"\"\n",
10291029
"\n",
1030-
" client = pymongo.MongoClient(mongo_uri, appname=\"devrel.showcase.python\")\n",
1030+
" client = pymongo.MongoClient(\n",
1031+
" mongo_uri, appname=\"devrel.showcase.claude_llamaindex_agent\"\n",
1032+
" )\n",
10311033
"\n",
10321034
" # Validate the connection\n",
10331035
" ping_result = client.admin.command(\"ping\")\n",

notebooks/agents/how_to_build_ai_agent_openai_llamaindex_mongodb.ipynb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,9 @@
10271027
"def get_mongo_client(mongo_uri):\n",
10281028
" \"\"\"Establish and validate connection to the MongoDB.\"\"\"\n",
10291029
"\n",
1030-
" client = pymongo.MongoClient(mongo_uri, appname=\"devrel.showcase.python\")\n",
1030+
" client = pymongo.MongoClient(\n",
1031+
" mongo_uri, appname=\"devrel.showcase.openai_llamaindex_agent\"\n",
1032+
" )\n",
10311033
"\n",
10321034
" # Validate the connection\n",
10331035
" ping_result = client.admin.command(\"ping\")\n",

0 commit comments

Comments
 (0)