htm 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.architecture/decisions/adrs/001-use-postgresql-timescaledb-storage.md +227 -0
- data/.architecture/decisions/adrs/002-two-tier-memory-architecture.md +322 -0
- data/.architecture/decisions/adrs/003-ollama-default-embedding-provider.md +339 -0
- data/.architecture/decisions/adrs/004-multi-robot-shared-memory-hive-mind.md +374 -0
- data/.architecture/decisions/adrs/005-rag-based-retrieval-with-hybrid-search.md +443 -0
- data/.architecture/decisions/adrs/006-context-assembly-strategies.md +444 -0
- data/.architecture/decisions/adrs/007-working-memory-eviction-strategy.md +461 -0
- data/.architecture/decisions/adrs/008-robot-identification-system.md +550 -0
- data/.architecture/decisions/adrs/009-never-forget-explicit-deletion-only.md +570 -0
- data/.architecture/decisions/adrs/010-redis-working-memory-rejected.md +323 -0
- data/.architecture/decisions/adrs/011-database-side-embedding-generation-with-pgai.md +585 -0
- data/.architecture/decisions/adrs/012-llm-driven-ontology-topic-extraction.md +583 -0
- data/.architecture/decisions/adrs/013-activerecord-orm-and-many-to-many-tagging.md +299 -0
- data/.architecture/decisions/adrs/014-client-side-embedding-generation-workflow.md +569 -0
- data/.architecture/decisions/adrs/015-hierarchical-tag-ontology-and-llm-extraction.md +701 -0
- data/.architecture/decisions/adrs/016-async-embedding-and-tag-generation.md +694 -0
- data/.architecture/members.yml +144 -0
- data/.architecture/reviews/2025-10-29-llm-configuration-and-async-processing-review.md +1137 -0
- data/.architecture/reviews/initial-system-analysis.md +330 -0
- data/.envrc +32 -0
- data/.irbrc +145 -0
- data/CHANGELOG.md +150 -0
- data/COMMITS.md +196 -0
- data/LICENSE +21 -0
- data/README.md +1347 -0
- data/Rakefile +51 -0
- data/SETUP.md +268 -0
- data/config/database.yml +67 -0
- data/db/migrate/20250101000001_enable_extensions.rb +14 -0
- data/db/migrate/20250101000002_create_robots.rb +14 -0
- data/db/migrate/20250101000003_create_nodes.rb +42 -0
- data/db/migrate/20250101000005_create_tags.rb +38 -0
- data/db/migrate/20250101000007_add_node_vector_indexes.rb +30 -0
- data/db/schema.sql +473 -0
- data/db/seed_data/README.md +100 -0
- data/db/seed_data/presidents.md +136 -0
- data/db/seed_data/states.md +151 -0
- data/db/seeds.rb +208 -0
- data/dbdoc/README.md +173 -0
- data/dbdoc/public.node_stats.md +48 -0
- data/dbdoc/public.node_stats.svg +41 -0
- data/dbdoc/public.node_tags.md +40 -0
- data/dbdoc/public.node_tags.svg +112 -0
- data/dbdoc/public.nodes.md +54 -0
- data/dbdoc/public.nodes.svg +118 -0
- data/dbdoc/public.nodes_tags.md +39 -0
- data/dbdoc/public.nodes_tags.svg +112 -0
- data/dbdoc/public.ontology_structure.md +48 -0
- data/dbdoc/public.ontology_structure.svg +38 -0
- data/dbdoc/public.operations_log.md +42 -0
- data/dbdoc/public.operations_log.svg +130 -0
- data/dbdoc/public.relationships.md +39 -0
- data/dbdoc/public.relationships.svg +41 -0
- data/dbdoc/public.robot_activity.md +46 -0
- data/dbdoc/public.robot_activity.svg +35 -0
- data/dbdoc/public.robots.md +35 -0
- data/dbdoc/public.robots.svg +90 -0
- data/dbdoc/public.schema_migrations.md +29 -0
- data/dbdoc/public.schema_migrations.svg +26 -0
- data/dbdoc/public.tags.md +35 -0
- data/dbdoc/public.tags.svg +60 -0
- data/dbdoc/public.topic_relationships.md +45 -0
- data/dbdoc/public.topic_relationships.svg +32 -0
- data/dbdoc/schema.json +1437 -0
- data/dbdoc/schema.svg +154 -0
- data/docs/api/database.md +806 -0
- data/docs/api/embedding-service.md +532 -0
- data/docs/api/htm.md +797 -0
- data/docs/api/index.md +259 -0
- data/docs/api/long-term-memory.md +1096 -0
- data/docs/api/working-memory.md +665 -0
- data/docs/architecture/adrs/001-postgresql-timescaledb.md +314 -0
- data/docs/architecture/adrs/002-two-tier-memory.md +411 -0
- data/docs/architecture/adrs/003-ollama-embeddings.md +421 -0
- data/docs/architecture/adrs/004-hive-mind.md +437 -0
- data/docs/architecture/adrs/005-rag-retrieval.md +531 -0
- data/docs/architecture/adrs/006-context-assembly.md +496 -0
- data/docs/architecture/adrs/007-eviction-strategy.md +645 -0
- data/docs/architecture/adrs/008-robot-identification.md +625 -0
- data/docs/architecture/adrs/009-never-forget.md +648 -0
- data/docs/architecture/adrs/010-redis-working-memory-rejected.md +323 -0
- data/docs/architecture/adrs/011-pgai-integration.md +494 -0
- data/docs/architecture/adrs/index.md +215 -0
- data/docs/architecture/hive-mind.md +736 -0
- data/docs/architecture/index.md +351 -0
- data/docs/architecture/overview.md +538 -0
- data/docs/architecture/two-tier-memory.md +873 -0
- data/docs/assets/css/custom.css +83 -0
- data/docs/assets/images/htm-core-components.svg +63 -0
- data/docs/assets/images/htm-database-schema.svg +93 -0
- data/docs/assets/images/htm-hive-mind-architecture.svg +125 -0
- data/docs/assets/images/htm-importance-scoring-framework.svg +83 -0
- data/docs/assets/images/htm-layered-architecture.svg +71 -0
- data/docs/assets/images/htm-long-term-memory-architecture.svg +115 -0
- data/docs/assets/images/htm-working-memory-architecture.svg +120 -0
- data/docs/assets/images/htm.jpg +0 -0
- data/docs/assets/images/htm_demo.gif +0 -0
- data/docs/assets/js/mathjax.js +18 -0
- data/docs/assets/videos/htm_video.mp4 +0 -0
- data/docs/database_rake_tasks.md +322 -0
- data/docs/development/contributing.md +787 -0
- data/docs/development/index.md +336 -0
- data/docs/development/schema.md +596 -0
- data/docs/development/setup.md +719 -0
- data/docs/development/testing.md +819 -0
- data/docs/guides/adding-memories.md +824 -0
- data/docs/guides/context-assembly.md +1009 -0
- data/docs/guides/getting-started.md +577 -0
- data/docs/guides/index.md +118 -0
- data/docs/guides/long-term-memory.md +941 -0
- data/docs/guides/multi-robot.md +866 -0
- data/docs/guides/recalling-memories.md +927 -0
- data/docs/guides/search-strategies.md +953 -0
- data/docs/guides/working-memory.md +717 -0
- data/docs/index.md +214 -0
- data/docs/installation.md +477 -0
- data/docs/multi_framework_support.md +519 -0
- data/docs/quick-start.md +655 -0
- data/docs/setup_local_database.md +302 -0
- data/docs/using_rake_tasks_in_your_app.md +383 -0
- data/examples/basic_usage.rb +93 -0
- data/examples/cli_app/README.md +317 -0
- data/examples/cli_app/htm_cli.rb +270 -0
- data/examples/custom_llm_configuration.rb +183 -0
- data/examples/example_app/Rakefile +71 -0
- data/examples/example_app/app.rb +206 -0
- data/examples/sinatra_app/Gemfile +21 -0
- data/examples/sinatra_app/app.rb +335 -0
- data/lib/htm/active_record_config.rb +113 -0
- data/lib/htm/configuration.rb +342 -0
- data/lib/htm/database.rb +594 -0
- data/lib/htm/embedding_service.rb +115 -0
- data/lib/htm/errors.rb +34 -0
- data/lib/htm/job_adapter.rb +154 -0
- data/lib/htm/jobs/generate_embedding_job.rb +65 -0
- data/lib/htm/jobs/generate_tags_job.rb +82 -0
- data/lib/htm/long_term_memory.rb +965 -0
- data/lib/htm/models/node.rb +109 -0
- data/lib/htm/models/node_tag.rb +33 -0
- data/lib/htm/models/robot.rb +52 -0
- data/lib/htm/models/tag.rb +76 -0
- data/lib/htm/railtie.rb +76 -0
- data/lib/htm/sinatra.rb +157 -0
- data/lib/htm/tag_service.rb +135 -0
- data/lib/htm/tasks.rb +38 -0
- data/lib/htm/version.rb +5 -0
- data/lib/htm/working_memory.rb +182 -0
- data/lib/htm.rb +400 -0
- data/lib/tasks/db.rake +19 -0
- data/lib/tasks/htm.rake +147 -0
- data/lib/tasks/jobs.rake +312 -0
- data/mkdocs.yml +190 -0
- data/scripts/install_local_database.sh +309 -0
- metadata +341 -0
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
# Getting Started with HTM
|
|
2
|
+
|
|
3
|
+
Welcome to HTM! This guide will help you build your first intelligent memory system for LLM-based applications.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
Before starting, ensure you have:
|
|
8
|
+
|
|
9
|
+
1. **Ruby 3.0+** installed
|
|
10
|
+
2. **PostgreSQL with TimescaleDB** (or access to a TimescaleDB cloud instance)
|
|
11
|
+
3. **Ollama** installed and running (for embeddings)
|
|
12
|
+
4. Basic understanding of Ruby and LLMs
|
|
13
|
+
|
|
14
|
+
### Installing Ollama
|
|
15
|
+
|
|
16
|
+
HTM uses Ollama for generating vector embeddings by default:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
# Install Ollama
|
|
20
|
+
curl https://ollama.ai/install.sh | sh
|
|
21
|
+
|
|
22
|
+
# Pull the gpt-oss model (default for HTM)
|
|
23
|
+
ollama pull gpt-oss
|
|
24
|
+
|
|
25
|
+
# Verify Ollama is running
|
|
26
|
+
curl http://localhost:11434/api/version
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
!!! tip
|
|
30
|
+
The gpt-oss model provides high-quality embeddings optimized for semantic search. HTM uses these embeddings to understand the meaning of your memories, not just keyword matches.
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
Add HTM to your Gemfile:
|
|
35
|
+
|
|
36
|
+
```ruby
|
|
37
|
+
gem 'htm'
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Then install:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
bundle install
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Or install directly:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
gem install htm
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Database Setup
|
|
53
|
+
|
|
54
|
+
HTM requires a TimescaleDB database. Set your database connection:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
# Add to your .bashrc or .zshrc
|
|
58
|
+
export HTM_DBURL="postgresql://user:password@host:port/database"
|
|
59
|
+
|
|
60
|
+
# Or create a config file
|
|
61
|
+
echo "export HTM_DBURL='your-connection-string'" > ~/.bashrc__tiger
|
|
62
|
+
source ~/.bashrc__tiger
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
!!! warning
|
|
66
|
+
Keep your database credentials secure. Never commit them to version control.
|
|
67
|
+
|
|
68
|
+
Initialize the database schema:
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
require 'htm'
|
|
72
|
+
|
|
73
|
+
# Run once to create tables and indexes
|
|
74
|
+
HTM::Database.setup
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Or from the command line:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
ruby -r ./lib/htm -e "HTM::Database.setup"
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Your First HTM Application
|
|
84
|
+
|
|
85
|
+
Let's build a simple application that demonstrates HTM's core features.
|
|
86
|
+
|
|
87
|
+
### Basic Initialization
|
|
88
|
+
|
|
89
|
+
```ruby
|
|
90
|
+
require 'htm'
|
|
91
|
+
|
|
92
|
+
# Create an HTM instance for your robot
|
|
93
|
+
htm = HTM.new(
|
|
94
|
+
robot_name: "My Assistant",
|
|
95
|
+
working_memory_size: 128_000 # 128K tokens
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
puts "Robot initialized: #{htm.robot_name}"
|
|
99
|
+
puts "Robot ID: #{htm.robot_id}"
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
!!! note
|
|
103
|
+
Each HTM instance represents a "robot" - an agent with its own identity. All robots share the same long-term memory database (hive mind), but each has its own working memory.
|
|
104
|
+
|
|
105
|
+
### Adding Your First Memory
|
|
106
|
+
|
|
107
|
+
```ruby
|
|
108
|
+
# Add a fact about the user
|
|
109
|
+
htm.add_node(
|
|
110
|
+
"user_name", # Unique key
|
|
111
|
+
"The user's name is Alice", # Content
|
|
112
|
+
type: :fact, # Memory type
|
|
113
|
+
importance: 8.0, # Importance score (0-10)
|
|
114
|
+
tags: ["user", "identity"] # Tags for categorization
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
puts "Memory added successfully!"
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Understanding Memory Types
|
|
121
|
+
|
|
122
|
+
HTM supports six memory types, each optimized for different purposes:
|
|
123
|
+
|
|
124
|
+
```ruby
|
|
125
|
+
# Facts: Immutable truths
|
|
126
|
+
htm.add_node(
|
|
127
|
+
"fact_001",
|
|
128
|
+
"The user lives in San Francisco",
|
|
129
|
+
type: :fact,
|
|
130
|
+
importance: 7.0
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
# Context: Conversation state
|
|
134
|
+
htm.add_node(
|
|
135
|
+
"context_001",
|
|
136
|
+
"Currently discussing database architecture for a new project",
|
|
137
|
+
type: :context,
|
|
138
|
+
importance: 6.0
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
# Preferences: User preferences
|
|
142
|
+
htm.add_node(
|
|
143
|
+
"pref_001",
|
|
144
|
+
"User prefers Ruby over Python for scripting",
|
|
145
|
+
type: :preference,
|
|
146
|
+
importance: 5.0
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
# Decisions: Design decisions
|
|
150
|
+
htm.add_node(
|
|
151
|
+
"decision_001",
|
|
152
|
+
"Decided to use PostgreSQL instead of MongoDB for better consistency",
|
|
153
|
+
type: :decision,
|
|
154
|
+
importance: 9.0,
|
|
155
|
+
tags: ["architecture", "database"]
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
# Code: Code snippets
|
|
159
|
+
htm.add_node(
|
|
160
|
+
"code_001",
|
|
161
|
+
"def greet(name)\n puts \"Hello, \#{name}!\"\nend",
|
|
162
|
+
type: :code,
|
|
163
|
+
importance: 4.0,
|
|
164
|
+
tags: ["ruby", "functions"]
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
# Questions: Unresolved questions
|
|
168
|
+
htm.add_node(
|
|
169
|
+
"question_001",
|
|
170
|
+
"Should we add Redis caching to improve performance?",
|
|
171
|
+
type: :question,
|
|
172
|
+
importance: 6.0,
|
|
173
|
+
tags: ["performance", "caching"]
|
|
174
|
+
)
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
!!! tip "Choosing the Right Type"
|
|
178
|
+
- Use `:fact` for unchanging information
|
|
179
|
+
- Use `:context` for temporary conversation state
|
|
180
|
+
- Use `:preference` for user settings
|
|
181
|
+
- Use `:decision` for important architectural choices
|
|
182
|
+
- Use `:code` for code examples and snippets
|
|
183
|
+
- Use `:question` for tracking open questions
|
|
184
|
+
|
|
185
|
+
### Retrieving Memories
|
|
186
|
+
|
|
187
|
+
Retrieve a specific memory by its key:
|
|
188
|
+
|
|
189
|
+
```ruby
|
|
190
|
+
memory = htm.retrieve("user_name")
|
|
191
|
+
|
|
192
|
+
if memory
|
|
193
|
+
puts "Found: #{memory['value']}"
|
|
194
|
+
puts "Type: #{memory['type']}"
|
|
195
|
+
puts "Created: #{memory['created_at']}"
|
|
196
|
+
puts "Importance: #{memory['importance']}"
|
|
197
|
+
end
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Recalling from the Past
|
|
201
|
+
|
|
202
|
+
Use HTM's RAG capabilities to recall relevant memories:
|
|
203
|
+
|
|
204
|
+
```ruby
|
|
205
|
+
# Recall memories about databases from the last week
|
|
206
|
+
memories = htm.recall(
|
|
207
|
+
timeframe: "last week",
|
|
208
|
+
topic: "database architecture",
|
|
209
|
+
limit: 10
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
memories.each do |memory|
|
|
213
|
+
puts "- #{memory['value']}"
|
|
214
|
+
puts " Similarity: #{memory['similarity']}"
|
|
215
|
+
puts
|
|
216
|
+
end
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
!!! note "How Recall Works"
|
|
220
|
+
HTM uses vector embeddings to understand the semantic meaning of your query. It finds memories that are conceptually related, not just keyword matches.
|
|
221
|
+
|
|
222
|
+
### Creating Context for LLMs
|
|
223
|
+
|
|
224
|
+
Assemble working memory into context for your LLM:
|
|
225
|
+
|
|
226
|
+
```ruby
|
|
227
|
+
# Get a balanced mix of important and recent memories
|
|
228
|
+
context = htm.create_context(
|
|
229
|
+
strategy: :balanced,
|
|
230
|
+
max_tokens: 50_000
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
# Use this context in your LLM prompt
|
|
234
|
+
prompt = <<~PROMPT
|
|
235
|
+
Context from memory:
|
|
236
|
+
#{context}
|
|
237
|
+
|
|
238
|
+
User: What database did we decide to use?
|
|
239
|
+
|
|
240
|
+
Assistant:
|
|
241
|
+
PROMPT
|
|
242
|
+
|
|
243
|
+
# Send to your LLM...
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Common Patterns
|
|
247
|
+
|
|
248
|
+
### Pattern 1: Session Memory
|
|
249
|
+
|
|
250
|
+
Store conversation turns as memories:
|
|
251
|
+
|
|
252
|
+
```ruby
|
|
253
|
+
class ConversationTracker
|
|
254
|
+
def initialize(session_id)
|
|
255
|
+
@htm = HTM.new(robot_name: "Chat-#{session_id}")
|
|
256
|
+
@turn = 0
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def add_turn(user_message, assistant_response)
|
|
260
|
+
@turn += 1
|
|
261
|
+
|
|
262
|
+
# Store user message
|
|
263
|
+
@htm.add_node(
|
|
264
|
+
"turn_#{@turn}_user",
|
|
265
|
+
user_message,
|
|
266
|
+
type: :context,
|
|
267
|
+
importance: 5.0,
|
|
268
|
+
tags: ["conversation", "user"]
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
# Store assistant response
|
|
272
|
+
@htm.add_node(
|
|
273
|
+
"turn_#{@turn}_assistant",
|
|
274
|
+
assistant_response,
|
|
275
|
+
type: :context,
|
|
276
|
+
importance: 5.0,
|
|
277
|
+
tags: ["conversation", "assistant"],
|
|
278
|
+
related_to: ["turn_#{@turn}_user"]
|
|
279
|
+
)
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
def recall_context
|
|
283
|
+
@htm.create_context(strategy: :recent, max_tokens: 10_000)
|
|
284
|
+
end
|
|
285
|
+
end
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### Pattern 2: Knowledge Base
|
|
289
|
+
|
|
290
|
+
Build a queryable knowledge base:
|
|
291
|
+
|
|
292
|
+
```ruby
|
|
293
|
+
class KnowledgeBase
|
|
294
|
+
def initialize
|
|
295
|
+
@htm = HTM.new(robot_name: "Knowledge Bot")
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
def add_fact(key, fact, category:, tags: [])
|
|
299
|
+
@htm.add_node(
|
|
300
|
+
key,
|
|
301
|
+
fact,
|
|
302
|
+
type: :fact,
|
|
303
|
+
category: category,
|
|
304
|
+
importance: 8.0,
|
|
305
|
+
tags: tags
|
|
306
|
+
)
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
def query(question)
|
|
310
|
+
# Search all time for relevant facts
|
|
311
|
+
@htm.recall(
|
|
312
|
+
timeframe: "last 10 years", # Effectively all memories
|
|
313
|
+
topic: question,
|
|
314
|
+
limit: 5
|
|
315
|
+
)
|
|
316
|
+
end
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
# Usage
|
|
320
|
+
kb = KnowledgeBase.new
|
|
321
|
+
kb.add_fact(
|
|
322
|
+
"ruby_version",
|
|
323
|
+
"Ruby 3.0 introduced Ractors for parallel execution",
|
|
324
|
+
category: "programming",
|
|
325
|
+
tags: ["ruby", "concurrency"]
|
|
326
|
+
)
|
|
327
|
+
|
|
328
|
+
results = kb.query("How does Ruby handle parallelism?")
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
### Pattern 3: Decision Journal
|
|
332
|
+
|
|
333
|
+
Track architectural decisions over time:
|
|
334
|
+
|
|
335
|
+
```ruby
|
|
336
|
+
class DecisionJournal
|
|
337
|
+
def initialize(project_name)
|
|
338
|
+
@htm = HTM.new(robot_name: "Architect-#{project_name}")
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
def record_decision(title, rationale, alternatives: [], tags: [])
|
|
342
|
+
key = "decision_#{Time.now.to_i}"
|
|
343
|
+
|
|
344
|
+
decision = <<~DECISION
|
|
345
|
+
Decision: #{title}
|
|
346
|
+
|
|
347
|
+
Rationale: #{rationale}
|
|
348
|
+
|
|
349
|
+
#{alternatives.any? ? "Alternatives considered: #{alternatives.join(', ')}" : ''}
|
|
350
|
+
DECISION
|
|
351
|
+
|
|
352
|
+
@htm.add_node(
|
|
353
|
+
key,
|
|
354
|
+
decision,
|
|
355
|
+
type: :decision,
|
|
356
|
+
importance: 9.0,
|
|
357
|
+
tags: tags + ["decision"]
|
|
358
|
+
)
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
def get_decision_history(topic)
|
|
362
|
+
@htm.recall(
|
|
363
|
+
timeframe: "last year",
|
|
364
|
+
topic: topic,
|
|
365
|
+
limit: 20
|
|
366
|
+
).sort_by { |d| d['created_at'] }
|
|
367
|
+
end
|
|
368
|
+
end
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
## Monitoring Memory Usage
|
|
372
|
+
|
|
373
|
+
Check working memory utilization:
|
|
374
|
+
|
|
375
|
+
```ruby
|
|
376
|
+
# Get current statistics
|
|
377
|
+
wm = htm.working_memory
|
|
378
|
+
puts "Nodes in working memory: #{wm.node_count}"
|
|
379
|
+
puts "Tokens used: #{wm.token_count} / #{wm.max_tokens}"
|
|
380
|
+
puts "Utilization: #{wm.utilization_percentage}%"
|
|
381
|
+
|
|
382
|
+
# Get comprehensive stats
|
|
383
|
+
stats = htm.memory_stats
|
|
384
|
+
puts "Total nodes in long-term: #{stats[:total_nodes]}"
|
|
385
|
+
puts "Active robots: #{stats[:active_robots]}"
|
|
386
|
+
puts "Database size: #{stats[:database_size] / (1024.0 * 1024.0)} MB"
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
!!! tip
|
|
390
|
+
Monitor working memory utilization regularly. If you consistently hit 100%, consider increasing `working_memory_size` or implementing more aggressive eviction strategies.
|
|
391
|
+
|
|
392
|
+
## Best Practices for Beginners
|
|
393
|
+
|
|
394
|
+
### 1. Use Meaningful Keys
|
|
395
|
+
|
|
396
|
+
```ruby
|
|
397
|
+
# Good: Descriptive, unique keys
|
|
398
|
+
htm.add_node("user_pref_theme_dark", "User prefers dark theme", ...)
|
|
399
|
+
|
|
400
|
+
# Bad: Generic keys that might conflict
|
|
401
|
+
htm.add_node("pref", "User prefers dark theme", ...)
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
### 2. Set Appropriate Importance
|
|
405
|
+
|
|
406
|
+
```ruby
|
|
407
|
+
# Critical facts: 9-10
|
|
408
|
+
htm.add_node("api_key", "API key is: ...", importance: 10.0)
|
|
409
|
+
|
|
410
|
+
# Important decisions: 7-9
|
|
411
|
+
htm.add_node("arch_001", "Using microservices", importance: 8.0)
|
|
412
|
+
|
|
413
|
+
# Contextual information: 4-6
|
|
414
|
+
htm.add_node("ctx_001", "Discussing weather", importance: 5.0)
|
|
415
|
+
|
|
416
|
+
# Temporary notes: 1-3
|
|
417
|
+
htm.add_node("note_001", "Remember to check logs", importance: 2.0)
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
### 3. Use Tags Liberally
|
|
421
|
+
|
|
422
|
+
```ruby
|
|
423
|
+
htm.add_node(
|
|
424
|
+
"decision_001",
|
|
425
|
+
"Chose PostgreSQL for data persistence",
|
|
426
|
+
type: :decision,
|
|
427
|
+
importance: 9.0,
|
|
428
|
+
tags: [
|
|
429
|
+
"database",
|
|
430
|
+
"architecture",
|
|
431
|
+
"backend",
|
|
432
|
+
"persistence",
|
|
433
|
+
"postgresql"
|
|
434
|
+
]
|
|
435
|
+
)
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
### 4. Leverage Relationships
|
|
439
|
+
|
|
440
|
+
```ruby
|
|
441
|
+
# Add related memories
|
|
442
|
+
htm.add_node("decision_db", "Use PostgreSQL", type: :decision)
|
|
443
|
+
|
|
444
|
+
htm.add_node(
|
|
445
|
+
"code_db_connect",
|
|
446
|
+
"Connection code for PostgreSQL",
|
|
447
|
+
type: :code,
|
|
448
|
+
related_to: ["decision_db"] # Link to the decision
|
|
449
|
+
)
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
### 5. Clean Up When Needed
|
|
453
|
+
|
|
454
|
+
```ruby
|
|
455
|
+
# Explicitly forget outdated information
|
|
456
|
+
htm.forget("old_api_key", confirm: :confirmed)
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
!!! warning
|
|
460
|
+
The `forget` method requires explicit confirmation to prevent accidental data loss. HTM never deletes memories automatically.
|
|
461
|
+
|
|
462
|
+
## Troubleshooting
|
|
463
|
+
|
|
464
|
+
### Ollama Connection Issues
|
|
465
|
+
|
|
466
|
+
If you see embedding errors:
|
|
467
|
+
|
|
468
|
+
```bash
|
|
469
|
+
# Check Ollama is running
|
|
470
|
+
curl http://localhost:11434/api/version
|
|
471
|
+
|
|
472
|
+
# If not running, start it
|
|
473
|
+
ollama serve
|
|
474
|
+
|
|
475
|
+
# Verify the model is available
|
|
476
|
+
ollama list
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
### Database Connection Issues
|
|
480
|
+
|
|
481
|
+
```ruby
|
|
482
|
+
# Test your connection
|
|
483
|
+
require 'htm'
|
|
484
|
+
|
|
485
|
+
begin
|
|
486
|
+
HTM::Database.setup
|
|
487
|
+
puts "Connection successful!"
|
|
488
|
+
rescue => e
|
|
489
|
+
puts "Connection failed: #{e.message}"
|
|
490
|
+
end
|
|
491
|
+
```
|
|
492
|
+
|
|
493
|
+
### Memory Not Found
|
|
494
|
+
|
|
495
|
+
```ruby
|
|
496
|
+
memory = htm.retrieve("my_key")
|
|
497
|
+
|
|
498
|
+
if memory.nil?
|
|
499
|
+
puts "Memory not found. Check the key spelling."
|
|
500
|
+
else
|
|
501
|
+
puts "Found: #{memory['value']}"
|
|
502
|
+
end
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
## Next Steps
|
|
506
|
+
|
|
507
|
+
Now that you understand the basics, explore these guides:
|
|
508
|
+
|
|
509
|
+
- [**Adding Memories**](adding-memories.md) - Deep dive into memory types and metadata
|
|
510
|
+
- [**Recalling Memories**](recalling-memories.md) - Master search strategies and retrieval
|
|
511
|
+
- [**Context Assembly**](context-assembly.md) - Optimize context for your LLM
|
|
512
|
+
- [**Working Memory**](working-memory.md) - Understand token limits and eviction
|
|
513
|
+
|
|
514
|
+
## Complete Example
|
|
515
|
+
|
|
516
|
+
Here's a complete working example combining everything:
|
|
517
|
+
|
|
518
|
+
```ruby
|
|
519
|
+
require 'htm'
|
|
520
|
+
|
|
521
|
+
# Initialize
|
|
522
|
+
htm = HTM.new(
|
|
523
|
+
robot_name: "Demo Bot",
|
|
524
|
+
working_memory_size: 128_000
|
|
525
|
+
)
|
|
526
|
+
|
|
527
|
+
# Add various memories
|
|
528
|
+
htm.add_node(
|
|
529
|
+
"user_001",
|
|
530
|
+
"User's name is Alice and she's a software engineer",
|
|
531
|
+
type: :fact,
|
|
532
|
+
importance: 8.0,
|
|
533
|
+
tags: ["user", "identity", "profession"]
|
|
534
|
+
)
|
|
535
|
+
|
|
536
|
+
htm.add_node(
|
|
537
|
+
"decision_001",
|
|
538
|
+
"Decided to use HTM for managing conversation memory",
|
|
539
|
+
type: :decision,
|
|
540
|
+
importance: 9.0,
|
|
541
|
+
tags: ["architecture", "memory"]
|
|
542
|
+
)
|
|
543
|
+
|
|
544
|
+
htm.add_node(
|
|
545
|
+
"pref_001",
|
|
546
|
+
"Alice prefers detailed explanations with examples",
|
|
547
|
+
type: :preference,
|
|
548
|
+
importance: 7.0,
|
|
549
|
+
tags: ["user", "communication"],
|
|
550
|
+
related_to: ["user_001"]
|
|
551
|
+
)
|
|
552
|
+
|
|
553
|
+
# Recall relevant memories
|
|
554
|
+
memories = htm.recall(
|
|
555
|
+
timeframe: "last 7 days",
|
|
556
|
+
topic: "user preferences",
|
|
557
|
+
limit: 5
|
|
558
|
+
)
|
|
559
|
+
|
|
560
|
+
puts "Found #{memories.length} relevant memories:"
|
|
561
|
+
memories.each do |m|
|
|
562
|
+
puts "- #{m['value']} (importance: #{m['importance']})"
|
|
563
|
+
end
|
|
564
|
+
|
|
565
|
+
# Create context for LLM
|
|
566
|
+
context = htm.create_context(strategy: :balanced)
|
|
567
|
+
puts "\nContext length: #{context.length} characters"
|
|
568
|
+
|
|
569
|
+
# Check stats
|
|
570
|
+
stats = htm.memory_stats
|
|
571
|
+
puts "\nMemory statistics:"
|
|
572
|
+
puts "- Total nodes: #{stats[:total_nodes]}"
|
|
573
|
+
puts "- Working memory: #{stats[:working_memory][:utilization]}% full"
|
|
574
|
+
puts "- Database size: #{(stats[:database_size] / 1024.0 / 1024.0).round(2)} MB"
|
|
575
|
+
```
|
|
576
|
+
|
|
577
|
+
Happy coding with HTM!
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# HTM User Guides
|
|
2
|
+
|
|
3
|
+
Welcome to the HTM (Hierarchical Temporary Memory) user guide collection. These guides will help you understand and effectively use HTM for building intelligent LLM-based applications with persistent memory.
|
|
4
|
+
|
|
5
|
+
## What is HTM?
|
|
6
|
+
|
|
7
|
+
HTM is an intelligent memory management system for LLM robots that implements a two-tier architecture:
|
|
8
|
+
|
|
9
|
+
- **Working Memory**: Token-limited active context for immediate LLM use
|
|
10
|
+
- **Long-term Memory**: Durable PostgreSQL storage for permanent knowledge
|
|
11
|
+
|
|
12
|
+
HTM enables your robots to recall context from past conversations using RAG (Retrieval-Augmented Generation), creating continuity across sessions and enabling sophisticated multi-robot collaboration.
|
|
13
|
+
|
|
14
|
+
## Guide Categories
|
|
15
|
+
|
|
16
|
+
### Getting Started
|
|
17
|
+
|
|
18
|
+
Perfect for developers new to HTM or those building their first application.
|
|
19
|
+
|
|
20
|
+
- [**Getting Started Guide**](getting-started.md) - Your first HTM application, basic concepts, and common patterns
|
|
21
|
+
|
|
22
|
+
### Core Operations
|
|
23
|
+
|
|
24
|
+
Learn how to work with HTM's memory system effectively.
|
|
25
|
+
|
|
26
|
+
- [**Adding Memories**](adding-memories.md) - How to store different types of information in HTM
|
|
27
|
+
- [**Recalling Memories**](recalling-memories.md) - Search strategies and retrieval techniques
|
|
28
|
+
- [**Working Memory Management**](working-memory.md) - Understanding token limits and eviction
|
|
29
|
+
- [**Long-term Memory**](long-term-memory.md) - Database operations and maintenance
|
|
30
|
+
|
|
31
|
+
### Advanced Features
|
|
32
|
+
|
|
33
|
+
Dive deeper into HTM's powerful capabilities.
|
|
34
|
+
|
|
35
|
+
- [**Multi-Robot Usage**](multi-robot.md) - Building hive mind systems with multiple robots
|
|
36
|
+
- [**Search Strategies**](search-strategies.md) - Vector, full-text, and hybrid search
|
|
37
|
+
- [**Context Assembly**](context-assembly.md) - Creating optimized context for LLMs
|
|
38
|
+
|
|
39
|
+
## Learning Path
|
|
40
|
+
|
|
41
|
+
We recommend the following progression:
|
|
42
|
+
|
|
43
|
+
1. **Start Here**: [Getting Started Guide](getting-started.md)
|
|
44
|
+
- Understand HTM's architecture
|
|
45
|
+
- Build your first application
|
|
46
|
+
- Learn basic operations
|
|
47
|
+
|
|
48
|
+
2. **Core Skills**: Memory Operations
|
|
49
|
+
- [Adding Memories](adding-memories.md) - Store information effectively
|
|
50
|
+
- [Recalling Memories](recalling-memories.md) - Retrieve what you need
|
|
51
|
+
- [Context Assembly](context-assembly.md) - Use memories with LLMs
|
|
52
|
+
|
|
53
|
+
3. **Deep Understanding**: Memory Management
|
|
54
|
+
- [Working Memory](working-memory.md) - Token management
|
|
55
|
+
- [Long-term Memory](long-term-memory.md) - Database operations
|
|
56
|
+
- [Search Strategies](search-strategies.md) - Optimize retrieval
|
|
57
|
+
|
|
58
|
+
4. **Advanced Topics**: Multi-Robot Systems
|
|
59
|
+
- [Multi-Robot Usage](multi-robot.md) - Build collaborative systems
|
|
60
|
+
|
|
61
|
+
## Quick Reference
|
|
62
|
+
|
|
63
|
+
### Common Tasks
|
|
64
|
+
|
|
65
|
+
- **Initialize HTM**: See [Getting Started](getting-started.md#basic-initialization)
|
|
66
|
+
- **Add a memory**: See [Adding Memories](adding-memories.md#basic-usage)
|
|
67
|
+
- **Search for memories**: See [Recalling Memories](recalling-memories.md#basic-recall)
|
|
68
|
+
- **Create LLM context**: See [Context Assembly](context-assembly.md#basic-usage)
|
|
69
|
+
- **Monitor memory usage**: See [Working Memory](working-memory.md#monitoring-utilization)
|
|
70
|
+
- **Multi-robot setup**: See [Multi-Robot Usage](multi-robot.md#setting-up-multiple-robots)
|
|
71
|
+
|
|
72
|
+
### Memory Types
|
|
73
|
+
|
|
74
|
+
HTM supports six memory types, each optimized for different use cases:
|
|
75
|
+
|
|
76
|
+
| Type | Purpose | Example |
|
|
77
|
+
|------|---------|---------|
|
|
78
|
+
| `:fact` | Immutable facts | "User's name is Alice" |
|
|
79
|
+
| `:context` | Conversation state | "Discussing database architecture" |
|
|
80
|
+
| `:code` | Code snippets | "Ruby function for parsing dates" |
|
|
81
|
+
| `:preference` | User preferences | "Prefers dark theme" |
|
|
82
|
+
| `:decision` | Design decisions | "Chose PostgreSQL for storage" |
|
|
83
|
+
| `:question` | Unresolved questions | "Should we add caching?" |
|
|
84
|
+
|
|
85
|
+
### Search Strategies
|
|
86
|
+
|
|
87
|
+
| Strategy | Method | Best For |
|
|
88
|
+
|----------|--------|----------|
|
|
89
|
+
| Vector | Semantic similarity | Conceptual searches, related topics |
|
|
90
|
+
| Full-text | Keyword matching | Exact terms, specific phrases |
|
|
91
|
+
| Hybrid | Combined approach | Best overall accuracy |
|
|
92
|
+
|
|
93
|
+
## Getting Help
|
|
94
|
+
|
|
95
|
+
- **Examples**: Check the `examples/` directory in the HTM repository
|
|
96
|
+
- **API Reference**: See the [API documentation](../api/index.md)
|
|
97
|
+
- **Tests**: Look at `test/` directory for usage examples
|
|
98
|
+
- **Issues**: Report bugs on [GitHub](https://github.com/madbomber/htm/issues)
|
|
99
|
+
|
|
100
|
+
## Documentation Conventions
|
|
101
|
+
|
|
102
|
+
Throughout these guides, you'll see these admonitions:
|
|
103
|
+
|
|
104
|
+
!!! tip
|
|
105
|
+
Helpful advice and best practices
|
|
106
|
+
|
|
107
|
+
!!! warning
|
|
108
|
+
Important warnings about potential issues
|
|
109
|
+
|
|
110
|
+
!!! note
|
|
111
|
+
Additional information and context
|
|
112
|
+
|
|
113
|
+
!!! example
|
|
114
|
+
Code examples and usage demonstrations
|
|
115
|
+
|
|
116
|
+
## Next Steps
|
|
117
|
+
|
|
118
|
+
Ready to get started? Head over to the [Getting Started Guide](getting-started.md) to build your first HTM application.
|