Class: Cloudflare::AI::Client

Inherits:
Object
  • Object
show all
Includes:
Cloudflare::AI::Clients::MediaHelpers, Cloudflare::AI::Clients::TextGenerationHelpers
Defined in:
lib/cloudflare/ai/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cloudflare::AI::Clients::TextGenerationHelpers

#create_streamable_payload, #default_max_tokens, #default_text_generation_model_name, #post_streamable_request

Constructor Details

#initialize(account_id:, api_token:) ⇒ Client

Returns a new instance of Client.



10
11
12
13
# File 'lib/cloudflare/ai/client.rb', line 10

def initialize(account_id:, api_token:)
  @account_id = 
  @api_token = api_token
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



8
9
10
# File 'lib/cloudflare/ai/client.rb', line 8

def 
  @account_id
end

#api_tokenObject (readonly)

Returns the value of attribute api_token.



8
9
10
# File 'lib/cloudflare/ai/client.rb', line 8

def api_token
  @api_token
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/cloudflare/ai/client.rb', line 8

def url
  @url
end

Instance Method Details

#caption(image: nil, model_name: Cloudflare::AI::Models.image_to_text.first) ⇒ Object



15
16
17
18
19
20
# File 'lib/cloudflare/ai/client.rb', line 15

def caption(image: nil, model_name: Cloudflare::AI::Models.image_to_text.first)
  url = service_url_for(account_id: , model_name: model_name)

  image = File.open(image) if image.is_a?(String)
  Cloudflare::AI::Results::ImageToText.new(post_request_with_binary_file(url, image).body)
end

#chat(messages:, model_name: default_text_generation_model_name, max_tokens: default_max_tokens, &block) ⇒ Object



22
23
24
25
26
27
# File 'lib/cloudflare/ai/client.rb', line 22

def chat(messages:, model_name: default_text_generation_model_name, max_tokens: default_max_tokens, &block)
  url = service_url_for(account_id: , model_name: model_name)
  stream = block ? true : false
  payload = create_streamable_payload({messages: messages.map(&:serializable_hash)}, stream: stream, max_tokens: max_tokens)
  post_streamable_request(url, payload, &block)
end

#classify(text: nil, image: nil, model_name: nil) ⇒ Object

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cloudflare/ai/client.rb', line 29

def classify(text: nil, image: nil, model_name: nil)
  raise ArgumentError, "Must provide either text or image (and not both)" if [text, image].compact.size != 1

  model_name ||= text ? Cloudflare::AI::Models.text_classification.first : Cloudflare::AI::Models.image_classification.first
  url = service_url_for(account_id: , model_name: model_name)

  if text
    payload = {text: text}.to_json
    Cloudflare::AI::Results::TextClassification.new(connection.post(url, payload).body)
  else
    image = File.open(image) if image.is_a?(String)
    Cloudflare::AI::Results::ImageClassification.new(post_request_with_binary_file(url, image).body)
  end
end

#complete(prompt:, model_name: default_text_generation_model_name, max_tokens: default_max_tokens, &block) ⇒ Object



44
45
46
47
48
49
# File 'lib/cloudflare/ai/client.rb', line 44

def complete(prompt:, model_name: default_text_generation_model_name, max_tokens: default_max_tokens, &block)
  url = service_url_for(account_id: , model_name: model_name)
  stream = block ? true : false
  payload = create_streamable_payload({prompt: prompt}, stream: stream, max_tokens: max_tokens)
  post_streamable_request(url, payload, &block)
end

#detect_objects(image: nil, model_name: Cloudflare::AI::Models.object_detection.first) ⇒ Object



51
52
53
54
55
56
# File 'lib/cloudflare/ai/client.rb', line 51

def detect_objects(image: nil, model_name: Cloudflare::AI::Models.object_detection.first)
  url = service_url_for(account_id: , model_name: model_name)

  image = File.open(image) if image.is_a?(String)
  Cloudflare::AI::Results::ObjectDetection.new(post_request_with_binary_file(url, image).body)
end

#draw(prompt:, num_steps: 20, model_name: Cloudflare::AI::Models.text_to_image.first) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/cloudflare/ai/client.rb', line 58

def draw(prompt:, num_steps: 20, model_name: Cloudflare::AI::Models.text_to_image.first)
  url = service_url_for(account_id: , model_name: model_name)
  payload = {prompt: prompt, num_steps: num_steps}.to_json

  result = connection.post(url, payload).body
  binary_data = result.split(",").map(&:to_i).pack("C*")
  Cloudflare::AI::Results::TextToImage.new(
    Tempfile.new(["cloudflare-ai", ".png"], binmode: true).tap { |result| result.write(binary_data) }
  )
end

#embed(text:, model_name: Cloudflare::AI::Models.text_embedding.first) ⇒ Object



69
70
71
72
73
74
# File 'lib/cloudflare/ai/client.rb', line 69

def embed(text:, model_name: Cloudflare::AI::Models.text_embedding.first)
  url = service_url_for(account_id: , model_name: model_name)
  payload = {text: text}.to_json

  Cloudflare::AI::Results::TextEmbedding.new(connection.post(url, payload).body)
end

#summarize(text:, model_name: Cloudflare::AI::Models.summarization.first, max_tokens: 1024) ⇒ Object



76
77
78
79
80
81
# File 'lib/cloudflare/ai/client.rb', line 76

def summarize(text:, model_name: Cloudflare::AI::Models.summarization.first, max_tokens: 1024)
  url = service_url_for(account_id: , model_name: model_name)
  payload = {input_text: text, max_tokens: max_tokens}.to_json

  Cloudflare::AI::Results::Summarization.new(connection.post(url, payload).body)
end

#transcribe(source_url: nil, audio: nil, model_name: Cloudflare::AI::Models.automatic_speech_recognition.first) ⇒ Object

Raises:

  • (ArgumentError)


83
84
85
86
87
88
89
90
91
92
# File 'lib/cloudflare/ai/client.rb', line 83

def transcribe(source_url: nil, audio: nil, model_name: Cloudflare::AI::Models.automatic_speech_recognition.first)
  raise ArgumentError, "Must provide either audio_url or audio" if [source_url, audio].compact.size != 1

  audio = download_audio(source_url) if source_url

  url = service_url_for(account_id: , model_name: model_name)
  response = post_request_with_binary_file(url, audio)

  Cloudflare::AI::Results::AutomaticSpeechRecognition.new(response.body)
end

#translate(text:, target_lang:, source_lang: "en", model_name: Cloudflare::AI::Models.translation.first) ⇒ Object



94
95
96
97
98
# File 'lib/cloudflare/ai/client.rb', line 94

def translate(text:, target_lang:, source_lang: "en", model_name: Cloudflare::AI::Models.translation.first)
  url = service_url_for(account_id: , model_name: model_name)
  payload = {text: text, target_lang: target_lang, source_lang: source_lang}.to_json
  Cloudflare::AI::Results::Translation.new(connection.post(url, payload).body)
end