Building a Q&A System

Building a Q&A System

If you are looking for a simple way to build a Q&A system based on your knowledge graph, you should check out LangChain. Langchain allows you to easily query your Knowledge Graph using natural language. 

In this blog post, I will show you in a simple 4 steps how to use Langchain query a knowledge graph based on FalkorDB.

1. Installing LangChain

First, you need to install LangChain on your machine. You can download it from the official website or use the command line:

            > pip install langchain


        

2. Starting FalkorDB server locally

Staring a local FalkorDB is as simple as running a local docker you can go read on the documentation other ways to run it 

 
             > docker run -p 6379:6379 -it --rm falkordb/falkordb:latest

6:C 26 Aug 2023 08:36:26.297 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo

6:C 26 Aug 2023 08:36:26.297 # Redis version=7.0.12, bits=64, commit=00000000, modified=0, pid=6, just started

...

...

6:M 26 Aug 2023 08:36:26.322 * <graph> Starting up FalkorDB version 99.99.99.

6:M 26 Aug 2023 08:36:26.324 * <graph> Thread pool created, using 8 threads.

6:M 26 Aug 2023 08:36:26.324 * <graph> Maximum number of OpenMP threads set to 8

6:M 26 Aug 2023 08:36:26.324 * <graph> Query backlog size: 1000

6:M 26 Aug 2023 08:36:26.324 * Module 'graph' loaded from /FalkorDB/bin/linux-x64-release/src/falkordb.so

6:M 26 Aug 2023 08:36:26.324 * Ready to accept connections


        

 Running the demo

The rest of this blog will cover the simple steps you can take to get started, you can find the notebook as part of the LlamaIndex repository: falkordb.ipynb 

3. Create a Knowledge Graph

Now, let’s create a demo knowledge graph of movies and the leading actors. 

            from langchain.chat_models import ChatOpenAI
from langchain.graphs import FalkorDBGraph
from langchain.chains import FalkorDBQAChain

graph = FalkorDBGraph(database="movies")

graph.query("""
   CREATE
       (al:Person {name: 'Al Pacino', birthDate: '1940-04-25'}),
       (robert:Person {name: 'Robert De Niro', birthDate: '1943-08-17'}),
       (tom:Person {name: 'Tom Cruise', birthDate: '1962-07-3'}),
       (val:Person {name: 'Val Kilmer', birthDate: '1959-12-31'}),
       (anthony:Person {name: 'Anthony Edwards', birthDate: '1962-7-19'}),
       (meg:Person {name: 'Meg Ryan', birthDate: '1961-11-19'}),
       (god1:Movie {title: 'The Godfather'}),
       (god2:Movie {title: 'The Godfather: Part II'}),
       (god3:Movie {title: 'The Godfather Coda: The Death of Michael Corleone'}),
       (top:Movie {title: 'Top Gun'}),
       (al)-[:ACTED_IN]->(god1),
       (al)-[:ACTED_IN]->(god2),
       (al)-[:ACTED_IN]->(god3),
       (robert)-[:ACTED_IN]->(god2),
       (tom)-[:ACTED_IN]->(top),
       (val)-[:ACTED_IN]->(top),
       (anthony)-[:ACTED_IN]->(top),
       (meg)-[:ACTED_IN]->(top)
""")
        

4Creating the FalkorDB QA Chain

Last step, before we start querying the graph is setting up the Langchain’s chain, first we need to set our OpenAI key and then connect it all using FalkorDBQAChain. 

            import os

os.environ['OPENAI_API_KEY']='API_KEY_HERE'

graph.refresh_schema()

chain = FalkorDBQAChain.from_llm( ChatOpenAI(temperature=0), graph=graph, verbose=True)
        

5Querying the Graph

You are all set, you can start querying the Knowledge Graph… Let’s try a couple of questions.

            chain.run("Who played in Top Gun?")

> Entering new FalkorDBQAChain chain...

Generated Cypher:

MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE m.title = 'Top Gun'
RETURN p.name

Full Context:

[['Tom Cruise'], ['Val Kilmer'], ['Anthony Edwards'], ['Meg Ryan'], ['Tom Cruise'], ['Val Kilmer'], ['Anthony Edwards'], ['Meg Ryan']]

> Finished chain.

'Tom Cruise, Val Kilmer, Anthony Edwards, and Meg Ryan played in Top Gun.'

chain.run("Robert De Niro played in which movies?")

> Entering new FalkorDBQAChain chain...

Generated Cypher:

MATCH (p:Person {name: 'Robert De Niro'})-[:ACTED_IN]->(m:Movie)
RETURN m.title

Full Context:

[['The Godfather: Part II'], ['The Godfather: Part II']]

> Finished chain.

'Robert De Niro played in "The Godfather: Part II".'