Class: Phronomy::KnowledgeSource::EntityKnowledge

Inherits:
Base
  • Object
show all
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" }

Examples:

ks = Phronomy::KnowledgeSource::EntityKnowledge.new
ks.update(messages: chat_messages)
agent.invoke("What is my name?", config: { knowledge_sources: [ks] })

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

Methods inherited from Base

#static?

Constructor Details

#initializeEntityKnowledge

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

#entitiesHash

Returns the current entity store (primarily for testing).

Returns:

  • (Hash)


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.

Parameters:

  • query (String, nil) (defaults to: nil)

    unused — entity knowledge is always fully injected

Returns:

  • (Array<Hash>)


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).

Parameters:

  • messages (Array)

    message objects responding to #role and #content



46
47
48
49
50
51
52
# File 'lib/phronomy/knowledge_source/entity_knowledge.rb', line 46

def update(messages:)
  messages.each do |msg|
    next unless msg.role.to_sym == :user

    extract(msg.content.to_s).each { |key, value| @entities[key] = value }
  end
end