Class: RCrewAI::Memory::LlmEntityExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/rcrewai/memory/llm_entity_extractor.rb

Overview

Extracts named entities from text with an LLM. Pass an instance as the extractor: for EntityMemory. Responds to call(text) -> [names]. Returns [] on any parse/LLM failure so EntityMemory falls back to the heuristic extractor — extraction is best-effort and never fatal.

Constant Summary collapse

PROMPT =
<<~PROMPT
  Extract the named entities (people, organizations, systems, products,
  places, and key concepts) from the text below. Respond ONLY with a JSON
  array of strings, e.g. ["Alice", "Payments", "Redis"]. No prose.

  Text:
PROMPT

Instance Method Summary collapse

Constructor Details

#initialize(llm) ⇒ LlmEntityExtractor

Returns a new instance of LlmEntityExtractor.



20
21
22
# File 'lib/rcrewai/memory/llm_entity_extractor.rb', line 20

def initialize(llm)
  @llm = llm
end

Instance Method Details

#call(text) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/rcrewai/memory/llm_entity_extractor.rb', line 24

def call(text)
  response = @llm.chat(messages: [{ role: 'user', content: "#{PROMPT}#{text}" }])
  content = response.is_a?(Hash) ? response[:content] : response
  parsed = OutputSchema.parse(content.to_s)
  parsed.is_a?(Array) ? parsed.map(&:to_s) : []
rescue StandardError
  []
end