mirror of
https://github.com/codeflash-ai/codeflash-internal.git
synced 2026-05-04 18:25:18 +00:00
Extract inline prompts from Java LP optimizer into .md files
- lineprof_context_prompt.md — line profiler context description - lineprof_user_prompt.md — single-file user prompt - lineprof_user_prompt_multifile.md — multi-file user prompt - multifile_instructions.md — multi-file system prompt addition
This commit is contained in:
parent
78691549a7
commit
cd7d11134f
5 changed files with 48 additions and 52 deletions
|
|
@ -40,12 +40,12 @@ if TYPE_CHECKING:
|
|||
current_dir = Path(__file__).parent
|
||||
JAVA_PROMPTS_DIR = current_dir / "prompts" / "optimizer"
|
||||
|
||||
# Load Java system prompt
|
||||
JAVA_SYSTEM_PROMPT = JAVA_PROMPTS_DIR / "system_prompt.md"
|
||||
if JAVA_SYSTEM_PROMPT.exists():
|
||||
JAVA_SYSTEM_PROMPT_TEXT = JAVA_SYSTEM_PROMPT.read_text()
|
||||
else:
|
||||
JAVA_SYSTEM_PROMPT_TEXT = ""
|
||||
# Load prompts from files
|
||||
JAVA_SYSTEM_PROMPT_TEXT = (JAVA_PROMPTS_DIR / "system_prompt.md").read_text()
|
||||
JAVA_LINE_PROF_CONTEXT = (JAVA_PROMPTS_DIR / "lineprof_context_prompt.md").read_text()
|
||||
JAVA_LP_USER_PROMPT = (JAVA_PROMPTS_DIR / "lineprof_user_prompt.md").read_text()
|
||||
JAVA_LP_USER_PROMPT_MULTIFILE = (JAVA_PROMPTS_DIR / "lineprof_user_prompt_multifile.md").read_text()
|
||||
JAVA_MULTIFILE_INSTRUCTIONS = (JAVA_PROMPTS_DIR / "multifile_instructions.md").read_text()
|
||||
|
||||
# Pattern to extract code blocks from Java LLM response (single file, no file path)
|
||||
JAVA_CODE_PATTERN = re.compile(r"```(?:java)\s*\n(.*?)```", re.MULTILINE | re.DOTALL)
|
||||
|
|
@ -53,19 +53,6 @@ JAVA_CODE_PATTERN = re.compile(r"```(?:java)\s*\n(.*?)```", re.MULTILINE | re.DO
|
|||
# Pattern to extract code blocks with file paths (multi-file context)
|
||||
JAVA_CODE_WITH_PATH_PATTERN = re.compile(r"```(?:java):([^\n]+)\n(.*?)```", re.MULTILINE | re.DOTALL)
|
||||
|
||||
# Line profiler context prompt for Java
|
||||
JAVA_LINE_PROF_CONTEXT = """
|
||||
Here are the results of the line profiling of the Java code you will be optimizing.
|
||||
The profiling data shows:
|
||||
- Line numbers with execution counts (hits)
|
||||
- Time spent on each line (in nanoseconds)
|
||||
- Percentage of total time per line
|
||||
|
||||
Use this data to identify performance bottlenecks and focus your optimization on the hottest code paths.
|
||||
|
||||
{line_profiler_results}
|
||||
"""
|
||||
|
||||
|
||||
def extract_java_code_and_explanation(content: str, is_multi_file: bool = False) -> tuple[str | dict[str, str], str]:
|
||||
"""Extract Java code and explanation from LLM response.
|
||||
|
|
@ -168,52 +155,27 @@ async def optimize_java_code_line_profiler_single(
|
|||
system_prompt = JAVA_SYSTEM_PROMPT_TEXT.format(language_version=f"Java {language_version}")
|
||||
|
||||
# Build user prompt with line profiler results
|
||||
lineprof_context = JAVA_LINE_PROF_CONTEXT.format(line_profiler_results=line_profiler_results)
|
||||
|
||||
if is_multi_file:
|
||||
# For multi-file, identify the first file as the target and others as helper context
|
||||
file_paths = list(original_file_to_code.keys())
|
||||
target_file = file_paths[0] if file_paths else "main file"
|
||||
helper_files = file_paths[1:] if len(file_paths) > 1 else []
|
||||
|
||||
# Build multi-file instructions
|
||||
helper_notice = ""
|
||||
if helper_files:
|
||||
helper_list = ", ".join(f"`{f}`" for f in helper_files)
|
||||
helper_notice = f"""
|
||||
HELPER FILES: {helper_list}
|
||||
These files contain helper classes/methods that the target code uses. You may optimize these as well if needed.
|
||||
"""
|
||||
helper_notice = f"\nHELPER FILES: {helper_list}\nThese files contain helper classes/methods that the target code uses. You may optimize these as well if needed.\n"
|
||||
|
||||
multi_file_instructions = f"""
|
||||
The code is provided in a multi-file format. Each file is wrapped in a code block with its path.
|
||||
|
||||
TARGET FILE: `{target_file}`
|
||||
{helper_notice}
|
||||
Output the optimized code for each file that you modify. Wrap each file's code in:
|
||||
```java:<file_path>
|
||||
<optimized code>
|
||||
```
|
||||
|
||||
You MUST output the target file. You may also output helper files if you optimize them.
|
||||
"""
|
||||
multi_file_instructions = JAVA_MULTIFILE_INSTRUCTIONS.format(
|
||||
target_file=target_file, helper_notice=helper_notice
|
||||
)
|
||||
system_prompt = system_prompt + "\n" + multi_file_instructions
|
||||
|
||||
user_prompt = f"""Optimize the following Java code for better performance.
|
||||
|
||||
{JAVA_LINE_PROF_CONTEXT.format(line_profiler_results=line_profiler_results)}
|
||||
|
||||
Here is the code to optimize:
|
||||
{source_code}
|
||||
"""
|
||||
user_prompt = JAVA_LP_USER_PROMPT_MULTIFILE.format(lineprof_context=lineprof_context, source_code=source_code)
|
||||
else:
|
||||
user_prompt = f"""Optimize the following Java code for better performance.
|
||||
|
||||
{JAVA_LINE_PROF_CONTEXT.format(line_profiler_results=line_profiler_results)}
|
||||
|
||||
Here is the code to optimize:
|
||||
```java
|
||||
{source_code}
|
||||
```
|
||||
"""
|
||||
user_prompt = JAVA_LP_USER_PROMPT.format(lineprof_context=lineprof_context, source_code=source_code)
|
||||
|
||||
if dependency_code:
|
||||
user_prompt = f"Dependencies (read-only):\n```java\n{dependency_code}\n```\n\n{user_prompt}"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
Here are the results of the line profiling of the Java code you will be optimizing.
|
||||
The profiling data shows:
|
||||
- Line numbers with execution counts (hits)
|
||||
- Time spent on each line (in nanoseconds)
|
||||
- Percentage of total time per line
|
||||
|
||||
Use this data to identify performance bottlenecks and focus your optimization on the hottest code paths.
|
||||
|
||||
{line_profiler_results}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
Optimize the following Java code for better performance.
|
||||
|
||||
{lineprof_context}
|
||||
|
||||
Here is the code to optimize:
|
||||
```java
|
||||
{source_code}
|
||||
```
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
Optimize the following Java code for better performance.
|
||||
|
||||
{lineprof_context}
|
||||
|
||||
Here is the code to optimize:
|
||||
{source_code}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
|
||||
The code is provided in a multi-file format. Each file is wrapped in a code block with its path.
|
||||
|
||||
TARGET FILE: `{target_file}`
|
||||
{helper_notice}
|
||||
Output the optimized code for each file that you modify. Wrap each file's code in:
|
||||
```java:<file_path>
|
||||
<optimized code>
|
||||
```
|
||||
|
||||
You MUST output the target file. You may also output helper files if you optimize them.
|
||||
Loading…
Reference in a new issue