Cognee simple document example (#521)
<!-- .github/pull_request_template.md --> ## Description Notebook and python example for cognee simple example ## DCO Affirmation I affirm that all code in every commit of this pull request conforms to the terms of the Topoteretes Developer Certificate of Origin <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced an interactive demo showcasing asynchronous document processing and querying for key insights from a sample text. - **Documentation** - Added an in-depth, step-by-step guide in a Jupyter Notebook that walks users through setup, configuration, querying, and visualizing processed data. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
parent
8f84713b54
commit
3850e9c7a1
4 changed files with 3753 additions and 0 deletions
1772
examples/data/alice_in_wonderland.txt
Normal file
1772
examples/data/alice_in_wonderland.txt
Normal file
File diff suppressed because it is too large
Load diff
34
examples/python/cognee_simple_document_demo.py
Normal file
34
examples/python/cognee_simple_document_demo.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import asyncio
|
||||
import cognee
|
||||
|
||||
import os
|
||||
|
||||
# By default cognee uses OpenAI's gpt-4o-mini LLM model
|
||||
# Provide your OpenAI LLM API KEY
|
||||
os.environ["LLM_API_KEY"] = ""
|
||||
|
||||
|
||||
async def cognee_demo():
|
||||
# Get file path to document to process
|
||||
from pathlib import Path
|
||||
|
||||
current_directory = Path(__file__).resolve().parent.parent
|
||||
file_path = os.path.join(current_directory, "data", "alice_in_wonderland.txt")
|
||||
|
||||
# Call Cognee to process document
|
||||
await cognee.add(file_path)
|
||||
await cognee.cognify()
|
||||
|
||||
# Query Cognee for information from provided document
|
||||
answer = await cognee.search("List me all the important characters in Alice in Wonderland.")
|
||||
print(answer)
|
||||
|
||||
answer = await cognee.search("How did Alice end up in Wonderland?")
|
||||
print(answer)
|
||||
|
||||
answer = await cognee.search("Tell me about Alice's personality.")
|
||||
print(answer)
|
||||
|
||||
|
||||
# Cognee is an async library, it has to be called in an async context
|
||||
asyncio.run(cognee_demo())
|
||||
175
notebooks/cognee_simple_demo.ipynb
Normal file
175
notebooks/cognee_simple_demo.ipynb
Normal file
|
|
@ -0,0 +1,175 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "efb41c9e450b5a29",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Cognee GraphRAG Simple Example"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"id": "982b897a29a26f7d",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"!pip install cognee==0.1.26"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": null
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f51e92e9fdcf77b7",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
" By default cognee uses OpenAI's gpt-4o-mini LLM model.\n",
|
||||
"\n",
|
||||
" Provide your OpenAI LLM API KEY in the step bellow. Here's a guide on how to get your [OpenAI API key](https://help.openai.com/en/articles/4936850-where-do-i-find-my-openai-api-key)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"id": "initial_id",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"os.environ[\"LLM_API_KEY\"] = \"\""
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": null
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f1fec64bc573bb",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"In this step we'll get the location of the file to store and process."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"id": "5805c346f03d8070",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"current_directory = os.getcwd()\n",
|
||||
"file_path = os.path.join(current_directory, \"data\", \"alice_in_wonderland.txt\")"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": null
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2826a80ca1ad0438",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Give the file location to cognee to save it and process its contents"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"id": "875763366723ee48",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"import cognee\n",
|
||||
"await cognee.add(file_path)\n",
|
||||
"await cognee.cognify()"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": null
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4944567387ec5821",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Your data is ready to be queried:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"id": "29b3a1e3279100d2",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"await cognee.search(\"List me all the influential characters in Alice in Wonderland.\")"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": null
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"id": "883ce50d2d9dc584",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"await cognee.search(\"How did Alice end up in Wonderland?\")"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": null
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"id": "677e1bc52aa078b6",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"await cognee.search(\"Tell me about Alice's personality.\")"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": null
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "fd521e182fb66d49",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Bonus: See your processed data visualized in a knowledge graph"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"id": "6effdae590b795d3",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"import webbrowser\n",
|
||||
"import os\n",
|
||||
"from cognee.api.v1.visualize.visualize import visualize_graph\n",
|
||||
"html = await visualize_graph()\n",
|
||||
"home_dir = os.path.expanduser(\"~\")\n",
|
||||
"html_file = os.path.join(home_dir, \"graph_visualization.html\")\n",
|
||||
"display(html_file)\n",
|
||||
"webbrowser.open(f\"file://{html_file}\")"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": null
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f0945d6f1d962ab",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"For more examples and information on how Cognee GraphRAG works checkout our other more detailed notebooks."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Cognee Env",
|
||||
"language": "python",
|
||||
"name": "venv"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.12.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
1772
notebooks/data/alice_in_wonderland.txt
Normal file
1772
notebooks/data/alice_in_wonderland.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue