Merge pull request #177 from topoteretes/simple-python-example

Simple python example
This commit is contained in:
Vasilije 2024-11-05 13:58:59 +01:00 committed by GitHub
commit 401befc687
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 65 additions and 10 deletions

View file

@ -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

View file

@ -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())