Knowledge Usage Documentation
Overview
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 enables Agents to access and retrieve relevant knowledge information, thereby providing more accurate and well-founded responses.
Usage Pattern
The usage of the Knowledge system follows this pattern:
- Create Knowledge: Configure vector storage, Embedder, and knowledge sources
- Load Documents: Load and index documents from various sources
- Integrate with Agent: Use
WithKnowledge()
to integrate Knowledge into LLM Agent - Agent Auto Retrieval: Agent automatically performs knowledge retrieval through built-in
knowledge_search
tool
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 memory, PostgreSQL, TcVector, and other storage backends
- High Performance Processing: Concurrent processing and batch document loading
- Extensible Architecture: Support for custom Embedders, Retrievers, and Rerankers
Agent Integration
How the Knowledge system integrates with Agents:
- Automatic Tool Registration: Use
WithKnowledge()
option to automatically addknowledge_search
tool - Tool Invocation: Agents can call knowledge search tools to obtain relevant information
- Context Enhancement: Retrieved knowledge content is automatically added to Agent's context
Quick Start
Environment Requirements
- Go 1.24.1 or higher
- Valid LLM API key (OpenAI compatible interface)
- Vector database (optional, for production environment)
Configure Environment Variables
Minimal Example
Core Concepts
The knowledge module is the knowledge management core of the tRPC-Agent-Go framework, providing complete RAG capabilities. The module adopts a modular design, supporting multiple document sources, vector storage backends, and embedding models.
Usage Guide
Integration with Agent
Use llmagent.WithKnowledge(kb)
to integrate Knowledge into Agent. The framework automatically registers the knowledge_search
tool without needing to manually create custom tools.
Vector Store
Vector storage can be configured through options in code. Configuration sources can be configuration files, command line parameters, or environment variables, which users can implement themselves.
Vector Store Configuration Examples
Elasticsearch
Embedder
Embedder is responsible for converting text to vector representations and is a core component of the Knowledge system. Currently, the framework mainly supports OpenAI embedding models:
Supported embedding models:
- OpenAI embedding models (text-embedding-3-small, etc.)
- Other OpenAI API compatible embedding services
- Gemini embedding model (via
knowledge/embedder/gemini
)
Note:
- Retriever and Reranker are currently implemented internally by Knowledge, users don't need to configure them separately. Knowledge automatically handles document retrieval and result ranking.
- The
OPENAI_EMBEDDING_MODEL
environment variable needs to be manually read in code, the framework won't read it automatically. Refer to thegetEnvOrDefault("OPENAI_EMBEDDING_MODEL", "")
implementation in the example code.
Document Source Configuration
The source module provides multiple document source types, each supporting rich configuration options:
Batch Document Processing and Concurrency
Knowledge supports batch document processing and concurrent loading, which can significantly improve processing performance for large amounts of documents:
Note on performance and rate limits:
- Higher concurrency increases embedder API request rates (OpenAI/Gemini) and may hit rate limits.
- Tune
WithSourceConcurrency()
andWithDocConcurrency()
based on throughput, cost, and limits.- Defaults are balanced for most scenarios; increase for speed, decrease to avoid throttling.
Advanced Features
QueryEnhancer
QueryEnhancer is used to preprocess and optimize user queries before searching. Currently, the framework only provides a default implementation:
Note: QueryEnhancer is not a required component. If not specified, Knowledge will directly use the original query for search. This option only needs to be configured when custom query preprocessing logic is required.
Performance Optimization
The Knowledge system provides various performance optimization strategies, including concurrent processing, vector storage optimization, and caching mechanisms:
Complete Example
The following is a complete example showing how to create an Agent with Knowledge access capabilities:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
The environment variable configuration is as follows:
Command Line Parameters
Troubleshooting
Common Issues and Handling Suggestions
-
Create embedding failed/HTTP 4xx/5xx
-
Possible causes: Invalid or missing API Key; Incorrect BaseURL configuration; Network access restrictions; Text too long; The configured BaseURL doesn't provide Embeddings interface or doesn't support the selected embedding model (e.g., returns 404 Not Found).
- Troubleshooting steps:
- Confirm
OPENAI_API_KEY
is set and available; - If using compatible gateway, explicitly set
WithBaseURL(os.Getenv("OPENAI_BASE_URL"))
; - Confirm
WithModel("text-embedding-3-small")
or the actual embedding model name supported by your service; - Use minimal example to call embedding API once to verify connectivity;
- Use curl to verify if target BaseURL implements
/v1/embeddings
and model exists: If returns 404/model doesn't exist, please switch to BaseURL that supports Embeddings or switch to valid embedding model name provided by that service. - Gradually shorten text to confirm it's not caused by overly long input.
- Confirm
-
Reference code:
-
Slow loading speed or high CPU usage
-
Possible causes: Single-core sequential loading; Inappropriate concurrency settings; Unreasonable large file chunking strategy.
- Troubleshooting steps:
- Set source-level/document-level concurrency:
WithSourceConcurrency(N)
,WithDocConcurrency(M)
; - Adjust chunk size to avoid too many small chunks;
- Temporarily disable statistics output to reduce log overhead:
WithShowStats(false)
.
- Set source-level/document-level concurrency:
-
Reference code:
-
Storage connection failure (pgvector/TcVector)
-
Possible causes: Incorrect connection parameters; Network/authentication failure; Service not started or port not accessible.
-
Troubleshooting steps:
- Use native client to connect once first (psql/curl);
- Explicitly print current configuration (host/port/user/db/url);
- For minimal example, only insert/query one record to verify.
-
High memory usage
-
Possible causes: Loading too many documents at once; Chunk size/overlap too large; Similarity filtering too wide.
-
Troubleshooting steps:
- Reduce concurrency and chunk overlap;
- Load directories in batches.
-
Dimension/vector mismatch
-
Symptoms: Search phase errors or abnormal scores of 0.
-
Troubleshooting:
- Confirm embedding model dimension matches existing vectors (
text-embedding-3-small
is 1536); - After replacing embedding model, need to rebuild (clear and reload) vector database.
- Confirm embedding model dimension matches existing vectors (
-
Path/format reading failure
-
Symptoms: Loading logs show 0 documents or specific source errors.
- Troubleshooting:
- Confirm files exist and extensions are supported (.md/.txt/.pdf/.csv/.json/.docx, etc.);
- Whether directory source needs
WithRecursive(true)
; - Use
WithFileExtensions
for whitelist filtering.