Graph based ChatBot with neo4j, GPT-4 & Streamlit (2024)

Graph based ChatBot with neo4j, GPT-4 & Streamlit (2)

Artificial intelligence (AI) and machine learning (ML) are transforming the world in many ways. They enable us to be more productive and creative, and to solve complex problems with data and algorithms. One of the most exciting applications of AI and ML is chatbot, a system that can communicate with humans using natural language. Chatbots can provide us with new ways to interact with information, services, and even personalities. In this blog post, we will explore how can we use chatbot to surface information to the end data users.

When users want to access data, they usually have two options: either they view static dashboards that display the data in charts and graphs, or they query the data directly from the data sources. In both cases, there is some kind of query that runs behind the scenes to fetch the data.

The main goal of data users is to analyse the data and answer business questions that can inform their decisions. However, due to security reasons, we cannot use openAI LLM model to train on the actual data. A possible solution is to train the LLM model on the schema of the data, which is a representation of the structure and relationships of the data. Furthermore, the LLM model can be trained to write queries that can answer business questions in different languages and formats. This can reduce the time and effort required to get the answers from the data.

Large language models (LLMs) are powerful tools that can write code and queries with ease. We can leverage their abilities to generate queries that can run on the data and provide us with business insights.

To illustrate the use case more clearly, let’s consider a hypothetical example of a bike manufacturing firm. The firm has a supply chain data that contains information about the bill of material, which is a hierarchical list of the spare parts used to build the final product. Each part can have sub-parts and parent parts, as well as suppliers and cost elements associated with it. There can also be alternative parts for a given part. The following diagram shows a simplified sketch of the data structure.

Graph based ChatBot with neo4j, GPT-4 & Streamlit (3)

We want to answer some business questions that are related to the supply chain data of the bike manufacturing firm. These questions include: How many parts does a specific supplier provide? What are the details of each part, such as its name, type, and function? How much does each part cost? How are the parts connected to each other in the hierarchy? What are the upstream and downstream parts for a given part id? What happens if we remove a part from the hierarchy? These questions are not easy to answer by just looking at the reports, because they require complex analysis and reasoning.

We can use a graph database to store the data, because it suits the nature and access patterns of the data (Of course, there may be other ways to represent the data besides a graph database). We can choose the most popular graph database to build the knowledge graph. We can also use streamlit to create a chatbot quickly and easily. We will need a large language model (LLM) that can understand the business questions and translate them into cypher queries. We can use openAI’s GPT, which is one of the most popular LLMs, for our use case.

In our case we are using neo4j as it suites to the nature of the data. However, we can replace neo4j with any other data storage and train LLM to write it’s compatible queries.

The flow of the solution would looks like this.

Graph based ChatBot with neo4j, GPT-4 & Streamlit (4)

We need to setup neo4j for the solution

  • Install neo4j: You can also install it on docker.
  • Create openAI account.
  • You need to make payment of at least $5 to use the API.
  • Learn more about openAI API documentation here.

I am using below model to build the graph in neo4j.

Next step is to load the data into neo4j in above graph model. We can use below statements for the same.

# Loading of data into neo4j

LOAD CSV WITH HEADERS FROM "file:///parts.csv" AS row
CREATE (n:Parts)
SET n = row;

LOAD CSV WITH HEADERS FROM "file:///cost.csv" AS row
CREATE (n:Cost)
SET n = row;

LOAD CSV WITH HEADERS FROM "file:///supplier.csv" AS row
CREATE (n:Supplier)
SET n = row;

# Build relationships between the nodes

MATCH (p:Parts),(s:Supplier)
WHERE p.supplier_id = s.supplier_id
CREATE (p)-[:SUPPLIED_BY]->(s);

MATCH (s:Supplier),(c:Cost)
WHERE c.supplier_id = s.supplier_id
CREATE (s)-[:HAS]->(c);

# A part can be a alternate part of another part
MATCH (p1:Parts), (p2:Parts)
WHERE p1.part_id = p2.alternate_part_id
CREATE (p2)-[:ALTERNATE_TO]->(p1);

The schema of the files can be.

# parts.csv
part_id,supplier_id,part_name,part_description,alternate_part_id

