Module: RubyLLM::Text::Extract

Defined in:
lib/ruby_llm/text/extract.rb

Class Method Summary collapse

Class Method Details

.build_prompt(text, schema) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby_llm/text/extract.rb', line 15

def self.build_prompt(text, schema)
  # Support both simple field-hash schemas and JSON Schema-style hashes
  properties = schema[:properties] || schema["properties"] if schema.respond_to?(:[])
  field_keys = if properties.is_a?(Hash)
                 properties.keys
  else
                 schema.keys
  end
  fields = field_keys.join(", ")

  "    Extract the following information from the text: \#{fields}\n    Return the data as structured JSON matching the provided schema.\n\n    Text:\n    \#{text}\n  PROMPT\nend\n"

.call(text, schema: nil, model: nil, **options) ⇒ Object

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
11
# File 'lib/ruby_llm/text/extract.rb', line 4

def self.call(text, schema: nil, model: nil, **options)
  model ||= RubyLLM::Text.config.model_for(:extract)
  raise ArgumentError, "schema is required for extraction" unless schema

  prompt = build_prompt(text, schema)

  Base.call_llm(prompt, model: model, schema: schema, **options)
end