Class: AI::Engine::Chat
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- AI::Engine::Chat
- Defined in:
- app/models/ai/engine/chat.rb
Instance Method Summary collapse
- #messages_for_openai ⇒ Object
- #run(model:) ⇒ Object
- #stream(model:) ⇒ Object
- #to_partial_path ⇒ Object
Instance Method Details
#messages_for_openai ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'app/models/ai/engine/chat.rb', line 11 def .order(:created_at).map do || { role: .role, content: .content } end.filter { || [:content].present? } end |
#run(model:) ⇒ Object
6 7 8 9 |
# File 'app/models/ai/engine/chat.rb', line 6 def run(model:) # Run the Chat, sending the complete message history to OpenAI. AI::Engine::OpenAI::Chats::Stream.call(chat_id: id, stream: stream(model: model), model: model) end |
#stream(model:) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'app/models/ai/engine/chat.rb', line 20 def stream(model:) = .create( role: "assistant", content: "", model: model ) proc do |chunk, _bytesize| if chunk["object"] == "chat.completion.chunk" new_content = chunk.dig("choices", 0, "delta", "content") .update(content: .content + new_content) if new_content end if chunk["usage"] .update( prompt_token_usage: chunk.dig("usage", "prompt_tokens"), completion_token_usage: chunk.dig("usage", "completion_tokens") ) end end end |
#to_partial_path ⇒ Object
41 42 43 |
# File 'app/models/ai/engine/chat.rb', line 41 def to_partial_path "chats/chat" end |