ruby-openai 3.6.0 → 6.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.devcontainer/Dockerfile +16 -0
- data/.devcontainer/devcontainer.json +36 -0
- data/.devcontainer/docker-compose.yml +19 -0
- data/.github/FUNDING.yml +13 -0
- data/.gitignore +65 -7
- data/.rubocop.yml +8 -0
- data/CHANGELOG.md +141 -0
- data/Gemfile +3 -3
- data/Gemfile.lock +28 -20
- data/README.md +595 -56
- data/Rakefile +14 -1
- data/lib/openai/assistants.rb +27 -0
- data/lib/openai/audio.rb +19 -0
- data/lib/openai/client.rb +53 -63
- data/lib/openai/compatibility.rb +1 -0
- data/lib/openai/files.rb +8 -9
- data/lib/openai/finetunes.rb +8 -17
- data/lib/openai/http.rb +127 -0
- data/lib/openai/http_headers.rb +36 -0
- data/lib/openai/images.rb +5 -6
- data/lib/openai/messages.rb +23 -0
- data/lib/openai/models.rb +4 -5
- data/lib/openai/run_steps.rb +15 -0
- data/lib/openai/runs.rb +32 -0
- data/lib/openai/threads.rb +23 -0
- data/lib/openai/version.rb +1 -1
- data/lib/openai.rb +43 -2
- data/ruby-openai.gemspec +5 -4
- metadata +58 -14
data/lib/openai.rb
CHANGED
@@ -1,28 +1,57 @@
|
|
1
|
-
require "
|
1
|
+
require "faraday"
|
2
|
+
require "faraday/multipart"
|
2
3
|
|
4
|
+
require_relative "openai/http"
|
3
5
|
require_relative "openai/client"
|
4
6
|
require_relative "openai/files"
|
5
7
|
require_relative "openai/finetunes"
|
6
8
|
require_relative "openai/images"
|
7
9
|
require_relative "openai/models"
|
10
|
+
require_relative "openai/assistants"
|
11
|
+
require_relative "openai/threads"
|
12
|
+
require_relative "openai/messages"
|
13
|
+
require_relative "openai/runs"
|
14
|
+
require_relative "openai/run_steps"
|
15
|
+
require_relative "openai/audio"
|
8
16
|
require_relative "openai/version"
|
9
17
|
|
10
18
|
module OpenAI
|
11
19
|
class Error < StandardError; end
|
12
20
|
class ConfigurationError < Error; end
|
13
21
|
|
22
|
+
class MiddlewareErrors < Faraday::Middleware
|
23
|
+
def call(env)
|
24
|
+
@app.call(env)
|
25
|
+
rescue Faraday::Error => e
|
26
|
+
raise e unless e.response.is_a?(Hash)
|
27
|
+
|
28
|
+
logger = Logger.new($stdout)
|
29
|
+
logger.formatter = proc do |_severity, _datetime, _progname, msg|
|
30
|
+
"\033[31mOpenAI HTTP Error (spotted in ruby-openai #{VERSION}): #{msg}\n\033[0m"
|
31
|
+
end
|
32
|
+
logger.error(e.response[:body])
|
33
|
+
|
34
|
+
raise e
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
14
38
|
class Configuration
|
15
39
|
attr_writer :access_token
|
16
|
-
attr_accessor :api_version, :organization_id, :request_timeout
|
40
|
+
attr_accessor :api_type, :api_version, :organization_id, :uri_base, :request_timeout,
|
41
|
+
:extra_headers
|
17
42
|
|
18
43
|
DEFAULT_API_VERSION = "v1".freeze
|
44
|
+
DEFAULT_URI_BASE = "https://api.openai.com/".freeze
|
19
45
|
DEFAULT_REQUEST_TIMEOUT = 120
|
20
46
|
|
21
47
|
def initialize
|
22
48
|
@access_token = nil
|
49
|
+
@api_type = nil
|
23
50
|
@api_version = DEFAULT_API_VERSION
|
24
51
|
@organization_id = nil
|
52
|
+
@uri_base = DEFAULT_URI_BASE
|
25
53
|
@request_timeout = DEFAULT_REQUEST_TIMEOUT
|
54
|
+
@extra_headers = {}
|
26
55
|
end
|
27
56
|
|
28
57
|
def access_token
|
@@ -44,4 +73,16 @@ module OpenAI
|
|
44
73
|
def self.configure
|
45
74
|
yield(configuration)
|
46
75
|
end
|
76
|
+
|
77
|
+
# Estimate the number of tokens in a string, using the rules of thumb from OpenAI:
|
78
|
+
# https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them
|
79
|
+
def self.rough_token_count(content = "")
|
80
|
+
raise ArgumentError, "rough_token_count requires a string" unless content.is_a? String
|
81
|
+
return 0 if content.empty?
|
82
|
+
|
83
|
+
count_by_chars = content.size / 4.0
|
84
|
+
count_by_words = content.split.size * 4.0 / 3
|
85
|
+
estimate = ((count_by_chars + count_by_words) / 2.0).round
|
86
|
+
[1, estimate].max
|
87
|
+
end
|
47
88
|
end
|
data/ruby-openai.gemspec
CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |spec|
|
|
6
6
|
spec.authors = ["Alex"]
|
7
7
|
spec.email = ["alexrudall@users.noreply.github.com"]
|
8
8
|
|
9
|
-
spec.summary = "
|
9
|
+
spec.summary = "OpenAI API + Ruby! 🤖🩵"
|
10
10
|
spec.homepage = "https://github.com/alexrudall/ruby-openai"
|
11
11
|
spec.license = "MIT"
|
12
12
|
spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
|
@@ -15,6 +15,7 @@ Gem::Specification.new do |spec|
|
|
15
15
|
spec.metadata["source_code_uri"] = "https://github.com/alexrudall/ruby-openai"
|
16
16
|
spec.metadata["changelog_uri"] = "https://github.com/alexrudall/ruby-openai/blob/main/CHANGELOG.md"
|
17
17
|
spec.metadata["rubygems_mfa_required"] = "true"
|
18
|
+
spec.metadata["funding_uri"] = "https://github.com/sponsors/alexrudall"
|
18
19
|
|
19
20
|
# Specify which files should be added to the gem when it is released.
|
20
21
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
@@ -25,7 +26,7 @@ Gem::Specification.new do |spec|
|
|
25
26
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
27
|
spec.require_paths = ["lib"]
|
27
28
|
|
28
|
-
spec.add_dependency "
|
29
|
-
|
30
|
-
spec.
|
29
|
+
spec.add_dependency "event_stream_parser", ">= 0.3.0", "< 2.0.0"
|
30
|
+
spec.add_dependency "faraday", ">= 1"
|
31
|
+
spec.add_dependency "faraday-multipart", ">= 1"
|
31
32
|
end
|
metadata
CHANGED
@@ -1,30 +1,64 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-openai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 6.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: event_stream_parser
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: 0.3.0
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.0.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.3.0
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.0.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: faraday
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: faraday-multipart
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1'
|
20
54
|
type: :runtime
|
21
55
|
prerelease: false
|
22
56
|
version_requirements: !ruby/object:Gem::Requirement
|
23
57
|
requirements:
|
24
58
|
- - ">="
|
25
59
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
27
|
-
description:
|
60
|
+
version: '1'
|
61
|
+
description:
|
28
62
|
email:
|
29
63
|
- alexrudall@users.noreply.github.com
|
30
64
|
executables: []
|
@@ -32,6 +66,10 @@ extensions: []
|
|
32
66
|
extra_rdoc_files: []
|
33
67
|
files:
|
34
68
|
- ".circleci/config.yml"
|
69
|
+
- ".devcontainer/Dockerfile"
|
70
|
+
- ".devcontainer/devcontainer.json"
|
71
|
+
- ".devcontainer/docker-compose.yml"
|
72
|
+
- ".github/FUNDING.yml"
|
35
73
|
- ".github/ISSUE_TEMPLATE/bug_report.md"
|
36
74
|
- ".github/ISSUE_TEMPLATE/feature_request.md"
|
37
75
|
- ".github/dependabot.yml"
|
@@ -49,12 +87,20 @@ files:
|
|
49
87
|
- bin/console
|
50
88
|
- bin/setup
|
51
89
|
- lib/openai.rb
|
90
|
+
- lib/openai/assistants.rb
|
91
|
+
- lib/openai/audio.rb
|
52
92
|
- lib/openai/client.rb
|
53
93
|
- lib/openai/compatibility.rb
|
54
94
|
- lib/openai/files.rb
|
55
95
|
- lib/openai/finetunes.rb
|
96
|
+
- lib/openai/http.rb
|
97
|
+
- lib/openai/http_headers.rb
|
56
98
|
- lib/openai/images.rb
|
99
|
+
- lib/openai/messages.rb
|
57
100
|
- lib/openai/models.rb
|
101
|
+
- lib/openai/run_steps.rb
|
102
|
+
- lib/openai/runs.rb
|
103
|
+
- lib/openai/threads.rb
|
58
104
|
- lib/openai/version.rb
|
59
105
|
- lib/ruby/openai.rb
|
60
106
|
- pull_request_template.md
|
@@ -67,10 +113,8 @@ metadata:
|
|
67
113
|
source_code_uri: https://github.com/alexrudall/ruby-openai
|
68
114
|
changelog_uri: https://github.com/alexrudall/ruby-openai/blob/main/CHANGELOG.md
|
69
115
|
rubygems_mfa_required: 'true'
|
70
|
-
|
71
|
-
|
72
|
-
change `require ''ruby/openai''` to `require ''openai''` and change all references
|
73
|
-
to `Ruby::OpenAI` to `OpenAI`.'
|
116
|
+
funding_uri: https://github.com/sponsors/alexrudall
|
117
|
+
post_install_message:
|
74
118
|
rdoc_options: []
|
75
119
|
require_paths:
|
76
120
|
- lib
|
@@ -85,8 +129,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
129
|
- !ruby/object:Gem::Version
|
86
130
|
version: '0'
|
87
131
|
requirements: []
|
88
|
-
rubygems_version: 3.
|
89
|
-
signing_key:
|
132
|
+
rubygems_version: 3.4.22
|
133
|
+
signing_key:
|
90
134
|
specification_version: 4
|
91
|
-
summary:
|
135
|
+
summary: "OpenAI API + Ruby! \U0001F916\U0001FA75"
|
92
136
|
test_files: []
|