Class: LastLLM::StructuredOutput
- Inherits:
-
Object
- Object
- LastLLM::StructuredOutput
- Defined in:
- lib/last_llm/structured_output.rb
Overview
Class for generating structured data from LLM providers
Class Method Summary collapse
-
.format_prompt(prompt, schema) ⇒ String
Format a prompt with schema information.
Instance Method Summary collapse
-
#generate(prompt, schema, options = {}) ⇒ Hash
Generate a structured object from a prompt.
-
#initialize(client) ⇒ StructuredOutput
constructor
Initialize a new structured output generator.
Constructor Details
#initialize(client) ⇒ StructuredOutput
Initialize a new structured output generator
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
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
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, = {}) # Format the prompt with schema information formatted_prompt = self.class.format_prompt(prompt, schema) # Set a lower temperature by default for structured data = { temperature: 0.2 }.merge() # Generate the object using the provider result = @client.provider.generate_object(formatted_prompt, schema, ) # 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 |