llmemory 0.1.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.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +193 -0
  4. data/lib/generators/llmemory/install/install_generator.rb +24 -0
  5. data/lib/generators/llmemory/install/templates/create_llmemory_tables.rb +73 -0
  6. data/lib/llmemory/configuration.rb +51 -0
  7. data/lib/llmemory/extractors/entity_relation_extractor.rb +74 -0
  8. data/lib/llmemory/extractors/fact_extractor.rb +74 -0
  9. data/lib/llmemory/extractors.rb +9 -0
  10. data/lib/llmemory/llm/anthropic.rb +48 -0
  11. data/lib/llmemory/llm/base.rb +17 -0
  12. data/lib/llmemory/llm/openai.rb +46 -0
  13. data/lib/llmemory/llm.rb +18 -0
  14. data/lib/llmemory/long_term/file_based/category.rb +22 -0
  15. data/lib/llmemory/long_term/file_based/item.rb +31 -0
  16. data/lib/llmemory/long_term/file_based/memory.rb +83 -0
  17. data/lib/llmemory/long_term/file_based/resource.rb +22 -0
  18. data/lib/llmemory/long_term/file_based/retrieval.rb +90 -0
  19. data/lib/llmemory/long_term/file_based/storage.rb +35 -0
  20. data/lib/llmemory/long_term/file_based/storages/active_record_models.rb +26 -0
  21. data/lib/llmemory/long_term/file_based/storages/active_record_storage.rb +144 -0
  22. data/lib/llmemory/long_term/file_based/storages/base.rb +71 -0
  23. data/lib/llmemory/long_term/file_based/storages/database_storage.rb +231 -0
  24. data/lib/llmemory/long_term/file_based/storages/file_storage.rb +180 -0
  25. data/lib/llmemory/long_term/file_based/storages/memory_storage.rb +100 -0
  26. data/lib/llmemory/long_term/file_based.rb +15 -0
  27. data/lib/llmemory/long_term/graph_based/conflict_resolver.rb +33 -0
  28. data/lib/llmemory/long_term/graph_based/edge.rb +49 -0
  29. data/lib/llmemory/long_term/graph_based/knowledge_graph.rb +114 -0
  30. data/lib/llmemory/long_term/graph_based/memory.rb +143 -0
  31. data/lib/llmemory/long_term/graph_based/node.rb +42 -0
  32. data/lib/llmemory/long_term/graph_based/storage.rb +24 -0
  33. data/lib/llmemory/long_term/graph_based/storages/active_record_models.rb +23 -0
  34. data/lib/llmemory/long_term/graph_based/storages/active_record_storage.rb +132 -0
  35. data/lib/llmemory/long_term/graph_based/storages/base.rb +39 -0
  36. data/lib/llmemory/long_term/graph_based/storages/memory_storage.rb +106 -0
  37. data/lib/llmemory/long_term/graph_based.rb +15 -0
  38. data/lib/llmemory/long_term.rb +9 -0
  39. data/lib/llmemory/maintenance/consolidator.rb +55 -0
  40. data/lib/llmemory/maintenance/reindexer.rb +27 -0
  41. data/lib/llmemory/maintenance/runner.rb +34 -0
  42. data/lib/llmemory/maintenance/summarizer.rb +57 -0
  43. data/lib/llmemory/maintenance.rb +8 -0
  44. data/lib/llmemory/memory.rb +96 -0
  45. data/lib/llmemory/retrieval/context_assembler.rb +53 -0
  46. data/lib/llmemory/retrieval/engine.rb +74 -0
  47. data/lib/llmemory/retrieval/temporal_ranker.rb +23 -0
  48. data/lib/llmemory/retrieval.rb +10 -0
  49. data/lib/llmemory/short_term/checkpoint.rb +47 -0
  50. data/lib/llmemory/short_term/stores/active_record_checkpoint.rb +14 -0
  51. data/lib/llmemory/short_term/stores/active_record_store.rb +58 -0
  52. data/lib/llmemory/short_term/stores/base.rb +21 -0
  53. data/lib/llmemory/short_term/stores/memory_store.rb +37 -0
  54. data/lib/llmemory/short_term/stores/postgres_store.rb +80 -0
  55. data/lib/llmemory/short_term/stores/redis_store.rb +54 -0
  56. data/lib/llmemory/short_term.rb +8 -0
  57. data/lib/llmemory/vector_store/base.rb +19 -0
  58. data/lib/llmemory/vector_store/memory_store.rb +53 -0
  59. data/lib/llmemory/vector_store/openai_embeddings.rb +49 -0
  60. data/lib/llmemory/vector_store.rb +10 -0
  61. data/lib/llmemory/version.rb +5 -0
  62. data/lib/llmemory.rb +19 -0
  63. metadata +163 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 84ebe29d1042b2efa07d02ded2a5374bbdcc1b0bad195a90d6cc5a15c1a2f73a
