Bringing the Mahabharata Epic to Life: A Neo4j-Powered Chatbot With Google Gemini (Part 2)


Source: DALL-E (Prompt: Create an image in landscape showing relationships between characters in Mahabharata)

Welcome back to our exploration of the Mahabharata through the lens of graph databases! In Part 1, we built a comprehensive graph of characters and their relationships within the epic using Neo4j.

Read Part 1 here if you have’t already:
Unveiling the Mahabharata’s Web: Analyzing Epic Relationships With Neo4j Graph Database (Part 1)

Now, it’s time to breathe life into this intricate web by introducing a powerful tool — the chatbot.

What’s the Big Challenge?

While Neo4j provides a powerful platform for storing and analyzing relationships, traditional Cypher queries can be a hurdle for non-technical users. Deciphering the syntax and understanding the data model can be intimidating. This limits accessibility and hinders the true potential of exploring the Mahabharata’s web.

For example, if I have to find out who are the parents of Lord Kashyapa, the query will be something like this:

MATCH (kashyapa:Person{name:'Kashyapa'})-[:SON_OF]->(m:Person) RETURN m

Talk to the Mahabharata: Bridging the Gap with Natural Language

This is where our chatbot comes in. Imagine a user simply asking, “Who are Arjuna’s closest allies?” or “How is Draupadi related to Krishna?” or “Who killed Ghatotakach,” etc. The chatbot, acting as an intermediary, would translate these natural language queries into efficient Cypher statements behind the scenes.

Building the Chatbot with Python and Gradio

Here’s where our Python expertise shines. We’ll leverage the capabilities of Gradio, a user-friendly Python web framework, to construct the chatbot interface. This allows us to create a visually appealing and intuitive platform for user interaction by understanding the user queries, converting them to Cypher statements using Gemini API, searching the Neo4j database, which we built in Part 1, returning the response if available, and displaying it to the user.

Gradio Chatbot Screenshot

Introducing Gemini

To further bridge the gap between natural language and Cypher, we explored the exciting potential of Google’s Gemini API in Google AI Studio. This large language model can act as a bridge by translating user queries into (nearly) accurate Cypher statements, enabling the chatbot to interact with the Neo4j graph seamlessly.

Did you notice I mentioned the word “nearly” in above paragraph? Do you know why?

While Gemini is a powerful tool, it’s important to acknowledge that translating natural language into precise Cypher queries can be challenging. The accuracy of the translation may depend on various factors, such as:

  • The complexity of the user query: Simpler questions with clear keywords will likely translate more accurately than intricate or ambiguous ones.
  • The training data used for Gemini: The effectiveness of Gemini’s translation depends on the data it was trained on. Refining the training data specific to converting text-to-cypher could improve accuracy. But this is very hard.
  • The need for refinement: Even with “nearly accurate” translations, some user queries might require minor adjustments to ensure they perfectly align with Neo4j’s query language.

Note: I used Gemini 1.0 Pro for this project.

Screenshot of Google AI Studio with chat prompt

Play around with the user-friendly chatbot deployed on Hugging Face Spaces here. If you are looking for steps to replicate, head to my GitHub repo. Fork it and submit pull requests to make it better. 🙂

A Glimpse into the Future

This project represents just the beginning of a fascinating journey. We can envision future iterations incorporating features like:

  • Sentiment analysis: The chatbot could analyze user queries and respond with a tailored tone, making the experience more immersive.
  • Creative text formats: Utilizing Gen-AI, the chatbot could generate poems, character profiles, or even short narratives based on user queries.

Knowledge Graphs: Expanding the Narrative

Beyond these enhancements, integrating a knowledge graph alongside the character relationship graph holds immense potential. A knowledge graph could encompass additional details about characters, locations, events, and even cultural aspects related to the Mahabharata. This richer data tapestry would allow the chatbot to:

  • Provide Context and Backstory: When a user asks about a character, the chatbot can not only reveal their relationships but also access their backstory, personality traits, and significant events from the epic.
  • Uncover Hidden Connections: By analyzing connections within the knowledge graph, the chatbot could identify surprising links between seemingly unrelated characters, events, or locations.
  • Offer a Holistic Understanding: The combined power of relationship and knowledge graphs would enable the user to gain a more comprehensive understanding of the Mahabharata’s intricate world and its characters’ motivations.

Conclusion

Interactive storytelling is the future!

This confluence of technologies — chatbots, Gen-AI, and graph databases — has the potential to revolutionize how we interact with narratives like the Mahabharata. Imagine a future where users can not only explore relationships but also delve into the rich tapestry of the story through an interactive and personalized conversation.


Bringing the Mahabharata Epic to Life: A Neo4j-Powered Chatbot using Google Gemini (Part 2) was originally published in Neo4j Developer Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.