Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit e0e1f07

Browse files
committed
feat(llm): try to squeeze even more out of the tools
1 parent e98954c commit e0e1f07

File tree

11 files changed

+996
-31
lines changed

11 files changed

+996
-31
lines changed

apps/server/src/assets/llm/prompts/providers/anthropic_tool_prompt.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,31 @@ When responding to queries:
3636
5. For general questions about the user's notes, provide a summary of all relevant notes found, including brief summaries of individual notes
3737
6. For specific questions, provide detailed information from the user's notes that directly addresses the question
3838
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
39+
40+
IMPORTANT: EXECUTE 10-30 TOOLS PER REQUEST FOR COMPREHENSIVE ANALYSIS
41+
42+
Tool Usage Requirements:
43+
1. BATCH EXECUTE multiple searches for speed:
44+
<function_calls>
45+
<invoke name="execute_batch"><parameter name="tools">[{"tool": "search", "params": {"query": "main topic"}}, {"tool": "search", "params": {"query": "related topic"}}]</parameter></invoke>
46+
</function_calls>
47+
48+
2. BATCH READ all discovered notes:
49+
<function_calls>
50+
<invoke name="execute_batch"><parameter name="tools">[{"tool": "read", "params": {"noteId": "id1"}}, {"tool": "read", "params": {"noteId": "id2"}}, {"tool": "read", "params": {"noteId": "id3"}}]</parameter></invoke>
51+
</function_calls>
52+
53+
3. AUTO-RETRY failed searches:
54+
<function_calls>
55+
<invoke name="retry_search"><parameter name="originalQuery">failed search</parameter><parameter name="strategy">all</parameter></invoke>
56+
</function_calls>
57+
58+
SIMPLIFIED TOOLS:
59+
- search (replaces search_notes, keyword_search_notes, attribute_search)
60+
- read (replaces read_note)
61+
- execute_batch (parallel execution)
62+
- retry_search (automatic variations)
63+
64+
WORKFLOW: batch search → batch read → auto-retry → analyze → repeat
65+
Target 15+ tools per request using batching!
3966
```

apps/server/src/assets/llm/prompts/providers/ollama_tool_prompt.md

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,37 @@ When responding to queries:
3434
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
3535
3636
CRITICAL INSTRUCTIONS FOR TOOL USAGE:
37-
1. YOU MUST TRY MULTIPLE TOOLS AND SEARCH VARIATIONS before concluding information isn't available
38-
2. ALWAYS PERFORM AT LEAST 3 DIFFERENT SEARCHES with different parameters before giving up on finding information
39-
3. If a search returns no results, IMMEDIATELY TRY ANOTHER SEARCH with different parameters:
40-
- Use broader terms: If "Kubernetes deployment" fails, try just "Kubernetes" or "container orchestration"
41-
- Try synonyms: If "meeting notes" fails, try "conference", "discussion", or "conversation"
42-
- Remove specific qualifiers: If "quarterly financial report 2024" fails, try just "financial report"
43-
- Try semantic variations: If keyword_search fails, use vector_search which finds conceptually related content
44-
4. CHAIN TOOLS TOGETHER: Use the results of one tool to inform parameters for the next tool
45-
5. NEVER respond with "there are no notes about X" until you've tried at least 3 different search variations
46-
6. DO NOT ask the user what to do next when searches fail - AUTOMATICALLY try different approaches
47-
7. ALWAYS EXPLAIN what you're doing: "I didn't find results for X, so I'm now searching for Y instead"
48-
8. If all reasonable search variations fail (minimum 3 attempts), THEN you may inform the user that the information might not be in their notes
37+
YOU ARE EXPECTED TO USE 10-30 TOOLS PER REQUEST. This is NORMAL and EXPECTED behavior.
38+
39+
TOOL EXECUTION STRATEGY:
40+
USE BATCH EXECUTION FOR SPEED:
41+
1. execute_batch([{tool:"search",params:{query:"main topic"}},{tool:"search",params:{query:"related topic"}}])
42+
2. execute_batch([{tool:"read",params:{noteId:"id1"}},{tool:"read",params:{noteId:"id2"}},{tool:"read",params:{noteId:"id3"}}])
43+
44+
SMART RETRY ON FAILURES:
45+
- Empty results? → retry_search("original query") automatically tries variations
46+
- Don't manually retry - use retry_search tool
47+
48+
SIMPLIFIED TOOL NAMES:
49+
- search (not search_notes) - auto-detects search type
50+
- read (not read_note) - reads content
51+
- execute_batch - run multiple tools in parallel
52+
53+
WORKFLOW EXAMPLES:
54+
A) Comprehensive Search:
55+
execute_batch([{tool:"search",params:{query:"AI"}},{tool:"search",params:{query:"machine learning"}},{tool:"search",params:{query:"#important"}}])
56+
→ execute_batch([{tool:"read",params:{noteId:"..."}} for all found IDs])
57+
→ retry_search("broader terms") if needed
58+
59+
B) Failed Search Recovery:
60+
search("specific term") → empty results
61+
→ retry_search("specific term") → auto-tries "term", "concepts", synonyms
62+
→ execute_batch with all variations
63+
64+
C) Analysis Chain:
65+
search → read batch → note_summarization → content_extraction → relationship
66+
67+
ALWAYS USE BATCH EXECUTION when possible - it's much faster than individual tools!
68+
69+
REMEMBER: Users expect THOROUGH exploration. Execute tools rapidly and extensively!
4970
```

apps/server/src/assets/llm/prompts/providers/openai_tool_prompt.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,32 @@ When responding to queries:
4040
5. For general questions about the user's notes, provide a summary of all relevant notes found, including brief summaries of individual notes
4141
6. For specific questions, provide detailed information from the user's notes that directly addresses the question
4242
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
43+
44+
CRITICAL: YOU MUST USE 10-30 TOOL CALLS PER REQUEST
45+
This is expected behavior. Users want comprehensive exploration of their knowledge base.
46+
47+
MANDATORY TOOL USAGE PATTERN:
48+
1. Use BATCH EXECUTION for parallel processing:
49+
<tool_calls>
50+
<tool_call id="1"><n>execute_batch</n><parameters>{"tools": [{"tool": "search", "params": {"query": "main topic"}}, {"tool": "search", "params": {"query": "related topic"}}]}</parameters></tool_call>
51+
</tool_calls>
52+
53+
2. Read ALL found notes in batches:
54+
<tool_calls>
55+
<tool_call id="2"><n>execute_batch</n><parameters>{"tools": [{"tool": "read", "params": {"noteId": "id1"}}, {"tool": "read", "params": {"noteId": "id2"}}, {"tool": "read", "params": {"noteId": "id3"}}]}</parameters></tool_call>
56+
</tool_calls>
57+
58+
3. Use SMART RETRY for empty results:
59+
<tool_calls>
60+
<tool_call id="3"><n>retry_search</n><parameters>{"originalQuery": "failed query", "strategy": "all"}</parameters></tool_call>
61+
</tool_calls>
62+
63+
SIMPLIFIED TOOL NAMES:
64+
- search (auto-detects type) instead of search_notes/keyword_search_notes
65+
- read instead of read_note
66+
- execute_batch for parallel execution
67+
- retry_search for automatic variations
68+
69+
WORKFLOW: search batch → read batch → retry if needed → analyze → repeat
70+
Minimum 10+ tools per request using batch execution for speed!
4371
```

apps/server/src/services/llm/pipeline/stages/tool_calling_stage.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,24 @@ export class ToolCallingStage extends BasePipelineStage<ToolExecutionInput, { re
490490
let directiveMessage = '';
491491

492492
if (hasEmptyResults) {
493-
directiveMessage = `No results found. Try alternative search approaches: use different search tools, broader terms, or alternative keywords. Continue searching - don't ask the user for guidance.`;
493+
directiveMessage = `IMPORTANT: No results found with your search. You MUST continue searching with different approaches:
494+
1. Use discover_tools to find alternative search methods
495+
2. Try broader search terms or synonyms
496+
3. Use different search tools (search_notes, keyword_search_notes, attribute_search)
497+
4. Search for related concepts instead of specific terms
498+
5. Use read_note on any noteIds you've found previously
499+
500+
CRITICAL: Continue executing tools to find information. Do NOT ask the user for guidance yet - exhaust all search options first.`;
494501
} else {
495-
directiveMessage = `You found results! Use read_note with the noteId values to get full content and continue your analysis.`;
502+
directiveMessage = `EXCELLENT! You found ${toolResultMessages.length} results. Now you MUST continue with these actions:
503+
1. Use read_note with ALL noteId values to get full content
504+
2. After reading notes, use search_notes or keyword_search_notes to find related information
505+
3. Use attribute_search to find notes with similar tags/labels
506+
4. Use note_summarization on long notes
507+
5. Use content_extraction to pull specific information
508+
6. Use relationship tool to find connected notes
509+
510+
REMEMBER: Execute multiple tools in sequence to gather comprehensive information. The user expects thorough analysis using 10-20+ tool calls. Continue executing tools!`;
496511
}
497512

