Skip to content

Commit

Permalink
Merge pull request #177 from topoteretes/simple-python-example
Browse files Browse the repository at this point in the history
Simple python example
  • Loading branch information
Vasilije1990 authored Nov 5, 2024
2 parents d3d49b6 + 6c395d7 commit 401befc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
36 changes: 26 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.add(text) # Add text to cognee
await cognee.cognify() # Use LLMs and cognee to create knowledge graph

await cognee.cognify() # Use LLMs and cognee to create a knowledge graph
search_results = await cognee.search( # Search cognee for insights
SearchType.INSIGHTS,
{'query': 'Tell me about NLP'}
)

search_results = await cognee.search("INSIGHTS", {'query': 'NLP'}) # Query cognee for the insights
for result_text in search_results: # Display results
print(result_text)

for result in search_results:
do_something_with_result(result)
asyncio.run(main())
```

A version of this example is here: `examples/pyton/simple_example.py`

### Create your own memory store

Expand Down
39 changes: 39 additions & 0 deletions examples/python/simple_example.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit 401befc

Please sign in to comment.