ruby-gpt-client 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 987ea9cc94c099a4adc7c2b2bc497897b85cd0b90f31fc099abf7114c7aaced5
4
+ data.tar.gz: fc51db3d3114c685024eeb6e3a143dadbe615d1efce5801330315542581340d9
5
+ SHA512:
6
+ metadata.gz: b963d76d4c8fdbd268f2ab45a845c910bcd554a6606b9a90a89d7c921fba11b6388e4dcb19b273a57457697039fdcf19ea194d56b4a2f1ce8ce471aa49d37b4d
7
+ data.tar.gz: d95e48c42094cef5f621c9d37d3e9ff4e93de9a229196bc216611029af463f58abdeef8a71fdf5072d87f833e67ef97986cd0367288b20b67937c6aa15443ad0
@@ -0,0 +1,44 @@
1
+ require_relative 'fetcher'
2
+
3
+ module RubyGPT
4
+ # The `RubyGPT::Client` class provides a simple interface for interacting
5
+ # with ChatGPT models. This class is initialized with an API key and
6
+ # includes methods to interact with the ChatGPT API.
7
+ class Client
8
+ OPEN_AI_COMPLETIONS_URL = 'https://api.openai.com/v1/chat/completions'.freeze
9
+ DEFAULT_MODEL = 'gpt-3.5-turbo'.freeze
10
+ DEFAULT_TEMPERATURE = 0.7
11
+
12
+ def initialize(api_key:)
13
+ raise ArgumentError, "The 'api_key' argument is required" unless api_key
14
+
15
+ @headers = Headers.new(api_key: api_key)
16
+ @fetcher = Fetcher.new
17
+ end
18
+
19
+ def completions(messages:, model: DEFAULT_MODEL, temperature: DEFAULT_TEMPERATURE)
20
+ validate_messages(messages)
21
+
22
+ body = {
23
+ messages: messages.map(&:to_h),
24
+ model: model,
25
+ temperature: temperature
26
+ }
27
+
28
+ @fetcher.post(OPEN_AI_COMPLETIONS_URL, @headers.get, body)
29
+ end
30
+
31
+ private
32
+
33
+ def validate_messages(messages)
34
+ if messages.nil? || messages.empty?
35
+ raise ArgumentError,
36
+ "The 'messages' parameter is required"
37
+ end
38
+
39
+ return if messages.all? { |message| message.is_a?(RubyGPT::Message) }
40
+
41
+ raise ArgumentError, 'All messages must be instances of RubyGPT::Message'
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,45 @@
1
+ require 'http'
2
+ require_relative 'headers'
3
+
4
+ module RubyGPT
5
+ # Fetcher is a private class responsible for handling the actual
6
+ # data retrieval process from the API.
7
+ class Fetcher
8
+ def post(endpoint, headers, body)
9
+ response = make_post_request(endpoint, headers, body)
10
+ parse_response(response)
11
+ end
12
+
13
+ private
14
+
15
+ def make_post_request(endpoint, headers, body)
16
+ HTTP.headers(headers).post(endpoint, json: body)
17
+ end
18
+
19
+ def parse_response(response)
20
+ parsed_body = JSON.parse(response.body)
21
+
22
+ if response.code.to_i >= 400 || parsed_body['error']
23
+ error_message = parsed_body['error'] ? parsed_body['error']['message'] : "HTTP Error: #{response.code}"
24
+ raise StandardError, error_message
25
+ end
26
+
27
+ parse_choices(parsed_body)
28
+ end
29
+
30
+ # Convert messages from the response into RubyGPT::Message instances
31
+ def parse_choices(body_json)
32
+ return body_json unless body_json.dig('choices', 0, 'message')
33
+
34
+ body_json['choices'].map! do |choice|
35
+ message = choice['message']
36
+ choice['message'] = RubyGPT::Message.new(role: message['role'], content: message['content'])
37
+ choice
38
+ end
39
+
40
+ body_json
41
+ end
42
+ end
43
+
44
+ private_constant :Fetcher
45
+ end
@@ -0,0 +1,18 @@
1
+ module RubyGPT
2
+ # Headers is a private class responsible for generating HTTP request
3
+ # headers required for API requests.
4
+ class Headers
5
+ def initialize(api_key:)
6
+ @headers = {
7
+ 'Authorization' => "Bearer #{api_key}",
8
+ 'Content-Type' => 'application/json'
9
+ }
10
+ end
11
+
12
+ def get
13
+ @headers
14
+ end
15
+ end
16
+
17
+ private_constant :Headers
18
+ end
@@ -0,0 +1,34 @@
1
+ module RubyGPT
2
+ # The `RubyGPT::Message` class provides a simple interface for creating message objects [TODO].
3
+ class Message
4
+ VALID_ROLES = %w[user assistant system].freeze
5
+
6
+ attr_reader :role, :content
7
+
8
+ def initialize(role:, content:)
9
+ validate_role(role)
10
+ validate_content(content)
11
+
12
+ @role = role
13
+ @content = content
14
+ end
15
+
16
+ def to_h
17
+ { role: @role, content: @content }
18
+ end
19
+
20
+ private
21
+
22
+ def validate_role(role)
23
+ return if VALID_ROLES.include?(role)
24
+
25
+ raise ArgumentError, "Invalid role: #{role}. Valid roles are: #{VALID_ROLES.join(', ')}"
26
+ end
27
+
28
+ def validate_content(content)
29
+ return unless content.nil? || content.strip.empty?
30
+
31
+ raise ArgumentError, 'Content cannot be empty.'
32
+ end
33
+ end
34
+ end
data/lib/ruby_gpt.rb ADDED
@@ -0,0 +1,3 @@
1
+ require_relative 'ruby_gpt/version'
2
+ require_relative 'ruby_gpt/client'
3
+ require_relative 'ruby_gpt/message'
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-gpt-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - energywraith
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-10-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ChatGPT client written in Ruby.
14
+ email: energywraith@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/ruby_gpt.rb
20
+ - lib/ruby_gpt/client.rb
21
+ - lib/ruby_gpt/fetcher.rb
22
+ - lib/ruby_gpt/headers.rb
23
+ - lib/ruby_gpt/message.rb
24
+ homepage: https://github.com/energywraith/ruby-gpt
25
+ licenses: []
26
+ metadata:
27
+ rubygems_mfa_required: 'true'
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 2.6.0
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubygems_version: 3.4.10
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Ruby GPT Client - ChatGPT client written in Ruby
47
+ test_files: []