{ "cells": [ { "cell_type": "markdown", "id": "efb41c9e450b5a29", "metadata": {}, "source": [ "# Cognee GraphRAG Simple Example" ] }, { "cell_type": "code", "execution_count": null, "id": "982b897a29a26f7d", "metadata": {}, "outputs": [], "source": [ "!pip install cognee==0.1.39" ] }, { "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", "execution_count": null, "id": "initial_id", "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "os.environ[\"LLM_API_KEY\"] = \"\"" ] }, { "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", "execution_count": null, "id": "5805c346f03d8070", "metadata": {}, "outputs": [], "source": [ "current_directory = os.getcwd()\n", "file_path = os.path.join(current_directory, \"data\", \"alice_in_wonderland.txt\")" ] }, { "cell_type": "markdown", "id": "2826a80ca1ad0438", "metadata": {}, "source": [ "Give the file location to cognee to save it and process its contents" ] }, { "cell_type": "code", "execution_count": null, "id": "875763366723ee48", "metadata": {}, "outputs": [], "source": [ "import cognee\n", "await cognee.add(file_path)\n", "await cognee.cognify()" ] }, { "cell_type": "markdown", "id": "4944567387ec5821", "metadata": {}, "source": [ "Your data is ready to be queried:" ] }, { "cell_type": "code", "execution_count": null, "id": "29b3a1e3279100d2", "metadata": {}, "outputs": [], "source": [ "await cognee.search(\"List me all the influential characters in Alice in Wonderland.\")" ] }, { "cell_type": "code", "execution_count": null, "id": "883ce50d2d9dc584", "metadata": {}, "outputs": [], "source": [ "await cognee.search(\"How did Alice end up in Wonderland?\")" ] }, { "cell_type": "code", "execution_count": null, "id": "677e1bc52aa078b6", "metadata": {}, "outputs": [], "source": [ "await cognee.search(\"Tell me about Alice's personality.\")" ] }, { "cell_type": "markdown", "id": "fd521e182fb66d49", "metadata": {}, "source": [ "Bonus: See your processed data visualized in a knowledge graph" ] }, { "cell_type": "code", "execution_count": null, "id": "6effdae590b795d3", "metadata": {}, "outputs": [], "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}\")" ] }, { "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 }