Method: OpenAI::Completion#get_json

Defined in:
lib/monadic_chat/open_ai.rb

#get_json(data) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/monadic_chat/open_ai.rb', line 189

def get_json(data)
  case data
  when %r{<JSON>\n*(\{.+?\})\n*</JSON>}m
    json = Regexp.last_match(1).gsub(/\r\n?/, "\n").gsub(/\r\n/) { "\n" }
    res = JSON.parse(json)
  when /(\{.+\})/m
    json = Regexp.last_match(1).gsub(/\r\n?/, "\n").gsub(/\r\n/) { "\n" }
    res = JSON.parse(json)
  else
    res = data
  end
  res
end