From bcdf0db5bef597d60a4b8c2b707c3517bf5bd5aa Mon Sep 17 00:00:00 2001 From: Casey Clements Date: Mon, 17 Feb 2025 09:46:51 -0500 Subject: [PATCH 1/5] Added sentence to prompt to not include explanation odf what it was doing. --- libs/langchain-mongodb/langchain_mongodb/graphrag/prompts.py | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/langchain-mongodb/langchain_mongodb/graphrag/prompts.py b/libs/langchain-mongodb/langchain_mongodb/graphrag/prompts.py index 6083b3f7..15def95d 100644 --- a/libs/langchain-mongodb/langchain_mongodb/graphrag/prompts.py +++ b/libs/langchain-mongodb/langchain_mongodb/graphrag/prompts.py @@ -21,6 +21,7 @@ INPUT: You will be provided a text document. OUTPUT: - You will produce valid json according the "Output Schema" section below. +- Your response **must be** a **valid JSON document** with NO extra text, explanations, or markdown formatting. - The extracted entities and relationships **MUST STRICTLY CONFORM** to the constraints outlined below. - Any entities or relationships not matching the allowed types must be **EXCLUDED**. From f3de56f2676db4c18f1fb2999edc2913bdaf2310 Mon Sep 17 00:00:00 2001 From: Casey Clements Date: Wed, 19 Feb 2025 08:54:49 -0500 Subject: [PATCH 2/5] Set cache to false in extraction model in tests --- libs/langchain-mongodb/tests/integration_tests/test_graphrag.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/langchain-mongodb/tests/integration_tests/test_graphrag.py b/libs/langchain-mongodb/tests/integration_tests/test_graphrag.py index 2b8ce272..175270a3 100644 --- a/libs/langchain-mongodb/tests/integration_tests/test_graphrag.py +++ b/libs/langchain-mongodb/tests/integration_tests/test_graphrag.py @@ -36,7 +36,7 @@ def collection() -> Collection: def entity_extraction_model() -> BaseChatModel: """LLM for converting documents into Graph of Entities and Relationships""" try: - return ChatOpenAI(model="gpt-4o", temperature=0.0) + return ChatOpenAI(model="gpt-4o", temperature=0.0, cache=False) except Exception: pass From 6922a6d9b04f0de030f28bcb032fa4e7f5f71e0d Mon Sep 17 00:00:00 2001 From: Casey Clements Date: Mon, 17 Feb 2025 09:46:51 -0500 Subject: [PATCH 3/5] Added sentence to prompt to exclude explanation of what it was doing. From a8078e110bb9b8cab590bd2764ce0db87d076b51 Mon Sep 17 00:00:00 2001 From: Casey Clements Date: Wed, 19 Feb 2025 10:51:16 -0500 Subject: [PATCH 4/5] Change a couple fixtures scope to reduce llm calls --- .../tests/integration_tests/test_graphrag.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/langchain-mongodb/tests/integration_tests/test_graphrag.py b/libs/langchain-mongodb/tests/integration_tests/test_graphrag.py index 175270a3..ae875258 100644 --- a/libs/langchain-mongodb/tests/integration_tests/test_graphrag.py +++ b/libs/langchain-mongodb/tests/integration_tests/test_graphrag.py @@ -17,7 +17,7 @@ COLLECTION_NAME = "langchain_test_graphrag" -@pytest.fixture(scope="function") +@pytest.fixture(scope="module") def collection() -> Collection: client = MongoClient(MONGODB_URI) db = client[DB_NAME] @@ -114,7 +114,7 @@ def entity_example(): """ -@pytest.fixture(scope="function") +@pytest.fixture(scope="module") def graph_store(collection, entity_extraction_model, documents) -> MongoDBGraphStore: store = MongoDBGraphStore( collection, entity_extraction_model, entity_prompt, query_prompt From 258064f002077db14cec884c915d6e5cb9a88ed9 Mon Sep 17 00:00:00 2001 From: Casey Clements Date: Wed, 19 Feb 2025 10:52:11 -0500 Subject: [PATCH 5/5] Wrap addition of validation in try/except with error message. --- .../langchain_mongodb/graphrag/graph.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/libs/langchain-mongodb/langchain_mongodb/graphrag/graph.py b/libs/langchain-mongodb/langchain_mongodb/graphrag/graph.py index 413d9804..e24c0261 100644 --- a/libs/langchain-mongodb/langchain_mongodb/graphrag/graph.py +++ b/libs/langchain-mongodb/langchain_mongodb/graphrag/graph.py @@ -14,6 +14,7 @@ from pymongo import MongoClient, UpdateOne from pymongo.collection import Collection from pymongo.driver_info import DriverInfo +from pymongo.errors import OperationFailure from pymongo.results import BulkWriteResult from langchain_mongodb.graphrag import example_templates, prompts @@ -140,12 +141,18 @@ def __init__( self.allowed_relationship_types = [] if validate: - collection.database.command( - "collMod", - collection.name, - validator={"$jsonSchema": self._schema}, - validationAction=validation_action, - ) + try: + collection.database.command( + "collMod", + collection.name, + validator={"$jsonSchema": self._schema}, + validationAction=validation_action, + ) + except OperationFailure: + logger.warning( + "Validation will NOT be performed. User must be DB Admin to add validation **after** a Collection is created. \n" + "Please add validator when you create collection: db.create_collection.(coll_name, validator={'$jsonSchema': self._schema})" + ) self.collection = collection # Include examples