Class: Maglev::Adapters::FaradayPlanner

Inherits:
PlannerAdapter show all
Defined in:
lib/maglev/adapters/faraday_planner.rb

Constant Summary collapse

MAX_QUESTION_BYTES =
8_192
RESPONSE_FORMATS =
%i[json_schema json_object].freeze

Instance Method Summary collapse

Constructor Details

#initialize(provider: Maglev.configuration.generation_provider, connection: nil, response_format: :json_schema) ⇒ FaradayPlanner

Returns a new instance of FaradayPlanner.



15
16
17
18
19
20
21
22
# File 'lib/maglev/adapters/faraday_planner.rb', line 15

def initialize(provider: Maglev.configuration.generation_provider, connection: nil, response_format: :json_schema)
  unless response_format.respond_to?(:to_sym) && RESPONSE_FORMATS.include?(response_format.to_sym)
    raise ConfigurationError, "Unsupported planner response format #{response_format.inspect}"
  end
  @response_format = response_format.to_sym
  @provider = provider
  @client = FaradayClient.new(@provider, connection: connection)
end

Instance Method Details

#plan(question:, schema_snapshot:, constraints:, query_ir_schema:, repair: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/maglev/adapters/faraday_planner.rb', line 24

def plan(question:, schema_snapshot:, constraints:, query_ir_schema:, repair: nil)
  question = question.to_s
  raise ArgumentError, "question exceeds planner limit" if question.bytesize > MAX_QUESTION_BYTES

  response = @client.post("chat/completions", payload(question, schema_snapshot, constraints,
    query_ir_schema, repair))
  content = response.dig("choices", 0, "message", "content")
  raise PermanentProviderError, "Planner provider returned invalid structured output" unless content.is_a?(String)

  JSON.parse(content)
rescue JSON::ParserError
  raise PermanentProviderError, "Planner provider returned invalid structured output"
end