{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6f22c8fe6d92cfcc",
   "metadata": {},
   "source": [
    "# Using Cognee with Python Development Data\n",
    "\n",
    "Unite authoritative Python practice (Guido van Rossum's own contributions!), normative guidance (Zen/PEP 8), and your lived context (rules + conversations) into one *AI memory* that produces answers that are relevant, explainable, and consistent."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe69acbf9ab1a22b",
   "metadata": {},
   "source": [
    "## What You'll Learn\n",
    "\n",
    "In this comprehensive tutorial, you'll discover how to transform scattered development data into an intelligent knowledge system that enhances your coding workflow. By the end, you'll have:\n",
    "\n",
    "- **Connected disparate data sources** (Guido's CPython contributions, mypy development, PEP discussions, your Python projects) into a unified AI memory graph\n",
    "- **Built an memory layer** that understands Python design philosophy, best practice coding patterns, and your preferences and experience\n",
    "- **Learn how to use intelligent search capabilities** that combine the diverse context\n",
    "\n",
    "This tutorial demonstrates the power of **knowledge graphs** and **retrieval-augmented generation (RAG)** for software development, showing you how to build systems that learn from Python's creator and improve your own Python development."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b03b59c064213dd4",
   "metadata": {},
   "source": [
    "## Cognee and its core operations\n",
    "\n",
    "Before we dive in, let's understand the core Cognee operations we'll be working with:\n",
    "\n",
    "- **`cognee.add()`** - Ingests raw data (files, text, APIs) into the system\n",
    "- **`cognee.cognify()`** - Processes and structures data into a knowledge graph using AI\n",
    "- **`cognee.search()`** - Queries the knowledge graph with natural language or Cypher\n",
    "- **`cognee.memify()`** - Cognee's \"secret sauce\" that infers implicit connections and rules from your data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a7669fbb6a3e6c7",
   "metadata": {},
   "source": [
    "## Data used in this tutorial\n",
    "\n",
    "Cognee can ingest many types of sources. In this tutorial, we use a small, concrete set of files that cover different perspectives:\n",
    "\n",
    "- **`guido_contributions.json` — Authoritative exemplars.** Real PRs and commits from Guido van Rossum (mypy, CPython). These show how Python’s creator solved problems and provide concrete anchors for patterns.\n",
    "- **`pep_style_guide.md` — Norms.** Encodes community style and typing conventions (PEP 8 and related). Ensures that search results and inferred rules align with widely accepted standards.\n",
    "- **`zen_principles.md` — Philosophy.** The Zen of Python. Grounds design trade‑offs (simplicity, explicitness, readability) beyond syntax or mechanics.\n",
    "- **`my_developer_rules.md` — Local constraints.** Your house rules, conventions, and project‑specific requirements (scope, privacy, Spec.md). Keeps recommendations relevant to your actual workflow.\n",
    "- **`copilot_conversations.json` — Personal history.** Transcripts of real assistant conversations, including your questions, code snippets, and discussion topics. Captures “how you code” and connects it to “how Guido codes.”"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a5dac2c6fdc7ca7",
   "metadata": {},
   "source": [
    "# Preliminaries\n",
    "\n",
    "Cognee relies heavily on async functions.\n",
    "We need `nest_asyncio` so `await` works in this notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20cb02b49e3c53e2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T14:35:00.836706Z",
     "start_time": "2025-09-07T14:35:00.832646Z"
    }
   },
   "outputs": [],
   "source": [
    "import nest_asyncio\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30e66c894fb4cfd5",
   "metadata": {},
   "source": [
    "To strike the balanace between speed, cost, anc quality, we recommend using OpenAI's `4o-mini` model; make sure your `.env` file contains this line:\n",
    "\n",
    "```LLM_MODEL=\"gpt-4o-mini\"```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45e1caaec20c9518",
   "metadata": {},
   "source": [
    "We will do a quick import check."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9386ecb596860399",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T14:35:03.910260Z",
     "start_time": "2025-09-07T14:35:00.938966Z"
    }
   },
   "outputs": [],
   "source": [
    "import cognee\n",
    "import os\n",
    "from pathlib import Path\n",
    "\n",
    "print('🔍 Quick Cognee Import Check')\n",
    "print('=' * 30)\n",
    "print(f'📍 Cognee location: {cognee.__file__}')\n",
    "print(f'📁 Package directory: {os.path.dirname(cognee.__file__)}')\n",
    "\n",
    "# Check if it's local or installed\n",
    "current_dir = Path.cwd()\n",
    "cognee_path = Path(cognee.__file__)\n",
    "if current_dir in cognee_path.parents:\n",
    "    print('🏠 Status: LOCAL DEVELOPMENT VERSION')\n",
    "else:\n",
    "    print('📦 Status: INSTALLED PACKAGE')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76895c6570d1a4dc",
   "metadata": {},
   "source": [
    "And just to be safe, we will make sure that the path contains the root directory, so Python can find everything it needs to run the notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19e74e6b691020db",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T14:35:03.921217Z",
     "start_time": "2025-09-07T14:35:03.918659Z"
    }
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "from pathlib import Path\n",
    "notebook_dir = Path.cwd()\n",
    "if notebook_dir.name == 'notebooks':\n",
    "    project_root = notebook_dir.parent\n",
    "else:\n",
    "    project_root = Path.cwd()\n",
    "\n",
    "# Add project root to the beginning of sys.path\n",
    "project_root_str = str(project_root.absolute())\n",
    "if project_root_str not in sys.path:\n",
    "    sys.path.insert(0, project_root_str)\n",
    "\n",
    "print(f\"📁 Project root: {project_root_str}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af584b935cbdc8d",
   "metadata": {},
   "source": [
    "Finally, we will begin with a clean slate, by removing any previous Cognee data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd47383aa9519465",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T14:35:06.194073Z",
     "start_time": "2025-09-07T14:35:03.929446Z"
    }
   },
   "outputs": [],
   "source": [
    "await cognee.prune.prune_data()\n",
    "await cognee.prune.prune_system(metadata=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93c9783037715026",
   "metadata": {},
   "source": [
    "### First data ingestion: Exploring Guido's Python Contributions\n",
    "\n",
    "We'll begin with a document that contains detailed PRs and commits from Guido van Rossum's work on mypy and CPython, showing real-world examples of Python's creator solving type system and language design challenges.\n",
    "\n",
    "We'll use Cognee's `add()` and `cognify()` functions to ingest this data and build a knowledge graph that connects Guido's development patterns with Python best practices."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8743ed520b4de37",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T14:39:53.511862Z",
     "start_time": "2025-09-07T14:35:06.228778Z"
    }
   },
   "outputs": [],
   "source": [
    "import cognee\n",
    "\n",
    "result = await cognee.add(\n",
    "    os.path.abspath(\"data/guido_contributions.json\"),\n",
    "    node_set=[\"guido_data\"]\n",
    ")\n",
    "await cognee.cognify(temporal_cognify=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f08b362cbf12b398",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T14:39:53.561679Z",
     "start_time": "2025-09-07T14:39:53.559528Z"
    }
   },
   "outputs": [],
   "source": [
    "results = await cognee.search(\"Show me commits\")\n",
    "print(results[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10d582d02ead905e",
   "metadata": {},
   "source": [
    "### What's just happened?\n",
    "The `search()` function uses natural language to query a knowledge graph containing Guido's development history.\n",
    "Unlike traditional databases, Cognee understands the relationships between commits, language features, design decisions, and evolution over time.\n",
    "\n",
    "Cognee also allows you to visualize the graphs created:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1fb068f422bda6cf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T14:39:53.688017Z",
     "start_time": "2025-09-07T14:39:53.598467Z"
    }
   },
   "outputs": [],
   "source": [
    "from cognee import visualize_graph\n",
    "await visualize_graph('./guido_contributions.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f24341c97d6eaccb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T14:39:53.733197Z",
     "start_time": "2025-09-07T14:39:53.729922Z"
    }
   },
   "outputs": [],
   "source": [
    "from IPython.display import IFrame, HTML, display\n",
    "display(IFrame(\"./guido_contributions.html\", width=\"100%\", height=\"500\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3418aa17bf35e3bb",
   "metadata": {},
   "source": [
    "**Why visualization matters:** Knowledge graphs reveal hidden patterns in data. In this case, patterins in Guido's contributions to Python's development. The interactive visualization shows how different projects (CPython, mypy, PEPs), features, and time periods connect - insights that show Python's thoughtful evolution.\n",
    "\n",
    "Take a moment to explore the graph. Notice how:\n",
    "\n",
    "- CPython core development clusters around 2020\n",
    "- Mypy contributions focus on fixtures and run classes\n",
    "- PEP discussions mention Thomas Grainiger and Adam Turner\n",
    "- Time-based connections show how ideas evolved into features\n",
    "\n",
    "*Note: You can open the visualization on your browser using the html file from path provided above.*"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e8d9094a09ae05d",
   "metadata": {},
   "source": [
    "### Ingesting more data\n",
    "\n",
    "Now we'll add the remaining data and see how they connections emerge between Guido's contributions, Python best practices, and user conversations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5315318324968f0f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T14:40:48.589875Z",
     "start_time": "2025-09-07T14:39:53.785054Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "await cognee.add(os.path.abspath(\"data/copilot_conversations.json\"), node_set=[\"developer_data\"])\n",
    "await cognee.add(os.path.abspath(\"data/my_developer_rules.md\"), node_set=[\"developer_data\"])\n",
    "await cognee.add(os.path.abspath(\"data/zen_principles.md\"), node_set=[\"principles_data\"])\n",
    "await cognee.add(os.path.abspath(\"data/pep_style_guide.md\"), node_set=[\"principles_data\"])\n",
    "\n",
    "await cognee.cognify(temporal_cognify=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98b69c45db2fca3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T14:40:51.305617Z",
     "start_time": "2025-09-07T14:40:48.605622Z"
    }
   },
   "outputs": [],
   "source": [
    "results = await cognee.search(\n",
    "    \"What Python type hinting challenges did I face, and how does Guido approach similar problems in mypy?\",\n",
    "    query_type=cognee.SearchType.GRAPH_COMPLETION\n",
    ")\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c49c4c252036fa1",
   "metadata": {},
   "source": [
    "You'll see that cognee has connected your Python development challenges with Guido's approaches, revealing patterns like:\n",
    "\n",
    "- \"Type hint implementation failed due to circular imports - similar to issue Guido solved in mypy PR #1234\"\n",
    "- \"Performance bottleneck in list comprehension matches pattern Guido optimized in CPython commit abc123\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1f4606bfed8fc45",
   "metadata": {},
   "source": [
    "### Memify\n",
    "\n",
    "Let's now introduce the memory functions. These algorithms run on top of your semantic layer, connecting the dots and improving the search.\n",
    "\n",
    "Memify is customizable and can use any transformation you'd like to write."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20234960f7566b15",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T14:43:42.390990Z",
     "start_time": "2025-09-07T14:40:51.321301Z"
    }
   },
   "outputs": [],
   "source": [
    "await cognee.memify()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58d3ccec16f67c24",
   "metadata": {},
   "source": [
    "**What `memify()` does for Python:** This advanced function uses AI to:\n",
    "\n",
    "- **Infer rule patterns** from your code (e.g., \"When implementing iterators, always follow the protocol Guido established\")\n",
    "- **Connect design philosophy to practice** (e.g., linking \"explicit is better than implicit\" to your type hinting decisions)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a304033f9f0f5dcf",
   "metadata": {},
   "source": [
    "Now let's see how the system has connected your Python development patterns with established best practices:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "518fa9b17a604657",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T14:43:44.837614Z",
     "start_time": "2025-09-07T14:43:42.465351Z"
    }
   },
   "outputs": [],
   "source": [
    "# Search for connections between your async patterns and Python philosophy\n",
    "results = await cognee.search(\n",
    "    query_text= \"How does my AsyncWebScraper implementation align with Python's design principles?\",\n",
    "    query_type=cognee.SearchType.GRAPH_COMPLETION\n",
    ")\n",
    "print(\"Python Pattern Analysis:\", results)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c77c1582ab9fc32",
   "metadata": {},
   "source": [
    "### Nodeset filtering\n",
    "\n",
    "You may have noticed that we added different documents to different datasets. This allows us to narrow our retrieval at search time.\n",
    "\n",
    "By constraining searches to particular node sets, you can ensure that responses draw from appropriate sources. Questions about style guidelines can be directed specifically to PEP documents and design principles, while implementation questions can focus on actual code examples and developer experiences."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c64036c03abe41e7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T14:43:49.542281Z",
     "start_time": "2025-09-07T14:43:44.852455Z"
    }
   },
   "outputs": [],
   "source": [
    "from cognee.modules.engine.models.node_set import NodeSet\n",
    "results = await cognee.search(\n",
    "    query_text= \"How should variables be named?\",\n",
    "    query_type=cognee.SearchType.GRAPH_COMPLETION,\n",
    "    node_type=NodeSet,\n",
    "    node_name=['principles_data']\n",
    ")\n",
    "\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c641b8b7e50dd2ae",
   "metadata": {},
   "source": [
    "### Temporal graphs\n",
    "\n",
    "The `temporal_cognify` option enabled during graph construction provides powerful capabilities for understanding how Python development has evolved over time. This temporal awareness allows queries that explore trends, identify periods of intense development activity, or understand how specific features emerged and matured.\n",
    "\n",
    "Temporal queries can reveal insights about development velocity, the relationship between different features introduced in similar timeframes, or how implementation approaches have changed as the language has evolved. This historical perspective provides valuable context for understanding current best practices and anticipating future directions.\n",
    "\n",
    "For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28e7d5a75e076b8f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T14:43:55.278031Z",
     "start_time": "2025-09-07T14:43:49.555704Z"
    }
   },
   "outputs": [],
   "source": [
    "result = await cognee.search(\n",
    "    query_text = \"What can we learn from Guido's contributions in 2025?\",\n",
    "    query_type=cognee.SearchType.TEMPORAL\n",
    ")\n",
    "\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec6cf074a6c272ab",
   "metadata": {},
   "source": [
    "### Feedback loops\n",
    "\n",
    "cognee supports continuous improvement through a feedback mechanism that captures the utility and relevance of search results based on actualy user messages. This creates a learning system that adapts to your specific needs and preferences over time.\n",
    "\n",
    "When search interactions are saved, you can store the results use SearchType.FEEDBACK to provide feedback about the last_k answer from the system. This feedback becomes part of the graph itself, helping the system provide increasingly relevant results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67dec85a658aad76",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T14:43:58.472950Z",
     "start_time": "2025-09-07T14:43:55.288993Z"
    }
   },
   "outputs": [],
   "source": [
    "answer = await cognee.search(\n",
    "    query_type=cognee.SearchType.GRAPH_COMPLETION,\n",
    "    query_text=\"What is the most zen thing about Python?\",\n",
    "    save_interaction=True,  # This enables feedback later\n",
    ")\n",
    "\n",
    "print(answer)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f64d90e6dadaefb",
   "metadata": {},
   "source": [
    "This enables giving feedback, that itself can also be stored in the graph and will be included in future searches:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "870149ea58fd109c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-07T14:44:00.503091Z",
     "start_time": "2025-09-07T14:43:58.480893Z"
    }
   },
   "outputs": [],
   "source": [
    "feedback = await cognee.search(\n",
    "    query_type=cognee.SearchType.FEEDBACK,\n",
    "    query_text=\"Last result was useful, I like code that complies with best practices.\",\n",
    "    last_k=1,\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "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.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
