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

JVM: Combine target prompts #832

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 12 additions & 37 deletions llm_toolkit/prompt_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,10 +671,8 @@ def __init__(self,
template_dir, 'jvm_requirement.txt')
self.problem_template_file = self._find_template(template_dir,
'jvm_problem.txt')
self.constructor_template_file = self._find_template(
template_dir, 'jvm_problem_constructor.txt')
self.method_template_file = self._find_template(template_dir,
'jvm_problem_method.txt')
self.target_template_file = self._find_template(template_dir,
'jvm_target.txt')
self.arg_description_template_file = self._find_template(
template_dir, 'jvm_arg_description.txt')
self.generic_arg_description_template_file = self._find_template(
Expand All @@ -701,23 +699,6 @@ def _get_template(self, template_file: str) -> str:
with open(template_file) as file:
return file.read()

def _format_target_constructor(self, signature: str) -> str:
"""Formats a constructor based on the prompt template."""
class_name = signature.split('].')[0][1:]

constructor = self._get_template(self.constructor_template_file)
constructor = constructor.replace('{CONSTRUCTOR_CLASS}', class_name)
constructor = constructor.replace('{CONSTRUCTOR_SIGNATURE}', signature)

return constructor

def _format_target_method(self, signature: str) -> str:
"""Formats a method based on the prompt template."""
method = self._get_template(self.method_template_file)
method = method.replace('{METHOD_SIGNATURE}', signature)

return method

def _format_exceptions(self) -> str:
"""Formats the exception thrown from this method or constructor."""
if self.exceptions:
Expand Down Expand Up @@ -812,15 +793,6 @@ def _format_argument(self, count: int, arg_type: str) -> str:
argument = argument.replace('{ARG_TYPE}', arg_type)
return argument

def _format_target(self, signature: str) -> tuple[bool, str]:
"""Determine if the signature is a constructor or a general
method and format it for the prompts creation.
"""
if '<init>' in signature:
return True, self._format_target_constructor(signature)

return False, self._format_target_method(signature)

def _format_requirement(self, signature: str) -> str:
"""Formats a requirement based on the prompt template."""
classes = []
Expand Down Expand Up @@ -960,10 +932,14 @@ def _format_source_reference(self, signature: str) -> Tuple[str, str]:

def _format_problem(self, signature: str) -> str:
"""Formats a problem based on the prompt template."""
is_constructor = bool('<init>' in signature)

base = self._get_template(self.base_template_file)
problem = base + self._get_template(self.problem_template_file)
is_constructor, target_str = self._format_target(signature)
problem = problem.replace('{TARGET}', target_str)
problem = problem.replace('{TARGET}',
self._get_template(self.target_template_file))
problem = problem.replace('{SIGNATURE}', signature)
problem = problem.replace('{CLASS}', signature.split('].')[0][1:])
problem = problem.replace('{REQUIREMENTS}',
self._format_requirement(signature))
problem = problem.replace('{ARGUMENTS}', self._format_arguments())
Expand All @@ -973,16 +949,15 @@ def _format_problem(self, signature: str) -> str:
self_source, cross_source = self._format_source_reference(signature)
problem = problem.replace('{SELF_SOURCE}', self_source)
problem = problem.replace('{CROSS_SOURCE}', cross_source)
problem = problem.replace("{PROJECT_NAME}", self.benchmark.project)
problem = problem.replace("{PROJECT_URL}", self.project_url)
problem = problem.replace('{DATA_MAPPING}', self._format_data_filler())

if is_constructor:
problem = problem.replace('{METHOD_OR_CONSTRUCTOR}', 'constructor')
else:
problem = problem.replace('{METHOD_OR_CONSTRUCTOR}', 'method')

problem = problem.replace("{PROJECT_NAME}", self.benchmark.project)
problem = problem.replace("{PROJECT_URL}", self.project_url)

problem = problem.replace('{DATA_MAPPING}', self._format_data_filler())

return problem

def _prepare_prompt(self, prompt_str: str):
Expand Down
10 changes: 0 additions & 10 deletions prompts/template_xml/jvm_problem_constructor.txt

This file was deleted.

9 changes: 0 additions & 9 deletions prompts/template_xml/jvm_problem_method.txt

This file was deleted.

16 changes: 16 additions & 0 deletions prompts/template_xml/jvm_target.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<target>
The target is a {METHOD_OR_CONSTRUCTOR} of the class {CLASS} with the following signature.
<signature>
{SIGNATURE}
</signature>
The signature follows the format of <code>[Full qualified name of the class].method_name(method_arguments)</code>. The method_name will be <init> for constructors.
For examples:

1) For the constructor of class <code>Test</code> of package <code>org.test</code> which takes in a single integer would have the following signature:
<code>[org.test.Test].<init>(int)</code>

2) The method signature follows the format of <code>[Full qualified name of the class].method_name(method_arguments)</code>.
<code>[org.test.Test].test(int)</code>

The target method belongs to the Java project {PROJECT_NAME} ({PROJECT_URL}).
</target>