openai-mapper 0.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.
@@ -0,0 +1,15 @@
1
+ # typed: false
2
+
3
+ module Openai
4
+ class Model < ::Openai::Resource::Api
5
+ def initialize
6
+ @path = "/v1/models"
7
+ super()
8
+ end
9
+
10
+ def request(model_id)
11
+ @response = @connection.get(path: [@path, model_id].join("/"))
12
+ @data = ::Openai::Mapper::Model.from_json(super())
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # typed: false
2
+
3
+ module Openai
4
+ class Models < ::Openai::Resource::Api
5
+ def initialize
6
+ @path = "/v1/models"
7
+ super()
8
+ end
9
+
10
+ def request
11
+ @response = @connection.get(path: @path)
12
+ @data = ::Openai::Mapper::Models.from_json(super())
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ # typed: false
2
+
3
+ module Openai
4
+ module Request
5
+ module Chat
6
+ class Completion < ::Openai::Mapper::BaseMapper
7
+ attribute :model, ::Shale::Type::String
8
+ attribute :messages, ::Openai::Mapper::Message, collection: true
9
+ attribute :functions, ::Openai::Mapper::Functions, default: -> {}
10
+ attribute :function_call, ::Openai::Mapper::Functions, default: -> {}
11
+ attribute :temperature, ::Shale::Type::Float, default: -> { 1.0 } # Selector with top_p
12
+ attribute :top_p, ::Shale::Type::Float, default: -> { 1.0 } # Selector with temperature
13
+ attribute :n, ::Shale::Type::Integer, default: -> { 1 }
14
+ attribute :stream, ::Shale::Type::Boolean, default: -> { false }
15
+ attribute :stop, ::Shale::Type::String, default: -> {} # Selector of types
16
+ attribute :max_tokens, ::Shale::Type::Integer, default: -> { 5 }
17
+ attribute :presence_penalty, ::Shale::Type::Float, default: -> { 0 }
18
+ attribute :frequency_penalty, ::Shale::Type::Float, default: -> { 0 }
19
+ attribute :logit_bias, ::Shale::Type::String, default: -> {}
20
+ attribute :user, ::Shale::Type::String, default: -> {}
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ # typed: false
2
+
3
+ module Openai
4
+ module Request
5
+ module Images
6
+ class Create < ::Openai::Mapper::BaseMapper
7
+ attribute :prompt, ::Shale::Type::String
8
+ attribute :response_format, ::Shale::Type::String, default: -> { "url" }
9
+ attribute :n, ::Shale::Type::Integer, default: -> { 1 }
10
+ attribute :size, ::Shale::Type::String, default: -> {}
11
+ attribute :user, ::Shale::Type::String, default: -> {}
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ # typed: false
2
+
3
+ module Openai
4
+ module Request
5
+ module Images
6
+ class Edit < ::Openai::Mapper::BaseMapper
7
+ attribute :image, ::Shale::Type::String
8
+ attribute :prompt, ::Shale::Type::String
9
+ attribute :mask, ::Shale::Type::String
10
+ attribute :n, ::Shale::Type::Integer, default: -> { 1 }
11
+ attribute :size, ::Shale::Type::String, default: -> { "1024x1024" }
12
+ attribute :response_format, ::Shale::Type::String, default: -> { "url" }
13
+ attribute :user, ::Shale::Type::String, default: -> {}
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ # typed: false
2
+
3
+ module Openai
4
+ module Request
5
+ module Images
6
+ class Variation < ::Openai::Mapper::BaseMapper
7
+ attribute :image, ::Shale::Type::String
8
+ attribute :n, ::Shale::Type::Integer, default: -> { 1 }
9
+ attribute :response_format, ::Shale::Type::String, default: -> { "url" }
10
+ attribute :size, ::Shale::Type::String, default: -> { "1024x1024" }
11
+ attribute :user, ::Shale::Type::String, default: -> {}
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,73 @@
1
+ # typed: false
2
+ # frozen_string_literal: true
3
+
4
+ require "securerandom"
5
+
6
+ module Openai
7
+ module Resource
8
+ class Api
9
+ attr_reader :connection, :path, :data, :response
10
+
11
+ def initialize
12
+ @file_source = ::File.join(__dir__, "../../../input/")
13
+ Excon.defaults[:ssl_verify_peer] = false
14
+ @connection = Excon.new(BASE_URL, headers: headers)
15
+ end
16
+
17
+ def request
18
+ parse_body
19
+ end
20
+
21
+ private
22
+
23
+ def ssl_options
24
+ {verify: true, verify_mode: 0}
25
+ end
26
+
27
+ def headers
28
+ {
29
+ "Content-Type" => "application/json",
30
+ "Authorization" => "Bearer #{ACCESS_TOKEN}"
31
+ }
32
+ end
33
+
34
+ def parse_body
35
+ @data = @response.body
36
+ end
37
+
38
+ def multipart_attachment(content_type, key, filename, data)
39
+ data.binmode if data.respond_to?(:binmode)
40
+ data.pos = 0 if data.respond_to?(:pos=)
41
+ body = "--#{@boundary}#{Excon::CR_NL}"
42
+ body << multipart_file_header(key, filename, content_type)
43
+ body << Excon::CR_NL
44
+ body << data.to_s
45
+ body << Excon::CR_NL
46
+ body
47
+ end
48
+
49
+ def multipart_file_header(key, filename, content_type)
50
+ body = %(Content-Disposition: form-data; name="#{key}"; filename="#{filename}"#{Excon::CR_NL})
51
+ body << "Content-Type: #{content_type}#{Excon::CR_NL}"
52
+ body
53
+ end
54
+
55
+ def multipart_field(key, value)
56
+ body = "--#{@boundary}#{Excon::CR_NL}"
57
+ body << %(Content-Disposition: form-data; name="#{key}"#{Excon::CR_NL})
58
+ body << Excon::CR_NL
59
+ body << value.to_s
60
+ body << Excon::CR_NL
61
+ body
62
+ end
63
+
64
+ def attach_image(key, filename, image, mime = "image/png")
65
+ multipart_attachment(mime, key, filename, image)
66
+ end
67
+
68
+ def read_image(image)
69
+ ::File.read(::File.join(@file_source, image))
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,6 @@
1
+ # typed: false
2
+ # frozen_string_literal: true
3
+
4
+ module Openai
5
+ VERSION = "0.1.0"
6
+ end
data/lib/openai.rb ADDED
@@ -0,0 +1,28 @@
1
+ # typed: false
2
+
3
+ if ["test", "development"].include?(ENV["BUNDLE_ENV"])
4
+ require "dotenv"
5
+ Dotenv.load
6
+ end
7
+
8
+ require "zeitwerk"
9
+ require "excon"
10
+ require "shale"
11
+ require "ox"
12
+ require "tomlib"
13
+ require "shale/adapter/ox"
14
+
15
+ loader = Zeitwerk::Loader.for_gem
16
+ loader.setup
17
+
18
+ Shale.xml_adapter = Shale::Adapter::Ox
19
+ Shale.toml_adapter = Tomlib
20
+
21
+ module Openai
22
+ BASE_URL = "https://api.openai.com"
23
+ ACCESS_TOKEN = ENV.fetch("OPENAI_TOKEN")
24
+
25
+ class Error < StandardError; end
26
+ end
27
+
28
+ loader.eager_load
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/openai/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "openai-mapper"
7
+ spec.version = Openai::VERSION
8
+ spec.authors = ["Sergey Bezugliy"]
9
+ spec.email = ["s.bezugliy@gmail.com"]
10
+ spec.licenses = ['MIT']
11
+
12
+ spec.summary = "OpenAI resources API client and object mapper"
13
+ spec.description = "OpenAI resources API client and object mapper"
14
+ spec.homepage = "https://github.com/sbezugliy/openai-mapper"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/sbezugliy/openai-mapper"
21
+ spec.metadata["changelog_uri"] = "https://github.com/sbezugliy/openai-mapper/CHANGELOG.MD"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (File.expand_path(f) == __FILE__) ||
28
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile output])
29
+ end
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_dependency "excon"
36
+ spec.add_dependency "shale"
37
+ # spec.add_dependency "faraday_middleware"
38
+ # spec.add_dependency "async-http-faraday"
39
+ spec.add_dependency "rake"
40
+ spec.add_dependency "zeitwerk"
41
+ spec.add_dependency "mimemagic"
42
+ spec.add_dependency "ox"
43
+ spec.add_dependency "tomlib"
44
+ spec.add_dependency "pry"
45
+
46
+ # Uncomment to register a new dependency of your gem
47
+ spec.add_development_dependency "dotenv"
48
+ spec.add_development_dependency "rspec"
49
+ spec.add_development_dependency "rubocop", "~> 1.56.0"
50
+ spec.add_development_dependency "rubocop-capybara"
51
+ spec.add_development_dependency "rubocop-factory_bot"
52
+ spec.add_development_dependency "rubocop-performance", "~> 1.18.0"
53
+ spec.add_development_dependency "rubocop-rake"
54
+ spec.add_development_dependency "rubocop-rspec"
55
+ spec.add_development_dependency "rubocop-packs"
56
+ spec.add_development_dependency "simplecov"
57
+ spec.add_development_dependency "standard", "~> 1.30"
58
+ spec.add_development_dependency "standard-performance", "~> 1.1.0"
59
+ spec.add_development_dependency "shoulda-matchers"
60
+ spec.add_development_dependency "webmock"
61
+ spec.add_development_dependency "async-rspec", "~> 1.2"
62
+
63
+ spec.add_development_dependency "bundler-audit"
64
+ spec.add_development_dependency "dawnscanner"
65
+ spec.add_development_dependency "ruby_audit"
66
+
67
+ # For more information and examples about making a new gem, check out our
68
+ # guide at: https://bundler.io/guides/creating_gem.html
69
+ end
@@ -0,0 +1,6 @@
1
+ module Openai
2
+ module Mapper
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end