Extend the FalkorDB QA demo (#9992)

- Description: Extend the FalkorDB QA demo
  - Tag maintainer: @baskaryan
This commit is contained in:
Guy Korland 2023-08-30 17:13:18 +03:00 committed by GitHub
parent 588237ef30
commit 24c0b01c38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 149 additions and 25 deletions

View File

@ -33,6 +33,13 @@
"from langchain.chains import FalkorDBQAChain" "from langchain.chains import FalkorDBQAChain"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create a graph connection and insert some demo data."
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
@ -44,7 +51,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 4,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -53,38 +60,72 @@
"[]" "[]"
] ]
}, },
"execution_count": 8, "execution_count": 4,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"graph.query(\n", "graph.query(\"\"\"\n",
" \"\"\"\n", " CREATE \n",
"MERGE (m:Movie {name:\"Top Gun\"})\n", " (al:Person {name: 'Al Pacino', birthDate: '1940-04-25'}),\n",
"WITH m\n", " (robert:Person {name: 'Robert De Niro', birthDate: '1943-08-17'}),\n",
"UNWIND [\"Tom Cruise\", \"Val Kilmer\", \"Anthony Edwards\", \"Meg Ryan\"] AS actor\n", " (tom:Person {name: 'Tom Cruise', birthDate: '1962-07-3'}),\n",
"MERGE (a:Actor {name:actor})\n", " (val:Person {name: 'Val Kilmer', birthDate: '1959-12-31'}),\n",
"MERGE (a)-[:ACTED_IN]->(m)\n", " (anthony:Person {name: 'Anthony Edwards', birthDate: '1962-7-19'}),\n",
"\"\"\"\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",
"\"\"\")"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "markdown",
"execution_count": 4,
"metadata": {}, "metadata": {},
"outputs": [],
"source": [ "source": [
"graph.refresh_schema()\n", "### Creating FalkorDBQAChain"
"import os\n",
"os.environ['OPENAI_API_KEY']='API_KEY_HERE'\n"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 5,
"metadata": {}, "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",
"os.environ['OPENAI_API_KEY']='API_KEY_HERE'"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"chain = FalkorDBQAChain.from_llm(\n", "chain = FalkorDBQAChain.from_llm(\n",
@ -92,9 +133,16 @@
")" ")"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Querying the graph"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 7,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -105,10 +153,11 @@
"\n", "\n",
"\u001b[1m> Entering new FalkorDBQAChain chain...\u001b[0m\n", "\u001b[1m> Entering new FalkorDBQAChain chain...\u001b[0m\n",
"Generated Cypher:\n", "Generated Cypher:\n",
"\u001b[32;1m\u001b[1;3mMATCH (:Movie {title: 'Top Gun'})<-[:ACTED_IN]-(actor:Person)\n", "\u001b[32;1m\u001b[1;3mMATCH (p:Person)-[:ACTED_IN]->(m:Movie)\n",
"RETURN actor.name AS output\u001b[0m\n", "WHERE m.title = 'Top Gun'\n",
"RETURN p.name\u001b[0m\n",
"Full Context:\n", "Full Context:\n",
"\u001b[32;1m\u001b[1;3m[]\u001b[0m\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", "\n",
"\u001b[1m> Finished chain.\u001b[0m\n" "\u001b[1m> Finished chain.\u001b[0m\n"
] ]
@ -116,7 +165,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"'The actor who played in Top Gun is Tom Cruise.'" "'Tom Cruise, Val Kilmer, Anthony Edwards, and Meg Ryan played in Top Gun.'"
] ]
}, },
"execution_count": 7, "execution_count": 7,
@ -127,6 +176,81 @@
"source": [ "source": [
"chain.run(\"Who played in Top Gun?\")" "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": { "metadata": {

View File

@ -52,9 +52,9 @@ class FalkorDBGraph:
def refresh_schema(self) -> None: def refresh_schema(self) -> None:
"""Refreshes the schema of the FalkorDB database""" """Refreshes the schema of the FalkorDB database"""
self.schema = ( self.schema = (
f"Node properties: {node_properties_query}\n" f"Node properties: {self.query(node_properties_query)}\n"
f"Relationships properties: {rel_properties_query}\n" f"Relationships properties: {self.query(rel_properties_query)}\n"
f"Relationships: {rel_query}\n" f"Relationships: {self.query(rel_query)}\n"
) )
def query(self, query: str, params: dict = {}) -> List[Dict[str, Any]]: def query(self, query: str, params: dict = {}) -> List[Dict[str, Any]]: