refactor: Enhance AgentQARequest structure with additional fields and improve session handling methods for clarity and functionality

This commit is contained in:
wizardchen
2025-11-27 17:56:53 +08:00
parent 0550d3669a
commit c222993777
8 changed files with 637 additions and 425 deletions
+94 -91
View File
@@ -240,70 +240,67 @@ func BuildProgressiveRAGSystemPrompt(knowledgeBases []*KnowledgeBaseInfo, webSea
// ProgressiveRAGSystemPromptWithWeb is the progressive RAG system prompt template with web search enabled
// This version emphasizes hybrid retrieval strategy: KB-first with web supplementation
var ProgressiveRAGSystemPromptWithWeb = `### Role
You are WeKnora, an intelligent retrieval assistant powered by Progressive Agentic RAG. You operate in a multi-tenant environment with strictly isolated knowledge bases (Document-based & FAQ-based). Your core philosophy is "Evidence-First": you never rely on internal parametric knowledge but construct answers solely from retrieved verified data.
You are WeKnora, an intelligent retrieval assistant powered by Progressive Agentic RAG. You operate in a multi-tenant environment with strictly isolated knowledge bases. Your core philosophy is "Evidence-First": you never rely on internal parametric knowledge but construct answers solely from verified data retrieved from the Knowledge Base (KB) or Web.
### Mission
To deliver accurate, traceable, and verifiable answers by orchestrating a dynamic retrieval process. You must first gauge the information landscape through preliminary retrieval, then rigorously execute and reflect upon specific research tasks until the user's intent is fully satisfied.
To deliver accurate, traceable, and verifiable answers by orchestrating a dynamic retrieval process. You must first gauge the information landscape through preliminary retrieval, then rigorously execute and reflect upon specific research tasks. **You prioritize "Deep Reading" over superficial scanning.**
### Critical Constraints (ABSOLUTE RULES)
1. **NO Internal Knowledge:** You must behave as if your training data does not exist regarding facts. If it's not in the retrieved content (KB or Web), it does not exist.
2. **KB First, Web Second:** Always exhaust Knowledge Base retrieval strategies before attempting Web Search. Web Search is a fallback for missing or outdated data, not the default.
3. **Strict Plan Adherence:** If a todo_write plan exists, you must follow it sequentially. You cannot skip steps or summarize until all tasks are explicitly marked "completed".
4. **Tool Privacy:** Never expose tool names or internal mechanics to the user.
1. **NO Internal Knowledge:** You must behave as if your training data does not exist regarding facts.
2. **Mandatory Deep Read:** Whenever grep_chunks or knowledge_search returns matched knowledge_ids or chunk_ids, you **MUST** immediately call list_knowledge_chunks to read the full content of those specific chunks. Do not rely on search snippets alone.
3. **KB First, Web Second:** Always exhaust KB strategies (including the Deep Read) before attempting Web Search.
4. **Strict Plan Adherence:** If a todo_write plan exists, execute it sequentially. No skipping.
5. **Tool Privacy:** Never expose tool names to the user.
### Workflow: The "Reconnaissance-Plan-Execute" Cycle
You must follow this **Specific Operational Sequence** for every user query:
#### Phase 1: Preliminary Reconnaissance (Mandatory Initial Step)
Before answering or creating a complex plan, you MUST perform an initial "test" of the knowledge base to gain preliminary cognition.
* **Action:** Immediately execute grep_chunks (keyword match) and knowledge_search (semantic match) based on the core entities of the user's query.
* **Purpose:** To determine: Does the KB contain direct answers? Is the data fragmented? Is the query complex enough to require a multi-step plan?
* **Output:** Analyze these initial results in your think block.
Before answering or creating a plan, you MUST perform a "Deep Read" test of the KB to gain preliminary cognition.
1. **Search:** Execute grep_chunks (keyword) and knowledge_search (semantic) based on core entities.
2. **DEEP READ (Crucial):** If the search returns IDs, you **MUST** call list_knowledge_chunks on the top relevant IDs to fetch their actual text.
3. **Analyze:** In your think block, evaluate the *full text* you just retrieved.
* *Does this text fully answer the user?*
* *Is the information complete or partial?*
#### Phase 2: Strategic Decision & Planning
Based on Phase 1 results, decide your path in the think block:
* **Path A (Direct Answer):** If the initial retrieval contains sufficient, unambiguous, and complete evidence to answer the user fully → Proceed directly to **Answer Generation**.
* **Path B (Complex Research):** If the query involves comparison, multi-hop reasoning, missing data, or the initial results are fragmented → You MUST use todo_write to formulate a Work Plan.
* **Plan Structure:** Break the problem into distinct, independent retrieval tasks (e.g., "Retrieve specs for Product A", "Retrieve specs for Product B", "Search for regulatory changes").
Based on the **Deep Read** results from Phase 1:
* **Path A (Direct Answer):** If the full text provides sufficient, unambiguous evidence → Proceed to **Answer Generation**.
* **Path B (Complex Research):** If the query involves comparison, missing data, or the content requires synthesis → Use todo_write to formulate a Work Plan.
* *Structure:* Break the problem into distinct retrieval tasks (e.g., "Deep read specs for Product A", "Deep read safety protocols").
#### Phase 3: Disciplined Execution & Deep Reflection (The Loop)
If you are in **Path B**, execute the tasks in todo_write one by one. For **EACH** task:
1. **Execute Retrieval:** Perform grep_chunks → knowledge_search (→ web_search if KB fails) specific to *that sub-task*.
2. **MANDATORY Deep Reflection (in think):** After receiving results for a task, you MUST pause and reflect:
* *Validity Check:* "Does this content actually answer the specific sub-task?"
* *Gap Analysis:* "Is the information outdated? Is it missing key details?"
* *Correction:* If the content is insufficient, you CANNOT mark the task as done. You must immediately formulate a *remedial action* (e.g., "Try different keywords", "Use knowledge graph", or "Escalate to Web Search") and execute it.
* *Completion:* Only when the sub-task is truly satisfied by evidence can you update todo_write to "completed".
If in **Path B**, execute tasks in todo_write sequentially. For **EACH** task:
1. **Search:** Perform grep_chunks / knowledge_search for the sub-task.
2. **DEEP READ (Mandatory):** Call list_knowledge_chunks for any relevant IDs found. **Never skip this step.**
3. **MANDATORY Deep Reflection (in think):** Pause and evaluate the full text:
* *Validity:* "Does this full text specifically address the sub-task?"
* *Gap Analysis:* "Is anything missing?"
* *Correction:* If insufficient, formulate a remedial action (e.g., "Search for synonym X", "Web Search") immediately.
* *Completion:* Mark task as "completed" ONLY when evidence is secured.
#### Phase 4: Final Synthesis
Only when ALL todo_write tasks are "completed":
* Synthesize the findings from all tasks.
* Check for consistency across different retrieved chunks.
Only when ALL tasks are "completed":
* Synthesize findings from the full text of all retrieved chunks.
* Check for consistency.
* Generate the final response.
### Core Retrieval Strategy (For Phase 1 & Phase 3)
For every retrieval attempt, strictly follow this hierarchy:
1. **Entity Anchoring (grep_chunks):** precise keyword matching to find specific documents.
* *Rule:* Use short, specific tokens (1-3 words).
2. **Semantic Expansion (knowledge_search):** vector-based search to find context.
* *Rule:* If grep_chunks returned IDs, filter this search by those knowledge_ids. Use 2-3 query variations.
3. **Graph Exploration (query_knowledge_graph):** *Optional*. Use only if the task involves relationships (e.g., "manager of", "dependency of").
4. **Fallback (web_search):** Use ONLY if specific data is missing from KB after steps 1-3.
* *Rule:* If web content is truncated, you MUST use web_fetch to get the full text.
### Core Retrieval Strategy (Strict Sequence)
For every retrieval attempt (Phase 1 or Phase 3), follow this exact chain:
1. **Entity Anchoring (grep_chunks):** Use short keywords (1-3 words) to find candidate documents.
2. **Semantic Expansion (knowledge_search):** Use vector search for context (filter by IDs from step 1 if applicable).
3. **Content Verification (list_knowledge_chunks):** **MANDATORY.** You cannot process the results of Step 1 or 2 without this. You must fetch the content of the top candidates to verify details.
4. **Fallback (web_search):** Use ONLY if the Deep Read in Step 3 confirms the data is missing or irrelevant.
### Tool Selection Guidelines
* **grep_chunks** Your "eyes". Use first to locate entities.
* **knowledge_search** Your "brain". Use to understand concepts within the documents found by grep.
* **todo_write** Your "project manager". Use for tracking multi-step research. **CRITICAL:** Do not summarize if this tool reports "pending" tasks.
* **think** Your "conscience". Use BEFORE every tool call to plan, and AFTER every tool output to reflect/verify.
* **grep_chunks / knowledge_search:** Your "Index". Use these to find *where* the information might be.
* **list_knowledge_chunks:** Your "Eyes". **Required** after every search to actually *read* the information.
* **todo_write:** Your "Manager". Tracks multi-step research.
* **think:** Your "Brain". Use to analyze the content returned by list_knowledge_chunks.
### Final Output Standards
Your final answer must be:
1. **Definitive:** Avoid vague phrases like "based on general understanding".
2. **Sourced:** Every key claim must be immediately followed by a citation: <kb doc="..." chunk_id="..." /> or <web url="..." title="..." />.
3. **Structured:** Use clear headings, bullet points, and tables (if comparing data).
4. **Verified:** If conflict exists between KB and Web, prioritize the most recent source but explicitly note the conflict.
* **Definitive:** Based strictly on the "Deep Read" content.
* **Sourced:** Cite sources immediately after claims: <kb doc="..." chunk_id="..." />.
* **Structured:** Clear hierarchy and logic.
### System Status
Current Time: {{current_time}}
@@ -313,71 +310,77 @@ Knowledge Bases: {{knowledge_bases}}
// ProgressiveRAGSystemPromptWithoutWeb is the progressive RAG system prompt template without web search
// This version emphasizes deep KB-only retrieval with advanced techniques
var ProgressiveRAGSystemPromptWithoutWeb = `### Role
You are WeKnora, an intelligent retrieval assistant powered by Progressive Agentic RAG. You operate in a strictly isolated, **Closed-Loop Knowledge Environment**. You do NOT have access to the internet. Your sole source of truth is the provided internal Knowledge Bases.
You are WeKnora, a meticulous retrieval assistant powered by Progressive Agentic RAG. You operate in a strictly isolated, **Closed-Loop Knowledge Environment** (No Internet). You are defined by your "Deep Reading" philosophy: you never trust a snippet alone; you always verify the full context.
### Mission
To deliver accurate, traceable answers by exhaustively retrieving and synthesizing information **exclusively** from internal Knowledge Bases. You must first gauge the information landscape through preliminary retrieval, then rigorously execute specific research tasks.
To provide answers that are not only accurate but contextually complete. You achieve this by following a strict **"Locate-then-Read"** protocol: finding documents via search, then reading their full content before synthesizing an answer.
### Critical Constraints (ABSOLUTE RULES)
1. **Strict Closed Loop:** You are FORBIDDEN from accessing the internet. Do not attempt to use web tools.
2. **NO Internal Knowledge:** You must behave as if your pre-training data does not exist regarding facts. If the answer is not in the retrieved KB documents, it does not exist.
3. **Honest Gap Reporting:** If, after exhaustive search, the information is not found in the KB, you must explicitly state: "Information not found in internal documents." NEVER fabricate, infer, or hallucinate an answer.
4. **Strict Plan Adherence:** If a todo_write plan exists, you must follow it sequentially. You cannot summarize until all tasks are explicitly marked "completed".
1. **No Snippet-Only Answers:** You are FORBIDDEN from answering based solely on the short text snippets returned by grep_chunks or knowledge_search.
2. **Mandatory Deep Reading:** Whenever a search tool returns relevant knowledge_ids, you MUST use list_knowledge_chunks to read the actual content of those chunks/documents before using them as evidence.
3. **No Internet:** You are strictly confined to internal Knowledge Bases.
4. **Evidence Verification:** If the full text read via list_knowledge_chunks contradicts the search snippet or shows the info is irrelevant, you must discard it and search again.
### Workflow: The "Reconnaissance-Plan-Execute" Cycle
### Workflow: The "Locate-Read-Plan-Execute" Cycle
You must follow this **Specific Operational Sequence** for every user query:
#### Phase 1: Preliminary Reconnaissance (Mandatory Initial Step)
Before answering or creating a complex plan, you MUST perform an initial "test" of the knowledge base to gain preliminary cognition.
* **Action:** Immediately execute grep_chunks (keyword match) and knowledge_search (semantic match) based on the core entities of the user's query.
* **Purpose:** To determine: Is the data present? Is it fragmented? Do I need a complex plan?
* **Output:** Analyze these initial results in your think block.
#### Phase 1: Preliminary Reconnaissance & Context Verification
Before answering or creating a plan, you MUST perform an initial "Test & Read" loop.
1. **Locate:** Execute grep_chunks (keyword) and knowledge_search (semantic) to find potential documents.
2. **READ (Mandatory):** Identify the most relevant knowledge_ids from step 1 (you can select multiple, e.g., top 3-5). **IMMEDIATELY call list_knowledge_chunks** on these IDs to retrieve their full content.
3. **Analyze:** In your think block, evaluate the *full text* you just read. Does it cover the user's intent?
* *Decision:* If this full text is sufficient → Go to **Answer Generation**.
* *Decision:* If complex/incomplete → Go to **Phase 2**.
#### Phase 2: Strategic Decision & Planning
Based on Phase 1 results, decide your path in the think block:
* **Path A (Direct Answer):** If the initial retrieval contains sufficient, unambiguous evidence → Proceed directly to **Answer Generation**.
* **Path B (Complex Research):** If the query involves comparison, multi-hop reasoning, or the initial results are fragmented → You MUST use todo_write to formulate a Work Plan.
* **Plan Structure:** Break the problem into distinct, independent retrieval tasks (e.g., "Retrieve specs for Product A", "Analyze warranty policy").
If Phase 1 is insufficient, create a todo_write Work Plan.
* **Plan Structure:** Break the problem into distinct retrieval tasks.
* **Context Awareness:** Use the full text read in Phase 1 to inform your plan (e.g., "Doc A mentions Protocol X, I need to create a task to specifically search for Protocol X details").
#### Phase 3: Disciplined Execution & Deep Reflection (The Loop)
If you are in **Path B**, execute the tasks in todo_write one by one. For **EACH** task:
1. **Execute Retrieval:** Perform grep_chunks → knowledge_search specific to *that sub-task*.
2. **MANDATORY Deep Reflection (in think):** After receiving results for a task, you MUST pause and reflect:
* *Validity Check:* "Does this content actually answer the specific sub-task?"
* *Gap Analysis:* "Is the information missing?"
* *Correction (Iterative Search):* If results are poor, do NOT give up immediately. Formulate a *remedial action* (e.g., "Try synonym keywords", "Search for broader concept", "Check Knowledge Graph").
* *Dead End Handling:* If, after remedial attempts, the KB still yields nothing, you must mark the task as "completed" but record the finding as **"Data Unavailable in KB"**.
* *Completion:* Update todo_write to "completed" only when you have exhausted all search avenues for that task.
#### Phase 3: Disciplined Execution with Deep Reading
Execute tasks in todo_write sequentially. For **EACH** task:
1. **Search:** Perform grep_chunks or knowledge_search specific to the sub-task.
2. **READ (Mandatory):**
* Extract the knowledge_ids of the most promising results.
* **Call list_knowledge_chunks** to fetch the content for these IDs. **Do not skip this step.**
* *Note:* You are encouraged to check multiple files if the answer might be spread across them.
3. **Reflect (Deep Reflection):**
* "Based on the *full text* I just read, is this sub-task resolved?"
* If no, formulate a remedial search action immediately.
* Only mark as "completed" when the full text evidence is secured.
#### Phase 4: Final Synthesis
Only when ALL todo_write tasks are "completed":
* Synthesize the findings from all tasks.
* Check for consistency across retrieved chunks.
* If some tasks resulted in "Data Unavailable", explicitly mention this limitation in the final answer.
* Generate the final response.
* Synthesize findings based **only** on the content read via list_knowledge_chunks.
* Generate the final response with citations.
### Core Retrieval Strategy (Strict Hierarchy)
For every retrieval attempt, strictly follow this hierarchy to maximize Recall within the KB:
1. **Entity Anchoring (grep_chunks):** Precise keyword matching.
* *Rule:* Use short, specific tokens (1-3 words). Try multiple aliases (e.g., "HR", "Human Resources").
2. **Semantic Expansion (knowledge_search):** Vector-based search.
* *Rule:* Use 2-4 query variations to capture different phrasings. Filter by knowledge_ids from step 1 if applicable.
3. **Graph Exploration (query_knowledge_graph):** *Optional*. Use only if the task involves relationships or if standard search fails to connect concepts.
4. **Deep Reading (list_knowledge_chunks):** Use if you need the full context of a specific document found in previous steps.
### Core Retrieval Strategy (The "Locate-then-Read" Pattern)
For every information seeking step, strictly follow this 3-step atomic unit:
1. **Step A: Locate (Search)**
* Use grep_chunks for specific entities (Error codes, product names).
* Use knowledge_search for concepts.
* *Goal:* Get a list of candidate knowledge_ids.
2. **Step B: Read (Fetch Context)**
* **Action:** Call list_knowledge_chunks(knowledge_ids=[id1, id2, ...]).
* *Rule:* Always fetch the content. Snippets are often truncated or lack necessary context (like prerequisites or exceptions).
* *Scope:* It is acceptable and encouraged to fetch 3-5 distinct documents to ensure comprehensive coverage.
3. **Step C: Evaluate (Filter)**
* Read the output of list_knowledge_chunks.
* Discard irrelevant documents.
* Extract facts from valid documents to build your answer.
### Tool Selection Guidelines
* **grep_chunks:** Your "eyes". Use first to locate entities.
* **knowledge_search:** Your "brain". Use to understand concepts.
* **todo_write:** Your "project manager". Use for tracking multi-step research.
* **think:** Your "conscience". Use to plan before tools and reflect after tools.
* **grep_chunks / knowledge_search:** Used ONLY as "Pointers" or "Index Lookups". They tell you *where* to look, not *what* the answer is.
* **list_knowledge_chunks:** Your "Eyes". **This is your primary reading tool.** You must use it after every search to verify data.
* **todo_write:** Use for managing multi-step research.
### Final Output Standards
Your final answer must be:
1. **Definitive & Honest:** If data is found, answer confidently. If data is missing, admit it clearly.
2. **Sourced:** Every key claim must be immediately followed by a citation: <kb doc="..." chunk_id="..." />.
3. **Structured:** Use clear headings, bullet points, and tables.
4. **No External References:** Do not mention "Google", "Web", or "Internet".
1. **Context-Backed:** Your answer must reflect the nuance found in the full text (e.g., conditions, warnings, detailed steps) which might be missing from search snippets.
2. **Sourced:** Cite the specific documents you read.
3. **Honest:** If the full text reveals the search hit was a false positive, admit it and search again.
### System Status
Current Time: {{current_time}}
+170 -183
View File
@@ -45,10 +45,12 @@ Provide only the **minimal core entities** extracted from user query, such as:
- Focus exclusively on **core entities**, not descriptions
- Break complex input into individual, essential keywords
- Avoid phrases, explanations, or anything that reduces match probability
- Preserve precision details embedded in the query (e.g., version numbers, build IDs) when they materially define the entity being matched.
Long phrases dramatically reduce recall because chunks rarely contain identical wording.
Only short, atomic keywords ensure accurate matching and avoid unrelated retrieval.
## Usage
grep_chunks scans enabled chunks across the specified knowledge bases and returns those containing any provided keyword. Matching is case-insensitive, with chunk indices and local context included.
@@ -88,13 +90,13 @@ func (t *GrepChunksTool) Parameters() map[string]interface{} {
"type": "string",
},
},
"knowledge_ids": map[string]interface{}{
"type": "array",
"description": "Filter by document/knowledge IDs. If empty, searches all documents.",
"items": map[string]interface{}{
"type": "string",
},
},
// "knowledge_ids": map[string]interface{}{
// "type": "array",
// "description": "Filter by document/knowledge IDs. If empty, searches all documents.",
// "items": map[string]interface{}{
// "type": "string",
// },
// },
"max_results": map[string]interface{}{
"type": "integer",
"description": "Maximum number of matching chunks to return (default: 50, max: 200)",
@@ -135,9 +137,7 @@ func (t *GrepChunksTool) Execute(ctx context.Context, args map[string]interface{
}
// Use default values for all options
contextLines := 50 // default: 50 context characters
countOnly := false // default: show results
showLineNumbers := true // default: show chunk indices
countOnly := false // default: show results
maxResults := 50
if mr, ok := args["max_results"].(float64); ok {
@@ -162,21 +162,21 @@ func (t *GrepChunksTool) Execute(ctx context.Context, args map[string]interface{
kbIDs = t.knowledgeBaseIDs
}
// Parse knowledge_ids filter
var knowledgeIDs []string
if knowledgeIDsRaw, ok := args["knowledge_ids"].([]interface{}); ok {
for _, id := range knowledgeIDsRaw {
if idStr, ok := id.(string); ok && idStr != "" {
knowledgeIDs = append(knowledgeIDs, idStr)
}
}
}
// // Parse knowledge_ids filter
// var knowledgeIDs []string
// if knowledgeIDsRaw, ok := args["knowledge_ids"].([]interface{}); ok {
// for _, id := range knowledgeIDsRaw {
// if idStr, ok := id.(string); ok && idStr != "" {
// knowledgeIDs = append(knowledgeIDs, idStr)
// }
// }
// }
logger.Infof(ctx, "[Tool][GrepChunks] Patterns: %v, MaxResults: %d",
patterns, maxResults)
// Build and execute query
results, totalCount, err := t.searchChunks(ctx, patterns, kbIDs, knowledgeIDs, maxResults)
results, totalCount, err := t.searchChunks(ctx, patterns, kbIDs)
if err != nil {
logger.Errorf(ctx, "[Tool][GrepChunks] Search failed: %v", err)
return &types.ToolResult{
@@ -213,29 +213,37 @@ func (t *GrepChunksTool) Execute(ctx context.Context, args map[string]interface{
// Sort by match score (descending), then by chunk index
sort.Slice(finalResults, func(i, j int) bool {
if finalResults[i].MatchedPatterns != finalResults[j].MatchedPatterns {
return finalResults[i].MatchedPatterns > finalResults[j].MatchedPatterns
}
if finalResults[i].MatchScore != finalResults[j].MatchScore {
return finalResults[i].MatchScore > finalResults[j].MatchScore
}
return finalResults[i].ChunkIndex < finalResults[j].ChunkIndex
})
if len(finalResults) > 20 {
finalResults = finalResults[:20]
aggregatedResults := t.aggregateByKnowledge(finalResults, patterns)
totalKnowledge := len(aggregatedResults)
if len(aggregatedResults) > 20 {
aggregatedResults = aggregatedResults[:20]
}
logger.Infof(ctx, "[Tool][GrepChunks] Aggregated results: %d", len(aggregatedResults))
// Format output
output := t.formatOutput(ctx, finalResults, totalCount, patterns, contextLines, countOnly, showLineNumbers, kbIDs, knowledgeIDs)
output := t.formatOutput(ctx, aggregatedResults, totalCount, patterns, countOnly)
return &types.ToolResult{
Success: true,
Output: output,
Data: map[string]interface{}{
"patterns": patterns,
"results": finalResults,
"result_count": len(finalResults),
"total_matches": totalCount,
"knowledge_results": aggregatedResults,
"result_count": len(aggregatedResults),
"total_matches": totalKnowledge,
"knowledge_base_ids": kbIDs,
"knowledge_ids": knowledgeIDs,
"max_results": maxResults,
"display_type": "grep_results",
},
@@ -244,8 +252,9 @@ func (t *GrepChunksTool) Execute(ctx context.Context, args map[string]interface{
type chunkWithTitle struct {
types.Chunk
KnowledgeTitle string `json:"knowledge_title" gorm:"column:knowledge_title"`
MatchScore float64 `json:"match_score" gorm:"column:match_score"` // Score based on match count and position
KnowledgeTitle string `json:"knowledge_title" gorm:"column:knowledge_title"`
MatchScore float64 `json:"match_score" gorm:"column:match_score"` // Score based on match count and position
MatchedPatterns int `json:"matched_patterns"` // Number of unique patterns matched
}
// searchChunks performs the database search with pattern matching
@@ -253,11 +262,9 @@ func (t *GrepChunksTool) searchChunks(
ctx context.Context,
patterns []string,
kbIDs []string,
knowledgeIDs []string,
maxResults int,
) ([]chunkWithTitle, int64, error) {
// Build base query
query := t.db.WithContext(ctx).Table("chunks").
query := t.db.Debug().WithContext(ctx).Table("chunks").
Select("chunks.id, chunks.content, chunks.chunk_index, chunks.knowledge_id, chunks.knowledge_base_id, chunks.chunk_type, chunks.created_at, knowledges.title as knowledge_title").
Joins("LEFT JOIN knowledges ON chunks.knowledge_id = knowledges.id").
Where("chunks.tenant_id = ?", t.tenantID).
@@ -270,11 +277,6 @@ func (t *GrepChunksTool) searchChunks(
query = query.Where("chunks.knowledge_base_id IN ?", kbIDs)
}
// Apply knowledge filter
if len(knowledgeIDs) > 0 {
query = query.Where("chunks.knowledge_id IN ?", knowledgeIDs)
}
// Apply pattern matching (case-insensitive fixed string matching, OR logic for multiple patterns)
if len(patterns) == 1 {
query = query.Where("chunks.content ILIKE ?", "%"+patterns[0]+"%")
@@ -297,7 +299,7 @@ func (t *GrepChunksTool) searchChunks(
// Fetch results
var results []chunkWithTitle
if err := query.Limit(maxResults).Order("chunks.created_at DESC").Find(&results).Error; err != nil {
if err := query.Order("chunks.created_at DESC").Find(&results).Error; err != nil {
logger.Errorf(ctx, "[Tool][GrepChunks] Failed to fetch results: %v", err)
return nil, 0, err
}
@@ -308,14 +310,10 @@ func (t *GrepChunksTool) searchChunks(
// formatOutput formats the search results for display (grep-style output)
func (t *GrepChunksTool) formatOutput(
ctx context.Context,
results []chunkWithTitle,
results []knowledgeAggregation,
totalCount int64,
patterns []string,
contextLines int,
countOnly bool,
showLineNumbers bool,
kbIDs []string,
knowledgeIDs []string,
) string {
var output strings.Builder
@@ -331,7 +329,7 @@ func (t *GrepChunksTool) formatOutput(
} else {
output.WriteString(fmt.Sprintf("Patterns (%d): %v (case-insensitive, OR logic)\n", len(patterns), patterns))
}
output.WriteString(fmt.Sprintf("Matches: %d chunk(s)\n\n", len(results)))
output.WriteString(fmt.Sprintf("Matches: %d knowledge item(s)\n\n", len(results)))
if len(results) == 0 {
output.WriteString("No matches found.\n")
@@ -343,156 +341,143 @@ func (t *GrepChunksTool) formatOutput(
return output.String()
}
// Group by document (like grep showing filename)
docGroups := make(map[string]struct {
title string
knowledgeBaseID string
chunks []chunkWithTitle
})
for _, result := range results {
knowledgeID := fmt.Sprintf("%v", result.KnowledgeID)
title := "Untitled"
if t := result.KnowledgeTitle; t != "" {
title = fmt.Sprintf("%v", t)
for idx, result := range results {
var patternSummaries []string
for _, pattern := range patterns {
count := result.PatternCounts[pattern]
patternSummaries = append(patternSummaries, fmt.Sprintf("%s=%d", pattern, count))
}
group := docGroups[knowledgeID]
group.title = title
group.knowledgeBaseID = result.KnowledgeBaseID
group.chunks = append(group.chunks, result)
docGroups[knowledgeID] = group
}
// Display results in grep-style format
for docID, group := range docGroups {
// Show document header (like grep showing filename)
output.WriteString(fmt.Sprintf("\n--- knowledge_base_id: %s, knowledge_id: %s, title: %s ---\n",
group.knowledgeBaseID, docID, group.title))
// Show each matching chunk
for _, chunk := range group.chunks {
chunkIndex := chunk.ChunkIndex
content := chunk.Content
// Extract preview with context around match (case-insensitive)
// Try each pattern and use the first match
preview := extractPreviewMultiPattern(content, patterns, false, contextLines)
// Format like grep: [filename:]line:content
if showLineNumbers {
if len(docGroups) > 1 {
output.WriteString(fmt.Sprintf("%s:chunk[%d]:%s\n", group.title, chunkIndex, preview))
} else {
output.WriteString(fmt.Sprintf("chunk[%d]:%s\n", chunkIndex, preview))
}
} else {
if len(docGroups) > 1 {
output.WriteString(fmt.Sprintf("%s:%s\n", group.title, preview))
} else {
output.WriteString(fmt.Sprintf("%s\n", preview))
}
}
}
output.WriteString(fmt.Sprintf("%d) knowledge_id=%s | title=%s | chunk_hits=%d | pattern_hits=[%s]\n",
idx+1,
result.KnowledgeID,
result.KnowledgeTitle,
result.ChunkHitCount,
strings.Join(patternSummaries, ", "),
))
}
return output.String()
}
// extractPreview extracts a preview of content around the matched pattern
func extractPreview(content, pattern string, caseSensitive bool, contextLen int) string {
if content == "" {
return ""
}
// Find the pattern in content
searchContent := content
searchPattern := pattern
if !caseSensitive {
searchContent = strings.ToLower(content)
searchPattern = strings.ToLower(pattern)
}
pos := strings.Index(searchContent, searchPattern)
if pos == -1 {
// If pattern not found (might be regex), just return first 60 chars
runes := []rune(content)
if len(runes) <= contextLen*2 {
return string(runes)
}
return string(runes[:contextLen*2])
}
// Extract context around the match
runes := []rune(content)
start := pos
// Convert to rune positions for proper unicode handling
runePos := 0
for i, r := range runes {
if runePos >= start {
start = i
break
}
runePos += len(string(r))
}
// Calculate preview bounds
previewStart := start - contextLen
if previewStart < 0 {
previewStart = 0
}
previewEnd := start + len([]rune(pattern)) + contextLen
if previewEnd > len(runes) {
previewEnd = len(runes)
}
preview := string(runes[previewStart:previewEnd])
// Clean up whitespace
preview = strings.ReplaceAll(preview, "\n", " ")
preview = strings.ReplaceAll(preview, "\t", " ")
// Collapse multiple spaces
for strings.Contains(preview, " ") {
preview = strings.ReplaceAll(preview, " ", " ")
}
return strings.TrimSpace(preview)
type knowledgeAggregation struct {
KnowledgeID string `json:"knowledge_id"`
KnowledgeBaseID string `json:"knowledge_base_id"`
KnowledgeTitle string `json:"knowledge_title"`
ChunkHitCount int `json:"chunk_hit_count"`
PatternCounts map[string]int `json:"pattern_counts"`
TotalPatternHits int `json:"total_pattern_hits"`
DistinctPatterns int `json:"distinct_patterns"`
}
// extractPreviewMultiPattern extracts a preview around the first matched pattern
func extractPreviewMultiPattern(content string, patterns []string, caseSensitive bool, contextLen int) string {
func (t *GrepChunksTool) aggregateByKnowledge(results []chunkWithTitle, patterns []string) []knowledgeAggregation {
if len(results) == 0 {
return nil
}
patternKeys := make([]string, 0, len(patterns))
for _, p := range patterns {
if strings.TrimSpace(p) == "" {
continue
}
patternKeys = append(patternKeys, p)
}
aggregated := make(map[string]*knowledgeAggregation)
for _, chunk := range results {
knowledgeID := chunk.KnowledgeID
if knowledgeID == "" {
knowledgeID = fmt.Sprintf("chunk-%s", chunk.ID)
}
if _, ok := aggregated[knowledgeID]; !ok {
title := chunk.KnowledgeTitle
if strings.TrimSpace(title) == "" {
title = "Untitled"
}
aggregated[knowledgeID] = &knowledgeAggregation{
KnowledgeID: knowledgeID,
KnowledgeBaseID: chunk.KnowledgeBaseID,
KnowledgeTitle: title,
PatternCounts: make(map[string]int, len(patternKeys)),
}
for _, pKey := range patternKeys {
aggregated[knowledgeID].PatternCounts[pKey] = 0
}
}
entry := aggregated[knowledgeID]
entry.ChunkHitCount++
patternOccurrences := t.countPatternOccurrences(chunk.Content, patternKeys)
for _, p := range patternKeys {
count := patternOccurrences[p]
if count == 0 {
continue
}
entry.PatternCounts[p] += count
entry.TotalPatternHits += count
}
}
resultSlice := make([]knowledgeAggregation, 0, len(aggregated))
for _, entry := range aggregated {
distinct := 0
for _, count := range entry.PatternCounts {
if count > 0 {
distinct++
}
}
entry.DistinctPatterns = distinct
resultSlice = append(resultSlice, *entry)
}
sort.Slice(resultSlice, func(i, j int) bool {
if resultSlice[i].DistinctPatterns != resultSlice[j].DistinctPatterns {
return resultSlice[i].DistinctPatterns > resultSlice[j].DistinctPatterns
}
if resultSlice[i].TotalPatternHits != resultSlice[j].TotalPatternHits {
return resultSlice[i].TotalPatternHits > resultSlice[j].TotalPatternHits
}
if resultSlice[i].ChunkHitCount != resultSlice[j].ChunkHitCount {
return resultSlice[i].ChunkHitCount > resultSlice[j].ChunkHitCount
}
return resultSlice[i].KnowledgeTitle < resultSlice[j].KnowledgeTitle
})
return resultSlice
}
func (t *GrepChunksTool) countPatternOccurrences(content string, patterns []string) map[string]int {
counts := make(map[string]int, len(patterns))
if content == "" || len(patterns) == 0 {
return ""
return counts
}
// Try each pattern and find the first match
contentLower := strings.ToLower(content)
for _, pattern := range patterns {
preview := extractPreview(content, pattern, caseSensitive, contextLen)
// Check if this pattern found a match (not just truncated content)
searchContent := content
searchPattern := pattern
if !caseSensitive {
searchContent = strings.ToLower(content)
searchPattern = strings.ToLower(pattern)
p := strings.ToLower(pattern)
if strings.TrimSpace(p) == "" {
continue
}
if strings.Contains(searchContent, searchPattern) {
return preview
counts[pattern] = countOccurrences(contentLower, p)
}
return counts
}
func countOccurrences(text string, pattern string) int {
if pattern == "" {
return 0
}
count := 0
index := 0
for index < len(text) {
pos := strings.Index(text[index:], pattern)
if pos == -1 {
break
}
count++
index += pos + len(pattern)
}
// If no pattern matched, return preview from first pattern
if len(patterns) > 0 {
return extractPreview(content, patterns[0], caseSensitive, contextLen)
}
// Fallback: return first N chars
runes := []rune(content)
if len(runes) <= contextLen*2 {
return string(runes)
}
return string(runes[:contextLen*2])
return count
}
// deduplicateChunks removes duplicate or near-duplicate chunks using content signature
@@ -573,15 +558,17 @@ func (t *GrepChunksTool) scoreChunks(ctx context.Context, results []chunkWithTit
scored := make([]chunkWithTitle, len(results))
for i := range results {
scored[i] = results[i]
scored[i].MatchScore = t.calculateMatchScore(results[i].Content, patterns)
score, patternCount := t.calculateMatchScore(results[i].Content, patterns)
scored[i].MatchScore = score
scored[i].MatchedPatterns = patternCount
}
return scored
}
// calculateMatchScore calculates a score based on how many patterns match and their positions
func (t *GrepChunksTool) calculateMatchScore(content string, patterns []string) float64 {
func (t *GrepChunksTool) calculateMatchScore(content string, patterns []string) (float64, int) {
if content == "" || len(patterns) == 0 {
return 0.0
return 0.0, 0
}
contentLower := strings.ToLower(content)
@@ -613,7 +600,7 @@ func (t *GrepChunksTool) calculateMatchScore(content string, patterns []string)
positionBonus = positionRatio * 0.1
}
return math.Min(baseScore+positionBonus, 1.0)
return math.Min(baseScore+positionBonus, 1.0), matchCount
}
// applyMMR applies Maximal Marginal Relevance algorithm to reduce redundancy
-1
View File
@@ -58,7 +58,6 @@ type CreateKnowledgeQARequest struct {
AgentEnabled bool `json:"agent_enabled"` // Whether agent mode is enabled for this request
WebSearchEnabled bool `json:"web_search_enabled"` // Whether web search is enabled for this request
SummaryModelID string `json:"summary_model_id"` // Optional summary model ID for this request (overrides session default)
MCPServiceIDs []string `json:"mcp_service_ids"` // Optional MCP service IDs to use in agent mode
}
// SearchKnowledgeRequest defines the request structure for searching knowledge without LLM summarization