Class: ActiveGenie::Extractor::Data

Inherits:
BaseModule show all
Defined in:
lib/active_genie/extractor/data.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", age: 25 }

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



26
27
28
29
30
# File 'lib/active_genie/extractor/data.rb', line 26

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

Instance Method Details

#callObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_genie/extractor/data.rb', line 32

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

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

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

  response_formatted(provider_response)
end