Add more graceful exception handling when tool selection doesn't work

This commit is contained in:
sabaimran 2024-11-18 09:34:49 -08:00
parent a2ccf6f59f
commit 3f70d2f685
2 changed files with 11 additions and 11 deletions

View file

@ -634,6 +634,7 @@ You are Khoj, an extremely smart and helpful search assistant.
- You can use the data sources listed below to collect more relevant information - You can use the data sources listed below to collect more relevant information
- You can select certain types of output to respond to the user's question. Select just one output type to answer the user's question - You can select certain types of output to respond to the user's question. Select just one output type to answer the user's question
- You can use any combination of these data sources and output types to answer the user's question - You can use any combination of these data sources and output types to answer the user's question
- You can only select one output type to answer the user's question
Which of the tools listed below you would use to answer the user's question? You **only** have access to the following: Which of the tools listed below you would use to answer the user's question? You **only** have access to the following:
@ -685,7 +686,7 @@ AI: Learning to play the guitar is a great hobby. It can be a lot of fun and a g
Q: Draw a painting of a guitar. Q: Draw a painting of a guitar.
Khoj: {{"source": ["general"], "output": ["image"]}} Khoj: {{"source": ["general"], "output": ["image"]}}
Now it's your turn to pick the data sources you would like to use to answer the user's question. Provide the data sources as a list of strings in a JSON object. Do not say anything else. Now it's your turn to pick the sources and output to answer the user's query. Respond with a JSON object, including both `source` and `output`. The values should be a list of strings. Do not say anything else.
Chat History: Chat History:
{chat_history} {chat_history}

View file

@ -402,15 +402,14 @@ async def aget_relevant_tools_to_execute(
try: try:
response = clean_json(response) response = clean_json(response)
response = json.loads(response) response = json.loads(response)
input_tools = [q.strip() for q in response["source"] if q.strip()]
if not isinstance(input_tools, list) or not input_tools or len(input_tools) == 0:
logger.error(f"Invalid response for determining relevant tools: {input_tools}")
return tool_options
output_modes = [q.strip() for q in response["output"] if q.strip()] input_tools = [q.strip() for q in response.get("source", []) if q.strip()]
if not isinstance(output_modes, list) or not output_modes or len(output_modes) == 0: output_modes = [q.strip() for q in response.get("output", ["text"]) if q.strip()] # Default to text output
logger.error(f"Invalid response for determining relevant output modes: {output_modes}")
return mode_options if not isinstance(input_tools, list) or not input_tools or len(input_tools) == 0:
raise ValueError(
f"Invalid response for determining relevant tools: {input_tools}. Raw Response: {response}"
)
final_response = [] if not is_task else [ConversationCommand.AutomatedTask] final_response = [] if not is_task else [ConversationCommand.AutomatedTask]
for llm_suggested_tool in input_tools: for llm_suggested_tool in input_tools:
@ -434,8 +433,8 @@ async def aget_relevant_tools_to_execute(
final_response = [ConversationCommand.Default, ConversationCommand.Text] final_response = [ConversationCommand.Default, ConversationCommand.Text]
else: else:
final_response = [ConversationCommand.General, ConversationCommand.Text] final_response = [ConversationCommand.General, ConversationCommand.Text]
except Exception: except Exception as e:
logger.error(f"Invalid response for determining relevant tools: {response}") logger.error(f"Invalid response for determining relevant tools: {response}. Error: {e}", exc_info=True)
if len(agent_tools) == 0: if len(agent_tools) == 0:
final_response = [ConversationCommand.Default, ConversationCommand.Text] final_response = [ConversationCommand.Default, ConversationCommand.Text]
else: else: