cloudflare-ai 0.4.1 → 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: 4de1d8c20767aab8d40b96fa01a447e1ab83a1586cad6c4a3d7597331cabc5bd
4
- data.tar.gz: 7b912c4bf7bb23ec4f2befca92c966656ac4ab0527b03857adaf14b8e8b87a34
3
+ metadata.gz: 9085ff95f730366db7b3261cc94935f782d719b8129dd1341796ee6d66fd373c
4
+ data.tar.gz: 60b29886c88200aebdd1c46e743a28dd597540144494e8288098e05c6ddb8e04
5
5
  SHA512:
6
- metadata.gz: c87c773d129790a865a524a56c1ab545a51f3d8a54825b4e02d092282b733f5dbee9a59d1536fd8df096aac47c7d94ae4466e6d6f94aa90115ec4cebdd4d4b0e
7
- data.tar.gz: 45ead04646ae3756d05d0d4805bfbfcd509d421b08bfec63a53ff3736abd16887786d17741df542e4596500779881d35e9d8af6ffdb07c2bd2ffdfc632e17883
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.
@@ -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
@@ -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.1"
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.1
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: