The Knowledge system provides powerful filter functionality, allowing precise search based on document metadata. This includes static filters (manually preset) and intelligent filters (automatically generated by LLM based on document metadata).
Configuring Metadata Sources
For filter functionality to work properly, metadata needs to be added when creating document sources:
import("trpc.group/trpc-go/trpc-agent-go/knowledge/source"filesource"trpc.group/trpc-go/trpc-agent-go/knowledge/source/file"dirsource"trpc.group/trpc-go/trpc-agent-go/knowledge/source/dir"urlsource"trpc.group/trpc-go/trpc-agent-go/knowledge/source/url")sources:=[]source.Source{// File source with metadatafilesource.New([]string{"./docs/api.md"},filesource.WithName("API Documentation"),filesource.WithMetadataValue("category","documentation"),filesource.WithMetadataValue("topic","api"),filesource.WithMetadataValue("service_type","gateway"),filesource.WithMetadataValue("protocol","trpc-go"),filesource.WithMetadataValue("version","v1.0"),),// Directory source with metadatadirsource.New([]string{"./tutorials"},dirsource.WithName("Tutorials"),dirsource.WithMetadataValue("category","tutorial"),...),// URL source with metadataurlsource.New([]string{"https://example.com/wiki/rpc"},urlsource.WithName("RPC Wiki"),urlsource.WithMetadataValue("category","encyclopedia"),...),}
Intelligent Filters: The JSON field names generated by LLM must also include the prefix, e.g., {"metadata.topic": "api"} (system handles this automatically).
Exception: Custom table fields added via WithDocBuilder (like status, priority, etc.) use the field name directly without prefix.
Filter Hierarchy
The Knowledge system supports multiple filter levels, all filters are implemented using FilterCondition and combined using AND logic. The system doesn't distinguish priority; all levels of filters are merged equally.
Filter Hierarchy:
Tool Level Filters / Agent Level Filters:
Manually Created Tools (injected via llmagent.WithTools(customSearchTool)): Set via knowledgetool.WithConditionedFilter().
Automatically Created Tools (injected via llmagent.WithKnowledge): Set via llmagent.WithKnowledgeConditionedFilter().
Note: Both are essentially the same, acting as static filters on the Tool instance.
Runner Level Filters:
Pass conditioned filters via agent.WithKnowledgeConditionedFilter() when calling runner.Run().
LLM Intelligent Filters:
Filter conditions dynamically generated by LLM based on user queries.
Important Note:
All filters are combined using AND logic, meaning all filter conditions from all levels must be satisfied simultaneously.
Basic Filters
Basic filters support two ways of setting: Tool/Agent-level fixed filters and Runner-level runtime filters.
Tool/Agent Level Filters
Way 1: Agent-Level Filters when Automatically Creating Tools
When creating an Agent and automatically injecting the Knowledge Tool via WithKnowledge, you can preset fixed search filter conditions using WithKnowledgeConditionedFilter:
import("trpc.group/trpc-go/trpc-agent-go/agent/llmagent""trpc.group/trpc-go/trpc-agent-go/knowledge/searchfilter"knowledgetool"trpc.group/trpc-go/trpc-agent-go/knowledge/tool")// Manually create Tool and set filterssearchTool:=knowledgetool.NewKnowledgeSearchTool(kb,knowledgetool.WithConditionedFilter(searchfilter.Equal("metadata.language","en"),),)// Inject Tool into AgentllmAgent:=llmagent.New("knowledge-assistant",llmagent.WithModel(modelInstance),llmagent.WithTools(searchTool),)
Runner-Level Filters
Dynamically pass filters when calling runner.Run(), suitable for scenarios requiring filtering based on different request contexts:
Intelligent filters are an advanced feature of the Knowledge system, allowing LLM Agents to dynamically select appropriate filter conditions based on user queries.
Enabling Intelligent Filters
When enabling intelligent filters, you need to provide available metadata field information via WithKnowledgeAgenticFilterInfo. This information serves as part of the prompt to guide the LLM in generating correct filter conditions.
Three configuration methods are supported:
Way 1: Automatic Extraction (Recommended)
Automatically extract metadata information from configured document sources:
Extract Fields and Values: Use source.GetAllMetadata(sources), LLM will select from the extracted enum values (suitable for finite enum values).
Extract Fields Only: Use source.GetAllMetadataWithoutValues(sources), LLM will infer values freely based on user queries (suitable for open-domain values).
import("trpc.group/trpc-go/trpc-agent-go/agent/llmagent""trpc.group/trpc-go/trpc-agent-go/knowledge/source")// 1. Extract all metadata info (including field names and all occurred values)// e.g. {"metadata.category": ["doc", "tutorial"], "metadata.topic": ["api", "rpc"]}sourcesMetadata:=source.GetAllMetadata(sources)// 2. Or extract field names only (unlimited values, inferred by LLM)// e.g. {"metadata.category": [], "metadata.topic": []}// sourcesMetadata := source.GetAllMetadataWithoutValues(sources)llmAgent:=llmagent.New(// ...llmagent.WithEnableKnowledgeAgenticFilter(true),llmagent.WithKnowledgeAgenticFilterInfo(sourcesMetadata),)
Way 2: Manual Configuration (Specify Fields and Values)
Manually specify allowed filter fields and their enum values, suitable for scenarios with limited enum values (e.g., status, category):
// Manually specify fields and optional valuescustomMetadata:=map[string][]string{"category":{"documentation","tutorial","blog"},"language":{"en","zh"},}llmAgent:=llmagent.New(// ...llmagent.WithEnableKnowledgeAgenticFilter(true),llmagent.WithKnowledgeAgenticFilterInfo(customMetadata),)
Way 3: Manual Configuration (Specify Fields, Infer Values by LLM)
Only specify allowed filter fields, leave value lists empty (nil or empty slice), letting LLM infer values freely (suitable for fields with too many enum values like ID, name):
// Only specify fields, no limit on value rangecustomMetadata:=map[string][]string{"author_id":nil,// LLM automatically extracts author_id"publish_year":nil,// LLM automatically extracts year"category":{"news"},// Mixed use: category limited to "news"}llmAgent:=llmagent.New(// ...llmagent.WithEnableKnowledgeAgenticFilter(true),llmagent.WithKnowledgeAgenticFilterInfo(customMetadata),)
import("trpc.group/trpc-go/trpc-agent-go/agent/llmagent""trpc.group/trpc-go/trpc-agent-go/knowledge/searchfilter"knowledgetool"trpc.group/trpc-go/trpc-agent-go/knowledge/tool")// Manually create Tool with conditioned filtersearchTool:=knowledgetool.NewKnowledgeSearchTool(kb,knowledgetool.WithConditionedFilter(searchfilter.And(searchfilter.Equal("metadata.source_type","web"),searchfilter.Or(searchfilter.Equal("metadata.topic","programming"),searchfilter.Equal("metadata.topic","api"),),),),)llmAgent:=llmagent.New("knowledge-assistant",llmagent.WithModel(modelInstance),llmagent.WithTools(searchTool),// Manually pass Tool)// Final filter condition:// metadata.source_type = "web" AND (metadata.topic = "programming" OR metadata.topic = "api")// Meaning: Must be web source, and topic is either programming or API
importknowledgetool"trpc.group/trpc-go/trpc-agent-go/knowledge/tool"// Create search tool, limit to at most 5 documentssearchTool:=knowledgetool.NewKnowledgeSearchTool(kb,knowledgetool.WithMaxResults(5),)// Or use intelligent filter search toolagenticSearchTool:=knowledgetool.NewAgenticFilterSearchTool(kb,sourcesMetadata,knowledgetool.WithMaxResults(10),)
Each returned document contains text content, metadata, and relevance score, sorted by score in descending order.