trading_agent.knowledge.knowledge_base
¶
A searchable index of the user's documents in workspace/shared/knowledge/.
Put Markdown, text or CSV files in the folder, such as strategy write-ups, broker rules or exchange circulars, and every desk can search them with search_knowledge. Each file is cut into passages of about 1,500 characters, kept under the nearest Markdown heading, and indexed with SQLite's FTS5 full-text search. Words are matched by their stem, so "gapped" finds "gap", and results are ranked by BM25, the usual relevance score of search engines.
The index lives in workspace/shared/knowledge.db. Before each search, files that were added, changed or removed since the last search are indexed again, so there is no separate import step.
Typical usage example:
knowledge = KnowledgeBase(Path('workspace/shared/knowledge'), Path('workspace/shared/knowledge.db')) knowledge.refresh() hits = knowledge.search('opening range breakout', 5)
KnowledgeHit
dataclass
¶
One passage that matched.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The file, relative to the knowledge folder. |
heading |
str
|
The Markdown heading the passage sits under, or the file name. |
snippet |
str
|
The matching part, with matched words in [brackets]. |
RefreshCounts
dataclass
¶
What one refresh did.
Attributes:
| Name | Type | Description |
|---|---|---|
indexed |
int
|
Files indexed because they were new or changed. |
removed |
int
|
Files dropped because they were deleted. |
passages |
int
|
Passages in the index afterwards. |
KnowledgeBase
¶
The index over one knowledge folder.
Describes the knowledge base without touching the disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
knowledge_directory
|
Path
|
The folder of documents. |
required |
index_file
|
Path
|
The SQLite file holding the index. |
required |
Source code in src/trading_agent/knowledge/knowledge_base.py
refresh
¶
Indexes new and changed files and forgets deleted ones.
Returns:
| Type | Description |
|---|---|
RefreshCounts
|
What changed. |
Source code in src/trading_agent/knowledge/knowledge_base.py
search
¶
Finds the passages that best match the query.
Passages holding every word come first; when there are none, passages holding any of the words are returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Words to look for. |
required |
limit
|
int
|
The most passages to return. |
required |
Returns:
| Type | Description |
|---|---|
list[KnowledgeHit]
|
The best passages, most relevant first. |