""" End-to-end integration test for edge-centered payload and triplet embeddings. """ import os import pathlib import cognee from cognee.infrastructure.databases.graph import get_graph_engine from cognee.infrastructure.databases.vector import get_vector_engine_async from cognee.modules.search.types import SearchType from cognee.shared.logging_utils import get_logger from cognee.modules.ontology.rdf_xml.RDFLibOntologyResolver import RDFLibOntologyResolver from cognee.modules.ontology.ontology_config import Config logger = get_logger() text_data = """ Apple is a technology company that produces the iPhone, iPad, and Mac computers. The company is known for its innovative products and ecosystem integration. Microsoft develops the Windows operating system and Office productivity suite. They are also major players in cloud computing with Azure. Google created the Android operating system and provides search engine services. The company is a leader in artificial intelligence and machine learning. """ ontology_content = """ A company operating in the technology sector. Software products and applications. Physical hardware products. Apple Microsoft Google iPhone Windows Android """ async def main(): data_directory_path = str( pathlib.Path( os.path.join( pathlib.Path(__file__).parent, ".data_storage/test_edge_centered_payload", ) ).resolve() ) cognee_directory_path = str( pathlib.Path( os.path.join( pathlib.Path(__file__).parent, ".cognee_system/test_edge_centered_payload", ) ).resolve() ) cognee.config.data_root_directory(data_directory_path) cognee.config.system_root_directory(cognee_directory_path) # Enable triplet embedding for this test os.environ["TRIPLET_EMBEDDING"] = "true" dataset_name = "tech_companies" await cognee.prune.prune_data() await cognee.prune.prune_system(metadata=True) await cognee.add(data=text_data, dataset_name=dataset_name) import tempfile with tempfile.NamedTemporaryFile(mode="w", suffix=".owl", delete=False) as f: f.write(ontology_content) ontology_file_path = f.name try: logger.info(f"Loading ontology from: {ontology_file_path}") config: Config = { "ontology_config": { "ontology_resolver": RDFLibOntologyResolver(ontology_file=ontology_file_path) } } await cognee.cognify(datasets=[dataset_name], config=config) graph_engine = await get_graph_engine() nodes_phase2, edges_phase2 = await graph_engine.get_graph_data() vector_engine = await get_vector_engine_async() triplets_phase2 = await vector_engine.search( query_text="technology", limit=None, collection_name="Triplet_text" ) assert len(triplets_phase2) > 0, ( "Expected triplet embeddings to be created, but found none." ) assert len(triplets_phase2) == len(edges_phase2), ( f"Triplet count ({len(triplets_phase2)}) should be equal to edge count: ({len(edges_phase2)})." ) logger.info(f"Created {len(triplets_phase2)} triplets from {len(edges_phase2)} edges") search_results_phase2 = await cognee.search( query_type=SearchType.TRIPLET_COMPLETION, query_text="What products does Apple make?", ) assert search_results_phase2 is not None, ( "Search should return results for triplet embeddings in simple ontology use case." ) finally: if os.path.exists(ontology_file_path): os.unlink(ontology_file_path) if __name__ == "__main__": import asyncio from cognee.shared.logging_utils import setup_logging setup_logging() loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) try: loop.run_until_complete(main()) finally: loop.run_until_complete(loop.shutdown_asyncgens()) loop.close()