1212import pandas as pd
1313import requests
1414from agentlab .llm .chat_api import ChatModel
15-
15+ import re
16+ from agentlab .llm .response_api import APIPayload
1617logger = logging .getLogger (__name__ )
1718
1819
1920class HintsSource :
21+
2022 def __init__ (
2123 self ,
2224 hint_db_path : str ,
@@ -27,7 +29,8 @@ def __init__(
2729 embedder_server : str = "http://localhost:5000" ,
2830 llm_prompt : str = """We're choosing hints to help solve the following task:\n {goal}.\n
2931You 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.""" ,
32+ Choose hint topic for the task and return only its number. Use the following output format:
33+ <choice>index</choice> for e.g. <choice>1</choice> for the first choice. If you don't know the answer, return <choice>-1</choice>""" ,
3134 ) -> None :
3235 self .hint_db_path = hint_db_path
3336 self .hint_retrieval_mode = hint_retrieval_mode
@@ -96,7 +99,10 @@ def choose_hints_llm(self, llm, goal: str, task_name: str) -> list[str]:
9699 else :
97100 response : str = llm (APIPayload (messages = [llm .msg .user ().add_text (prompt )])).think
98101 try :
99- topic_number = json .loads (response )
102+ matches = re .findall (r"<choice>(-?\d+)</choice>" , response )
103+ if len (matches ) > 1 :
104+ logger .warning (f"LLM selected multiple topics for retrieval using only the first one." )
105+ topic_number = int (matches [0 ])
100106 if topic_number < 0 or topic_number >= len (hint_topics ):
101107 logger .error (f"Wrong LLM hint id response: { response } , no hints" )
102108 return []
0 commit comments