Class: Phronomy::KnowledgeSource::EntityKnowledge
- Defined in:
- lib/phronomy/knowledge_source/entity_knowledge.rb
Overview
A KnowledgeSource that extracts named-entity facts from conversation history.
This is the knowledge-injection counterpart of the old EntityMemory. It scans saved user messages with a regex heuristic (no LLM call) and returns the discovered facts as a single knowledge chunk tagged :entity.
EntityKnowledge is stateful: it accumulates extracted facts via #update(messages:) which should be called each time new messages are saved.
Supported extraction patterns (case-insensitive): "my name is Alice" → { name: "Alice" } "I am Alice" → { identity: "Alice" } "I'm a software engineer" → { occupation: "software engineer" } "I work at / for Acme" → { workplace: "Acme" } "I live in Tokyo" → { location: "Tokyo" } "I'm from Tokyo" → { location: "Tokyo" } "I like / love Ruby" → { preference: "Ruby" }
Constant Summary collapse
- PATTERNS =
[ [:name, /\bmy name is\s+([A-Za-z][A-Za-z0-9 \-']*)/i], [:identity, /\bI\s+am\s+([A-Z][A-Za-z0-9 \-']+)/], [:occupation, /\bI(?:'m| am) a(?:n)?\s+([A-Za-z][A-Za-z0-9 \-']*)/i], [:workplace, /\bI (?:work|worked) (?:at|for|in)\s+([A-Za-z0-9][A-Za-z0-9 \-'.&,]*)/i], [:location, /\bI live in\s+([A-Za-z][A-Za-z0-9 \-']*)/i], [:location, /\bI(?:'m| am) from\s+([A-Za-z][A-Za-z0-9 \-']*)/i], [:preference, /\bI (?:like|love|enjoy)\s+([A-Za-z][A-Za-z0-9 \-']*)/i] ].freeze
Instance Method Summary collapse
-
#entities ⇒ Hash
Returns the current entity store (primarily for testing).
-
#fetch(query: nil) ⇒ Array<Hash>
Returns a single chunk containing all known entity facts in XML context format.
-
#initialize ⇒ EntityKnowledge
constructor
A new instance of EntityKnowledge.
-
#update(messages:) ⇒ Object
Scan messages and accumulate entity facts.
Methods inherited from Base
Constructor Details
#initialize ⇒ EntityKnowledge
Returns a new instance of EntityKnowledge.
38 39 40 |
# File 'lib/phronomy/knowledge_source/entity_knowledge.rb', line 38 def initialize @entities = {} end |
Instance Method Details
#entities ⇒ Hash
Returns the current entity store (primarily for testing).
73 74 75 |
# File 'lib/phronomy/knowledge_source/entity_knowledge.rb', line 73 def entities @entities.dup end |
#fetch(query: nil) ⇒ Array<Hash>
Returns a single chunk containing all known entity facts in XML context format. Returns an empty array when no entities have been discovered.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/phronomy/knowledge_source/entity_knowledge.rb', line 59 def fetch(query: nil) return [] if @entities.empty? lines = @entities.map { |key, value| "- #{key}: #{value}" }.join("\n") content = <<~CONTENT.chomp Known facts about the user: #{lines} CONTENT [{content: content, type: :entity}] end |
#update(messages:) ⇒ Object
Scan messages and accumulate entity facts. Call this after saving a new set of messages (e.g. from a ConversationManager save hook).
46 47 48 49 50 51 52 |
# File 'lib/phronomy/knowledge_source/entity_knowledge.rb', line 46 def update(messages:) .each do |msg| next unless msg.role.to_sym == :user extract(msg.content.to_s).each { |key, value| @entities[key] = value } end end |