Class: Rails::Llm::Structured::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/llm/structured/base.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: nil) ⇒ Base

Returns a new instance of Base.



23
24
25
# File 'lib/rails/llm/structured/base.rb', line 23

def initialize(client: nil)
  @client = client || Client.new
end

Class Attribute Details

.model_name ⇒ Object

Returns the value of attribute model_name.



8
9
10
# File 'lib/rails/llm/structured/base.rb', line 8

def model_name
  @model_name
end

.schema_definition ⇒ Object

Returns the value of attribute schema_definition.



8
9
10
# File 'lib/rails/llm/structured/base.rb', line 8

def schema_definition
  @schema_definition
end

.system_prompt_text ⇒ Object

Returns the value of attribute system_prompt_text.



8
9
10
# File 'lib/rails/llm/structured/base.rb', line 8

def system_prompt_text
  @system_prompt_text
end

Class Method Details

.model(name) ⇒ Object



10
11
12
# File 'lib/rails/llm/structured/base.rb', line 10

def model(name)
  self.model_name = name
end

.structured_output(&block) ⇒ Object



14
15
16
# File 'lib/rails/llm/structured/base.rb', line 14

def structured_output(&block)
  self.schema_definition = Schema.new(&block)
end

.system(prompt) ⇒ Object



18
19
20
# File 'lib/rails/llm/structured/base.rb', line 18

def system(prompt)
  self.system_prompt_text = prompt
end

Instance Method Details

#call(prompt, **options) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rails/llm/structured/base.rb', line 27

def call(prompt, **options)
  raise Error, "No model specified" unless self.class.model_name
  raise Error, "No schema defined" unless self.class.schema_definition
  
  messages = build_messages(prompt)
  
  raw_response = @client.chat(
    model: self.class.model_name,
    messages: messages,
    response_format: self.class.schema_definition.to_json_schema,
    temperature: options[:temperature] || 0.7,
    max_tokens: options[:max_tokens]
  )
  
  Response.new(raw_response, schema: self.class.schema_definition)
end