ruby-openai 5.0.0 → 5.1.0

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: 4f487bd64d6f7a7c4f0ccc3198d9bec592b199792763b6885b12b21f267ff80a
4
- data.tar.gz: 566af61bb906edbb2315ee343aae62f82e6837aa318dfb505d67d73497fcb27a
3
+ metadata.gz: 6ea6e5d9149ffa94f53c0952491e827a5a082830cb5a4ecbdafe4e4d2523f54e
4
+ data.tar.gz: 1e8072b9fce1c48612b0120e1df4d6f45e422daec13a41e4b985f60d6cc07f6a
5
5
  SHA512:
6
- metadata.gz: 7e500ce6b1cff92bdb78b4cf455aac251cb229e43e52c4c863a98f44a351ac1ef4abd9692dde13f822f458b7373ee063e34dc0670b72dc67e02827533e316b13
7
- data.tar.gz: 5d90d4ae80e14da163655a29d598c09834cfd0cad728bebe18f09b015aff7fa0881e736408360b228aa4921497de3f41b6122ee0a5e7a6011001b32403f70b41
6
+ metadata.gz: 00b71588418d3c33fb2511147e9a500755cf864c1d4cd7c420599b7b7af7d10bd4bb0b1490ce5399efbf38ce2527461ad40c21f20685b6aba40db275d7c9c633
7
+ data.tar.gz: e2574855121d6ed5126aa809b32feab815b1bd8f668c9eff1d3f6c9e9a25ed83cbb45c19e1900695b814d79b708a9cedfaf911ea5112ba2ff6eadcc76332f980
data/CHANGELOG.md CHANGED
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [5.1.0] - 2023-08-20
9
+
10
+ ### Added
11
+
12
+ - Added rough_token_count to estimate tokens in a string according to OpenAI's "rules of thumb". Thank you to [@jamiemccarthy](https://github.com/jamiemccarthy) for the idea and implementation!
13
+
8
14
  ## [5.0.0] - 2023-08-14
9
15
 
10
16
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-openai (5.0.0)
4
+ ruby-openai (5.1.0)
5
5
  faraday (>= 1)
6
6
  faraday-multipart (>= 1)
7
7
 
data/README.md CHANGED
@@ -24,13 +24,17 @@ gem "ruby-openai"
24
24
 
25
25
  And then execute:
26
26
 
27
+ ```bash
27
28
  $ bundle install
29
+ ```
28
30
 
29
31
  ### Gem install
30
32
 
31
33
  Or install with:
32
34
 
35
+ ```bash
33
36
  $ gem install ruby-openai
37
+ ```
34
38
 
35
39
  and require with:
36
40
 
@@ -86,7 +90,8 @@ client = OpenAI::Client.new(
86
90
  extra_headers: {
87
91
  "X-Proxy-TTL" => "43200", # For https://github.com/6/openai-caching-proxy-worker#specifying-a-cache-ttl
88
92
  "X-Proxy-Refresh": "true", # For https://github.com/6/openai-caching-proxy-worker#refreshing-the-cache
89
- "Helicone-Auth": "Bearer HELICONE_API_KEY" # For https://docs.helicone.ai/getting-started/integration-method/openai-proxy
93
+ "Helicone-Auth": "Bearer HELICONE_API_KEY", # For https://docs.helicone.ai/getting-started/integration-method/openai-proxy
94
+ "helicone-stream-force-format" => "true", # Use this with Helicone otherwise streaming drops chunks # https://github.com/alexrudall/ruby-openai/issues/251
90
95
  }
91
96
  )
92
97
  ```
@@ -122,6 +127,18 @@ To use the [Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/cognit
122
127
 
123
128
  where `AZURE_OPENAI_URI` is e.g. `https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo`
124
129
 
130
+ ### Counting Tokens
131
+
132
+ OpenAI parses prompt text into [tokens](https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them), which are words or portions of words. (These tokens are unrelated to your API access_token.) Counting tokens can help you estimate your [costs](https://openai.com/pricing). It can also help you ensure your prompt text size is within the max-token limits of your model's context window, and choose an appropriate [`max_tokens`](https://platform.openai.com/docs/api-reference/chat/create#chat/create-max_tokens) completion parameter so your response will fit as well.
133
+
134
+ To estimate the token-count of your text:
135
+
136
+ ```ruby
137
+ OpenAI.rough_token_count("Your text")
138
+ ```
139
+
140
+ If you need a more accurate count, try [tiktoken_ruby](https://github.com/IAPark/tiktoken_ruby).
141
+
125
142
  ### Models
126
143
 
127
144
  There are different models that can be used to generate text. For a full list and to retrieve information about a single model:
@@ -180,7 +197,7 @@ client.chat(
180
197
  # => "Anna is a young woman in her mid-twenties, with wavy chestnut hair that falls to her shoulders..."
181
198
  ```
182
199
 
183
- Note: the API docs state that token usage is included in the streamed chat chunk objects, but this doesn't currently appear to be the case. If you need to work out how many tokens are being used while streaming, try [tiktoken_ruby](https://github.com/IAPark/tiktoken_ruby).
200
+ Note: the API docs state that token usage is included in the streamed chat chunk objects, but this doesn't currently appear to be the case. To count tokens while streaming, try `OpenAI.rough_token_count` or [tiktoken_ruby](https://github.com/IAPark/tiktoken_ruby).
184
201
 
185
202
  ### Functions
186
203
 
@@ -1,3 +1,3 @@
1
1
  module OpenAI
2
- VERSION = "5.0.0".freeze
2
+ VERSION = "5.1.0".freeze
3
3
  end
data/lib/openai.rb CHANGED
@@ -52,4 +52,16 @@ module OpenAI
52
52
  def self.configure
53
53
  yield(configuration)
54
54
  end
55
+
56
+ # Estimate the number of tokens in a string, using the rules of thumb from OpenAI:
57
+ # https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them
58
+ def self.rough_token_count(content = "")
59
+ raise ArgumentError, "rough_token_count requires a string" unless content.is_a? String
60
+ return 0 if content.empty?
61
+
62
+ count_by_chars = content.size / 4.0
63
+ count_by_words = content.split.size * 4.0 / 3
64
+ estimate = ((count_by_chars + count_by_words) / 2.0).round
65
+ [1, estimate].max
66
+ end
55
67
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-openai
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0
4
+ version: 5.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-14 00:00:00.000000000 Z
11
+ date: 2023-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday