Skip to content

Commit 47a081c

Browse files
committed
Allow text tool to give agent ability to terminate research
We'd moved research planner to only use tools in enum of schema. This enum tool enforcement prevented model from terminating research by setting tool field to empty. Fix the issue by adding text tool to research tools enum and tell model to use that to terminate research and start response instead.
1 parent 38dd02a commit 47a081c

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

src/khoj/processor/conversation/prompts.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@
735735
- Ensure that all required context is passed to the tool AIs for successful execution. They only know the context provided in your query.
736736
- Think step by step to come up with creative strategies when the previous iteration did not yield useful results.
737737
- You are allowed upto {max_iterations} iterations to use the help of the provided tool AIs to answer the user's question.
738-
- Stop when you have the required information by returning a JSON object with an empty "tool" field. E.g., {{scratchpad: "I have all I need", tool: "", query: ""}}
738+
- Stop when you have the required information by returning a JSON object with the "tool" field set to "text" and "query" field empty. E.g., {{"scratchpad": "I have all I need", "tool": "text", "query": ""}}
739739
740740
# Examples
741741
Assuming you can search the user's notes and the internet.

src/khoj/routers/research.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ async def apick_next_tool(
9999
# Skip showing Notes tool as an option if user has no entries
100100
if tool == ConversationCommand.Notes and not user_has_entries:
101101
continue
102+
# Add tool if agent does not have any tools defined or the tool is supported by the agent.
102103
if len(agent_tools) == 0 or tool.value in agent_tools:
103104
tool_options[tool.name] = tool.value
104105
tool_options_str += f'- "{tool.value}": "{description}"\n'
@@ -170,7 +171,9 @@ async def apick_next_tool(
170171
# Only send client status updates if we'll execute this iteration
171172
elif send_status_func:
172173
determined_tool_message = "**Determined Tool**: "
173-
determined_tool_message += f"{selected_tool}({generated_query})." if selected_tool else "respond."
174+
determined_tool_message += (
175+
f"{selected_tool}({generated_query})." if selected_tool != ConversationCommand.Text else "respond."
176+
)
174177
determined_tool_message += f"\nReason: {scratchpad}" if scratchpad else ""
175178
async for event in send_status_func(f"{scratchpad}"):
176179
yield {ChatEvent.STATUS: event}
@@ -237,8 +240,8 @@ async def execute_information_collection(
237240
if this_iteration.warning:
238241
logger.warning(f"Research mode: {this_iteration.warning}.")
239242

240-
# Terminate research if query, tool not set for next iteration
241-
elif not this_iteration.query or not this_iteration.tool:
243+
# Terminate research if selected text tool or query, tool not set for next iteration
244+
elif not this_iteration.query or not this_iteration.tool or this_iteration.tool == ConversationCommand.Text:
242245
current_iteration = MAX_ITERATIONS
243246

244247
elif this_iteration.tool == ConversationCommand.Notes:

src/khoj/utils/helpers.py

+1
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ class ConversationCommand(str, Enum):
389389
ConversationCommand.Online: "To search the internet for information. Useful to get a quick, broad overview from the internet. Provide all relevant context to ensure new searches, not in previous iterations, are performed.",
390390
ConversationCommand.Webpage: "To extract information from webpages. Useful for more detailed research from the internet. Usually used when you know the webpage links to refer to. Share the webpage links and information to extract in your query.",
391391
ConversationCommand.Code: e2b_tool_description if is_e2b_code_sandbox_enabled() else terrarium_tool_description,
392+
ConversationCommand.Text: "To respond to the user once you've completed your research and have the required information.",
392393
}
393394

394395
mode_descriptions_for_llm = {

0 commit comments

Comments
 (0)