Module: RailsAi::Streaming

Extended by:
ActiveSupport::Concern
Defined in:
app/controllers/concerns/ai/streaming.rb

Instance Method Summary collapse

Instance Method Details

#broadcast_ai_stream(stream_id, prompt, model: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/concerns/ai/streaming.rb', line 18

def broadcast_ai_stream(stream_id, prompt, model: nil)
  model ||= RailsAi.config.default_model

  ActionCable.server.broadcast("ai_stream_#{stream_id}", {
    type: "start",
    stream_id: stream_id
  })

  RailsAi.provider.stream_chat!(
    messages: [{role: "user", content: prompt}],
    model: model
  ) do |token|
    ActionCable.server.broadcast("ai_stream_#{stream_id}", {
      type: "token",
      content: token
    })
  end

  ActionCable.server.broadcast("ai_stream_#{stream_id}", {
    type: "complete"
  })
end

#stream_ai_response(prompt, model: nil, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'app/controllers/concerns/ai/streaming.rb', line 7

def stream_ai_response(prompt, model: nil, &block)
  model ||= RailsAi.config.default_model

  RailsAi.provider.stream_chat!(
    messages: [{role: "user", content: prompt}],
    model: model
  ) do |token|
    block.call(token) if block_given?
  end
end