Skip to content

Commit

Permalink
add explanation command
Browse files Browse the repository at this point in the history
  • Loading branch information
pgosar committed Apr 4, 2023
1 parent cdc2467 commit 9d49479
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 20 deletions.
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[project]
name = "ChatGDB"
version = "0.0.1"
version = "0.1.0"
authors = [
{ name="Pranay Gosar", email="[email protected]" },
]
description = "Harness the power of ChatGPT directly inside the GDB debugger!"
readme = "README.md"
requires-python = ">=3.7"
requires-python = ">=3.3"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]

Expand All @@ -19,4 +19,4 @@ classifiers = [

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
build-backend = "hatchling.build"
Empty file added src/ChatGDB/__init__.py
Empty file.
80 changes: 64 additions & 16 deletions src/ChatGDB/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
from os.path import abspath, dirname
from inspect import getfile, currentframe

URL = "https://api.openai.com/v1/chat/completions"
MODEL = "gpt-3.5-turbo"
PROMPT = "Give me GDB commands with no explanation. Do NOT write any \
English above or below the command. Only give me the command as text. \
Here is my question: "


def get_key():
"""Gets api key from .env file
Expand Down Expand Up @@ -40,7 +34,7 @@ def get_key():
# make api request
def make_request(url, headers=None, data=None):
"""Makes API request
Params:
url (str): url to make request to
headers (dict, optional): headers to send with request. Defaults to None.
Expand All @@ -52,16 +46,37 @@ def make_request(url, headers=None, data=None):
return response.read(), response
except HTTPError as error:
print(error.status, error.reason)
quit("Exiting...")
except URLError as error:
print(error.reason)
quit("Exiting...")
except TimeoutError:
print("Request timed out")
quit("Exiting...")


class ChatGDB(gdb.Command):
prev_command = ""
HEADERS = {
"Authorization": "Bearer " + get_key(),
"Content-Type": "application/json"
}
URL = "https://api.openai.com/v1/chat/completions"
MODEL = "gpt-3.5-turbo"
COMMAND_PROMPT = "Give me GDB commands with no explanation. Do NOT write any \
English above or below the command. Only give me the command as text. \
Here is my question: "
EXPLANATION_PROMPT = "Give me an explanation for this GDB command: "


class GDBCommand(gdb.Command):
"""Custom GDB command - ?
This class creates a custom command denoted by ? that is used
to generate GDB commands based on plain English input.
"""
def __init__(self):
"""Initializes custom GDB command"""
super(ChatGDB, self).__init__("chat_gdb", gdb.COMMAND_DATA)
super(GDBCommand, self).__init__("chat", gdb.COMMAND_DATA)

# creates api request on command invocation
def invoke(self, arg, from_tty):
Expand All @@ -71,19 +86,52 @@ def invoke(self, arg, from_tty):
arg (str): argument passed to command
from_tty (bool): whether command was invoked from TTY
"""
headers = {
"Authorization": "Bearer " + get_key(),
"Content-Type": "application/json"}
global prev_command
data = {"model": MODEL,
"messages": [{"role": "user",
"content": PROMPT + arg}]}
"content": COMMAND_PROMPT + arg}]}

body, response = make_request(
URL, headers, data=bytes(
URL, HEADERS, data=bytes(
json.dumps(data), encoding="utf-8"))
body = json.loads(body)
command = body['choices'][0]['message']['content']
prev_command = command
print(command)
gdb.execute(command)


class ExplainCommand(gdb.Command):
"""Custom GDB command - explain
This class creates a custom command denoted by explain that is used
to generate explanations for either the previous command or a user query
"""

def __init__(self):
"""Initializes custom GDB command"""
super(ExplainCommand, self).__init__("explain", gdb.COMMAND_DATA)

# creates api request on command invocation
def invoke(self, arg, from_tty):
"""Invokes custom GDB command and sends API request
Params:
arg (str): argument passed to commands
from_tty (bool): whether command was invoked from from_tty
"""
content = arg
if arg == "":
content = EXPLANATION_PROMPT + prev_command

data = {"model": MODEL,
"messages": [{"role": "user",
"content": content}]}
body, response = make_request(
URL, HEADERS, data=bytes(json.dumps(data), encoding="utf-8"))
body = json.loads(body)
print(body['choices'][0]['message']['content'])
gdb.execute(body['choices'][0]['message']['content'])


ChatGDB()
GDBCommand()
ExplainCommand()

0 comments on commit 9d49479

Please sign in to comment.