# suppliers.csv
supplier_id,supplier_name,location

# cost.csv
supplier_id,part_cost,transportation_cost,labour_cost,total_cost

Let’s prepare the data model information and few sample queries.

train_cypher.py

examples = """
# Show part details for 19999?
MATCH (p:parts) WHERE p.part_id = "19999" RETURN ' Part Name:' +p.part_name+' | Part Description: '+p.part_description' as response

# Who is the supplier of the part 19999?
MATCH (p:parts)-[:SUPPLIED_BY]->(s:suppliers) WHERE p.part_id = "19999" RETURN ' Supplier id: ' + s.supplier_id + ' | Supplier Name: '+s.supplier_name as response

# The alternate parts for 19999
MATCH (a)-[:ALTERNATE_OF]->(b) WHERE b.part_id = "19999" RETURN ' Alternate Part: ' + a.part_id + ' | Part Name: '+ a.part_name as response
"""

node_properties = """
[
{
"properties": [
"part_id",
"supplier_id",
"part_name",
"part_description",
"alternate_part_id"
],
"labels": "parts"
},
{
"properties": [
"supplier_id",
"part_cost",
"transportation_cost",
"labour_cost",
"total_cost"
],
"labels": "cost"
},
{
"properties": [
"supplier_id",
"supplier_name",
"location"
],
"labels": "suppliers"
}
]
"""

relationships_props = """
[
{
"source": "parts",
"relationship": "ALTERNATE_OF",
"target": [
"parts"
]
},
{
"source": "parts",
"relationship": "SUPPLIED_BY",
"target": [
"suppliers"
]
},
{
"source": "suppliers",
"relationship": "HAS",
"target": [
"cost"
]
}
]
"""

Export the openAI key: $export OPENAI_KEY=<openAI api key>

app.py

import os
import openai
import streamlit as st
from streamlit_chat import message
from driver import read_query
from train_cypher import examples
from openaigpt4 import generate_cypher

st.set_page_config(page_title="💬 Ask Me, Rahul")
st.title("💬 Ask Me, Rahul ")
st.info("Ask me V0.01 - Rahul | Powered By GPT-4",icon="ℹ️")

openai.api_key = os.environ.get('OPENAI_KEY')

def generate_response(prompt, cypher=True):
usr_input = [{"role": "user", "content": prompt}]
cypher_query = generate_cypher(usr_input)
message = read_query(cypher_query)
return message, cypher_query

with st.sidebar:
st.markdown('📖 Learn more about-Me')

if "messages" not in st.session_state.keys():
st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]

for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.write(message["content"])

if user_input := st.chat_input():
st.session_state.messages.append({"role": "user", "content": user_input})
with st.chat_message("user"):
st.write(user_input)

if st.session_state.messages[-1]["role"] != "assistant":
with st.chat_message("assistant"):
response,cypher_query = generate_response(user_input)
message = {"role": "assistant", "content": response}
if "MATCH" in cypher_query:
st.write(cypher_query)
st.write(message["content"])
st.session_state.messages.append(message)

Few train utility methods and prompts:

train_utils.py

from train_cypher import examples
def get_schema_text(node_props, rels):
return f"""
This is the schema representation of the Neo4j database.
Node properties are the following:
{node_props}
Relationships from source to target nodes:
{rels}
Make sure to respect relationship types and directions
"""

def get_system_message(schema_text):
return f"""
You are an assistant with an ability to generate Cypher queries.
Task: Generate Cypher queries to query a Neo4j graph database based on the provided schema definition.
Instructions:
Use only the provided relationship types.
Do not use any other relationship types or properties that are not provided.
If you cannot generate a Cypher statement based on the provided schema, explain the reason to the user.
Schema:
{schema_text}
Example cypher queries are:
{examples}
"""

def get_graph_model_metadata():
return get_schema_text(node_props=node_properties,rels=relationships_props)

def get_sys_prompts():
schema_txt = get_graph_model_metadata()
return get_system_message(schema_txt)

Train and get the cypher statement generated from openAI

openaigpt4.py

import openai
from train_cypher import node_properties,relationships_props
from train_utils import schema_text,get_system_message

