Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify context generation slightly (xrefs) #272

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions data_prep/project_context/context_introspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,34 @@ def _get_xrefs_to_function(self) -> list[str]:
logging.warning(
'Could not retrieve xrefs for project: %s '
'function_signature: %s', project, func_sig)

# At times, xrefs can be noisy (multiple 100s of loc).
# Truncate them by default
xrefs = self._truncate_xrefs(xrefs)
return xrefs

def _truncate_xrefs(self, xrefs: list[str]) -> list[str]:
"""Truncates xrefs to 10 lines before and after the
function name is referenced."""
truncated = []
for xref in xrefs:
lines = xref.split('\n')
line_index = -1
start = 0
end = len(lines) - 1
for index, line in enumerate(lines):
if self._benchmark.function_name in line:
line_index = index
break
# If name was not found, then just return the entire function.
# If it was, truncate it.
if line_index != -1:
start = start if line_index <= 10 else line_index - 10
end = end if line_index >= end - 10 else line_index + 10
Copy link
Collaborator

@DonggeLiu DonggeLiu May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: lines[1:2] will not include line index 2.

start = max(0, line_index - 10) 
end = min(len(lines), line_index+10)

truncated.append('\n'.join(lines[start:end]))

return truncated

def get_context_info(self) -> dict:
"""Retrieves contextual information and stores them in a dictionary."""
xrefs = self._get_xrefs_to_function()
Expand Down
2 changes: 1 addition & 1 deletion llm_toolkit/prompt_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def format_context(self, context_info: dict) -> str:
headers='\n'.join(context_info['files']),
must_insert=context_info['decl'],
func_source=context_info['func_source'],
xrefs='\n'.join(context_info['xrefs']),
xrefs=context_info['xrefs'],
)

def _select_examples(self, examples: list[list],
Expand Down
7 changes: 5 additions & 2 deletions prompts/template_xml/context.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ Here is the source code of the function being tested:
{% endif %}
{% if xrefs %}

Here is the source code for functions which reference the function being tested:
Here is the source code for locations which reference the function being tested:
{% for xref in xrefs %}
Reference location {{ loop.index }}:
<code>
{{ xrefs }}
{{ xref }}
</code>
{% endfor %}
{% endif %}