Class: ActiveGenie::Extractor::Explanation

Inherits:
BaseModule
  • Object
show all
Defined in:
lib/active_genie/extractor/explanation.rb

Instance Method Summary collapse

Methods inherited from BaseModule

call, #config

Constructor Details

#initialize(text, data_to_extract, config: {}) ⇒ Hash

Extracts structured data from text based on a predefined schema.

Examples:

Extract a person’s details

schema = {
  name: { type: 'string', description: 'Full name of the person' },
  age: { type: 'integer', description: 'Age in years' }
}
text = "John Doe is 25 years old"
Extractor.with_explanation(text, schema)
# => { name: "John Doe", name_explanation: "Found directly in text",
#      age: 25, age_explanation: "Explicitly stated as 25 years old" }

Parameters:

  • text (String)

    The input text to analyze and extract data from

  • data_to_extract (Hash)

    Schema defining the data structure to extract. Each key in the hash represents a field to extract, and its value defines the expected type and constraints.

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

    Additional config for the extraction process



28
29
30
31
32
# File 'lib/active_genie/extractor/explanation.rb', line 28

def initialize(text, data_to_extract, config: {})
  @text = text
  @data_to_extract = data_to_extract
  super(config:)
end

Instance Method Details

#callObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/active_genie/extractor/explanation.rb', line 34

def call
  messages = [
    {  role: 'system', content: prompt },
    {  role: 'user', content: @text }
  ]

  properties = data_to_extract_with_explanation

  function = JSON.parse(File.read(File.join(__dir__, 'explanation.json')), symbolize_names: true)
  function[:parameters][:properties] = properties
  function[:parameters][:required] = properties.keys

  provider_response = ::ActiveGenie::Providers::UnifiedProvider.function_calling(
    messages,
    function,
    config: config
  )

  response_formatted(provider_response)
end