In this post, I will be creating Python code using Jupyter Notebook to store information from ChatGPT in an Oracle Database within a Docker container. The objective is to showcase how an application can retrieve various types of data from an AI-based tool. Additionally, the idea behind this is to illustrate how the implementation of supercomputers can further optimize this infrastructure.
By utilizing the Python programming language and Jupyter Notebook, we can develop code that captures and stores information generated by ChatGPT. This code will serve as a demonstration of how data from AI models can be effectively collected and managed within an application. Furthermore, I will explore the potential benefits of incorporating supercomputers into this infrastructure, highlighting how their computational power can enhance the overall performance and efficiency of the system.
By the end of this post, you will have a better understanding of how to gather and store information from ChatGPT, as well as the potential advantages of utilizing supercomputers to optimize such an infrastructure.
-- Create the table to store ChatGPT information CREATE TABLE brunotechdatabasket.chatgpt_information ( id NUMBER PRIMARY KEY, timestamp TIMESTAMP, conversation_id VARCHAR2(100), user_id VARCHAR2(100), message VARCHAR2(4000), response VARCHAR2(4000) ); -- Create a sequence for generating unique IDs CREATE SEQUENCE brunotechdatabasket.chatgpt_info_seq START WITH 1 INCREMENT BY 1 NOMAXVALUE NOCACHE NOCYCLE; -- Create a trigger to automatically populate the ID column CREATE TRIGGER brunotechdatabasket.chatgpt_info_trigger BEFORE INSERT ON brunotechdatabasket.chatgpt_information FOR EACH ROW BEGIN :NEW.id := chatgpt_info_seq.NEXTVAL; END;
Based on the provided code for storing ChatGPT data, the table chatgpt_information
is created with columns such as id
, timestamp
, conversation_id
, user_id
, message
, and response
. The id
column is defined as the primary key, and a sequence named chatgpt_info_seq
is created to generate unique IDs for each row. Additionally, a trigger named chatgpt_info_trigger
is created to automatically populate the id
column using the sequence.
To execute this code, you can utilize the PDB database created in the post “INSTALLING ORACLE DATABASE ON MACOS USING ORACLE DOCKER IMAGES“. As the post explains, Docker images are used to set up the PDB, and specifically, the PDB named XEPDB1 is created under this image. The execution process follows the steps outlined below, assuming you are logged in as the user sys followed by the creation of the user brunotechdatabasket
:
- Connect to the PDB database.
- Execute the code to create the table, sequence, and trigger within the PDB.
- Start storing ChatGPT data in the
chatgpt_information
table.
By following these steps, you can effectively store ChatGPT data in the Oracle database container using Python and Jupyter Notebook as below:
bash-4.2# sqlplus sys/123456@//localhost:1521 as sysdba SQL*Plus: Release 18.0.0.0.0 - Production on Mon May 22 18:48:43 2023 Version 18.4.0.0.0 Copyright (c) 1982, 2018, Oracle. All rights reserved. Connected to: Oracle Database 18c Express Edition Release 18.0.0.0.0 - Production Version 18.4.0.0.0 SQL> show con_name CON_NAME ------------------------------ CDB$ROOT SQL> alter session set container=XEPDB1; Session altered. SQL> create user brunotechdatabasket identified by "YDq9QEh)Fsrw45vE"; User created. SQL> grant create session, connect to brunotechdatabasket; Grant succeeded. SQL> grant unlimited tablespace to brunotechdatabasket; Grant succeeded. SQL> CREATE TABLE brunotechdatabasket.chatgpt_information ( id NUMBER PRIMARY KEY, timestamp TIMESTAMP, conversation_id VARCHAR2(100), user_id VARCHAR2(100), message VARCHAR2(4000), response VARCHAR2(4000) ); 2 3 4 5 6 7 8 Table created. SQL> CREATE SEQUENCE brunotechdatabasket.chatgpt_info_seq START WITH 1 INCREMENT BY 1 NOMAXVALUE NOCACHE NOCYCLE; 2 3 4 5 6 Sequence created. SQL> -- Create a trigger to automatically populate the ID column CREATE TRIGGER brunotechdatabasket.chatgpt_info_trigger BEFORE INSERT ON brunotechdatabasket.chatgpt_information FOR EACH ROW BEGIN :NEW.id := chatgpt_info_seq.NEXTVAL; END;SQL> 2 3 4 5 6 7 / Trigger created.
By simulating various questions and responses to ChatGPT, several inserts are added into the table:
SQL> INSERT INTO brunotechdatabasket.chatgpt_information (message, response) VALUES ('What is the capital of France?', 'The capital of France is Paris.'); 2 1 row created. SQL> SQL> INSERT INTO brunotechdatabasket.chatgpt_information (message, response) VALUES ('How tall is Mount Everest?', 'Mount Everest is approximately 8,848 meters (29,029 feet) tall.'); 2 1 row created. SQL> INSERT INTO brunotechdatabasket.chatgpt_information (message, response) VALUES ('What is the meaning of life?', 'The meaning of life can vary for each individual and is often a philosophical question.'); 2 1 row created. SQL> commit; Commit complete.
Here is an example of how to check if the Python code exists in the database and is stored in the table that contains information from ChatGPT via Jupyter Notebook:
import openai import cx_Oracle # Set up your OpenAI API credentials openai.api_key = 'YOUR_API_KEY' # Set up your Oracle database connection oracle_connection = cx_Oracle.connect('YOUR_USERNAME', 'YOUR_PASSWORD', 'YOUR_HOST:YOUR_PORT/YOUR_SERVICE_NAME') # Define a function to send a message and get a response from ChatGPT def send_message(message): response = openai.Completion.create( engine='text-davinci-003', # Specify the engine prompt=message, max_tokens=50, # Set the maximum number of tokens in the response temperature=0.7, # Control the randomness of the response n=1, # Specify the number of responses to generate stop=None, # Set a stopping criterion for the response generation ) if response.choices: return response.choices[0].text.strip() else: return "" # Example usage user_input = input("Enter your message: ") response = send_message(user_input) # Insert the message and response into the database cursor = oracle_connection.cursor() cursor.execute("INSERT INTO chatgpt_information (message, response) VALUES (:message, :response)", message=user_input, response=response) oracle_connection.commit() cursor.close() print("ChatGPT: " + response)
The code above is also available at my GitHub repository:https://github.com/brunorsreis/ChatGPT_table_example
Additionally, there is an option to directly gather the data from ChatGPT by using the code below:
import cx_Oracle # Function to check if the text was created by ChatGPT def is_text_from_chatgpt(text): # Create a DSN to connect to the Pluggable Database (PDB) dsn = cx_Oracle.makedsn( host='localhost', port=1521, sid='XE', service_name='XEPDB1' ) # Establish connection to Oracle database #If the connection is made via `sys`, it is necessary to use the `cx_Oracle.SYSDBA` clause after the `dsn` parameter. conn = cx_Oracle.connect('brunotechdatabasket', 'YDq9QEh)Fsrw45vE',dsn) # Create a cursor to execute SQL queries cursor = conn.cursor() # Test the function text_to_check = "Mount Everest is approximately 8,848 meters (29,029 feet) tall." # Prepare the SQL query query = "SELECT COUNT(*) FROM brunotechdatabasket.chatgpt_information WHERE response =:text" # Execute the query cursor.execute(query, text=text) # Bind the variable text # Fetch the result result = cursor.fetchone() # Close the cursor and connection cursor.close() conn.close() # Check if the text exists in the database if result[0] > 0: return True else: return False if is_text_from_chatgpt(text_to_check): print("The text was created by ChatGPT.") else: print("The text was not created by ChatGPT.")
This was a simple example showcasing how information from ChatGPT can be effectively stored in an Oracle database. It emphasizes the idea that utilizing a high-performance computer capable of executing and storing queries at a fast pace can greatly enhance the overall efficiency of the process.
Hi! I am Bruno, a Brazilian born and bred, and I am also a naturalized Swedish citizen. I am a former Oracle ACE and, to keep up with academic research, I am a Computer Scientist with an MSc in Data Science and another MSc in Software Engineering. I have over ten years of experience working with companies such as IBM, Epico Tech, and Playtech across three different countries (Brazil, Hungary, and Sweden), and I have joined projects remotely in many others. I am super excited to share my interests in Databases, Cybersecurity, Cloud, Data Science, Data Engineering, Big Data, AI, Programming, Software Engineering, and data in general.
(Continue reading)