Class: LumenLLM::Providers::OpenRouter::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/lumen_llm/providers/open_router/parser.rb

Class Method Summary collapse

Class Method Details

.extract(raw, output_type = "text") ⇒ Object

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lumen_llm/providers/open_router/parser.rb', line 7

def self.extract(raw, output_type = "text")
  content = raw.dig("choices", 0, "message", "content")
  raise ParserError, "Missing content in response." unless content

  clean = content.gsub(/```json|```/, "").strip

  case output_type.to_s
  when "json"
    begin
      JSON.parse(clean)
    rescue JSON::ParserError
      raise ParserError, "Invalid JSON output:\n#{clean}"
    end
  else
    clean
  end
end