cloudflare-ai 0.4.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f8c78786e06d2a3cc05b177aedc99672b4be56016c33d6be2e718533b6b3f507
4
- data.tar.gz: 535f23f9e6ffedbe0e090ded97459e021e57b84025456dab77d3d85dd2da194d
3
+ metadata.gz: 9085ff95f730366db7b3261cc94935f782d719b8129dd1341796ee6d66fd373c
4
+ data.tar.gz: 60b29886c88200aebdd1c46e743a28dd597540144494e8288098e05c6ddb8e04
5
5
  SHA512:
6
- metadata.gz: 6d35d01ae35a9407b97acfc0643c66c33b26d608d90373daf3c00001f86d7d2d2893162b370c417e2db7a96b43cec74d66e2652caa11ed89bbf31b968b4fd34d
7
- data.tar.gz: 5af4ef84fdfc0ed649f951f18c10105b97ee4ae55b5d951f90551f7b0e5e452ca98ce408ded86980e67da05bfaf4e42321097b0b3f1cb2cfd224d67da74f8d44
6
+ metadata.gz: 3b95c01b2cab4dec4c0bdd476d99f5ab65183215a64e6c6711a50caed5874a51ff34b78b530b1877b13a1be8851fc527202a1b250f94d57a554ff46b25716136
7
+ data.tar.gz: 86721eb5cf51bfd886a6f9e1a4be71c4b33492cd90e2a2adedbe2f6474c7e601b4e2f1bc52bfd315039a3b24ea202fd627dc032a2dda63e8ad6f04b0e01da08a
data/README.md CHANGED
@@ -21,9 +21,9 @@ It's still early days, and here are my immediate priorities:
21
21
  * [ ] Support for more AI model categories
