Home Artificial Intelligence Knowledge Graph-Based Chatbot With GPT-3 and Neo4j

Knowledge Graph-Based Chatbot With GPT-3 and Neo4j

1
Knowledge Graph-Based Chatbot With GPT-3 and Neo4j

Learn the best way to develop a chatbot that gives answers based on data stored in a knowledge graph.

Chatbot interface. Image by the creator.
Knowledge graph based chatbot architecture. Image by the creator.

Constructing a knowledge graph

With a view to have the opportunity to retrieve information from the knowledge graph, we first need to populate it. As mentioned, the thought is to construct a knowledge graph of stories articles. Due to this fact, we want to search out a source of quality and accurate news articles. For the aim of this demonstration, I actually have used the newest 1000 articles available as a Kaggle repository. The articles can be found under the CC BY-NC 4.0 license.

Information extraction pipeline. Image by the creator.
Using GraphGPT prompt to extract structured information from text. Image by the creator.
Asking GPT-3 to link extracted entities to Wikidata. Image by the creator.
Wikidata entry for ID Q1446. Image by the creator.
Schema of the populated knowledge graph. Image by the creator.

Using a GPT-3 model to generate Cypher statements

We now have already learned that GPT-3 does an excellent job of following orders given in a prompt. Moreover, Sixing Huang has already written about how easy it’s to train the GPT-3 model to generate Cypher statements. The concept is to offer the model just a few examples after which let it generate a Cypher statement given the brand new user input. Specifically, I actually have prepared the next Cypher examples to coach the GPT-3 model.

GPT-3 request to generate Cypher statements. Image by the creator.

Chatbot implementation

Now that we’ve prepared all of the pieces of the puzzle, we will mix them in a chatbot application. I actually have used a Streamlit application — specifically streamlit-chat — to implement the user interface for the chatbot. I just like the Streamlit application because it keeps things easy, and I can use Python to develop the user interface while avoiding any meddling with CSS.

# Make a request to GPT-3 endpoint
completions = openai.Completion.create(
engine="text-davinci-003",
# Construct the prompt using the training examples
# combined with user input
prompt=examples + "n#" + prompt,
max_tokens=1000,
n=1,
stop=None,
temperature=0.5,
)
# Extract Cypher query from GPT-3 response
cypher_query = completions.selections[0].text
# Use the Cypher query to read the knowledge graph
message = read_query(cypher_query)
return message, cypher_query
Image by the creator.
Image by the creator.
Image by the creator.
Image by the creator.
# Make a request to GPT-3 endpoint
completions = openai.Completion.create(
engine="text-davinci-003",
# Prefix the prompt with a request to supply a summary
prompt="Summarize the next article: n" + prompt,
max_tokens=256,
n=1,
stop=None,
temperature=0.5,
)
message = completions.selections[0].text
return message, None
Image by the creator.
Image by the creator.
Image by the creator.

Summary

I desired to create a project that uses natural language to explore and analyze knowledge graphs for a very long time. Nevertheless, the barrier to entry was too high for me as I’m not a machine learning expert, and developing and training a custom model that generates Cypher statements based on user inputs was too big of a task for me.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here