From 511d2c5e96f159076ad6569caac5330a8a0b49c6 Mon Sep 17 00:00:00 2001 From: perf3ct Date: Thu, 29 May 2025 21:04:06 +0000 Subject: [PATCH] feat(llm): add empty result handling and parameter adjustment suggestions for tool execution --- .../prompts/providers/ollama_tool_prompt.md | 11 ++ .../llm/pipeline/stages/tool_calling_stage.ts | 105 +++++++++++++++++- 2 files changed, 111 insertions(+), 5 deletions(-) diff --git a/apps/server/src/assets/llm/prompts/providers/ollama_tool_prompt.md b/apps/server/src/assets/llm/prompts/providers/ollama_tool_prompt.md index c89760444..499369ea8 100644 --- a/apps/server/src/assets/llm/prompts/providers/ollama_tool_prompt.md +++ b/apps/server/src/assets/llm/prompts/providers/ollama_tool_prompt.md @@ -32,4 +32,15 @@ When responding to queries: 5. For general questions about the user's notes, provide a summary of all relevant notes found, including brief summaries of individual notes 6. For specific questions, provide detailed information from the user's notes that directly addresses the question 7. Always prioritize information from the user's notes over your own knowledge, as the user's notes are likely more up-to-date and personally relevant + +When using tools, follow these best practices: +1. If a tool returns an error or no results, DO NOT give up immediately +2. Instead, try different parameters that might yield better results: + - For search tools: Try broader search terms, fewer filters, or synonyms + - For note navigation: Try parent or sibling notes if a specific note isn't found + - For content analysis: Try rephrasing or generalizing the query +3. When searching for information, start with specific search terms but be prepared to broaden your search if no results are found +4. If multiple attempts with different parameters still yield no results, clearly explain to the user that the information they're looking for might not be in their notes +5. When suggesting alternatives, be explicit about what parameters you've tried and what you're changing +6. Remember that empty results from tools don't mean the user's request can't be fulfilled - it often means the parameters need adjustment ``` \ No newline at end of file diff --git a/apps/server/src/services/llm/pipeline/stages/tool_calling_stage.ts b/apps/server/src/services/llm/pipeline/stages/tool_calling_stage.ts index a5a913f24..231bcb3eb 100644 --- a/apps/server/src/services/llm/pipeline/stages/tool_calling_stage.ts +++ b/apps/server/src/services/llm/pipeline/stages/tool_calling_stage.ts @@ -364,6 +364,7 @@ export class ToolCallingStage extends BasePipelineStage 150 ? '...' : ''}`); - log.info(`Tool result status: ${resultContent.startsWith('Error:') ? 'ERROR' : 'SUCCESS'}`); + log.info(`Tool result status: ${resultContent.startsWith('Error:') ? 'ERROR' : isEmptyResult ? 'EMPTY' : 'SUCCESS'}`); updatedMessages.push(toolMessage); toolResultMessages.push(toolMessage); @@ -398,7 +412,17 @@ export class ToolCallingStage extends BasePipelineStage { + private async getOrCreateDependency(dependencyType: string, toolName: string): Promise { const aiServiceManager = (await import('../../ai_service_manager.js')).default; try { @@ -485,7 +509,7 @@ export class ToolCallingStage extends BasePipelineStage { + private async validateToolBeforeExecution(tool: { execute: (args: Record) => Promise }, toolName: string): Promise { try { if (!tool) { log.error(`Tool '${toolName}' not found or failed validation`); @@ -550,6 +574,77 @@ export class ToolCallingStage extends BasePipelineStage; + + if (toolName === 'search_notes' && + 'results' in resultObj && + Array.isArray(resultObj.results) && + resultObj.results.length === 0) { + return true; + } + + if (toolName === 'vector_search' && + 'matches' in resultObj && + Array.isArray(resultObj.matches) && + resultObj.matches.length === 0) { + return true; + } + } + } + + return false; + } + /** * Preload the vector search tool to ensure it's available before tool execution */