Class: LastLLM::StructuredOutput

Inherits:
Object
  • Object
show all
Defined in:
lib/last_llm/structured_output.rb

Overview

Class for generating structured data from LLM providers

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ StructuredOutput

Initialize a new structured output generator

Parameters:



10
11
12
# File 'lib/last_llm/structured_output.rb', line 10

def initialize(client)
  @client = client
end

Class Method Details

.format_prompt(prompt, schema) ⇒ String

Format a prompt with schema information

Parameters:

  • prompt (String)

    The original prompt

  • schema (Dry::Schema::JSON)

    The schema to include

Returns:

  • (String)

    The formatted prompt



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/last_llm/structured_output.rb', line 44

def self.format_prompt(prompt, schema)
  schema_json = Schema.to_json_schema(schema)

  <<~PROMPT
    #{prompt}

    Respond with valid JSON that matches the following schema:

    #{schema_json}

    Ensure your response is a valid JSON object that strictly follows this schema.
  PROMPT
end

Instance Method Details

#generate(prompt, schema, options = {}) ⇒ Hash

Generate a structured object from a prompt

Parameters:

  • prompt (String)

    The prompt to generate the object from

  • schema (Dry::Schema::JSON)

    The schema to validate against

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

    Generation options

Options Hash (options):

  • :model (String)

    The model to use

  • :temperature (Float) — default: 0.2

    The temperature to use

Returns:

  • (Hash)

    The generated object

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/last_llm/structured_output.rb', line 22

def generate(prompt, schema, options = {})
  # Format the prompt with schema information
  formatted_prompt = self.class.format_prompt(prompt, schema)

  # Set a lower temperature by default for structured data
  options = { temperature: 0.2 }.merge(options)

  # Generate the object using the provider
  result = @client.provider.generate_object(formatted_prompt, schema, options)

  # Validate the result against the schema
  validation = schema.call(result)

  raise ValidationError, "Generated object failed validation: #{validation.errors.to_h}" unless validation.success?

  result
end