4
+ data.tar.gz: 6b6c0157acc13d89a07b9491c206a5a132d4c1e9c3c63a08f1810dd39681a067
5
+ SHA512:
6
+ metadata.gz: cfd74b6f30e3d049de41596344a15c7b7734794a146a22e9dc2ad61153eb8fa584982efe1a4969bbe24fdbe2a079f46f8f42aeabd1e90e6f78f65d511fbc10df
7
+ data.tar.gz: 644dd2c11dbcdb40486d4a1b3cc33e5c13aa0ae0f9616358b4aff069d7b0ac7a04a263bb09a98657e58bcbefd11def65c7c5c711bbf6be3d36645512367439e3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 llmemory
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,193 @@
1
+ # llmemory
2
+
3
+ Persistent memory system for LLM agents. Implements short-term checkpointing, long-term memory (file-based or **graph-based**), retrieval with time decay, and maintenance jobs.
4
+
5
+ ## Installation
6
+
7
+ Add to your Gemfile:
8
+
9
+ ```ruby
10
+ gem "llmemory"
11
+ ```
12
+
13
+ Then run `bundle install`.
14
+
15
+ ## Quick Start (Unified API)
16
+
17
+ The recommended way to use llmemory in a chat is the unified `Llmemory::Memory` API. It abstracts short-term (conversation history) and long-term (extracted facts) and combines retrieval from both:
18
+
19
+ ```ruby
20
+ # File-based long-term (default): facts and categories
21
+ memory = Llmemory::Memory.new(user_id: "user_123", session_id: "conv_456")
22
+
23
+ # Or graph-based long-term: entities and relations (knowledge graph + vector search)
24
+ memory = Llmemory::Memory.new(user_id: "user_123", session_id: "conv_456", long_term_type: :graph_based)
25
+
26
+ # Add user and assistant messages
27
+ memory.add_message(role: :user, content: "Soy vegano y trabajo en OpenAI")
28
+ memory.add_message(role: :assistant, content: "Entendido, lo recordaré")
29
+
30
+ # Get full context for the next LLM call (recent conversation + relevant long-term memories)
31
+ context = memory.retrieve("¿Qué preferencias tiene el usuario?", max_tokens: 2000)
32
+
33
+ # Optionally consolidate current conversation into long-term (extract facts)
34
+ memory.consolidate!
35
+
36
+ # Clear session (short-term) while keeping long-term intact
37
+ memory.clear_session!
38
+ ```
39
+
40
+ - **`add_message(role:, content:)`** — Persists messages in short-term.
41
+ - **`messages`** — Returns the current conversation history.
42
+ - **`retrieve(query, max_tokens: nil)`** — Returns combined context: recent conversation + relevant long-term memories.
43
+ - **`consolidate!`** — Extracts facts from the current conversation and stores them in long-term.
44
+ - **`clear_session!`** — Clears short-term only.
45
+
46
+ ## Configuration
47
+
48
+ ```ruby
49
+ Llmemory.configure do |config|
50
+ config.llm_provider = :openai
51
+ config.llm_api_key = ENV["OPENAI_API_KEY"]
52
+ config.llm_model = "gpt-4"
53
+ config.short_term_store = :memory # or :redis, :postgres, :active_record
54
+ config.redis_url = ENV["REDIS_URL"] # for :redis
55
+ config.long_term_type = :file_based # or :graph_based (entities + relations)
56
+ config.long_term_store = :memory # or :file, :postgres, :active_record
57
+ config.long_term_storage_path = "./llmemory_data" # for :file
58
+ config.database_url = ENV["DATABASE_URL"] # for :postgres
59
+ config.time_decay_half_life_days = 30
60
+ config.max_retrieval_tokens = 2000
61
+ config.prune_after_days = 90
62
+ end
63
+ ```
64
+
65
+ ## Long-Term Storage
66
+
67
+ Long-term memory can use different backends:
68
+
69
+ | Store | Class | Use case |
70
+ |------------------|-----------------------------|-----------------------------------|
71
+ | `:memory` | `Storages::MemoryStorage` | Default; in-memory, lost on exit |
72
+ | `:file` | `Storages::FileStorage` | Persist to disk (directory per user) |
73
+ | `:postgres` | `Storages::DatabaseStorage` | PostgreSQL (tables created automatically) |
74
+ | `:active_record` | `Storages::ActiveRecordStorage` | Rails: usa ActiveRecord y tu DB existente |
75
+
76
+ Set `config.long_term_store = :file`, `:postgres` or `:active_record` so that `Llmemory::Memory` and `FileBased::Memory` use it when no `storage:` is passed.
77
+
78
+ **Long-term type:** use `long_term_type: :graph_based` in `Llmemory::Memory.new(...)` for entity/relation memory (knowledge graph + hybrid retrieval). See [Long-Term Memory (Graph-Based)](#long-term-memory-graph-based) below.
79
+
80
+ **Rails (ActiveRecord):** añade `activerecord` a tu Gemfile si no está. Luego:
81
+
82
+ ```bash
83
+ rails g llmemory:install
84
+ rails db:migrate
85
+ ```
86
+
87
+ La migración crea las tablas de long-term file-based (resources, items, categories), short-term (checkpoints) y, para graph-based, nodos, aristas y embeddings (`llmemory_nodes`, `llmemory_edges`, `llmemory_embeddings`). Para embeddings se usa pgvector; asegúrate de tener la extensión `vector` en PostgreSQL. Para usar ambas con ActiveRecord:
88
+
89
+ ```ruby
90
+ # config/application.rb o config/initializers/llmemory.rb
91
+ Llmemory.configure do |config|
92
+ config.short_term_store = :active_record # historial de conversación en DB
93
+ config.long_term_store = :active_record # hechos extraídos en DB
94
+ # ... llm, etc.
95
+ end
96
+ ```
97
+
98
+ Explicit storage:
99
+
100
+ ```ruby
101
+ storage = Llmemory::LongTerm::FileBased::Storages.build(store: :file, base_path: "./data/llmemory")
102
+ memory = Llmemory::LongTerm::FileBased::Memory.new(user_id: "u1", storage: storage)
103
+
104
+ storage = Llmemory::LongTerm::FileBased::Storages.build(store: :postgres, database_url: ENV["DATABASE_URL"])
105
+ memory = Llmemory::LongTerm::FileBased::Memory.new(user_id: "u1", storage: storage)
106
+
107
+ # Rails
108
+ storage = Llmemory::LongTerm::FileBased::Storages.build(store: :active_record)
109
+ memory = Llmemory::LongTerm::FileBased::Memory.new(user_id: "u1", storage: storage)
110
+ ```
111
+
112
+ ## Long-Term Memory (Graph-Based)
113
+
114
+ When you need **entities and relations** (e.g. “User works_at OpenAI”, “User prefers Ruby”) instead of flat facts and categories, use graph-based long-term memory. It combines:
115
+
116
+ - **Knowledge graph** — Nodes (entities) and edges (subject–predicate–object relations).
117
+ - **Vector store** — Embeddings (e.g. OpenAI `text-embedding-3-small`) for semantic search.
118
+ - **Hybrid retrieval** — Vector search + graph traversal from matched nodes, then merged and ranked.
119
+ - **Conflict resolution** — Exclusive predicates (e.g. `works_at`, `lives_in`) archive previous values when a new one is stored.
120
+
121
+ ### Unified API with graph-based
122
+
123
+ ```ruby
124
+ memory = Llmemory::Memory.new(
125
+ user_id: "user_123",
126
+ session_id: "conv_456",
127
+ long_term_type: :graph_based
128
+ )
129
+ memory.add_message(role: :user, content: "Trabajo en Acme y vivo en Madrid")
130
+ memory.consolidate!
131
+ context = memory.retrieve("¿Dónde trabaja el usuario?")
132
+ ```
133
+
134
+ ### Lower-level graph-based API
135
+
136
+ ```ruby
137
+ storage = Llmemory::LongTerm::GraphBased::Storages.build(store: :memory) # or :active_record
138
+ vector_store = Llmemory::VectorStore::MemoryStore.new(
139
+ embedding_provider: Llmemory::VectorStore::OpenAIEmbeddings.new
140
+ )
141
+ memory = Llmemory::LongTerm::GraphBased::Memory.new(
142
+ user_id: "user_123",
143
+ storage: storage,
144
+ vector_store: vector_store
145
+ )
146
+ memory.memorize("User works at Acme. User lives in Madrid.")
147
+ context = memory.retrieve("where does user work", top_k: 10)
148
+ candidates = memory.search_candidates("job", top_k: 20)
149
+ ```
150
+
151
+ - **`memorize(conversation_text)`** — LLM extracts entities and relations (SPO triplets), upserts nodes/edges, resolves conflicts, and stores relation text in the vector store.
152
+ - **`retrieve(query, top_k:)`** — Hybrid search: vector similarity + graph traversal; returns formatted context string.
153
+ - **`search_candidates(query, user_id:, top_k:)`** — Used by `Retrieval::Engine`; returns `[{ text:, timestamp:, score: }]`.
154
+
155
+ **Graph storage:** `:memory` (in-memory) or `:active_record` (Rails). For ActiveRecord, run `rails g llmemory:install` and migrate; the migration creates `llmemory_nodes`, `llmemory_edges`, and `llmemory_embeddings` (pgvector). Enable the `vector` extension in PostgreSQL for embeddings.
156
+
157
+ ## Lower-Level APIs
158
+
159
+ ### Short-Term Memory (Checkpointing)
160
+
161
+ ```ruby
162
+ checkpoint = Llmemory::ShortTerm::Checkpoint.new(user_id: "user_123")
163
+ checkpoint.save_state(conversation_state)
164
+ state = checkpoint.restore_state
165
+ ```
166
+
167
+ ### Long-Term Memory (File-Based)
168
+
169
+ ```ruby
170
+ memory = Llmemory::LongTerm::FileBased::Memory.new(user_id: "user_123")
171
+ # or with explicit storage: storage: Llmemory::LongTerm::FileBased::Storages.build(store: :file)
172
+ memory.memorize(conversation_text)
173
+ context = memory.retrieve(query)
174
+ ```
175
+
176
+ ### Retrieval Engine
177
+
178
+ ```ruby
179
+ retrieval = Llmemory::Retrieval::Engine.new(long_term_memory)
180
+ context = retrieval.retrieve_for_inference(user_message, max_tokens: 2000)
181
+ ```
182
+
183
+ ### Maintenance
184
+
185
+ ```ruby
186
+ Llmemory::Maintenance::Runner.run_nightly(user_id, storage: memory.storage)
187
+ Llmemory::Maintenance::Runner.run_weekly(user_id, storage: memory.storage)
188
+ Llmemory::Maintenance::Runner.run_monthly(user_id, storage: memory.storage)
189
+ ```
190
+
191
+ ## License
192
+
193
+ MIT. See [LICENSE.txt](LICENSE.txt).
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators/migration"
4
+
5
+ module Llmemory
6
+ module Generators
7
+ class InstallGenerator < ::Rails::Generators::Base
8
+ include Rails::Generators::Migration
9
+
10
+ source_root File.expand_path("templates", __dir__)
11
+
12
+ desc "Create migration for llmemory long-term storage (ActiveRecord)"
13
+
14
+ def self.next_migration_number(dirname)
15
+ next_migration_number = current_migration_number(dirname) + 1
16
+ ActiveRecord::Migration.next_migration_number(next_migration_number)
17
+ end
18
+
19
+ def copy_migration
20
+ migration_template "create_llmemory_tables.rb", "db/migrate/create_llmemory_tables.rb"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateLlmemoryTables < ActiveRecord::Migration[7.0]
4
+ def change
5
+ create_table :llmemory_resources, id: false do |t|
6
+ t.string :id, null: false, primary_key: true
7
+ t.string :user_id, null: false
8
+ t.text :text, null: false
9
+ t.timestamps
10
+ end
11
+ add_index :llmemory_resources, :user_id
12
+
13
+ create_table :llmemory_items, id: false do |t|
14
+ t.string :id, null: false, primary_key: true
15
+ t.string :user_id, null: false
16
+ t.string :category, null: false
17
+ t.text :content, null: false
18
+ t.string :source_resource_id
19
+ t.timestamps
20
+ end
21
+ add_index :llmemory_items, :user_id
22
+
23
+ create_table :llmemory_categories do |t|
24
+ t.string :user_id, null: false
25
+ t.string :category_name, null: false
26
+ t.text :content, null: false
27
+ t.datetime :updated_at, null: false
28
+ end
29
+ add_index :llmemory_categories, [:user_id, :category_name], unique: true
30
+
31
+ create_table :llmemory_checkpoints do |t|
32
+ t.string :user_id, null: false
33
+ t.string :session_id, null: false
34
+ t.jsonb :state, null: false, default: {}
35
+ t.timestamps
36
+ end
37
+ add_index :llmemory_checkpoints, [:user_id, :session_id], unique: true
38
+
39
+ # Graph-based long-term memory (nodes = entities)
40
+ create_table :llmemory_nodes do |t|
41
+ t.string :user_id, null: false
42
+ t.string :entity_type, null: false
43
+ t.string :name, null: false
44
+ t.jsonb :properties, default: {}
45
+ t.timestamps
46
+ end
47
+ add_index :llmemory_nodes, [:user_id, :entity_type, :name], unique: true
48
+
49
+ # Graph-based long-term memory (edges = SPO relations)
50
+ create_table :llmemory_edges do |t|
51
+ t.string :user_id, null: false
52
+ t.references :subject, null: false, foreign_key: { to_table: :llmemory_nodes }
53
+ t.string :predicate, null: false
54
+ t.references :object, null: false, foreign_key: { to_table: :llmemory_nodes }
55
+ t.jsonb :properties, default: {}
56
+ t.datetime :archived_at
57
+ t.timestamps
58
+ end
59
+ add_index :llmemory_edges, [:user_id, :subject_id, :predicate]
60
+
61
+ # Vector store for hybrid retrieval (requires pgvector extension)
62
+ enable_extension "vector"
63
+ create_table :llmemory_embeddings do |t|
64
+ t.string :user_id, null: false
65
+ t.string :source_type, null: false
66
+ t.string :source_id, null: false
67
+ t.vector :embedding, limit: 1536
68
+ t.text :text_content
69
+ t.timestamps
70
+ end
71
+ add_index :llmemory_embeddings, [:user_id, :source_type, :source_id], unique: true
72
+ end
73
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Llmemory
4
+ class Configuration
5
+ attr_accessor :llm_provider,
6
+ :llm_api_key,
7
+ :llm_model,
8
+ :llm_base_url,
9
+ :short_term_store,
10
+ :redis_url,
11
+ :long_term_type,
12
+ :long_term_store,
13
+ :long_term_storage_path,
14
+ :database_url,
15
+ :vector_store,
16
+ :time_decay_half_life_days,
17
+ :max_retrieval_tokens,
18
+ :prune_after_days
19
+
20
+ def initialize
21
+ @llm_provider = :openai
22
+ @llm_api_key = ENV["OPENAI_API_KEY"]
23
+ @llm_model = "gpt-4"
24
+ @llm_base_url = nil
25
+ @short_term_store = :memory
26
+ @redis_url = ENV["REDIS_URL"] || "redis://localhost:6379/0"
27
+ @long_term_type = :file_based
28
+ @long_term_store = :memory
29
+ @long_term_storage_path = ENV["LLMEMORY_STORAGE_PATH"] || "./llmemory_data"
30
+ @database_url = ENV["DATABASE_URL"]
31
+ @vector_store = nil
32
+ @time_decay_half_life_days = 30
33
+ @max_retrieval_tokens = 2000
34
+ @prune_after_days = 90
35
+ end
36
+ end
37
+
38
+ class << self
39
+ def configuration
40
+ @configuration ||= Configuration.new
41
+ end
42
+
43
+ def configure
44
+ yield configuration
45
+ end
46
+
47
+ def reset_configuration!
48
+ @configuration = Configuration.new
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Llmemory
6
+ module Extractors
7
+ class EntityRelationExtractor
8
+ def initialize(llm: nil)
9
+ @llm = llm || Llmemory::LLM.client
10
+ end
11
+
12
+ def extract(conversation_text)
13
+ prompt = <<~PROMPT
14
+ Extract entities and relations from this conversation as a knowledge graph.
15
+ - Entities: people, companies, places, preferences, concepts (type and name).
16
+ - Relations: subject-predicate-object triplets (e.g. User works_at OpenAI).
17
+ Use "User" as subject when the user talks about themselves.
18
+ Predicates: works_at, lives_in, prefers, is_allergic_to, likes, knows, current_job, current_city, etc.
19
+ Return ONLY valid JSON with this shape:
20
+ {"entities": [{"type": "person", "name": "User"}, {"type": "company", "name": "OpenAI"}], "relations": [{"subject": "User", "predicate": "works_at", "object": "OpenAI"}]}
21
+ Conversation:
22
+ #{conversation_text}
23
+ PROMPT
24
+ response = @llm.invoke(prompt.strip)
25
+ parse_response(response)
26
+ end
27
+
28
+ private
29
+
30
+ def parse_response(response)
31
+ json = extract_json(response)
32
+ return { entities: [], relations: [] } unless json.is_a?(Hash)
33
+ entities = Array(json["entities"] || json[:entities]).map { |e| normalize_entity(e) }
34
+ relations = Array(json["relations"] || json[:relations]).map { |r| normalize_relation(r) }
35
+ { entities: entities, relations: relations }
36
+ end
37
+
38
+ def extract_json(response)
39
+ response = response.to_s.strip
40
+ start_idx = response.index("{")
41
+ return nil unless start_idx
42
+ depth = 0
43
+ end_idx = nil
44
+ response.each_char.with_index(start_idx) do |c, i|
45
+ depth += 1 if c == "{"
46
+ depth -= 1 if c == "}"
47
+ if depth == 0
48
+ end_idx = i
49
+ break
50
+ end
51
+ end
52
+ return nil unless end_idx
53
+ JSON.parse(response[start_idx..end_idx])
54
+ rescue JSON::ParserError
55
+ nil
56
+ end
57
+
58
+ def normalize_entity(e)
59
+ {
60
+ type: (e["type"] || e[:type] || "concept").to_s.downcase,
61
+ name: (e["name"] || e[:name]).to_s.strip
62
+ }
63
+ end
64
+
65
+ def normalize_relation(r)
66
+ {
67
+ subject: (r["subject"] || r[:subject]).to_s.strip,
68
+ predicate: (r["predicate"] || r[:predicate]).to_s.strip.downcase.gsub(/\s+/, "_"),
69
+ object: (r["object"] || r[:object]).to_s.strip
70
+ }
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Llmemory
6
+ module Extractors
7
+ class FactExtractor
8
+ def initialize(llm: nil)
9
+ @llm = llm || Llmemory::LLM.client
10
+ end
11
+
12
+ def extract_items(conversation_text)
13
+ prompt = <<~PROMPT
14
+ Extract discrete facts from this conversation.
15
+ Focus on preferences, behaviors, and important details.
16
+ Conversation: #{conversation_text}
17
+ Return as JSON array of objects with "content" key. Example: [{"content": "User prefers Ruby"}, {"content": "User is vegan"}]
18
+ PROMPT
19
+ response = @llm.invoke(prompt.strip)
20
+ parse_items_response(response)
21
+ end
22
+
23
+ def evolve_summary(existing:, new_memories:)
24
+ memory_list_text = Array(new_memories).map { |m| "- #{m}" }.join("\n")
25
+ prompt = <<~PROMPT
26
+ You are a Memory Synchronization Specialist.
27
+ Topic Scope: User Profile
28
+
29
+ ## Original Profile
30
+ #{existing.to_s.empty? ? "No existing profile." : existing}
31
+
32
+ ## New Memory Items to Integrate
33
+ #{memory_list_text}
34
+
35
+ # Task
36
+ 1. Update: If new items conflict with the Original Profile, overwrite the old facts.
37
+ 2. Add: If items are new, append them logically.
38
+ 3. Output: Return ONLY the updated markdown profile.
39
+ PROMPT
40
+ @llm.invoke(prompt.strip).to_s
41
+ end
42
+
43
+ def classify_item(content)
44
+ return "general" if content.to_s.strip.empty?
45
+ prompt = <<~PROMPT
46
+ Classify this fact into ONE category. Use lowercase with underscores. Examples: work_life, personal_life, preferences, general.
47
+ Fact: #{content}
48
+ Return ONLY the category name, nothing else.
49
+ PROMPT
50
+ result = @llm.invoke(prompt.strip).to_s.strip.downcase.gsub(/\s+/, "_")
51
+ result.empty? ? "general" : result
52
+ end
53
+
54
+ private
55
+
56
+ def parse_items_response(response)
57
+ json = extract_json_array(response)
58
+ return [] unless json
59
+ json.map { |item| item.is_a?(Hash) ? item : { "content" => item.to_s } }
60
+ end
61
+
62
+ def extract_json_array(response)
63
+ response = response.to_s.strip
64
+ start_idx = response.index("[")
65
+ return nil unless start_idx
66
+ end_idx = response.rindex("]")
67
+ return nil unless end_idx
68
+ JSON.parse(response[start_idx..end_idx])
69
+ rescue JSON::ParserError
70
+ nil
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "extractors/fact_extractor"
4
+ require_relative "extractors/entity_relation_extractor"
5
+
6
+ module Llmemory
7
+ module Extractors
8
+ end
9
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "json"
5
+ require_relative "base"
6
+
7
+ module Llmemory
8
+ module LLM
9
+ class Anthropic < Base
10
+ DEFAULT_BASE_URL = "https://api.anthropic.com"
11
+
12
+ def initialize(api_key: nil, model: nil, base_url: nil)
13
+ @api_key = api_key || config.llm_api_key || ENV["ANTHROPIC_API_KEY"]
14
+ @model = model || config.llm_model || "claude-3-sonnet-20240229"
15
+ @base_url = base_url || config.llm_base_url || DEFAULT_BASE_URL
16
+ end
17
+
18
+ def invoke(prompt)
19
+ response = connection.post("v1/messages") do |req|
20
+ req.body = {
21
+ model: @model,
22
+ max_tokens: 1024,
23
+ messages: [{ role: "user", content: prompt }]
24
+ }.to_json
25
+ req.headers["Content-Type"] = "application/json"
26
+ req.headers["x-api-key"] = @api_key
27
+ req.headers["anthropic-version"] = "2023-06-01"
28
+ end
29
+
30
+ raise Llmemory::LLMError, "Anthropic API error: #{response.body}" unless response.success?
31
+
32
+ body = response.body.is_a?(Hash) ? response.body : JSON.parse(response.body.to_s)
33
+ content = body.dig("content", 0, "text")
34
+ content&.strip || ""
35
+ end
36
+
37
+ private
38
+
39
+ def connection
40
+ @connection ||= Faraday.new(url: @base_url) do |f|
41
+ f.request :json
42
+ f.response :json
43
+ f.adapter Faraday.default_adapter
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Llmemory
4
+ module LLM
5
+ class Base
6
+ def invoke(prompt)
7
+ raise NotImplementedError, "#{self.class}#invoke must be implemented"
8
+ end
9
+
10
+ protected
11
+
12
+ def config
13
+ Llmemory.configuration
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "json"
5
+ require_relative "base"
6
+
7
+ module Llmemory
8
+ module LLM
9
+ class OpenAI < Base
10
+ DEFAULT_BASE_URL = "https://api.openai.com/v1"
11
+
12
+ def initialize(api_key: nil, model: nil, base_url: nil)
13
+ @api_key = api_key || config.llm_api_key
14
+ @model = model || config.llm_model
15
+ @base_url = base_url || config.llm_base_url || DEFAULT_BASE_URL
16
+ end
17
+
18
+ def invoke(prompt)
19
+ response = connection.post("chat/completions") do |req|
20
+ req.body = {
21
+ model: @model,
22
+ messages: [{ role: "user", content: prompt }],
23
+ temperature: 0.3
24
+ }.to_json
25
+ req.headers["Content-Type"] = "application/json"
26
+ req.headers["Authorization"] = "Bearer #{@api_key}"
27
+ end
28
+
29
+ raise Llmemory::LLMError, "OpenAI API error: #{response.body}" unless response.success?
30
+
31
+ body = response.body.is_a?(Hash) ? response.body : JSON.parse(response.body.to_s)
32
+ body.dig("choices", 0, "message", "content")&.strip || ""
33
+ end
34
+
35
+ private
36
+
37
+ def connection
38
+ @connection ||= Faraday.new(url: @base_url) do |f|
39
+ f.request :json
40
+ f.response :json
41
+ f.adapter Faraday.default_adapter
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "llm/base"
4
+ require_relative "llm/openai"
5
+ require_relative "llm/anthropic"
6
+
7
+ module Llmemory
8
+ module LLM
9
+ def self.client
10
+ case Llmemory.configuration.llm_provider.to_sym
11
+ when :openai then OpenAI.new
12
+ when :anthropic then Anthropic.new
13
+ else
14
+ raise Llmemory::ConfigurationError, "Unknown LLM provider: #{Llmemory.configuration.llm_provider}"
15
+ end
16
+ end
17
+ end
18
+ end