Skip to content

Commit

Permalink
Prompt improvements, read_line simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
nicovank committed Jan 23, 2024
1 parent d026ef1 commit 2db6d16
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 74 deletions.
29 changes: 1 addition & 28 deletions src/chatdbg/chatdbg_gdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,6 @@
rust_panic_log_filename = "panic_log.txt"


def read_lines_list(file_path: str, start_line: int, end_line: int) -> list[str]:
"""
Read lines from a file and return a list containing the lines between start_line and end_line.
Args:
file_path (str): The path of the file to read.
start_line (int): The line number of the first line to include (1-indexed).
end_line (int): The line number of the last line to include.
Returns:
[str]: A list of the lines between start_line and end_line.
"""
# open the file for reading
with open(file_path, "r") as f:
# read all the lines from the file
lines = f.readlines()
# remove trailing newline characters
lines = [line.rstrip() for line in lines]
# convert start_line to 0-based indexing
start_line = max(0, start_line - 1)
# ensure end_line is within range
end_line = min(len(lines), end_line)
# return the requested lines as a list
return lines[start_line:end_line]


# Set the prompt to gdb-ChatDBG
gdb.prompt_hook = lambda current_prompt: "(gdb-ChatDBG) "

Expand Down Expand Up @@ -147,7 +120,7 @@ def buildPrompt() -> tuple[str, str, str]:
)
try:
source_code += f"/* frame {i} */\n"
lines = read_lines_list(file_name, line_num - 10, line_num)
lines = chatdbg_utils.read_lines_adding_numbers(file_name, line_num - 10, line_num)
source_code += "\n".join(lines) + "\n"
# Get the spaces before the last line.
num_spaces = len(lines[-1]) - len(lines[-1].lstrip())
Expand Down
13 changes: 5 additions & 8 deletions src/chatdbg/chatdbg_lldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ def buildPrompt(debugger: Any) -> Tuple[str, str, str]:
max_line_length = 100

try:
lines = chatdbg_utils.read_lines(full_file_name, line_num - 10, line_num)
lines = chatdbg_utils.read_lines_adding_numbers(full_file_name, line_num - 10, line_num)
stack_trace += (
truncate_string(
f'frame {index}: {func_name}({",".join(arg_list)}) at {file_name}:{line_num}:{col_num}\n',
f'frame {index}: {func_name}({",".join(arg_list)}) at {file_name}:{line_num}:{col_num}',
max_line_length - 3,
)
+ "\n"
Expand All @@ -198,10 +198,7 @@ def buildPrompt(debugger: Any) -> Tuple[str, str, str]:
+ "\n"
)
source_code += f"/* frame {index} in {file_name} */\n"
source_code += lines + "\n"
source_code += (
"-" * (chatdbg_utils.read_lines_width() + col_num - 1) + "^" + "\n\n"
)
source_code += lines + "\n\n"
except:
# Couldn't find the source for some reason. Skip the file.
continue
Expand All @@ -214,7 +211,7 @@ def buildPrompt(debugger: Any) -> Tuple[str, str, str]:
error_reason = panic_log + "\n" + error_reason
except:
pass
return (source_code, stack_trace, error_reason)
return (source_code.strip(), stack_trace.strip(), error_reason.strip())


@lldb.command("why")
Expand Down Expand Up @@ -353,4 +350,4 @@ def helper_result(
json["value"] = fields
else:
json['value'] = str(var)[str(var).find("= ") + 2:]
return json
return json
75 changes: 37 additions & 38 deletions src/chatdbg/chatdbg_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import textwrap

import openai

from llm_utils import llm_utils
Expand All @@ -20,48 +22,45 @@ def get_model() -> str:

return model

def read_lines_adding_numbers(filename: str, start: int, end: int) -> str:
def format_group_code_block(group: list[str], first: int) -> str:
while group and not group[0].strip():
group = group[1:]
first += 1
while group and not group[-1].strip():
group = group[:-1]

last = first + len(group) - 1
max_line_number_length = len(str(last))
result = "\n".join(
[
"{0:>{1}} {2}".format(first + i, max_line_number_length, line)
for i, line in enumerate(group)
]
)
return result

def read_lines_width() -> int:
return 10


def read_lines(file_path: str, start_line: int, end_line: int) -> str:
"""
Read lines from a file and return a string containing the lines between start_line and end_line.
Args:
file_path (str): The path of the file to read.
start_line (int): The line number of the first line to include (1-indexed).
end_line (int): The line number of the last line to include.
Returns:
str: A string containing the lines between start_line and end_line.
"""
# open the file for reading
with open(file_path, "r") as f:
# read all the lines from the file
lines = f.readlines()
# remove trailing newline characters
lines = [line.rstrip() for line in lines]
# add line numbers
lines = [f" {index+1:<6} {line}" for index, line in enumerate(lines)]
# convert start_line to 0-based indexing
start_line = max(0, start_line - 1)
# ensure end_line is within range
end_line = min(len(lines), end_line)
# return the requested lines as a string
return "\n".join(lines[start_line:end_line])
(lines, first) = llm_utils.read_lines(filename, start, end)
return format_group_code_block(lines, first)


def explain(source_code: str, traceback: str, exception: str, really_run=True) -> None:
user_prompt = "Explain what the root cause of this error is, given the following source code context for each stack frame and a traceback, and propose a fix. In your response, never refer to the frames given below (as in, 'frame 0'). Instead, always refer only to specific lines and filenames of source code.\n"
user_prompt += "\n"
user_prompt += "Source code for each stack frame:\n```\n"
user_prompt += source_code + "\n```\n"
user_prompt += traceback + "\n\n"
user_prompt += "stop reason = " + exception + "\n"
text = ""
user_prompt = f"""
Explain what the root cause of this error is, given the following source code
context for each stack frame and a traceback, and propose a fix. In your
response, never refer to the frames given below (as in, 'frame 0'). Instead,
always refer only to specific lines and filenames of source code.
Source code for each stack frame:
```
{source_code}
```
Traceback:
{traceback}
Stop reason: {exception}
""".strip()

model = get_model()
if not model:
Expand Down

0 comments on commit 2db6d16

Please sign in to comment.