1717from agentlab .llm .llm_utils import HumanMessage , parse_html_tags_raise
1818from browsergym .core .action .base import AbstractActionSet
1919
20+ logger = logging .getLogger (__name__ )
2021
2122@dataclass
2223class GenericPromptFlags (dp .Flags ):
@@ -359,13 +360,14 @@ def _prompt(self) -> HumanMessage:
359360# Querying memory
360361
361362Before choosing an action, let's search our available documentation and memory for relevant context.
362- Generate a brief, general summary of the current status to help identify useful hints. Return your answer as follow
363+ Generate a brief, general summary of the current status to help identify useful hints. Return your answer in the following format:
363364<think>chain of thought</think>
364- <queries>json list of strings</queries> for the queries. Return exactly { self .n_queries }
365- queries in the list.
365+ <queries>json list of strings of queries</queries>
366366
367- # Concrete Example
367+ Additional instructions: List of queries should contain up to { self . n_queries } queries. Both the think and the queries blocks are required!
368368
369+ # Concrete Example
370+ ```
369371<think>
370372I have to sort by client and country. I could use the built-in sort on each column but I'm not sure if
371373I will be able to sort by both at the same time.
@@ -374,6 +376,10 @@ def _prompt(self) -> HumanMessage:
374376<queries>
375377{ example_queries_str }
376378</queries>
379+ ```
380+ Note: do not generate backticks.
381+ Now proceed to generate your own thoughts and queries.
382+ Always return non-empty answer, its very important!
377383"""
378384 )
379385
@@ -384,8 +390,19 @@ def shrink(self):
384390 self .obs .shrink ()
385391
386392 def _parse_answer (self , text_answer ):
387- ans_dict = parse_html_tags_raise (
388- text_answer , keys = ["think" , "queries" ], merge_multiple = True
389- )
390- ans_dict ["queries" ] = json .loads (ans_dict .get ("queries" , "[]" ))
393+ try :
394+ ans_dict = parse_html_tags_raise (
395+ text_answer , keys = ["think" , "queries" ], merge_multiple = True
396+ )
397+ except Exception as e :
398+ t = text_answer .replace ("\n " , "\\ n" )
399+ logger .warning (f"Failed to parse llm answer: { e } . RAW answer: '{ t } '. Will retry" )
400+ raise e
401+ raw_queries = ans_dict .get ("queries" , "[]" )
402+ try :
403+ ans_dict ["queries" ] = json .loads (raw_queries )
404+ except Exception as e :
405+ t = text_answer .replace ("\n " , "\\ n" )
406+ logger .warning (f"Failed to parse queries: { e } . Queries block content: '{ ans_dict ['queries' ]} '. RAW llm answer: '{ t } '. Will retry" )
407+ raise e
391408 return ans_dict
0 commit comments