Class: FactDb::Extractors::LLMExtractor

Inherits:
Base
  • Object
show all
Defined in:
lib/fact_db/extractors/llm_extractor.rb

Overview

LLM-based fact extractor using language models

Uses a configured LLM client to extract atomic facts and entities from unstructured text. Parses JSON responses from the LLM and builds standardized fact/entity hashes.

Examples:

Extract facts using LLM

FactDb.configure { |c| c.llm_client = MyLLMClient.new }
extractor = LLMExtractor.new
facts = extractor.extract("Paula joined Microsoft on January 10, 2024...")

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

available_types, #extraction_method, for, #initialize

Constructor Details

This class inherits a constructor from FactDb::Extractors::Base

Instance Method Details

#extract(text, context = {}) ⇒ Array<Hash>

Extracts atomic facts from text using the configured LLM

Prompts the LLM to identify factual assertions, temporal information, entity mentions with roles, and confidence scores.

Parameters:

  • text (String)

    raw text to extract from

  • context (Hash) (defaults to: {})

    additional context

Options Hash (context):

  • :captured_at (Date, Time)

    default timestamp for facts

Returns:

  • (Array<Hash>)

    array of fact hashes

Raises:



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fact_db/extractors/llm_extractor.rb', line 29

def extract(text, context = {})
  return [] if text.nil? || text.strip.empty?

  client = config.llm_client
  raise ConfigurationError, "LLM client not configured" unless client

  prompt = format(config.prompts.fact_extraction, text: text)
  response = call_llm(client, prompt)

  parse_fact_response(response, context)
end

#extract_entities(text) ⇒ Array<Hash>

Extracts entities from text using the configured LLM

Prompts the LLM to identify named entities, classify their types, and list any aliases or alternative names.

Parameters:

  • text (String)

    raw text to extract from

Returns:

  • (Array<Hash>)

    array of entity hashes with :name, :kind, :aliases

Raises:



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fact_db/extractors/llm_extractor.rb', line 49

def extract_entities(text)
  return [] if text.nil? || text.strip.empty?

  client = config.llm_client
  raise ConfigurationError, "LLM client not configured" unless client

  prompt = format(config.prompts.entity_extraction, text: text)
  response = call_llm(client, prompt)

  parse_entity_response(response)
end