Create a Knowledge Graph with Oracle Autonomous Database and Property Graph Query Language

Introduction

This tutorial explores the concepts of graph theory, knowledge graphs, and how they are implemented using the Oracle Autonomous Database with Property Graph Query Language (PGQL). It also explains the Python implementation used to extract relationships from documents using LLMs and store them as graph structures in Oracle.

What is Graph?

Graph is a field of mathematics and computer science focused on modelling relationships between objects. A graph consists of:

Graphs are widely used for representing data structures in social networks, semantic networks, knowledge graphs, and more.

What is a Knowledge Graph?

A knowledge graph is a graph-based representation of real-world knowledge where:

Knowledge graphs enhance semantic search, recommendation systems, and question-answering applications.

Why Use Oracle Autonomous Database with PGQL?

Oracle provides a fully managed environment to store and query property graphs:

Comparison with Other Graph Query Languages

img.png

Advantages of Oracle Autonomous Database with PGQL vs Traditional Graph Databases

img_1.png

Objectives

Prerequisites

Task 1: Install Python Packages

The Python code requires certain libraries for using Oracle Cloud Infrastructure (OCI) Generative AI. Run the following command to install the required Python packages. You can download the file from here: requirements.txt.

pip install -r requirements.txt

Task 2: Create an Oracle Database 23ai (Always Free)

In this task, we will learn how to provision an Oracle Database 23ai in Always Free mode. This version offers a fully managed environment, ideal for development, testing and learning, at no additional cost.

  1. Log in to the OCI Console, navigate to Oracle Database, Autonomous Database and click Create Autonomous Database Instance.

  2. Enter the following information.

    • Database Name: Enter an identifying name for your instance.
    • Workload Type: Select Data Warehouse or Transaction Processing, according to your needs.
    • Compartment: Select an appropriate compartment to organize your resources.
  3. Select Always Free to ensure that the instance is provisioned for free.

  4. Create a secure password for the ADMIN user, which will be used to access the database.

  5. Review the settings and click Create Autonomous Database. Wait a few minutes for the instance to be provisioned and available for use.

If you are not familiar with the process of connecting to the Oracle Autonomous Database, follow these links to understand and properly configure your code.

Note: You will need to connect to the database inside your Python code with the Wallet method.

Task 3: Download and Understand the Code

A very common use case for Graph is to use it as one of the components working together with LLMs and a knowledge base, such as PDF files.

We will use this tutorial: Analyze PDF Documents in Natural Language with OCI Generative AI as our foundation, which uses all the mentioned components. However, for the purpose of this document, we will focus on using Oracle Database 23ai together with Graph. Basically, the Python code (main.py) from the base material will be modified only in the parts that use Oracle Database 23ai.

These are the processes executed on this service:

Download the updated Python graph code compatible with Oracle Database 23ai from here: main.py.

Task 4: Run the Chatbot

Run the following command to run the chatbot.

python main.py

Acknowledgments

More Learning Resources

Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an Oracle Learning Explorer.

For product documentation, visit Oracle Help Center.