Skip to content

Commit 752fc2c

Browse files
INTPYTHON-787 Added check for AZURE_OPENAI_ENDPOINT test_graphrag.py and cleaned up closing clients (#232)
[Issue Key](https://jira.mongodb.org/browse/{ISSUE_KEY}) ## Summary <!-- What is this PR introducing? If context is already provided from the JIRA ticket, still place it in the Pull Request as you should not make the reviewer do digging for a basic summary. --> ## Changes in this PR <!-- What changes did you make to the code? What new APIs (public or private) were added, removed, or edited to generate the desired outcome explained in the above summary? --> This is a small bugfix. When we switched from including OPENAI_API_KEY in CI secrets to using AZURE_OPENAI_ENDPOINTS, we missed this one. ## Test Plan <!-- How did you test the code? If you added unit tests, you can say that. If you didn’t introduce unit tests, explain why. All code should be tested in some way – so please list what your validation strategy was. --> - Tests are run in github and evergreen. A passing patch build will be included. ## Checklist <!-- Do not delete the items provided on this checklist --> ### Checklist for Author - [ na] Did you update the changelog (if necessary)? - [X] Is the intention of the code captured in relevant tests? - [na] If there are new TODOs, has a related JIRA ticket been created? - [X] Has a MongoDB Employee run [the patch build of this PR](https://github.com/mongodb-labs/ai-ml-pipeline-testing?tab=readme-ov-file#running-a-patch-build-of-a-given-pr)? ### Checklist for Reviewer {@primary_reviewer} - [x] Does the title of the PR reference a JIRA Ticket? - [x] Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?) - [x] Have you checked for spelling & grammar errors? - [x] Is all relevant documentation (README or docstring) updated? ## Focus Areas for Reviewer (optional) <!-- List any complex portion of code you believe needs particular scrutiny and explain why. --> The author had thought of updating MongoDBGraphStore to be a context manager. All this would do is close the client. We decided to leave it with a close method only.
1 parent 0ebc7d0 commit 752fc2c

File tree

2 files changed

+70
-71
lines changed

2 files changed

+70
-71
lines changed

libs/langchain-mongodb/langchain_mongodb/graphrag/prompts.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,15 @@
115115
3. input: "For legal and operational purposes, many governments and organizations adopt specific definitions."
116116
output: []
117117
118-
In the final example, there are no entities.
118+
In the third example, there are no entities.
119119
Though there are concepts and nouns that might be types or attributes of entities,
120120
there is nothing here that could be seen as being a unique identifier or name.
121121
122+
4. input: ""
123+
output: []
124+
125+
In final third example, there are no entities.
126+
122127
### (Optional) Additional Examples
123128
124129
{entity_name_examples}

libs/langchain-mongodb/tests/integration_tests/test_graphrag.py

Lines changed: 64 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def collection() -> Generator[Collection]:
3030
client.close()
3131

3232

33-
if "OPENAI_API_KEY" not in os.environ:
33+
if not ("OPENAI_API_KEY" in os.environ or "AZURE_OPENAI_ENDPOINT" in os.environ):
3434
pytest.skip(
3535
"GraphRAG tests require OpenAI for chat responses.", allow_module_level=True
3636
)
@@ -157,39 +157,6 @@ def test_extract_entities_from_empty_string_names(graph_store):
157157
assert len(no_names) == 0
158158

159159

160-
def test_related_entities(graph_store):
161-
entity_names = ["John Doe", "Jane Smith"]
162-
related_entities = graph_store.related_entities(entity_names)
163-
assert len(related_entities) >= 4
164-
165-
no_entities = graph_store.related_entities([])
166-
assert isinstance(no_entities, list)
167-
assert len(no_entities) == 0
168-
169-
170-
def test_additional_entity_examples(entity_extraction_model, entity_example, documents):
171-
# First, create one client just to drop any existing collections
172-
client = MongoClient(CONNECTION_STRING)
173-
clxn_name = f"{COLLECTION_NAME}_addl_examples"
174-
client[DB_NAME][clxn_name].drop()
175-
# Test additional examples
176-
store_with_addl_examples = MongoDBGraphStore(
177-
connection_string=CONNECTION_STRING,
178-
database_name=DB_NAME,
179-
collection_name=clxn_name,
180-
entity_extraction_model=entity_extraction_model,
181-
entity_prompt=entity_prompt,
182-
query_prompt=query_prompt,
183-
entity_examples=entity_example,
184-
)
185-
store_with_addl_examples.collection.drop()
186-
187-
store_with_addl_examples.add_documents(documents)
188-
entity_names = ["ACME Corporation", "GreenTech Ltd."]
189-
new_entities = store_with_addl_examples.related_entities(entity_names)
190-
assert len(new_entities) >= 2
191-
192-
193160
def test_chat_response(graph_store, query_connection):
194161
"""Displays querying an existing Knowledge Graph Database"""
195162
answer = graph_store.chat_response(query_connection)
@@ -205,11 +172,42 @@ def test_similarity_search(graph_store, query_connection):
205172
assert any("attributes" in d.keys() for d in docs)
206173

207174

175+
def test_related_entities(graph_store):
176+
entity_names = ["John Doe", "Jane Smith"]
177+
related_entities = graph_store.related_entities(entity_names)
178+
assert len(related_entities) >= 4
179+
180+
no_entities = graph_store.related_entities([])
181+
assert isinstance(no_entities, list)
182+
assert len(no_entities) == 0
183+
184+
185+
def test_additional_entity_examples(entity_extraction_model, entity_example, documents):
186+
with MongoClient(CONNECTION_STRING) as client:
187+
collection = client[DB_NAME][f"{COLLECTION_NAME}_addl_examples"]
188+
collection.delete_many({})
189+
190+
store_with_addl_examples = MongoDBGraphStore(
191+
collection=collection,
192+
entity_extraction_model=entity_extraction_model,
193+
entity_prompt=entity_prompt,
194+
query_prompt=query_prompt,
195+
entity_examples=entity_example,
196+
)
197+
198+
store_with_addl_examples.add_documents(documents)
199+
entity_names = ["ACME Corporation", "GreenTech Ltd."]
200+
new_entities = store_with_addl_examples.related_entities(entity_names)
201+
assert len(new_entities) >= 2
202+
203+
208204
def test_validator(documents, entity_extraction_model):
209205
# Case 1. No existing collection.
210-
client = MongoClient(CONNECTION_STRING)
211206
clxn_name = f"{COLLECTION_NAME}_validation"
212-
client[DB_NAME][clxn_name].drop()
207+
208+
with MongoClient(CONNECTION_STRING) as client:
209+
client[DB_NAME][clxn_name].drop()
210+
213211
# now we call with validation that can be added without db admin privileges
214212
store = MongoDBGraphStore(
215213
connection_string=CONNECTION_STRING,
@@ -224,47 +222,43 @@ def test_validator(documents, entity_extraction_model):
224222
entities = store.collection.find({}).to_list()
225223
# Using subset because SolarGrid Initiative is not always considered an entity
226224
assert {"Person", "Organization"}.issubset(set(e["type"] for e in entities))
227-
client.close()
225+
store.close()
228226

229227
# Case 2: Existing collection with a validator
230-
client = MongoClient(CONNECTION_STRING)
231-
clxn_name = f"{COLLECTION_NAME}_validation"
232-
collection = client[DB_NAME][clxn_name]
233-
collection.delete_many({})
234-
235-
store = MongoDBGraphStore(
236-
collection=collection,
237-
entity_extraction_model=entity_extraction_model,
238-
validate=True,
239-
validation_action="error",
240-
)
241-
bulkwrite_results = store.add_documents(documents)
242-
assert len(bulkwrite_results) == len(documents)
243-
collection.drop()
244-
client.close()
228+
with MongoClient(CONNECTION_STRING) as client:
229+
collection = client[DB_NAME][clxn_name]
230+
collection.delete_many({})
231+
232+
store = MongoDBGraphStore(
233+
collection=collection,
234+
entity_extraction_model=entity_extraction_model,
235+
validate=True,
236+
validation_action="error",
237+
)
238+
bulkwrite_results = store.add_documents(documents)
239+
assert len(bulkwrite_results) == len(documents)
240+
collection.delete_many({})
245241

246242
# Case 3: Existing collection without a validator
247-
client = MongoClient(CONNECTION_STRING)
248-
clxn_name = f"{COLLECTION_NAME}_validation"
249-
collection = client[DB_NAME].create_collection(clxn_name)
250-
store = MongoDBGraphStore(
251-
collection=collection,
252-
entity_extraction_model=entity_extraction_model,
253-
validate=True,
254-
validation_action="error",
255-
)
256-
bulkwrite_results = store.add_documents(documents)
257-
assert len(bulkwrite_results) == len(documents)
258-
client.close()
243+
with MongoClient(CONNECTION_STRING) as client:
244+
collection = client[DB_NAME][clxn_name]
245+
store = MongoDBGraphStore(
246+
collection=collection,
247+
entity_extraction_model=entity_extraction_model,
248+
validate=True,
249+
validation_action="error",
250+
)
251+
bulkwrite_results = store.add_documents(documents)
252+
assert len(bulkwrite_results) == len(documents)
259253

260254

261255
def test_allowed_entity_types(documents, entity_extraction_model):
262256
"""Add allowed_entity_types. Use the validator to confirm behaviour."""
263257
allowed_entity_types = ["Person"]
264258
# drop collection
265-
client = MongoClient(CONNECTION_STRING)
266259
collection_name = f"{COLLECTION_NAME}_allowed_entity_types"
267-
client[DB_NAME][collection_name].drop()
260+
with MongoClient(CONNECTION_STRING) as client:
261+
client[DB_NAME][collection_name].drop()
268262
# create knowledge graph with only allowed_entity_types
269263
# this changes the schema at runtime
270264
store = MongoDBGraphStore(
@@ -283,15 +277,14 @@ def test_allowed_entity_types(documents, entity_extraction_model):
283277
all([len(e["relationships"].get("target_ids", [])) == 0 for e in entities])
284278
all([len(e["relationships"].get("types", [])) == 0 for e in entities])
285279
all([len(e["relationships"].get("attributes", [])) == 0 for e in entities])
280+
store.close()
286281

287282

288283
def test_allowed_relationship_types(documents, entity_extraction_model):
289284
# drop collection
290-
client = MongoClient(CONNECTION_STRING)
291285
clxn_name = f"{COLLECTION_NAME}_allowed_relationship_types"
292-
client[DB_NAME][clxn_name].drop()
293-
collection = client[DB_NAME].create_collection(clxn_name)
294-
collection.drop()
286+
with MongoClient(CONNECTION_STRING) as client:
287+
client[DB_NAME][clxn_name].drop()
295288
# create knowledge graph with only allowed_relationship_types=["partner"]
296289
# this changes the schema at runtime
297290
store = MongoDBGraphStore(
@@ -309,3 +302,4 @@ def test_allowed_relationship_types(documents, entity_extraction_model):
309302
for ent in store.collection.find({}):
310303
relationships.update(set(ent.get("relationships", {}).get("types", [])))
311304
assert relationships == {"partner"}
305+
store.close()

0 commit comments

Comments
 (0)