lib/durable/llm/providers/openai.rb in durable-llm-0.1.3 vs lib/durable/llm/providers/openai.rb in durable-llm-0.1.4
- old
+ new
@@ -53,43 +53,31 @@
end
handle_response(response).data.map { |model| model['id'] }
end
- def self.models
- self.new.models
- end
-
def self.stream?
true
end
- def stream(options, &block)
-
+ def stream(options)
options[:stream] = true
response = @conn.post('chat/completions') do |req|
-
req.headers['Authorization'] = "Bearer #{@api_key}"
req.headers['OpenAI-Organization'] = @organization if @organization
req.headers['Accept'] = 'text/event-stream'
- if options['temperature']
- options['temperature'] = options['temperature'].to_f
- end
+ options['temperature'] = options['temperature'].to_f if options['temperature']
req.body = options
- user_proc = Proc.new do |chunk, size, total|
-
-
+ user_proc = proc do |chunk, _size, _total|
yield OpenAIStreamResponse.new(chunk)
-
end
- req.options.on_data = to_json_stream( user_proc: user_proc )
-
+ req.options.on_data = to_json_stream(user_proc: user_proc)
end
handle_response(response)
end
@@ -111,11 +99,11 @@
raise_error = Faraday::Response::RaiseError.new
raise_error.on_complete(env.merge(body: try_parse_json(chunk)))
end
parser.feed(chunk) do |_type, data|
- user_proc.call(JSON.parse(data)) unless data == "[DONE]"
+ user_proc.call(JSON.parse(data)) unless data == '[DONE]'
end
end
end
def try_parse_json(maybe_json)
@@ -123,12 +111,12 @@
rescue JSON::ParserError
maybe_json
end
# END-CODE-FROM
-
- def handle_response(response, responseClass=OpenAIResponse)
+
+ def handle_response(response, responseClass = OpenAIResponse)
case response.status
when 200..299
responseClass.new(response.body)
when 401
raise Durable::Llm::AuthenticationError, parse_error_message(response)
@@ -142,11 +130,15 @@
raise Durable::Llm::APIError, "Unexpected response code: #{response.status}"
end
end
def parse_error_message(response)
- body = JSON.parse(response.body) rescue nil
+ body = begin
+ JSON.parse(response.body)
+ rescue StandardError
+ nil
+ end
message = body&.dig('error', 'message') || response.body
"#{response.status} Error: #{message}"
end
class OpenAIResponse
@@ -201,11 +193,10 @@
class OpenAIStreamResponse
attr_reader :choices
def initialize(parsed)
-
- @choices = OpenAIStreamChoice.new(parsed['choices'])
+ @choices = OpenAIStreamChoice.new(parsed['choices'])
end
def to_s
@choices.to_s
end