Skip to content

Commit

Permalink
Make use of llm_utils.number_group_of_lines
Browse files Browse the repository at this point in the history
  • Loading branch information
nicovank committed Jan 24, 2024
1 parent 47b464f commit 83cb123
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 49 deletions.
19 changes: 1 addition & 18 deletions src/cwhy/conversation/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,8 @@ def get_code_surrounding_schema(self):
}

def get_code_surrounding(self, filename: str, lineno: 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

(lines, first) = llm_utils.read_lines(filename, lineno - 7, lineno + 3)
return format_group_code_block(lines, first)
return llm_utils.number_group_of_lines(lines, first)

def list_directory_schema(self):
return {
Expand Down
33 changes: 2 additions & 31 deletions src/cwhy/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,35 +114,6 @@ def get_code(self):
if not self.code_locations:
return None

def format_group_code_block(group: list[str], last: int) -> str:
"""
Format a group of consecutive lines from a single file as a code block.
Include line numbers in front of each line.
Trim first / last few lines if they are blank.
Args:
group: The list of lines.
last: The line number of the last line in group.
Returns:
The formatted code block.
"""
while group and not group[0].strip():
group = group[1:]
while group and not group[-1].strip():
group = group[:-1]
last -= 1

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

def format_file_locations(filename: str, lines: dict[int, str]) -> str:
"""
Format all the lines from a single file as a code block.
Expand All @@ -167,12 +138,12 @@ def format_file_locations(filename: str, lines: dict[int, str]) -> str:
last = line_number
else:
result += f"File `{filename}`:\n"
result += format_group_code_block(group, last)
result += llm_utils.number_group_of_lines(group, last)
last = None
group = []
if last is not None:
result += f"File `{filename}`:\n"
result += format_group_code_block(group, last)
result += llm_utils.number_group_of_lines(group, last)
return result

formatted_file_locations = [
Expand Down

0 comments on commit 83cb123

Please sign in to comment.