Class: Nukitori::LlmExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/nukitori/llm_extractor.rb

Overview

Extracts data directly using LLM (no schema generation/caching)

Class Method Summary collapse

Class Method Details

.extract(html, model: nil, &block) ⇒ Hash

Extract data from HTML using LLM directly

Parameters:

  • html (String, Nokogiri::HTML::Document) —

    HTML content

  • model (String, nil) (defaults to: nil) —

    LLM model to use (overrides default_model)

  • block (Proc) —

    Schema definition block

Returns:

  • (Hash) —

    Extracted data

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nukitori/llm_extractor.rb', line 12

def extract(html, model: nil, &block)
  raise ArgumentError, 'Block required for schema definition' unless block_given?

  schema_class = Class.new(RubyLLM::Schema, &block)
  processed_html = HtmlPreprocessor.process(html)

  chat = ChatFactory.create(model:)
  chat.with_schema(schema_class) if support_structured_output?(chat.model)
  chat.with_instructions(build_prompt(schema_class))

  response = chat.ask(processed_html)
  ResponseParser.parse(response.content)
end