498513
updatedMessages.push({

apps/server/src/services/llm/tools/attribute_search_tool.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ export const attributeSearchToolDefinition: Tool = {
1919
type: 'function',
2020
function: {
2121
name: 'attribute_search',
22-
description: 'Search notes by attributes (labels/relations). attributeType must be exactly "label" or "relation" (lowercase).',
22+
description: 'Search notes by attributes (labels/relations). Finds notes with specific tags, categories, or relationships.',
2323
parameters: {
2424
type: 'object',
2525
properties: {
2626
attributeType: {
2727
type: 'string',
28-
description: 'Must be exactly "label" or "relation" (lowercase only).',
28+
description: 'Type of attribute: "label" for tags/categories or "relation" for connections. Case-insensitive.',
2929
enum: ['label', 'relation']
3030
},
3131
attributeName: {
@@ -57,27 +57,29 @@ export class AttributeSearchTool implements ToolHandler {
5757
*/
5858
public async execute(args: { attributeType: string, attributeName: string, attributeValue?: string, maxResults?: number }): Promise<string | object> {
5959
try {
60-
const { attributeType, attributeName, attributeValue, maxResults = 20 } = args;
60+
let { attributeType, attributeName, attributeValue, maxResults = 20 } = args;
61+
62+
// Normalize attributeType to lowercase for case-insensitive handling
63+
attributeType = attributeType?.toLowerCase();
6164

6265
log.info(`Executing attribute_search tool - Type: "${attributeType}", Name: "${attributeName}", Value: "${attributeValue || 'any'}", MaxResults: ${maxResults}`);
6366

6467
// Enhanced validation with helpful guidance
6568
if (attributeType !== 'label' && attributeType !== 'relation') {
6669
const suggestions: string[] = [];
6770

68-
if (attributeType.toLowerCase() === 'label' || attributeType.toLowerCase() === 'relation') {
69-
suggestions.push(`CASE SENSITIVE: Use "${attributeType.toLowerCase()}" (lowercase)`);
70-
}
71-
72-
if (attributeType.includes('label') || attributeType.includes('Label')) {
73-
suggestions.push('CORRECT: Use "label" for tags and categories');
71+
// Check for common variations and provide helpful guidance
72+
if (attributeType?.includes('tag') || attributeType?.includes('category')) {
73+
suggestions.push('Use "label" for tags and categories');
7474
}
7575

76-
if (attributeType.includes('relation') || attributeType.includes('Relation')) {
77-
suggestions.push('CORRECT: Use "relation" for connections and relationships');
76+
if (attributeType?.includes('link') || attributeType?.includes('connection')) {
77+
suggestions.push('Use "relation" for links and connections');
7878
}
7979

80-
const errorMessage = `Invalid attributeType: "${attributeType}". Must be exactly "label" or "relation" (lowercase). Example: {"attributeType": "label", "attributeName": "important"}`;
80+
const errorMessage = `Invalid attributeType: "${attributeType}". Use "label" for tags/categories or "relation" for connections. Examples:
81+
- Find tagged notes: {"attributeType": "label", "attributeName": "important"}
82+
- Find related notes: {"attributeType": "relation", "attributeName": "relatedTo"}`;
8183

8284
return errorMessage;
8385
}

0 commit comments

Comments
 (0)