You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+43Lines changed: 43 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,49 @@ 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
+
14
+
## [5.0.0] - 2023-08-14
15
+
16
+
### Added
17
+
18
+
- Support multi-tenant use of the gem! Each client now holds its own config, so you can create unlimited clients in the same project, for example to Azure and OpenAI, or for different headers, access keys, etc.
19
+
-[BREAKING-ish] This change should only break your usage of ruby-openai if you are directly calling class methods like `OpenAI::Client.get` for some reason, as they are now instance methods. Normal usage of the gem should be unaffected, just you can make new clients and they'll keep their own config if you want, overriding the global config.
20
+
- Huge thanks to [@petergoldstein](https://github.com/petergoldstein) for his original work on this, [@cthulhu](https://github.com/cthulhu) for testing and many others for reviews and suggestions.
21
+
22
+
### Changed
23
+
24
+
-[BREAKING] Move audio related method to Audio model from Client model. You will need to update your code to handle this change, changing `client.translate` to `client.audio.translate` and `client.transcribe` to `client.audio.transcribe`.
25
+
26
+
## [4.3.2] - 2023-08-14
27
+
28
+
### Fixed
29
+
30
+
- Don't overwrite config extra-headers when making a client without different ones. Thanks to [@swistaczek](https://github.com/swistaczek) for raising this!
31
+
- Include extra-headers for Azure requests.
32
+
33
+
## [4.3.1] - 2023-08-13
34
+
35
+
### Fixed
36
+
37
+
- Tempfiles can now be sent to the API as well as Files, eg for Whisper. Thanks to [@codergeek121](https://github.com/codergeek121) for the fix!
38
+
39
+
## [4.3.0] - 2023-08-12
40
+
41
+
### Added
42
+
43
+
- Add extra-headers to config to allow setting openai-caching-proxy-worker TTL, Helicone Auth and anything else ya need. Ty to [@deltaguita](https://github.com/deltaguita) and [@marckohlbrugge](https://github.com/marckohlbrugge) for the PR!
44
+
45
+
## [4.2.0] - 2023-06-20
46
+
47
+
### Added
48
+
49
+
- Add Azure OpenAI Service support. Thanks to [@rmachielse](https://github.com/rmachielse) and [@steffansluis](https://github.com/steffansluis) for the PR and to everyone who requested this feature!
Copy file name to clipboardExpand all lines: README.md
+119-6Lines changed: 119 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -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
@@ -68,15 +72,27 @@ Then you can create a client like this:
68
72
client =OpenAI::Client.new
69
73
```
70
74
75
+
You can still override the config defaults when making new clients; any options not included will fall back to any global config set with OpenAI.configure. e.g. in this example the organization_id, request_timeout, etc. will fallback to any set globally using OpenAI.configure, with only the access_token overridden:
The default timeout for any request using this library is 120 seconds. You can change that by passing a number of seconds to the `request_timeout` when initializing the client. You can also change the base URI used for all requests, eg. to use observability tools like [Helicone](https://docs.helicone.ai/quickstart/integrate-in-one-line-of-code):
83
+
The default timeout for any request using this library is 120 seconds. You can change that by passing a number of seconds to the `request_timeout` when initializing the client. You can also change the base URI used for all requests, eg. to use observability tools like [Helicone](https://docs.helicone.ai/quickstart/integrate-in-one-line-of-code), and add arbitrary other headers e.g. for [openai-caching-proxy-worker](https://github.com/6/openai-caching-proxy-worker):
74
84
75
85
```ruby
76
86
client =OpenAI::Client.new(
77
87
access_token:"access_token_goes_here",
78
88
uri_base:"https://oai.hconeai.com/",
79
-
request_timeout:240
89
+
request_timeout:240,
90
+
extra_headers: {
91
+
"X-Proxy-TTL" => "43200", # For https://github.com/6/openai-caching-proxy-worker#specifying-a-cache-ttl
92
+
"X-Proxy-Refresh": "true", # For https://github.com/6/openai-caching-proxy-worker#refreshing-the-cache
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
where `AZURE_OPENAI_URI` is e.g. `https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo`
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
+
94
142
### Models
95
143
96
144
There are different models that can be used to generate text. For a full list and to retrieve information about a single model:
@@ -149,6 +197,68 @@ client.chat(
149
197
# => "Anna is a young woman in her mid-twenties, with wavy chestnut hair that falls to her shoulders..."
150
198
```
151
199
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).
201
+
202
+
### Functions
203
+
204
+
You can describe and pass in functions and the model will intelligently choose to output a JSON object containing arguments to call those them. For example, if you want the model to use your method `get_current_weather` to get the current weather in a given location:
You can use the embeddings endpoint to get a vector of numbers representing an input. You can then compare these vectors for different inputs to efficiently check how similar the inputs are.
186
296
187
297
```ruby
188
-
client.embeddings(
298
+
response =client.embeddings(
189
299
parameters: {
190
-
model:"babbage-similarity",
300
+
model:"text-embedding-ada-002",
191
301
input:"The food was delicious and the waiter..."
192
302
}
193
303
)
304
+
305
+
puts response.dig("data", 0, "embedding")
306
+
# => Vector representation of your embedding
194
307
```
195
308
196
309
### Files
@@ -321,7 +434,7 @@ Whisper is a speech to text model that can be used to generate text based on aud
321
434
The translations API takes as input the audio file in any of the supported languages and transcribes the audio into English.
322
435
323
436
```ruby
324
-
response = client.translate(
437
+
response = client.audio.translate(
325
438
parameters: {
326
439
model:"whisper-1",
327
440
file:File.open("path_to_file", "rb"),
@@ -335,7 +448,7 @@ puts response["text"]
335
448
The transcriptions API takes as input the audio file you want to transcribe and returns the text in the desired output file format.
0 commit comments