Skip to content

Commit c281891

Browse files
committed
upd: enhance general instruction module prompt
1 parent b1bc264 commit c281891

2 files changed

Lines changed: 42 additions & 22 deletions

File tree

cli/decompose/prompt_modules/general_instructions/_general_instructions.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,45 @@
1313
T = TypeVar("T")
1414

1515
RE_GENERAL_INSTRUCTIONS = re.compile(
16-
r"<general_instructions>(.+?)</general_instructions>",
16+
r"<general_instructions>(.*?)</general_instructions>",
1717
flags=re.IGNORECASE | re.DOTALL,
1818
)
1919

20+
RE_GENERAL_INSTRUCTIONS_OPEN = re.compile(
21+
r"<general_instructions>(.*)",
22+
flags=re.IGNORECASE | re.DOTALL,
23+
)
24+
25+
RE_FINAL_SENTENCE = re.compile(
26+
r"\n*All tags are closed and my assignment is finished\.\s*$",
27+
flags=re.IGNORECASE,
28+
)
29+
2030

2131
@final
2232
class _GeneralInstructions(PromptModule):
2333
@staticmethod
2434
def _default_parser(generated_str: str) -> str:
2535
general_instructions_match = re.search(RE_GENERAL_INSTRUCTIONS, generated_str)
2636

27-
general_instructions_str: str | None = (
28-
general_instructions_match.group(1).strip()
29-
if general_instructions_match
30-
else None
31-
)
32-
33-
if general_instructions_str is None:
34-
raise TagExtractionError(
35-
'LLM failed to generate correct tags for extraction: "<general_instructions>"'
37+
if general_instructions_match:
38+
general_instructions_str = general_instructions_match.group(1).strip()
39+
else:
40+
# fallback: opening tag only (in case the closing tag is missing)
41+
general_instructions_match = re.search(
42+
RE_GENERAL_INSTRUCTIONS_OPEN, generated_str
3643
)
44+
if not general_instructions_match:
45+
raise TagExtractionError(
46+
'LLM failed to generate correct tags for extraction: "<general_instructions>"'
47+
)
48+
general_instructions_str = general_instructions_match.group(1).strip()
49+
50+
general_instructions_str = re.sub(
51+
RE_FINAL_SENTENCE,
52+
"",
53+
general_instructions_str,
54+
).strip()
3755

3856
return general_instructions_str
3957

@@ -50,20 +68,22 @@ def generate(
5068

5169
system_prompt = get_system_prompt()
5270
user_prompt = get_user_prompt(task_prompt=input_str)
53-
5471
action = Message("user", user_prompt)
5572

73+
model_options = {
74+
ModelOption.SYSTEM_PROMPT: system_prompt,
75+
ModelOption.TEMPERATURE: 0,
76+
ModelOption.MAX_NEW_TOKENS: max_new_tokens,
77+
}
78+
5679
try:
57-
gen_result = mellea_session.act(
80+
response = mellea_session.act(
5881
action=action,
59-
model_options={
60-
ModelOption.SYSTEM_PROMPT: system_prompt,
61-
ModelOption.TEMPERATURE: 0,
62-
ModelOption.MAX_NEW_TOKENS: max_new_tokens,
63-
},
64-
).value
82+
model_options=model_options,
83+
)
84+
gen_result = response.value
6585
except Exception as e:
66-
raise BackendGenerationError(f"LLM generation failed: {e}")
86+
raise BackendGenerationError(f"LLM generation failed: {e}") from e
6787

6888
if gen_result is None:
6989
raise BackendGenerationError(
@@ -73,4 +93,4 @@ def generate(
7393
return PromptModuleString(gen_result, parser)
7494

7595

76-
general_instructions = _GeneralInstructions()
96+
general_instructions = _GeneralInstructions()

cli/decompose/prompt_modules/general_instructions/_prompt/system_template.jinja2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Do not write anything between </general_instructions> and the final sentence exc
1313
Here are some complete examples to guide you on how to complete your assignment:
1414

1515
{% for item in icl_examples -%}
16-
<example>
16+
<example_{{ loop.index }}>
1717
<task_prompt>
1818
{{ item["task_prompt"] }}
1919
</task_prompt>
@@ -22,7 +22,7 @@ Here are some complete examples to guide you on how to complete your assignment:
2222
</general_instructions>
2323

2424
All tags are closed and my assignment is finished.
25-
</example>
25+
</example_{{ loop.index }}>
2626

2727
{% endfor -%}
2828
That concludes the complete examples of your assignment.

0 commit comments

Comments
 (0)