{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# FalkorDBQAChain"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook shows how to use LLMs to provide a natural language interface to FalkorDB database.\n",
    "\n",
    "FalkorDB is a low latency property graph database management system. You can simply run its docker locally:\n",
    "\n",
    "```bash\n",
    "docker run -p 6379:6379 -it --rm falkordb/falkordb:edge\n",
    "```\n",
    "\n",
    "Once launched, you can simply start creating a database on the local machine and connect to it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chains import FalkorDBQAChain\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.graphs import FalkorDBGraph"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create a graph connection and insert some demo data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "graph = FalkorDBGraph(database=\"movies\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "graph.query(\n",
    "    \"\"\"\n",
    "    CREATE \n",
    "        (al:Person {name: 'Al Pacino', birthDate: '1940-04-25'}),\n",
    "        (robert:Person {name: 'Robert De Niro', birthDate: '1943-08-17'}),\n",
    "        (tom:Person {name: 'Tom Cruise', birthDate: '1962-07-3'}),\n",
    "        (val:Person {name: 'Val Kilmer', birthDate: '1959-12-31'}),\n",
    "        (anthony:Person {name: 'Anthony Edwards', birthDate: '1962-7-19'}),\n",
    "        (meg:Person {name: 'Meg Ryan', birthDate: '1961-11-19'}),\n",
    "\n",
    "        (god1:Movie {title: 'The Godfather'}),\n",
    "        (god2:Movie {title: 'The Godfather: Part II'}),\n",
    "        (god3:Movie {title: 'The Godfather Coda: The Death of Michael Corleone'}),\n",
    "        (top:Movie {title: 'Top Gun'}),\n",
    "\n",
    "        (al)-[:ACTED_IN]->(god1),\n",
    "        (al)-[:ACTED_IN]->(god2),\n",
    "        (al)-[:ACTED_IN]->(god3),\n",
    "        (robert)-[:ACTED_IN]->(god2),\n",
    "        (tom)-[:ACTED_IN]->(top),\n",
    "        (val)-[:ACTED_IN]->(top),\n",
    "        (anthony)-[:ACTED_IN]->(top),\n",
    "        (meg)-[:ACTED_IN]->(top)\n",
    "\"\"\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Creating FalkorDBQAChain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Node properties: [[OrderedDict([('label', None), ('properties', ['name', 'birthDate', 'title'])])]]\n",
      "Relationships properties: [[OrderedDict([('type', None), ('properties', [])])]]\n",
      "Relationships: [['(:Person)-[:ACTED_IN]->(:Movie)']]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "graph.refresh_schema()\n",
    "print(graph.schema)\n",
    "\n",
    "import os\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"API_KEY_HERE\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = FalkorDBQAChain.from_llm(ChatOpenAI(temperature=0), graph=graph, verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Querying the graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new FalkorDBQAChain chain...\u001b[0m\n",
      "Generated Cypher:\n",
      "\u001b[32;1m\u001b[1;3mMATCH (p:Person)-[:ACTED_IN]->(m:Movie)\n",
      "WHERE m.title = 'Top Gun'\n",
      "RETURN p.name\u001b[0m\n",
      "Full Context:\n",
      "\u001b[32;1m\u001b[1;3m[['Tom Cruise'], ['Val Kilmer'], ['Anthony Edwards'], ['Meg Ryan'], ['Tom Cruise'], ['Val Kilmer'], ['Anthony Edwards'], ['Meg Ryan']]\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Tom Cruise, Val Kilmer, Anthony Edwards, and Meg Ryan played in Top Gun.'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain.run(\"Who played in Top Gun?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new FalkorDBQAChain chain...\u001b[0m\n",
      "Generated Cypher:\n",
      "\u001b[32;1m\u001b[1;3mMATCH (p:Person)-[r:ACTED_IN]->(m:Movie)\n",
      "WHERE m.title = 'The Godfather: Part II'\n",
      "RETURN p.name\n",
      "ORDER BY p.birthDate ASC\n",
      "LIMIT 1\u001b[0m\n",
      "Full Context:\n",
      "\u001b[32;1m\u001b[1;3m[['Al Pacino']]\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'The oldest actor who played in The Godfather: Part II is Al Pacino.'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain.run(\"Who is the oldest actor who played in The Godfather: Part II?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new FalkorDBQAChain chain...\u001b[0m\n",
      "Generated Cypher:\n",
      "\u001b[32;1m\u001b[1;3mMATCH (p:Person {name: 'Robert De Niro'})-[:ACTED_IN]->(m:Movie)\n",
      "RETURN m.title\u001b[0m\n",
      "Full Context:\n",
      "\u001b[32;1m\u001b[1;3m[['The Godfather: Part II'], ['The Godfather: Part II']]\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Robert De Niro played in \"The Godfather: Part II\".'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain.run(\"Robert De Niro played in which movies?\")"
   ]
  }
 ],
 "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.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