22
22
  * [x] [Text Generation](https://developers.cloudflare.com/workers-ai/models/text-generation/)
23
23
  * [x] [Text Embeddings](https://developers.cloudflare.com/workers-ai/models/text-embeddings/)
24
- * [ ] [Text Classification](https://developers.cloudflare.com/workers-ai/models/text-classification/)
24
+ * [x] [Text Classification](https://developers.cloudflare.com/workers-ai/models/text-classification/)
25
+ * [x] [Translation](https://developers.cloudflare.com/workers-ai/models/translation/)
25
26
  * [ ] [Image Classification](https://developers.cloudflare.com/workers-ai/models/image-classification/)
26
- * [ ] [Translation](https://developers.cloudflare.com/workers-ai/models/translation/)
27
27
  * [ ] [Text-to-Image](https://developers.cloudflare.com/workers-ai/models/text-to-image/)
28
28
  * [ ] [Automatic Speech Recognition](https://developers.cloudflare.com/workers-ai/models/speech-recognition/)
29
29
 
@@ -99,6 +99,12 @@ result = client.complete(prompt: "Hi!") { |data| puts data}
99
99
  # [DONE]
100
100
 
101
101
  ```
102
+ #### Token limits
103
+ Invocations of the `prompt` and `chat` can take an optional `max_tokens` argument that defaults to 256.
104
+ ```ruby
105
+ result = client.complete(prompt: "What is your name?", max_tokens: 512)
106
+ ```
107
+
102
108
  #### Result object
103
109
  All invocations of the `prompt` and `chat` methods return a `Cloudflare::AI::Results::TextGeneration` object. This object's serializable JSON output is
104
110
  based on the raw response from the Cloudflare API.
@@ -119,7 +125,6 @@ puts result.failure? # => true
119
125
  puts result.to_json # => {"result":null,"success":false,"errors":[{"code":7009,"message":"Upstream service unavailable"}],"messages":[]}
120
126
  ```
121
127
 
122
-
123
128
  ### Text embedding
124
129
  ```ruby
125
130
  result = client.embed(text: "Hello")
@@ -144,6 +149,15 @@ p result.result # => [{"label"=>"NEGATIVE", "score"=>0.6647962927818298}, {"labe
144
149
  #### Result object
145
150
  All invocations of the `classify` methods return a `Cloudflare::AI::Results::TextClassification`.
146
151
 
152
+ ### Translation
153
+ ```ruby
154
+ result = client.translate(text: "Hello Jello", source_lang: "en", target_lang: "fr")
155
+ p result.translated_text # => Hola Jello
156
+ ```
157
+
158
+ #### Result object
159
+ All invocations of the `translate` methods return a `Cloudflare::AI::Results::Translate`.
160
+
147
161
  # Logging
148
162
 
149
163
  This gem uses standard logging mechanisms and defaults to `:warn` level. Most messages are at info level, but we will add debug or warn statements as needed.
@@ -11,10 +11,10 @@ class Cloudflare::AI::Client
11
11
  @api_token = api_token
12
12
  end
13
13
 
14
- def chat(messages:, model_name: default_text_generation_model_name, &block)
14
+ def chat(messages:, model_name: default_text_generation_model_name, max_tokens: default_max_tokens, &block)
15
15
  url = service_url_for(account_id: account_id, model_name: model_name)
16
16
  stream = block ? true : false
17
- payload = create_streamable_payload({messages: messages.map(&:serializable_hash)}, stream: stream)
17
+ payload = create_streamable_payload({messages: messages.map(&:serializable_hash)}, stream: stream, max_tokens: max_tokens)
18
18
  post_streamable_request(url, payload, &block)
19
19
  end
20
20
 
@@ -25,10 +25,10 @@ class Cloudflare::AI::Client
25
25
  Cloudflare::AI::Results::TextClassification.new(connection.post(url, payload).body)
26
26
  end
27
27
 
28
- def complete(prompt:, model_name: default_text_generation_model_name, &block)
28
+ def complete(prompt:, model_name: default_text_generation_model_name, max_tokens: default_max_tokens, &block)
29
29
  url = service_url_for(account_id: account_id, model_name: model_name)
30
30
  stream = block ? true : false
31
- payload = create_streamable_payload({prompt: prompt}, stream: stream)
31
+ payload = create_streamable_payload({prompt: prompt}, stream: stream, max_tokens: max_tokens)
32
32
  post_streamable_request(url, payload, &block)
33
33
  end
34
34
 
@@ -39,6 +39,12 @@ class Cloudflare::AI::Client
39
39
  Cloudflare::AI::Results::TextEmbedding.new(connection.post(url, payload).body)
40
40
  end
41
41
 
42
+ def translate(text:, target_lang:, source_lang: "en", model_name: Cloudflare::AI::Models.translation.first)
43
+ url = service_url_for(account_id: account_id, model_name: model_name)
44
+ payload = {text: text, target_lang: target_lang, source_lang: source_lang}.to_json
45
+ Cloudflare::AI::Results::Translation.new(connection.post(url, payload).body)
46
+ end
47
+
42
48
  private
43
49
 
44
50
  def connection
@@ -2,12 +2,16 @@ module Cloudflare
2
2
  module AI
3
3
  module Clients
4
4
  module TextGenerationHelpers
5
- def default_text_generation_model_name
6
- Cloudflare::AI::Models.text_generation.first
5
+ def create_streamable_payload(data, stream:, max_tokens:)
6
+ data.merge({stream: stream, max_tokens: max_tokens}).to_json
7
+ end
8
+
9
+ def default_max_tokens
10
+ 256
7
11
  end
8
12
 
9
- def create_streamable_payload(data, stream: false)
10
- data.merge({stream: stream}).to_json
13
+ def default_text_generation_model_name
14
+ Cloudflare::AI::Models.text_generation.first
11
15
  end
12
16
 
13
17
  def post_streamable_request(url, payload, &block)
@@ -0,0 +1,5 @@
1
+ class Cloudflare::AI::Results::Translation < Cloudflare::AI::Result
2
+ def translated_text
3
+ result&.dig(:translated_text) # nil if no shape
4
+ end
5
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Cloudflare
4
4
  module AI
5
- VERSION = "0.4.0"
5
+ VERSION = "0.5.1"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudflare-ai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ajay Krishnan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-22 00:00:00.000000000 Z
11
+ date: 2024-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -101,6 +101,7 @@ files:
101
101
  - lib/cloudflare/ai/results/text_classification.rb
102
102
  - lib/cloudflare/ai/results/text_embedding.rb
103
103
  - lib/cloudflare/ai/results/text_generation.rb
104
+ - lib/cloudflare/ai/results/translation.rb
104
105
  - lib/cloudflare/ai/version.rb
105
106
  homepage: https://rubygems.org/gems/cloudflare-ai
106
107
  licenses: