From 17d4aca53829f1947c3de9d2587972d082b6d758 Mon Sep 17 00:00:00 2001 From: lxobr Date: Tue, 5 Nov 2024 10:05:14 +0100 Subject: [PATCH 1/2] feat: add simple python example --- examples/python/simple_example.py | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 examples/python/simple_example.py diff --git a/examples/python/simple_example.py b/examples/python/simple_example.py new file mode 100644 index 000000000..9b64142ed --- /dev/null +++ b/examples/python/simple_example.py @@ -0,0 +1,39 @@ +import cognee +import asyncio +from cognee.api.v1.search import SearchType + +# Prerequisites: +# 1. Copy `.env.template` and rename it to `.env`. +# 2. Add your OpenAI API key to the `.env` file in the `LLM_API_KEY` field: +# LLM_API_KEY = "your_key_here" +# 3. (Optional) To minimize setup effort, set `VECTOR_DB_PROVIDER="lancedb"` in `.env". + +async def main(): + # Create a clean slate for cognee -- reset data and system state + await cognee.prune.prune_data() + await cognee.prune.prune_system(metadata=True) + + # cognee knowledge graph will be created based on this text + text = """ + Natural language processing (NLP) is an interdisciplinary + subfield of computer science and information retrieval. + """ + + # Add the text, and make it available for cognify + await cognee.add(text) + + # Use LLMs and cognee to create knowledge graph + await cognee.cognify() + + # Query cognee for insights on the added text + search_results = await cognee.search( + SearchType.INSIGHTS, + {'query': 'Tell me about NLP'} + ) + + # Display search results + for result_text in search_results: + print(result_text) + +if __name__ == '__main__': + asyncio.run(main()) From 6c395d7d7f5c100a37499a8b0dfec9e9ef811036 Mon Sep 17 00:00:00 2001 From: lxobr Date: Tue, 5 Nov 2024 10:05:43 +0100 Subject: [PATCH 2/2] docs: update README simple example --- README.md | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6d3a89b73..28c07fab4 100644 --- a/README.md +++ b/README.md @@ -96,24 +96,40 @@ DB_PASSWORD=cognee ### Simple example -Run the default cognee pipeline: +First, copy `.env.template` to `.env` and add your OpenAI API key to the LLM_API_KEY field. -``` +Optionally, set `VECTOR_DB_PROVIDER="lancedb"` in `.env` to simplify setup. + +This script will run the default pipeline: + +```python import cognee +import asyncio +from cognee.api.v1.search import SearchType -text = """Natural language processing (NLP) is an interdisciplinary - subfield of computer science and information retrieval""" +async def main(): + await cognee.prune.prune_data() # Reset cognee data + await cognee.prune.prune_system(metadata=True) # Reset cognee system state -await cognee.add(text) # Add a new piece of information + text = """ + Natural language processing (NLP) is an interdisciplinary + subfield of computer science and information retrieval. + """ -await cognee.cognify() # Use LLMs and cognee to create a knowledge graph + await cognee.add(text) # Add text to cognee + await cognee.cognify() # Use LLMs and cognee to create knowledge graph -search_results = await cognee.search("INSIGHTS", {'query': 'NLP'}) # Query cognee for the insights + search_results = await cognee.search( # Search cognee for insights + SearchType.INSIGHTS, + {'query': 'Tell me about NLP'} + ) -for result in search_results: - do_something_with_result(result) + for result_text in search_results: # Display results + print(result_text) + +asyncio.run(main()) ``` - +A version of this example is here: `examples/pyton/simple_example.py` ### Create your own memory store