cloudflare-ai 0.3.0 → 0.4.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: b0a6b80e669a5c4d64ec87433ed1c3426ee0a2a1087016773c5648c799b34912
4
- data.tar.gz: fe1d3b2afb5f70cbe476b1b8e006819b2e6e469184169993a15c7c4c34b89ca8
3
+ metadata.gz: 4de1d8c20767aab8d40b96fa01a447e1ab83a1586cad6c4a3d7597331cabc5bd
4
+ data.tar.gz: 7b912c4bf7bb23ec4f2befca92c966656ac4ab0527b03857adaf14b8e8b87a34
5
5
  SHA512:
6
- metadata.gz: 84dcec88bcd44e4a7b004235b15b559d924c1e8dfce552234b2d9286762ab2a12d282954e8d863523b1c9170c24f49a772da7320b5b475b66b79b141ac2c532d
7
- data.tar.gz: 0f676b2accb65edc63b2b9b1c2b8a532ebfedba588dfdcb1ae38ccfdf7edb4869c1ebee2060e887e5ac166772cda91d3e43e2ca650e878eb63b8c964d7fc19e1
6
+ metadata.gz: c87c773d129790a865a524a56c1ab545a51f3d8a54825b4e02d092282b733f5dbee9a59d1536fd8df096aac47c7d94ae4466e6d6f94aa90115ec4cebdd4d4b0e
7
+ data.tar.gz: 45ead04646ae3756d05d0d4805bfbfcd509d421b08bfec63a53ff3736abd16887786d17741df542e4596500779881d35e9d8af6ffdb07c2bd2ffdfc632e17883
data/README.md CHANGED
@@ -61,6 +61,19 @@ Thiis gem provides a client that wraps around [Cloudflare's REST API](https://de
61
61
  client = Cloudflare::AI::Client.new(account_id: ENV["CLOUDFLARE_ACCOUNT_ID"], api_token: ENV["CLOUDFLARE_API_TOKEN"])
62
62
  ```
63
63
 
64
+ ### Model selection
65
+ The model name is an optional parameter to every one of the client methods described below.
66
+ For example, if an example is documented as
67
+ ```ruby
68
+ result = client.complete(prompt: "Hello my name is")
69
+ ```
70
+ this is implicitly the same as
71
+ ```ruby
72
+ result = client.complete(prompt: "Hello my name is", model: "@cf/meta/llama-2-7b-chat-fp16")
73
+ ```
74
+ The full list of supported models is available here: [models.rb](lib/cloudflare/ai/models.rb).
75
+ More information is available [in the cloudflare documentation](https://developers.cloudflare.com/workers-ai/models/).
76
+ The default model used is the first enumerated model in the applicable set in [models.rb](lib/cloudflare/ai/models.rb).
64
77
 
65
78
  ### Text generation (chat / scoped prompt)
66
79
  ```ruby
@@ -120,7 +133,16 @@ result = client.embed(text: ["Hello", "World"])
120
133
  ```
121
134
 
122
135
  #### Result object
123
- All invocations of the `embedding` methods return a `Cloudflare::AI::Results::TextEmbedding`.
136
+ All invocations of the `embed` methods return a `Cloudflare::AI::Results::TextEmbedding`.
137
+
138
+ ### Text classification
139
+ ```ruby
140
+ result = client.classify(text: "You meanie!")
141
+ p result.result # => [{"label"=>"NEGATIVE", "score"=>0.6647962927818298}, {"label"=>"POSITIVE", "score"=>0.3352036774158478}]
142
+ ```
143
+
144
+ #### Result object
145
+ All invocations of the `classify` methods return a `Cloudflare::AI::Results::TextClassification`.
124
146
 
125
147
  # Logging
126
148
 
@@ -11,17 +11,24 @@ 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
 
21
- def complete(prompt:, model_name: default_text_generation_model_name, &block)
21
+ def classify(text:, model_name: Cloudflare::AI::Models.text_classification.first)
22
+ url = service_url_for(account_id: account_id, model_name: model_name)
23
+ payload = {text: text}.to_json
24
+
25
+ Cloudflare::AI::Results::TextClassification.new(connection.post(url, payload).body)
26
+ end
27
+
28
+ def complete(prompt:, model_name: default_text_generation_model_name, max_tokens: default_max_tokens, &block)
22
29
  url = service_url_for(account_id: account_id, model_name: model_name)
23
30
  stream = block ? true : false
24
- payload = create_streamable_payload({prompt: prompt}, stream: stream)
31
+ payload = create_streamable_payload({prompt: prompt}, stream: stream, max_tokens: max_tokens)
25
32
  post_streamable_request(url, payload, &block)
26
33
  end
27
34
 
@@ -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,3 @@
1
+ class Cloudflare::AI::Results::TextClassification < Cloudflare::AI::Result
2
+ # Empty seam kept for consistency with other result objects that have more complexity.
3
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Cloudflare
4
4
  module AI
5
- VERSION = "0.3.0"
5
+ VERSION = "0.4.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.3.0
4
+ version: 0.4.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-21 00:00:00.000000000 Z
11
+ date: 2024-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -98,6 +98,7 @@ files:
98
98
  - lib/cloudflare/ai/message.rb
99
99
  - lib/cloudflare/ai/models.rb
100
100
  - lib/cloudflare/ai/result.rb
101
+ - lib/cloudflare/ai/results/text_classification.rb
101
102
  - lib/cloudflare/ai/results/text_embedding.rb
102
103
  - lib/cloudflare/ai/results/text_generation.rb
103
104
  - lib/cloudflare/ai/version.rb