Building an AI-Powered Oracle Copilot with Python and OpenAI API

alex buzunov
CodeX
Published in
4 min readJun 3, 2024

--

Simple alternative to Github Copilot for Oracle PL/SQL development.

The Oracle Copilot is an intelligent assistant designed to aid PL/SQL developers with database code debugging. By leveraging the capabilities of the OpenAI API, the copilot can understand natural language queries, provide relevant answers, and even offer code suggestions and improvements.

System Architecture

Core Components The Oracle Copilot consists of the following core components:

  • Python: The primary programming language used for development, chosen for its versatility and extensive library support.
  • OpenAI API: Provides access to advanced language models trained by OpenAI, enabling natural language understanding and generation.
  • wxPython: A Python wrapper for the wxWidgets toolkit, used to create the graphical user interface (GUI) for the copilot.
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

GUI Structure

The GUI of the Oracle Copilot is composed of several interconnected panels:

  • Workspace Panel: Serves as the main container for the vendor notebook and input/output panels.
  • Vendor Notebook Panel: Allows users to switch between different vendors and technologies supported by the copilot.
  • Chat Display Panel: Displays the conversation history and generated responses from the copilot.
  • Input Panel: Provides an interface for users to enter their queries and interact with the copilot.
class WorkspacePanel(wx.Panel):
def __init__(self, parent):
super(WorkspacePanel, self).__init__(parent)
# Create splitter windows and panels
# ...

class VendorNotebook(wx.Notebook):
def __init__(self, parent):
super().__init__(parent)
# Create vendor notebook and handle events
# ...

Key Features

Natural Language Understanding: The copilot employs OpenAI’s language models to comprehend user queries expressed in natural language. This allows users to ask questions or provide instructions in a conversational manner, making the interaction more intuitive and efficient.

class ResponseStreamer:
def __init__(self):
self.client = openai.OpenAI()

def stream_response(self, prompt, chatHistory, receiveing_tab_id, model):
response = self.client.chat.completions.create(
model=model,
messages=chatHistory,
stream=True
)
# ...

Context-Aware Responses: By maintaining a chat history and leveraging the power of OpenAI’s contextual understanding, the copilot can provide responses that take into account the context of the conversation. This enables more accurate and relevant answers to user queries.

chatHistory = {}

def AskQuestion(self):
global chatHistory, questionHistory, currentQuestion, currentModel
# ...
prompt = self.evaluate(templates.Chat.PROMPT, AttrDict(dict(question=question)))
chatHistory[self.tab_id] += [{"role": "user", "content": prompt}]
# ...

Code Suggestions and Improvements: One of the standout features of the Oracle Copilot is its ability to offer code suggestions and improvements. Users can input their code snippets, and the copilot will analyze the code and provide recommendations for optimization, best practices, and potential bug fixes.

def AskQuestion(self):
# ...
chatDisplay = apc.chat_panels[self.tab_id]
code = chatDisplay.GetCode(self.tab_id)
prompt = self.evaluate(templates.Copilot.FIX_CODE, AttrDict(dict(code=code, input=question)))
chatHistory[self.tab_id] += [{"role": "user", "content": prompt}]
# ...

SQL Tuning

The Oracle Copilot leverages its natural language understanding capabilities to assist developers with SQL tuning and optimization. Users can provide their SQL queries, and the copilot will analyze them, identify potential bottlenecks, and suggest improvements to enhance query performance.

SQL Tuning chat

Oracle Documentation Chat

One of the key features of the Oracle Copilot is its ability to quickly retrieve and provide relevant information from Oracle’s extensive documentation. Developers can ask natural language questions related to Oracle technologies, and the copilot will search through the documentation, understand the context, and provide concise and accurate answers.

Data Warehousing Concepts Chat

Oracle SGA

The Oracle Copilot leverages its natural language understanding capabilities to assist developers in comprehending and troubleshooting Oracle instance architecture components. Developers can ask questions about various instance components, their roles, and their interactions, and the copilot will provide clear and concise explanations.

Oracle Instance Architecture

Oracle Query Hints

The Oracle Copilot can assist developers in understanding and utilizing query hints effectively. Query hints are used to influence the optimizer’s decisions and can be powerful tools for addressing performance issues or specific query requirements.

Oracle Query Hints

PL/SQL Development Copilot

The Oracle Copilot provides invaluable assistance to PL/SQL developers by offering code suggestions, explanations, and best practices throughout the development process.

Oracle PL/SQL Copilot

Conclusion: The Oracle Copilot demonstrates the potential of combining Python, OpenAI API, and a well-designed GUI to create a powerful and user-friendly AI assistant. By leveraging natural language understanding, context-aware responses, and code analysis capabilities, the copilot can significantly enhance productivity and assist users in their Oracle-related tasks.

Source:

--

--