1212import pandas as pd
1313import requests
1414from agentlab .llm .chat_api import ChatModel
15+ import re
16+ from agentlab .llm .response_api import APIPayload
1517
1618logger = logging .getLogger (__name__ )
1719
1820
1921class HintsSource :
22+
2023 def __init__ (
2124 self ,
2225 hint_db_path : str ,
@@ -27,7 +30,8 @@ def __init__(
2730 embedder_server : str = "http://localhost:5000" ,
2831 llm_prompt : str = """We're choosing hints to help solve the following task:\n {goal}.\n
2932You need to choose the most relevant hints topic from the following list:\n \n Hint topics:\n {topics}\n
30- Choose hint topic for the task and return only its number, e.g. 1. If you don't know the answer, return -1.""" ,
33+ Choose hint topic for the task and return only its number. Use the following output format:
34+ <choice>index</choice> for e.g. <choice>0</choice> for the topic with index 0. If you don't know the answer, return <choice>-1</choice>""" ,
3135 ) -> None :
3236 self .hint_db_path = hint_db_path
3337 self .hint_retrieval_mode = hint_retrieval_mode
@@ -96,7 +100,15 @@ def choose_hints_llm(self, llm, goal: str, task_name: str) -> list[str]:
96100 else :
97101 response : str = llm (APIPayload (messages = [llm .msg .user ().add_text (prompt )])).think
98102 try :
99- topic_number = json .loads (response )
103+ matches = re .findall (r"<choice>(-?\d+)</choice>" , response )
104+ if not matches :
105+ logger .error (f"No choice tags found in LLM response: { response } " )
106+ return []
107+ if len (matches ) > 1 :
108+ logger .warning (
109+ f"LLM selected multiple topics for retrieval using only the first one."
110+ )
111+ topic_number = int (matches [0 ])
100112 if topic_number < 0 or topic_number >= len (hint_topics ):
101113 logger .error (f"Wrong LLM hint id response: { response } , no hints" )
102114 return []
0 commit comments