Knowledge is the knowledge management system in the tRPC-Agent-Go framework, providing Retrieval-Augmented Generation (RAG) capabilities for Agents. By integrating vector data, embedding models, and document processing components, the Knowledge system helps Agents access and retrieve relevant knowledge information, providing more accurate and well-grounded responses.
Usage Pattern
The Knowledge system follows this usage pattern:
Create Knowledge: Configure vector store, Embedder, and knowledge sources
Load Documents: Load and index documents from various sources
Create Search Tool: Use NewKnowledgeSearchTool to create a knowledge search tool
Integrate with Agent: Add the search tool to the Agent's tool list
Knowledge Base Management: Enable intelligent sync mechanism via WithEnableSourceSync(true) to keep vector store data consistent with configured sources (see Knowledge Base Management)
This pattern provides:
Intelligent Retrieval: Semantic search based on vector similarity
Multi-source Support: Support for files, directories, URLs, and other knowledge sources
Flexible Storage: Support for in-memory, PostgreSQL, TcVector, and other storage backends
High-performance Processing: Concurrent processing and batch document loading
Knowledge Filtering: Support for static filtering and Agent intelligent filtering via metadata
Extensible Architecture: Support for custom Embedder, Retriever, and Reranker
Agent Integration
Ways to integrate the Knowledge system with Agents:
Manual Tool Creation (Recommended): Use NewKnowledgeSearchTool to create search tools with flexible tool names and descriptions, supporting multiple knowledge bases
Intelligent Filter Tool: Use NewAgenticFilterSearchTool to create search tools with intelligent filtering
Auto Integration: Use WithKnowledge() option to automatically add knowledge_search tool (for simple scenarios)
Context Enhancement: Retrieved knowledge content is automatically added to the Agent's context
Metadata Filtering: Support for precise search based on document metadata
packagemainimport("context""log""trpc.group/trpc-go/trpc-agent-go/agent/llmagent""trpc.group/trpc-go/trpc-agent-go/knowledge"openaiembedder"trpc.group/trpc-go/trpc-agent-go/knowledge/embedder/openai""trpc.group/trpc-go/trpc-agent-go/knowledge/source"dirsource"trpc.group/trpc-go/trpc-agent-go/knowledge/source/dir"filesource"trpc.group/trpc-go/trpc-agent-go/knowledge/source/file"knowledgetool"trpc.group/trpc-go/trpc-agent-go/knowledge/tool"vectorinmemory"trpc.group/trpc-go/trpc-agent-go/knowledge/vectorstore/inmemory""trpc.group/trpc-go/trpc-agent-go/model""trpc.group/trpc-go/trpc-agent-go/model/openai""trpc.group/trpc-go/trpc-agent-go/runner""trpc.group/trpc-go/trpc-agent-go/session/inmemory""trpc.group/trpc-go/trpc-agent-go/tool"// To support PDF files, manually import PDF reader (separate go.mod to avoid unnecessary dependencies)// _ "trpc.group/trpc-go/trpc-agent-go/knowledge/document/reader/pdf")funcmain(){ctx:=context.Background()// 1. Create embedderembedder:=openaiembedder.New(openaiembedder.WithModel("text-embedding-3-small"),)// 2. Create vector storevectorStore:=vectorinmemory.New()// 3. Create knowledge sources, auto-detects file formatssources:=[]source.Source{filesource.New([]string{"./data/llm.md"}),dirsource.New([]string{"./dir"}),}// 4. Create Knowledgekb:=knowledge.New(knowledge.WithEmbedder(embedder),knowledge.WithVectorStore(vectorStore),knowledge.WithSources(sources),knowledge.WithEnableSourceSync(true),)// 5. Load documentsiferr:=kb.Load(ctx);err!=nil{log.Fatalf("Failed to load knowledge base: %v",err)}// 6. Create search toolsearchTool:=knowledgetool.NewKnowledgeSearchTool(kb,knowledgetool.WithToolName("knowledge_search"),knowledgetool.WithToolDescription("Search for relevant information in the knowledge base."),)// 7. Create Agent and add toolsmodelInstance:=openai.New("claude-4-sonnet-20250514")llmAgent:=llmagent.New("knowledge-assistant",llmagent.WithModel(modelInstance),llmagent.WithTools([]tool.Tool{searchTool}),)// 8. Create Runner and executesessionService:=inmemory.NewSessionService()appRunner:=runner.NewRunner("knowledge-chat",llmAgent,runner.WithSessionService(sessionService))message:=model.NewUserMessage("Tell me about LLM")_,err:=appRunner.Run(ctx,"user123","session456",message)iferr!=nil{log.Fatalf("Failed to run agent: %v",err)}}
Core Concepts
The knowledge module is the knowledge management core of the tRPC-Agent-Go framework, providing complete RAG capabilities. The module uses a modular design, supporting various document sources, vector storage backends, and embedding models.
import(knowledgetool"trpc.group/trpc-go/trpc-agent-go/knowledge/tool")searchTool:=knowledgetool.NewKnowledgeSearchTool(kb,knowledgetool.WithToolName("knowledge_search"),knowledgetool.WithToolDescription("Search for relevant information in the knowledge base."),knowledgetool.WithMaxResults(10),knowledgetool.WithMinScore(0.5),)
AgenticFilterSearchTool
Intelligent filter search tool, Agent can automatically build filter conditions based on user queries.
Supports multiple configuration methods such as automatic extraction, manually specifying enum values, and manually specifying fields:
Use llmagent.WithKnowledge(kb) to integrate Knowledge into the Agent, and the framework will automatically register the knowledge_search tool.
Note: The automatic integration method is simple and quick, but less flexible. It doesn't allow customizing tool names, descriptions, filter conditions, or other parameters, and doesn't support integrating multiple knowledge bases simultaneously. For more fine-grained control, it's recommended to use the manual tool addition approach.
Knowledge supports batch document processing and concurrent loading, which can significantly improve performance when handling large amounts of documents: