mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 07:39:39 +02:00
* Start refactor * Interface updates * Rework Assistant, Provider, and tests for better domain boundaries * Consolidate and simplify OpenAI provider and provider concepts * Clean up assistant streaming * Improve assistant message orchestration logic * Clean up "thinking" UI interactions * Remove stale class * Regenerate VCR test responses
62 lines
1.8 KiB
Ruby
62 lines
1.8 KiB
Ruby
class Provider::Openai < Provider
|
|
include LlmConcept
|
|
|
|
# Subclass so errors caught in this provider are raised as Provider::Openai::Error
|
|
Error = Class.new(Provider::Error)
|
|
|
|
MODELS = %w[gpt-4o]
|
|
|
|
def initialize(access_token)
|
|
@client = ::OpenAI::Client.new(access_token: access_token)
|
|
end
|
|
|
|
def supports_model?(model)
|
|
MODELS.include?(model)
|
|
end
|
|
|
|
def chat_response(prompt, model:, instructions: nil, functions: [], function_results: [], streamer: nil, previous_response_id: nil)
|
|
with_provider_response do
|
|
chat_config = ChatConfig.new(
|
|
functions: functions,
|
|
function_results: function_results
|
|
)
|
|
|
|
collected_chunks = []
|
|
|
|
# Proxy that converts raw stream to "LLM Provider concept" stream
|
|
stream_proxy = if streamer.present?
|
|
proc do |chunk|
|
|
parsed_chunk = ChatStreamParser.new(chunk).parsed
|
|
|
|
unless parsed_chunk.nil?
|
|
streamer.call(parsed_chunk)
|
|
collected_chunks << parsed_chunk
|
|
end
|
|
end
|
|
else
|
|
nil
|
|
end
|
|
|
|
raw_response = client.responses.create(parameters: {
|
|
model: model,
|
|
input: chat_config.build_input(prompt),
|
|
instructions: instructions,
|
|
tools: chat_config.tools,
|
|
previous_response_id: previous_response_id,
|
|
stream: stream_proxy
|
|
})
|
|
|
|
# If streaming, Ruby OpenAI does not return anything, so to normalize this method's API, we search
|
|
# for the "response chunk" in the stream and return it (it is already parsed)
|
|
if stream_proxy.present?
|
|
response_chunk = collected_chunks.find { |chunk| chunk.type == "response" }
|
|
response_chunk.data
|
|
else
|
|
ChatParser.new(raw_response).parsed
|
|
end
|
|
end
|
|
end
|
|
|
|
private
|
|
attr_reader :client
|
|
end
|