ruby-advanced-openai 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ebf6f6a2c422f1842883062ffe4e6dcc3315ed79323f126ebf1c759d005d68f4
4
+ data.tar.gz: fc2be4ce29643e0f38a67adda1488364f2f280a139e66b8a484548f4ac97c5ac
5
+ SHA512:
6
+ metadata.gz: '06879b030e8f7b254101db3e1b11ca024471dc3491ee853552a167e4768529045761860c8d177fb7075e66af52c4a87b6816a193e1fd8abaf04b73ca64d945c1'
7
+ data.tar.gz: '0028141fb741a851be88708ffa2c4eaffa0fd91470d45995f6b99a1297c411d16a387543f11849ee78602599746f63691b8aa003c43e78dde9c6aa5a8ba0784e'
@@ -0,0 +1,45 @@
1
+ version: 2.1 # Use 2.1 to enable using orbs and other features.
2
+
3
+ # Declare the orbs that we'll use in our config.
4
+ orbs:
5
+ ruby: circleci/ruby@1.0
6
+
7
+ jobs:
8
+ rubocop:
9
+ parallelism: 1
10
+ docker:
11
+ - image: cimg/ruby:3.1-node
12
+ steps:
13
+ - checkout
14
+ - ruby/install-deps
15
+ - run:
16
+ name: Run Rubocop
17
+ command: bundle exec rubocop
18
+ test:
19
+ parameters:
20
+ ruby-image:
21
+ type: string
22
+ parallelism: 1
23
+ docker:
24
+ - image: << parameters.ruby-image >>
25
+ steps:
26
+ - checkout
27
+ - ruby/install-deps
28
+ - run:
29
+ name: Run tests
30
+ command: bundle exec rspec -fd
31
+
32
+ workflows:
33
+ version: 2
34
+ checks:
35
+ jobs:
36
+ - rubocop
37
+ - test:
38
+ matrix:
39
+ parameters:
40
+ ruby-image:
41
+ - cimg/ruby:2.6-node
42
+ - cimg/ruby:2.7-node
43
+ - cimg/ruby:3.0-node
44
+ - cimg/ruby:3.1-node
45
+ - cimg/ruby:3.2-node
@@ -0,0 +1,38 @@
1
+ ---
2
+ name: Bug report
3
+ about: Create a report to help us improve
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Describe the bug**
11
+ A clear and concise description of what the bug is.
12
+
13
+ **To Reproduce**
14
+ Steps to reproduce the behavior:
15
+ 1. Go to '...'
16
+ 2. Click on '....'
17
+ 3. Scroll down to '....'
18
+ 4. See error
19
+
20
+ **Expected behavior**
21
+ A clear and concise description of what you expected to happen.
22
+
23
+ **Screenshots**
24
+ If applicable, add screenshots to help explain your problem.
25
+
26
+ **Desktop (please complete the following information):**
27
+ - OS: [e.g. iOS]
28
+ - Browser [e.g. chrome, safari]
29
+ - Version [e.g. 22]
30
+
31
+ **Smartphone (please complete the following information):**
32
+ - Device: [e.g. iPhone6]
33
+ - OS: [e.g. iOS8.1]
34
+ - Browser [e.g. stock browser, safari]
35
+ - Version [e.g. 22]
36
+
37
+ **Additional context**
38
+ Add any other context about the problem here.
@@ -0,0 +1,20 @@
1
+ ---
2
+ name: Feature request
3
+ about: Suggest an idea for this project
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Is your feature request related to a problem? Please describe.**
11
+ A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12
+
13
+ **Describe the solution you'd like**
14
+ A clear and concise description of what you want to happen.
15
+
16
+ **Describe alternatives you've considered**
17
+ A clear and concise description of any alternative solutions or features you've considered.
18
+
19
+ **Additional context**
20
+ Add any other context or screenshots about the feature request here.
@@ -0,0 +1,15 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: bundler
4
+ directory: "/"
5
+ schedule:
6
+ interval: daily
7
+ open-pull-requests-limit: 10
8
+ ignore:
9
+ - dependency-name: webmock
10
+ versions:
11
+ - 3.11.1
12
+ - 3.11.3
13
+ - dependency-name: rspec
14
+ versions:
15
+ - 3.10.0
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ .byebug_history
14
+ .env
15
+
16
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,26 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+ SuggestExtensions: false
5
+
6
+ Style/Documentation:
7
+ # Skips checking to make sure top level modules / classes have a comment.
8
+ Enabled: false
9
+
10
+ Layout/LineLength:
11
+ Max: 100
12
+ Exclude:
13
+ - "**/*.gemspec"
14
+
15
+ Metrics/AbcSize:
16
+ Max: 20
17
+
18
+ Metrics/BlockLength:
19
+ Exclude:
20
+ - "spec/**/*"
21
+
22
+ Style/StringLiterals:
23
+ EnforcedStyle: double_quotes
24
+
25
+ Style/FrozenStringLiteralComment:
26
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,281 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [4.1.0] - 2023-05-15
9
+
10
+ ### Added
11
+
12
+ - Add the ability to trigger any callable object as stream chunks come through, not just Procs. Big thanks to [@obie](https://github.com/obie) for this change.
13
+
14
+ ## [4.0.0] - 2023-04-25
15
+
16
+ ### Added
17
+
18
+ - Add the ability to stream Chat responses from the API! Thanks to everyone who requested this and made suggestions.
19
+ - Added instructions for streaming to the README.
20
+
21
+ ### Changed
22
+
23
+ - Switch HTTP library from HTTParty to Faraday to allow streaming and future feature and performance improvements.
24
+ - [BREAKING] Endpoints now return JSON rather than HTTParty objects. You will need to update your code to handle this change, changing `JSON.parse(response.body)["key"]` and `response.parsed_response["key"]` to just `response["key"]`.
25
+
26
+ ## [3.7.0] - 2023-03-25
27
+
28
+ ### Added
29
+
30
+ - Allow the usage of proxy base URIs like https://www.helicone.ai/. Thanks to [@mmmaia](https://github.com/mmmaia) for the PR!
31
+
32
+ ## [3.6.0] - 2023-03-22
33
+
34
+ ### Added
35
+
36
+ - Add much-needed ability to increase HTTParty timeout, and set default to 120 seconds. Thanks to [@mbackermann](https://github.com/mbackermann) for the PR and to everyone who requested this!
37
+
38
+ ## [3.5.0] - 2023-03-02
39
+
40
+ ### Added
41
+
42
+ - Add Client#transcribe and Client translate endpoints - Whisper over the wire! Thanks to [@Clemalfroy](https://github.com/Clemalfroy)
43
+
44
+ ## [3.4.0] - 2023-03-01
45
+
46
+ ### Added
47
+
48
+ - Add Client#chat endpoint - ChatGPT over the wire!
49
+
50
+ ## [3.3.0] - 2023-02-15
51
+
52
+ ### Changed
53
+
54
+ - Replace ::Ruby::OpenAI namespace with ::OpenAI - thanks [@kmcphillips](https://github.com/kmcphillips) for this work!
55
+ - To upgrade, change `require 'ruby/openai'` to `require 'openai'` and change all references to `Ruby::OpenAI` to `OpenAI`.
56
+
57
+ ## [3.2.0] - 2023-02-13
58
+
59
+ ### Added
60
+
61
+ - Add Files#content endpoint - thanks [@unixneo](https://github.com/unixneo) for raising!
62
+
63
+ ## [3.1.0] - 2023-02-13
64
+
65
+ ### Added
66
+
67
+ - Add Finetunes#delete endpoint - thanks [@lancecarlson](https://github.com/lancecarlson) for flagging this.
68
+ - Add VCR header and body matching - thanks [@petergoldstein](https://github.com/petergoldstein)!
69
+
70
+ ### Fixed
71
+
72
+ - Update File#upload specs to remove deprecated `answers` purpose.
73
+
74
+ ## [3.0.3] - 2023-01-07
75
+
76
+ ### Added
77
+
78
+ - Add ability to run the specs without VCR cassettes using `NO_VCR=true bundle exec rspec`.
79
+ - Add Ruby 3.2 to CircleCI config - thanks [@petergoldstein](https://github.com/petergoldstein)!
80
+ - A bit of detail added to the README on DALLE image sizes - thanks [@ndemianc](https://github.com/ndemianc)!
81
+
82
+ ### Fixed
83
+
84
+ - Fix finetunes and files uploads endpoints - thanks [@chaadow](https://github.com/chaadow) for your PR on this and [@petergoldstein](https://github.com/petergoldstein) for the PR we ultimately went with.
85
+
86
+ ## [3.0.2] - 2022-12-27
87
+
88
+ ### Fixed
89
+
90
+ - Fixed Images#generate and Finetunes#create which were broken by a double call of to_json.
91
+ - Thanks [@konung](https://github.com/konung) for spotting this!
92
+
93
+ ## [3.0.1] - 2022-12-26
94
+
95
+ ### Removed
96
+
97
+ - [BREAKING] Remove deprecated answers, classifications, embeddings, engines and search endpoints.
98
+ - [BREAKING] Remove ability to pass engine to completions and embeddings outside of the parameters hash.
99
+
100
+ ## [3.0.0] - 2022-12-26
101
+
102
+ ### Added
103
+
104
+ - Add ability to set access_token via gem configuration.
105
+ - Thanks [@grjones](https://github.com/grjones) and [@aquaflamingo](https://github.com/aquaflamingo) for raising this and [@feministy](https://github.com/feministy) for the [excellent guide](https://github.com/feministy/lizabinante.com/blob/stable/source/2016-01-30-creating-a-configurable-ruby-gem.markdown#configuration-block-the-end-goal) to adding config to a gem.
106
+
107
+ ### Removed
108
+
109
+ - [BREAKING] Remove ability to include access_token directly via ENV vars.
110
+ - [BREAKING] Remove ability to pass API version directly to endpoints.
111
+
112
+ ## [2.3.0] - 2022-12-23
113
+
114
+ ### Added
115
+
116
+ - Add Images#edit and Images#variations endpoint to modify images with DALL·E.
117
+
118
+ ## [2.2.0] - 2022-12-15
119
+
120
+ ### Added
121
+
122
+ - Add Organization ID to headers so users can charge credits to the correct organization.
123
+ - Thanks [@mridul911](https://github.com/mridul911) for raising this and [@maks112v](https://github.com/maks112v) for adding it!
124
+
125
+ ## [2.1.0] - 2022-11-13
126
+
127
+ ### Added
128
+
129
+ - Add Images#generate endpoint to generate images with DALL·E!
130
+
131
+ ## [2.0.1] - 2022-10-22
132
+
133
+ ### Removed
134
+
135
+ - Deprecate Client#answers endpoint.
136
+ - Deprecate Client#classifications endpoint.
137
+
138
+ ## [2.0.0] - 2022-09-19
139
+
140
+ ### Removed
141
+
142
+ - [BREAKING] Remove support for Ruby 2.5.
143
+ - [BREAKING] Remove support for passing `query`, `documents` or `file` as top-level parameters to `Client#search`.
144
+ - Deprecate Client#search endpoint.
145
+ - Deprecate Client#engines endpoints.
146
+
147
+ ### Added
148
+
149
+ - Add Client#models endpoints to list and query available models.
150
+
151
+ ## [1.5.0] - 2022-09-18
152
+
153
+ ### Added
154
+
155
+ - Add Client#moderations endpoint to check OpenAI's Content Policy.
156
+ - Add Client#edits endpoints to transform inputs according to instructions.
157
+
158
+ ## [1.4.0] - 2021-12-11
159
+
160
+ ### Added
161
+
162
+ - Add Client#engines endpoints to list and query available engines.
163
+ - Add Client#finetunes endpoints to create and use fine-tuned models.
164
+ - Add Client#embeddings endpoint to get vector representations of inputs.
165
+ - Add tests and examples for more engines.
166
+
167
+ ## [1.3.1] - 2021-07-14
168
+
169
+ ### Changed
170
+
171
+ - Add backwards compatibility from Ruby 2.5+.
172
+
173
+ ## [1.3.0] - 2021-04-18
174
+
175
+ ### Added
176
+
177
+ - Add Client#classifications to predict the most likely labels based on examples or a file.
178
+
179
+ ### Fixed
180
+
181
+ - Fixed Files#upload which was previously broken by the validation code!
182
+
183
+ ## [1.2.2] - 2021-04-18
184
+
185
+ ### Changed
186
+
187
+ - Add Client#search(parameters:) to allow passing `max_rerank` or `return_metadata`.
188
+ - Deprecate Client#search with query, file or document parameters at the top level.
189
+ - Thanks [@stevegeek](https://github.com/stevegeek) for pointing this issue out!
190
+
191
+ ## [1.2.1] - 2021-04-11
192
+
193
+ ### Added
194
+
195
+ - Add validation of JSONL files to make it easier to debug during upload.
196
+
197
+ ## [1.2.0] - 2021-04-08
198
+
199
+ ### Added
200
+
201
+ - Add Client#answers endpoint for question/answer response on documents or a file.
202
+
203
+ ## [1.1.0] - 2021-04-07
204
+
205
+ ### Added
206
+
207
+ - Add Client#files to allow file upload.
208
+ - Add Client#search(file:) so you can search a file.
209
+
210
+ ## [1.0.0] - 2021-02-01
211
+
212
+ ### Removed
213
+
214
+ - Remove deprecated method Client#call - use Client#completions instead.
215
+
216
+ ### Changed
217
+
218
+ - Rename 'master' branch to 'main' branch.
219
+ - Bump dependencies.
220
+
221
+ ## [0.3.0] - 2020-11-22
222
+
223
+ ### Added
224
+
225
+ - Add Client#completions to allow all parameters.
226
+
227
+ ### Changed
228
+
229
+ - Deprecate Client#call.
230
+ - Update README.
231
+
232
+ ## [0.2.0] - 2020-11-22
233
+
234
+ ### Added
235
+
236
+ - Add method to use the search endpoint.
237
+
238
+ ## [0.1.4] - 2020-10-18
239
+
240
+ ### Changed
241
+
242
+ - Bump Rubocop to 3.9.2.
243
+ - Bump Webmock to 3.9.1.
244
+
245
+ ## [0.1.3] - 2020-09-09
246
+
247
+ ### Changed
248
+
249
+ - Add ability to change API version in the future.
250
+ - Fix README typos.
251
+
252
+ ## [0.1.2] - 2020-09-09
253
+
254
+ ### Added
255
+
256
+ - Add tests and cached responses for the different engines.
257
+ - Add issue templates.
258
+
259
+ ### Changed
260
+
261
+ - Add README instructions for using the gem without dotenv.
262
+ - Add list of engines to README.
263
+
264
+ ## [0.1.1] - 2020-09-08
265
+
266
+ ### Added
267
+
268
+ - Run Rubocop on pulls using CircleCI.
269
+
270
+ ### Changed
271
+
272
+ - Clean up CircleCI config file.
273
+
274
+ ## [0.1.0] - 2020-09-06
275
+
276
+ ### Added
277
+
278
+ - Initialise repository.
279
+ - Add OpenAI::Client to connect to OpenAI API using user credentials.
280
+ - Add spec for Client with a cached response using VCR.
281
+ - Add CircleCI to run the specs on pull requests.
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at alexrudall@users.noreply.github.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,3 @@
1
+ ## Contributing
2
+
3
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alexrudall/ruby-openai. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/alexrudall/ruby-openai/blob/main/CODE_OF_CONDUCT.md).
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Include gem dependencies from ruby-openai.gemspec
4
+ gemspec
5
+
6
+ gem "byebug", "~> 11.1.3"
7
+ gem "dotenv", "~> 2.8.1"
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.12"
10
+ gem "rubocop", "~> 1.50.2"
11
+ gem "vcr", "~> 6.1.0"
12
+ gem "webmock", "~> 3.18.1"
data/Gemfile.lock ADDED
@@ -0,0 +1,84 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby-advanced-openai (4.1.0)
5
+ faraday (>= 1)
6
+ faraday-multipart (>= 1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.8.1)
12
+ public_suffix (>= 2.0.2, < 6.0)
13
+ ast (2.4.2)
14
+ byebug (11.1.3)
15
+ crack (0.4.5)
16
+ rexml
17
+ diff-lcs (1.5.0)
18
+ dotenv (2.8.1)
19
+ faraday (2.7.4)
20
+ faraday-net_http (>= 2.0, < 3.1)
21
+ ruby2_keywords (>= 0.0.4)
22
+ faraday-multipart (1.0.4)
23
+ multipart-post (~> 2)
24
+ faraday-net_http (3.0.2)
25
+ hashdiff (1.0.1)
26
+ json (2.6.3)
27
+ multipart-post (2.3.0)
28
+ parallel (1.22.1)
29
+ parser (3.2.2.0)
30
+ ast (~> 2.4.1)
31
+ public_suffix (5.0.1)
32
+ rainbow (3.1.1)
33
+ rake (13.0.6)
34
+ regexp_parser (2.8.0)
35
+ rexml (3.2.5)
36
+ rspec (3.12.0)
37
+ rspec-core (~> 3.12.0)
38
+ rspec-expectations (~> 3.12.0)
39
+ rspec-mocks (~> 3.12.0)
40
+ rspec-core (3.12.0)
41
+ rspec-support (~> 3.12.0)
42
+ rspec-expectations (3.12.2)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.12.0)
45
+ rspec-mocks (3.12.3)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.12.0)
48
+ rspec-support (3.12.0)
49
+ rubocop (1.50.2)
50
+ json (~> 2.3)
51
+ parallel (~> 1.10)
52
+ parser (>= 3.2.0.0)
53
+ rainbow (>= 2.2.2, < 4.0)
54
+ regexp_parser (>= 1.8, < 3.0)
55
+ rexml (>= 3.2.5, < 4.0)
56
+ rubocop-ast (>= 1.28.0, < 2.0)
57
+ ruby-progressbar (~> 1.7)
58
+ unicode-display_width (>= 2.4.0, < 3.0)
59
+ rubocop-ast (1.28.0)
60
+ parser (>= 3.2.1.0)
61
+ ruby-progressbar (1.13.0)
62
+ ruby2_keywords (0.0.5)
63
+ unicode-display_width (2.4.2)
64
+ vcr (6.1.0)
65
+ webmock (3.18.1)
66
+ addressable (>= 2.8.0)
67
+ crack (>= 0.3.2)
68
+ hashdiff (>= 0.4.0, < 2.0.0)
69
+
70
+ PLATFORMS
71
+ ruby
72
+
73
+ DEPENDENCIES
74
+ byebug (~> 11.1.3)
75
+ dotenv (~> 2.8.1)
76
+ rake (~> 13.0)
77
+ rspec (~> 3.12)
78
+ rubocop (~> 1.50.2)
79
+ ruby-advanced-openai!
80
+ vcr (~> 6.1.0)
81
+ webmock (~> 3.18.1)
82
+
83
+ BUNDLED WITH
84
+ 2.4.7
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Alex
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # Ruby Advanced OpenAI
2
+ I will be creating an advanced gem for utilizing OpenAI, based on https://github.com/alexrudall/ruby-openai.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "openai"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,56 @@
1
+ module OpenAI
2
+ class Client
3
+ extend OpenAI::HTTP
4
+
5
+ def initialize(access_token: nil, organization_id: nil, uri_base: nil, request_timeout: nil)
6
+ OpenAI.configuration.access_token = access_token if access_token
7
+ OpenAI.configuration.organization_id = organization_id if organization_id
8
+ OpenAI.configuration.uri_base = uri_base if uri_base
9
+ OpenAI.configuration.request_timeout = request_timeout if request_timeout
10
+ end
11
+
12
+ def chat(parameters: {})
13
+ OpenAI::Client.json_post(path: "/chat/completions", parameters: parameters)
14
+ end
15
+
16
+ def completions(parameters: {})
17
+ OpenAI::Client.json_post(path: "/completions", parameters: parameters)
18
+ end
19
+
20
+ def edits(parameters: {})
21
+ OpenAI::Client.json_post(path: "/edits", parameters: parameters)
22
+ end
23
+
24
+ def embeddings(parameters: {})
25
+ OpenAI::Client.json_post(path: "/embeddings", parameters: parameters)
26
+ end
27
+
28
+ def files
29
+ @files ||= OpenAI::Files.new
30
+ end
31
+
32
+ def finetunes
33
+ @finetunes ||= OpenAI::Finetunes.new
34
+ end
35
+
36
+ def images
37
+ @images ||= OpenAI::Images.new
38
+ end
39
+
40
+ def models
41
+ @models ||= OpenAI::Models.new
42
+ end
43
+
44
+ def moderations(parameters: {})
45
+ OpenAI::Client.json_post(path: "/moderations", parameters: parameters)
46
+ end
47
+
48
+ def transcribe(parameters: {})
49
+ OpenAI::Client.multipart_post(path: "/audio/transcriptions", parameters: parameters)
50
+ end
51
+
52
+ def translate(parameters: {})
53
+ OpenAI::Client.multipart_post(path: "/audio/translations", parameters: parameters)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,9 @@
1
+ module Ruby
2
+ module OpenAI
3
+ VERSION = ::OpenAI::VERSION
4
+
5
+ Error = ::OpenAI::Error
6
+ ConfigurationError = ::OpenAI::ConfigurationError
7
+ Configuration = ::OpenAI::Configuration
8
+ end
9
+ end
@@ -0,0 +1,43 @@
1
+ module OpenAI
2
+ class Files
3
+ def initialize(access_token: nil, organization_id: nil)
4
+ OpenAI.configuration.access_token = access_token if access_token
5
+ OpenAI.configuration.organization_id = organization_id if organization_id
6
+ end
7
+
8
+ def list
9
+ OpenAI::Client.get(path: "/files")
10
+ end
11
+
12
+ def upload(parameters: {})
13
+ validate(file: parameters[:file])
14
+
15
+ OpenAI::Client.multipart_post(
16
+ path: "/files",
17
+ parameters: parameters.merge(file: File.open(parameters[:file]))
18
+ )
19
+ end
20
+
21
+ def retrieve(id:)
22
+ OpenAI::Client.get(path: "/files/#{id}")
23
+ end
24
+
25
+ def content(id:)
26
+ OpenAI::Client.get(path: "/files/#{id}/content")
27
+ end
28
+
29
+ def delete(id:)
30
+ OpenAI::Client.delete(path: "/files/#{id}")
31
+ end
32
+
33
+ private
34
+
35
+ def validate(file:)
36
+ File.open(file).each_line.with_index do |line, index|
37
+ JSON.parse(line)
38
+ rescue JSON::ParserError => e
39
+ raise JSON::ParserError, "#{e.message} - found on line #{index + 1} of #{file}"
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,36 @@
1
+ module OpenAI
2
+ class Finetunes
3
+ def initialize(access_token: nil, organization_id: nil)
4
+ OpenAI.configuration.access_token = access_token if access_token
5
+ OpenAI.configuration.organization_id = organization_id if organization_id
6
+ end
7
+
8
+ def list
9
+ OpenAI::Client.get(path: "/fine-tunes")
10
+ end
11
+
12
+ def create(parameters: {})
13
+ OpenAI::Client.json_post(path: "/fine-tunes", parameters: parameters)
14
+ end
15
+
16
+ def retrieve(id:)
17
+ OpenAI::Client.get(path: "/fine-tunes/#{id}")
18
+ end
19
+
20
+ def cancel(id:)
21
+ OpenAI::Client.multipart_post(path: "/fine-tunes/#{id}/cancel")
22
+ end
23
+
24
+ def events(id:)
25
+ OpenAI::Client.get(path: "/fine-tunes/#{id}/events")
26
+ end
27
+
28
+ def delete(fine_tuned_model:)
29
+ if fine_tuned_model.start_with?("ft-")
30
+ raise ArgumentError, "Please give a fine_tuned_model name, not a fine-tune ID"
31
+ end
32
+
33
+ OpenAI::Client.delete(path: "/models/#{fine_tuned_model}")
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,95 @@
1
+ module OpenAI
2
+ module HTTP
3
+ def get(path:)
4
+ to_json(conn.get(uri(path: path)) do |req|
5
+ req.headers = headers
6
+ end&.body)
7
+ end
8
+
9
+ def json_post(path:, parameters:)
10
+ to_json(conn.post(uri(path: path)) do |req|
11
+ if parameters[:stream].respond_to?(:call)
12
+ req.options.on_data = to_json_stream(user_proc: parameters[:stream])
13
+ parameters[:stream] = true # Necessary to tell OpenAI to stream.
14
+ elsif parameters[:stream]
15
+ raise ArgumentError, "The stream parameter must be a Proc or have a #call method"
16
+ end
17
+
18
+ req.headers = headers
19
+ req.body = parameters.to_json
20
+ end&.body)
21
+ end
22
+
23
+ def multipart_post(path:, parameters: nil)
24
+ to_json(conn(multipart: true).post(uri(path: path)) do |req|
25
+ req.headers = headers.merge({ "Content-Type" => "multipart/form-data" })
26
+ req.body = multipart_parameters(parameters)
27
+ end&.body)
28
+ end
29
+
30
+ def delete(path:)
31
+ to_json(conn.delete(uri(path: path)) do |req|
32
+ req.headers = headers
33
+ end&.body)
34
+ end
35
+
36
+ private
37
+
38
+ def to_json(string)
39
+ return unless string
40
+
41
+ JSON.parse(string)
42
+ rescue JSON::ParserError
43
+ # Convert a multiline string of JSON objects to a JSON array.
44
+ JSON.parse(string.gsub("}\n{", "},{").prepend("[").concat("]"))
45
+ end
46
+
47
+ # Given a proc, returns an outer proc that can be used to iterate over a JSON stream of chunks.
48
+ # For each chunk, the inner user_proc is called giving it the JSON object. The JSON object could
49
+ # be a data object or an error object as described in the OpenAI API documentation.
50
+ #
51
+ # If the JSON object for a given data or error message is invalid, it is ignored.
52
+ #
53
+ # @param user_proc [Proc] The inner proc to call for each JSON object in the chunk.
54
+ # @return [Proc] An outer proc that iterates over a raw stream, converting it to JSON.
55
+ def to_json_stream(user_proc:)
56
+ proc do |chunk, _|
57
+ chunk.scan(/(?:data|error): (\{.*\})/i).flatten.each do |data|
58
+ user_proc.call(JSON.parse(data))
59
+ rescue JSON::ParserError
60
+ # Ignore invalid JSON.
61
+ end
62
+ end
63
+ end
64
+
65
+ def conn(multipart: false)
66
+ Faraday.new do |f|
67
+ f.options[:timeout] = OpenAI.configuration.request_timeout
68
+ f.request(:multipart) if multipart
69
+ end
70
+ end
71
+
72
+ def uri(path:)
73
+ OpenAI.configuration.uri_base + OpenAI.configuration.api_version + path
74
+ end
75
+
76
+ def headers
77
+ {
78
+ "Content-Type" => "application/json",
79
+ "Authorization" => "Bearer #{OpenAI.configuration.access_token}",
80
+ "OpenAI-Organization" => OpenAI.configuration.organization_id
81
+ }
82
+ end
83
+
84
+ def multipart_parameters(parameters)
85
+ parameters&.transform_values do |value|
86
+ next value unless value.is_a?(File)
87
+
88
+ # Doesn't seem like OpenAI need mime_type yet, so not worth
89
+ # the library to figure this out. Hence the empty string
90
+ # as the second argument.
91
+ Faraday::UploadIO.new(value, "", value.path)
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,28 @@
1
+ module OpenAI
2
+ class Images
3
+ def initialize(access_token: nil, organization_id: nil)
4
+ OpenAI.configuration.access_token = access_token if access_token
5
+ OpenAI.configuration.organization_id = organization_id if organization_id
6
+ end
7
+
8
+ def generate(parameters: {})
9
+ OpenAI::Client.json_post(path: "/images/generations", parameters: parameters)
10
+ end
11
+
12
+ def edit(parameters: {})
13
+ OpenAI::Client.multipart_post(path: "/images/edits", parameters: open_files(parameters))
14
+ end
15
+
16
+ def variations(parameters: {})
17
+ OpenAI::Client.multipart_post(path: "/images/variations", parameters: open_files(parameters))
18
+ end
19
+
20
+ private
21
+
22
+ def open_files(parameters)
23
+ parameters = parameters.merge(image: File.open(parameters[:image]))
24
+ parameters = parameters.merge(mask: File.open(parameters[:mask])) if parameters[:mask]
25
+ parameters
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ module OpenAI
2
+ class Models
3
+ def initialize(access_token: nil, organization_id: nil)
4
+ OpenAI.configuration.access_token = access_token if access_token
5
+ OpenAI.configuration.organization_id = organization_id if organization_id
6
+ end
7
+
8
+ def list
9
+ OpenAI::Client.get(path: "/models")
10
+ end
11
+
12
+ def retrieve(id:)
13
+ OpenAI::Client.get(path: "/models/#{id}")
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module OpenAI
2
+ VERSION = "4.1.0".freeze
3
+ end
data/lib/openai.rb ADDED
@@ -0,0 +1,51 @@
1
+ require "faraday"
2
+ require "faraday/multipart"
3
+
4
+ require_relative "openai/http"
5
+ require_relative "openai/client"
6
+ require_relative "openai/files"
7
+ require_relative "openai/finetunes"
8
+ require_relative "openai/images"
9
+ require_relative "openai/models"
10
+ require_relative "openai/version"
11
+
12
+ module OpenAI
13
+ class Error < StandardError; end
14
+ class ConfigurationError < Error; end
15
+
16
+ class Configuration
17
+ attr_writer :access_token
18
+ attr_accessor :api_version, :organization_id, :uri_base, :request_timeout
19
+
20
+ DEFAULT_API_VERSION = "v1".freeze
21
+ DEFAULT_URI_BASE = "https://api.openai.com/".freeze
22
+ DEFAULT_REQUEST_TIMEOUT = 120
23
+
24
+ def initialize
25
+ @access_token = nil
26
+ @api_version = DEFAULT_API_VERSION
27
+ @organization_id = nil
28
+ @uri_base = DEFAULT_URI_BASE
29
+ @request_timeout = DEFAULT_REQUEST_TIMEOUT
30
+ end
31
+
32
+ def access_token
33
+ return @access_token if @access_token
34
+
35
+ error_text = "OpenAI access token missing! See https://github.com/alexrudall/ruby-openai#usage"
36
+ raise ConfigurationError, error_text
37
+ end
38
+ end
39
+
40
+ class << self
41
+ attr_writer :configuration
42
+ end
43
+
44
+ def self.configuration
45
+ @configuration ||= OpenAI::Configuration.new
46
+ end
47
+
48
+ def self.configure
49
+ yield(configuration)
50
+ end
51
+ end
@@ -0,0 +1,2 @@
1
+ require_relative "../openai"
2
+ require_relative "../openai/compatibility"
@@ -0,0 +1,5 @@
1
+ ## All Submissions:
2
+
3
+ * [ ] Have you followed the guidelines in our [Contributing document](../blob/main/CONTRIBUTING.md)?
4
+ * [ ] Have you checked to ensure there aren't other open [Pull Requests](../pulls) for the same update/change?
5
+ * [ ] Have you added an explanation of what your changes do and why you'd like us to include them?
@@ -0,0 +1,30 @@
1
+ require_relative "lib/openai/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "ruby-advanced-openai"
5
+ spec.version = OpenAI::VERSION
6
+ spec.authors = ["Yudai-hub"]
7
+ spec.email = ["coffeecoffee@outlook.jp"]
8
+
9
+ spec.summary = "Advanced OpenAI API wrapper for Ruby."
10
+ spec.homepage = "https://github.com/Yudai-hub/ruby-advanced-openai"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/Yudai-hub/ruby-advanced-openai"
16
+ spec.metadata["changelog_uri"] = "https://github.com/Yudai-hub/ruby-advanced-openai/blob/main/CHANGELOG.md"
17
+ spec.metadata["rubygems_mfa_required"] = "true"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency "faraday", ">= 1"
29
+ spec.add_dependency "faraday-multipart", ">= 1"
30
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-advanced-openai
3
+ version: !ruby/object:Gem::Version
4
+ version: 4.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yudai-hub
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday-multipart
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ description:
42
+ email:
43
+ - coffeecoffee@outlook.jp
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".circleci/config.yml"
49
+ - ".github/ISSUE_TEMPLATE/bug_report.md"
50
+ - ".github/ISSUE_TEMPLATE/feature_request.md"
51
+ - ".github/dependabot.yml"
52
+ - ".gitignore"
53
+ - ".rspec"
54
+ - ".rubocop.yml"
55
+ - CHANGELOG.md
56
+ - CODE_OF_CONDUCT.md
57
+ - CONTRIBUTING.md
58
+ - Gemfile
59
+ - Gemfile.lock
60
+ - LICENSE.txt
61
+ - README.md
62
+ - Rakefile
63
+ - bin/console
64
+ - bin/setup
65
+ - lib/openai.rb
66
+ - lib/openai/client.rb
67
+ - lib/openai/compatibility.rb
68
+ - lib/openai/files.rb
69
+ - lib/openai/finetunes.rb
70
+ - lib/openai/http.rb
71
+ - lib/openai/images.rb
72
+ - lib/openai/models.rb
73
+ - lib/openai/version.rb
74
+ - lib/ruby/advanced/openai.rb
75
+ - pull_request_template.md
76
+ - ruby-advanced-openai.gemspec
77
+ homepage: https://github.com/Yudai-hub/ruby-advanced-openai
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ homepage_uri: https://github.com/Yudai-hub/ruby-advanced-openai
82
+ source_code_uri: https://github.com/Yudai-hub/ruby-advanced-openai
83
+ changelog_uri: https://github.com/Yudai-hub/ruby-advanced-openai/blob/main/CHANGELOG.md
84
+ rubygems_mfa_required: 'true'
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.6.0
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.1.6
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Advanced OpenAI API wrapper for Ruby.
104
+ test_files: []