def get_graph_model_metadata():
return schema_text(node_props=node_properties,rels=relationships_props)

def get_sys_prompts():
schema_txt = get_graph_model_metadata()
return get_system_message(schema_txt)

def generate_cypher(messages):
messages = [
{"role": "system", "content": get_sys_prompts()}
] + messages
# Make a request to OpenAI
completions = openai.ChatCompletion.create(
model="gpt-4",
messages=messages,
temperature=0.0
)
response = completions.choices[0].message.content
return response

Run the query on neo4j

driver.py

from neo4j import GraphDatabase

host = 'bolt://localhost:7687'
user = 'neo4j'
password = 'pass'
driver = GraphDatabase.driver(host, auth=(user, password))

def read_query(query, params={}):
with driver.session() as session:
try:
result = session.run(query, params)
response = [r.values()[0] for r in result]
if response == []:
return "Either there is no result found for your question Or please help me with additional context."
return response
except Exception as inst:
if "MATCH" in query:
return "Either there is no result found for your question Or please help me with additional context!"
else:
return query

Install the dependencies using $pip install -r requirements.txt & post that we can run the streamlit app.

$streamlit src/app.py

ChatBot in action:

Graph based ChatBot with neo4j, GPT-4 & Streamlit (6)

ChatGPT is an AI-powered language model that can generate SQL queries based on natural language input. It can help users with various SQL tasks, such as creating tables, inserting data, retrieving data, joining tables, and more. ChatGPT can also handle different SQL dialects, tools, and scenarios.

GPT-4 is very well trained to write the cypher queries based on the graph data model provided. However, sometimes it may produce incorrect cypher statements and hence we need a validation layer to validate the cypher queries generated by GPT.

The code used in this blog is available on GitHub.

Generating Cypher Queries With ChatGPT 4 on Any Graph SchemaWill we still need to learn query languages in the future?medium.com
Graph based ChatBot with neo4j, GPT-4 & Streamlit (2024)

References

Top Articles
Das Ende von Soap2day: Was ist passiert und welche Alternativen gibt es? - Go Gadget!
Die 10 besten Soap2day-Alternativen 2024 [Kostenlos ohne Werbung]📺
Funny Roblox Id Codes 2023
Www.mytotalrewards/Rtx
San Angelo, Texas: eine Oase für Kunstliebhaber
Golden Abyss - Chapter 5 - Lunar_Angel
Www.paystubportal.com/7-11 Login
Steamy Afternoon With Handsome Fernando
Craigslist Greenville Craigslist
Top Hat Trailer Wiring Diagram
World History Kazwire
R/Altfeet
George The Animal Steele Gif
Nalley Tartar Sauce
Chile Crunch Original
Teenleaks Discord
Immortal Ink Waxahachie
Craigslist Free Stuff Santa Cruz
Mflwer
Costco Gas Foster City
Obsidian Guard's Cutlass
Sprinkler Lv2
Amih Stocktwits
Uta Kinesiology Advising
Kcwi Tv Schedule
Nesb Routing Number
Olivia Maeday
Random Bibleizer
10 Best Places to Go and Things to Know for a Trip to the Hickory M...
Receptionist Position Near Me
Black Lion Backpack And Glider Voucher
Gopher Carts Pensacola Beach
Duke University Transcript Request
Nikki Catsouras: The Tragic Story Behind The Face And Body Images
Kiddie Jungle Parma
Lincoln Financial Field, section 110, row 4, home of Philadelphia Eagles, Temple Owls, page 1
The Latest: Trump addresses apparent assassination attempt on X
In Branch Chase Atm Near Me
Appleton Post Crescent Today's Obituaries
Craigslist Red Wing Mn
American Bully Xxl Black Panther
Ktbs Payroll Login
Jail View Sumter
Thotsbook Com
Funkin' on the Heights
Caesars Rewards Loyalty Program Review [Previously Total Rewards]
Marcel Boom X
Www Pig11 Net
Ty Glass Sentenced
Game Akin To Bingo Nyt
Ranking 134 college football teams after Week 1, from Georgia to Temple
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 5677

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.