Module: Cloudflare::AI::Clients::TextGenerationHelpers

Included in:
Cloudflare::AI::Client
Defined in:
lib/cloudflare/ai/clients/text_generation_helpers.rb

Instance Method Summary collapse

Instance Method Details

#create_streamable_payload(data, stream:, max_tokens:) ⇒ Object



5
6
7
# File 'lib/cloudflare/ai/clients/text_generation_helpers.rb', line 5

def create_streamable_payload(data, stream:, max_tokens:)
  data.merge({stream: stream, max_tokens: max_tokens}).to_json
end

#default_max_tokensObject



9
10
11
# File 'lib/cloudflare/ai/clients/text_generation_helpers.rb', line 9

def default_max_tokens
  256
end

#default_text_generation_model_nameObject



13
14
15
# File 'lib/cloudflare/ai/clients/text_generation_helpers.rb', line 13

def default_text_generation_model_name
  Cloudflare::AI::Models.text_generation.first
end

#post_streamable_request(url, payload, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cloudflare/ai/clients/text_generation_helpers.rb', line 17

def post_streamable_request(url, payload, &block)
  if block
    parser = EventStreamParser::Parser.new
    connection.post(url, payload) do |response|
      response.options.on_data = parser.stream do |_type, data, _id, _reconnection_time, _size|
        yield data
      end
    end
  else
    Cloudflare::AI::Results::TextGeneration.new(connection.post(url, payload).body)
  end
end