mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 07:39:39 +02:00
improvements(ai): Improve AI streaming UI/UX interactions + better separation of AI provider responsibilities (#2039)
* 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
This commit is contained in:
parent
6331788b33
commit
5cf758bd03
33 changed files with 1179 additions and 624 deletions
59
app/models/provider/openai/chat_parser.rb
Normal file
59
app/models/provider/openai/chat_parser.rb
Normal file
|
@ -0,0 +1,59 @@
|
|||
class Provider::Openai::ChatParser
|
||||
Error = Class.new(StandardError)
|
||||
|
||||
def initialize(object)
|
||||
@object = object
|
||||
end
|
||||
|
||||
def parsed
|
||||
ChatResponse.new(
|
||||
id: response_id,
|
||||
model: response_model,
|
||||
messages: messages,
|
||||
function_requests: function_requests
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
attr_reader :object
|
||||
|
||||
ChatResponse = Provider::LlmConcept::ChatResponse
|
||||
ChatMessage = Provider::LlmConcept::ChatMessage
|
||||
ChatFunctionRequest = Provider::LlmConcept::ChatFunctionRequest
|
||||
|
||||
def response_id
|
||||
object.dig("id")
|
||||
end
|
||||
|
||||
def response_model
|
||||
object.dig("model")
|
||||
end
|
||||
|
||||
def messages
|
||||
message_items = object.dig("output").filter { |item| item.dig("type") == "message" }
|
||||
|
||||
message_items.map do |message_item|
|
||||
ChatMessage.new(
|
||||
id: message_item.dig("id"),
|
||||
output_text: message_item.dig("content").map do |content|
|
||||
text = content.dig("text")
|
||||
refusal = content.dig("refusal")
|
||||
text || refusal
|
||||
end.flatten.join("\n")
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def function_requests
|
||||
function_items = object.dig("output").filter { |item| item.dig("type") == "function_call" }
|
||||
|
||||
function_items.map do |function_item|
|
||||
ChatFunctionRequest.new(
|
||||
id: function_item.dig("id"),
|
||||
call_id: function_item.dig("call_id"),
|
||||
function_name: function_item.dig("name"),
|
||||
function_args: function_item.dig("arguments")
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue