Class: Ollama::Client::Generate::ResponseFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama/client/generate/response_formatter.rb

Overview

Handles parsing, validation, and structured output formatting for generate responses

Instance Method Summary collapse

Constructor Details

#initialize(provider:, config:, schema_cache: {}) ⇒ ResponseFormatter

Returns a new instance of ResponseFormatter.



12
13
14
15
16
# File 'lib/ollama/client/generate/response_formatter.rb', line 12

def initialize(provider:, config:, schema_cache: {})
  @provider = provider
  @config = config
  @schema_cache = schema_cache
end

Instance Method Details

#extract_reasoning(raw_text, user_schema) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ollama/client/generate/response_formatter.rb', line 45

def extract_reasoning(raw_text, user_schema)
  reasoning = ""
  final_output = raw_text

  if raw_text.match?(%r{思考(.*?)</think>}mi)
    reasoning = raw_text.match(%r{思考(.*?)</think>}mi)[1].strip
    final_output = raw_text.sub(%r{思考.*?</think>}mi, "").strip
  elsif raw_text.match?(/思考(.*?)回答/mi)
    reasoning = raw_text.match(/思考(.*?)回答/mi)[1].strip
    final_output = raw_text.sub(/思考.*?回答/mi, "").strip
  elsif raw_text.include?("回答")
    parts = raw_text.split("回答", 2)
    reasoning = parts[0].sub("思考", "").strip
    final_output = parts[1].strip
  elsif raw_text.match?(%r{<think>(.*?)</think>}mi)
    reasoning = raw_text.match(%r{<think>(.*?)</think>}mi)[1].strip
    final_output = raw_text.sub(%r{<think>.*?</think>}mi, "").strip
  elsif raw_text.include?("\n")
    parts = raw_text.split("\n", 2)
    reasoning = parts[0].strip
    final_output = parts[1].strip
  end

  if user_schema
    parsed_final = parse_json_response(final_output)
    SchemaValidator.validate!(parsed_final, user_schema)
    final_output = parsed_final
  end

  {
    "reasoning" => reasoning,
    "final" => final_output
  }
end

#format(response_data, context, return_meta, model, attempts, started_at) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ollama/client/generate/response_formatter.rb', line 30

def format(response_data, context, return_meta, model, attempts, started_at)
  return response_data unless return_meta

  {
    "data" => response_data,
    "context" => context,
    "meta" => {
      "endpoint" => @provider.generate_endpoint.path,
      "model" => model || @config.model,
      "attempts" => attempts,
      "latency_ms" => elapsed_ms(started_at)
    }
  }
end

#parse_json_response(raw) ⇒ Object



80
81
82
83
84
85
# File 'lib/ollama/client/generate/response_formatter.rb', line 80

def parse_json_response(raw)
  json_text = JsonFragmentExtractor.call(raw)
  JSON.parse(json_text)
rescue JSON::ParserError => e
  raise InvalidJSONError, "Failed to parse extracted JSON: #{e.message}. Extracted: #{json_text&.slice(0, 200)}..."
end

#process(raw_response, schema, think, return_reasoning) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ollama/client/generate/response_formatter.rb', line 18

def process(raw_response, schema, think, return_reasoning)
  if think && return_reasoning
    extract_reasoning(raw_response, schema)
  elsif schema
    parsed = parse_json_response(raw_response)
    SchemaValidator.validate!(parsed, schema)
    parsed
  else
    raw_response
  end
end