1313T = TypeVar ("T" )
1414
1515RE_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
2232class _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 ()
0 commit comments