openai_chatgpt 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +18 -0
- data/CHANGELOG.md +4 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +50 -0
- data/README.md +108 -0
- data/Rakefile +20 -0
- data/lib/openai_chatgpt/choice.rb +14 -0
- data/lib/openai_chatgpt/client.rb +109 -0
- data/lib/openai_chatgpt/error.rb +5 -0
- data/lib/openai_chatgpt/object.rb +22 -0
- data/lib/openai_chatgpt/response.rb +14 -0
- data/lib/openai_chatgpt/usage.rb +14 -0
- data/lib/openai_chatgpt/version.rb +5 -0
- data/lib/openai_chatgpt.rb +14 -0
- data/sig/openai_chatgpt.rbs +4 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 35a7ddb611ab54537073b7230cd837cba5baa8db5a604a188cdddeda168638d3
|
4
|
+
data.tar.gz: da5d5bfc63d3065eedaa0380de6bbeb6948744ea611c5597efb84bfce8e8e5a8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e4dc88f770b1d259922afa8c4136f69db7996374a0fa85befad81b9a3864f353f13eb9cc8eef507c244660112a368cec1ff69df83be021b5b3185312aef5c53d
|
7
|
+
data.tar.gz: 0cc811e6103078c8ba3a950f7c76cc25273a20b818a932d185c6e7dfe903375fea3c02f5fbea07e3858e67780d392eae08c0aa3f7e6470af8fa10b36c2010f6c
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.6
|
3
|
+
NewCops: enable
|
4
|
+
SuggestExtensions: false
|
5
|
+
|
6
|
+
Style/StringLiterals:
|
7
|
+
Enabled: true
|
8
|
+
EnforcedStyle: double_quotes
|
9
|
+
|
10
|
+
Style/StringLiteralsInInterpolation:
|
11
|
+
Enabled: true
|
12
|
+
EnforcedStyle: double_quotes
|
13
|
+
|
14
|
+
Layout/LineLength:
|
15
|
+
Max: 120
|
16
|
+
|
17
|
+
Lint/RedundantCopDisableDirective:
|
18
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at sulman@hey.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
openai_chatgpt (0.1.0)
|
5
|
+
faraday (~> 2.5)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.2)
|
11
|
+
faraday (2.7.4)
|
12
|
+
faraday-net_http (>= 2.0, < 3.1)
|
13
|
+
ruby2_keywords (>= 0.0.4)
|
14
|
+
faraday-net_http (3.0.2)
|
15
|
+
json (2.6.3)
|
16
|
+
minitest (5.17.0)
|
17
|
+
parallel (1.22.1)
|
18
|
+
parser (3.2.1.0)
|
19
|
+
ast (~> 2.4.1)
|
20
|
+
rainbow (3.1.1)
|
21
|
+
rake (13.0.6)
|
22
|
+
regexp_parser (2.7.0)
|
23
|
+
rexml (3.2.5)
|
24
|
+
rubocop (1.47.0)
|
25
|
+
json (~> 2.3)
|
26
|
+
parallel (~> 1.10)
|
27
|
+
parser (>= 3.2.0.0)
|
28
|
+
rainbow (>= 2.2.2, < 4.0)
|
29
|
+
regexp_parser (>= 1.8, < 3.0)
|
30
|
+
rexml (>= 3.2.5, < 4.0)
|
31
|
+
rubocop-ast (>= 1.26.0, < 2.0)
|
32
|
+
ruby-progressbar (~> 1.7)
|
33
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
34
|
+
rubocop-ast (1.27.0)
|
35
|
+
parser (>= 3.2.1.0)
|
36
|
+
ruby-progressbar (1.12.0)
|
37
|
+
ruby2_keywords (0.0.5)
|
38
|
+
unicode-display_width (2.4.2)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
arm64-darwin-22
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
minitest (~> 5.0)
|
45
|
+
openai_chatgpt!
|
46
|
+
rake (~> 13.0)
|
47
|
+
rubocop (~> 1.21)
|
48
|
+
|
49
|
+
BUNDLED WITH
|
50
|
+
2.4.7
|
data/README.md
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
# OpenaiChatgpt
|
2
|
+
|
3
|
+
OpenAI ChatGPT API is a light-weight Ruby wrapper for the Rubyists. It gives nice struct objects for adresses, even the raw responses are returned in nice open struct objects. It uses Faraday for HTTP requests.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
$ bundle add openai_chatgpt
|
10
|
+
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
|
+
|
13
|
+
$ gem install openai_chatgpt
|
14
|
+
|
15
|
+
---
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
### API Documentation
|
20
|
+
|
21
|
+
The API documentation can be found at [https://platform.openai.com/docs/api-reference/chat/create](https://platform.openai.com/docs/api-reference/chat/create).
|
22
|
+
|
23
|
+
### Get an API key
|
24
|
+
You can get API for the chatgpt by doing the following [here](https://platform.openai.com/account/api-keys).
|
25
|
+
|
26
|
+
### Configuration
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
require 'openai_chatgpt'
|
30
|
+
client = OpenaiChatgpt::Client.new(api_key: ENV['CHATGPT_API_KEY'])
|
31
|
+
```
|
32
|
+
Params:
|
33
|
+
- `api_key`: String - Required - The API key for the openai chatgpt api.
|
34
|
+
|
35
|
+
Response:
|
36
|
+
```ruby
|
37
|
+
#<OpenaiChatgpt::Client:0x00000001055fc318 @adapter=:net_http, @api_key="YOUR_API_KEY", @stubs=nil>
|
38
|
+
```
|
39
|
+
|
40
|
+
### Get completion text
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
resp = OpenaiChatgpt::Client.new(api_key: "fake").completions(
|
44
|
+
messages: [
|
45
|
+
{role: "user", content: "Hello, how are you?"},
|
46
|
+
{role: "assistant", content: "I am good, how are you?"},
|
47
|
+
{role: "user", content: "What is your name?"},
|
48
|
+
]
|
49
|
+
)
|
50
|
+
```
|
51
|
+
Params:
|
52
|
+
- messages: Array[String] - Required - Messages to generate response for
|
53
|
+
`[{role: "user", text: "Hello"}, {role: "bot", text: "Hi"}]`
|
54
|
+
- model: String - Optional - Default: "gpt-3.5-turbo" - Model to use for generating response
|
55
|
+
["gpt-3.5-turbo", "gpt-3.5-turbo-0301"]
|
56
|
+
- temperature: Float - Optional - Default: 1.0 - Temperature for response generation between 0.0 and 2.0
|
57
|
+
- top_p: Float - Optional - Default: 1.0 - Top p for response generation between 0.0 and 1.0
|
58
|
+
- n: Integer - Optional - Default: 1 - Number of responses to generate
|
59
|
+
- stream: Boolean - Optional - Default: false - Stream response
|
60
|
+
- stop: Array[String] - Optional - Default: nil - Stop sequence for response generation
|
61
|
+
- max_tokens: Integer - Optional - Default: 4096 - Maximum number of tokens to generate
|
62
|
+
- presence_penalty: Float - Optional - Default: 0.0 - Presence penalty for response generation between -2.0 and 2.0
|
63
|
+
- frequency_penalty: Float - Optional - Default: 0.0 - Frequency penalty for response generation between
|
64
|
+
-2.0 and 2.0
|
65
|
+
- logit_bias: Hash - Optional - Default: nil - Logit bias for response generation
|
66
|
+
- user: String - Optional - Default: nil - User for response generation
|
67
|
+
|
68
|
+
Response:
|
69
|
+
```ruby
|
70
|
+
resp.results
|
71
|
+
=> [#<OpenaiChatgpt::Choice:0x0000000107abca68 @content="I am an AI language model created by Open", @index=0, @role="assistant">]
|
72
|
+
```
|
73
|
+
```ruby
|
74
|
+
resp.usage
|
75
|
+
=> #<OpenaiChatgpt::Usage:0x0000000107aba808 @completion_tokens=10, @prompt_tokens=36, @total_tokens=46>
|
76
|
+
```
|
77
|
+
|
78
|
+
> Also `raw_response` is available if you want the raw response from the API. This is useful if you want to do something with the raw response. Also that response is in OpenStruct Object which is a nice wrapper for the raw response and you can use 'dot' notation.
|
79
|
+
|
80
|
+
### Error Handling
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
begin
|
84
|
+
client.completions(messages: [{role: "user", text: "Hello, how are you?"}]) # this should be content instead of text
|
85
|
+
rescue OpenaiChatgpt::Error => e
|
86
|
+
puts e.message
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
---
|
91
|
+
|
92
|
+
## Development
|
93
|
+
|
94
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
95
|
+
|
96
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
97
|
+
|
98
|
+
## Contributing
|
99
|
+
|
100
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/openai_chatgpt. 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/[USERNAME]/openai_chatgpt/blob/main/CODE_OF_CONDUCT.md).
|
101
|
+
|
102
|
+
## License
|
103
|
+
|
104
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
105
|
+
|
106
|
+
## Code of Conduct
|
107
|
+
|
108
|
+
Everyone interacting in the OpenaiChatgpt project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/openai_chatgpt/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "rubocop/rake_task"
|
13
|
+
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
task :console do
|
17
|
+
exec "irb -I lib -r openai_chatgpt.rb"
|
18
|
+
end
|
19
|
+
|
20
|
+
task default: %i[test rubocop]
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OpenaiChatgpt
|
4
|
+
# @note: This class is the choice response object.
|
5
|
+
class Choice
|
6
|
+
attr_reader :content, :index, :role
|
7
|
+
|
8
|
+
def initialize(obj)
|
9
|
+
@content = obj.message.content
|
10
|
+
@index = obj.index
|
11
|
+
@role = obj.message.role
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OpenaiChatgpt
|
4
|
+
# Client having API methods and api_key
|
5
|
+
class Client
|
6
|
+
# Base url for OpenAI ChatGPT API
|
7
|
+
BASE_URL = "https://api.openai.com/v1/chat/completions"
|
8
|
+
attr_reader :api_key, :adapter
|
9
|
+
|
10
|
+
# #### Params:
|
11
|
+
# api_key: String - Required - API key for OpenAI ChatGPT API (https://platform.openai.com/account/api-keys)
|
12
|
+
# adapter: Symbol - Optional - Default: :net_http - Adapter for Faraday connection
|
13
|
+
# stubs: Faraday::Adapter::Test::Stubs - Optional - Default: nil - Stubs for Faraday connection
|
14
|
+
# #### Example:
|
15
|
+
# client = OpenaiChatgpt::Client.new api_key: "test"
|
16
|
+
# #### Description:
|
17
|
+
# Client for OpenAI ChatGPT API
|
18
|
+
def initialize(api_key:, adapter: Faraday.default_adapter, stubs: nil)
|
19
|
+
@api_key = api_key
|
20
|
+
@adapter = adapter
|
21
|
+
|
22
|
+
# Test stubs for requests
|
23
|
+
@stubs = stubs
|
24
|
+
end
|
25
|
+
|
26
|
+
# #### Params:
|
27
|
+
# messages: Array[String] - Required - Messages to generate response for
|
28
|
+
# [{role: "user", text: "Hello"}, {role: "bot", text: "Hi"}]
|
29
|
+
# model: String - Optional - Default: "gpt-3.5-turbo" - Model to use for generating response
|
30
|
+
# ["gpt-3.5-turbo", "gpt-3.5-turbo-0301"]
|
31
|
+
# temperature: Float - Optional - Default: 1.0 - Temperature for response generation between 0.0 and 2.0
|
32
|
+
# top_p: Float - Optional - Default: 1.0 - Top p for response generation between 0.0 and 1.0
|
33
|
+
# n: Integer - Optional - Default: 1 - Number of responses to generate
|
34
|
+
# stream: Boolean - Optional - Default: false - Stream response
|
35
|
+
# stop: Array[String] - Optional - Default: nil - Stop sequence for response generation
|
36
|
+
# max_tokens: Integer - Optional - Default: 4096 - Maximum number of tokens to generate
|
37
|
+
# presence_penalty: Float - Optional - Default: 0.0 - Presence penalty for response generation between -2.0 and 2.0
|
38
|
+
# frequency_penalty: Float - Optional - Default: 0.0 - Frequency penalty for response generation between
|
39
|
+
# -2.0 and 2.0
|
40
|
+
# logit_bias: Hash - Optional - Default: nil - Logit bias for response generation
|
41
|
+
# user: String - Optional - Default: nil - User for response generation
|
42
|
+
# #### Example:
|
43
|
+
# OpenaiChatgpt::Client.new(api_key: "fake")
|
44
|
+
# .completions(messages: [{role: "user", content: "Hello"}, {role: "assistant", content: "Hi"}])
|
45
|
+
|
46
|
+
# #### Description:
|
47
|
+
# completions method for OpenAI ChatGPT API
|
48
|
+
# #### Response:
|
49
|
+
# Response object with choices
|
50
|
+
# #### Error:
|
51
|
+
# OpenaiChatgpt::Error
|
52
|
+
def completions( # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
|
53
|
+
messages:,
|
54
|
+
model: "gpt-3.5-turbo",
|
55
|
+
temperature: 1.0,
|
56
|
+
top_p: 1.0,
|
57
|
+
n: 1, # rubocop:disable Naming/MethodParameterName
|
58
|
+
stream: false,
|
59
|
+
stop: nil,
|
60
|
+
max_tokens: 2000,
|
61
|
+
presence_penalty: 0,
|
62
|
+
frequency_penalty: 0,
|
63
|
+
logit_bias: {},
|
64
|
+
user: ""
|
65
|
+
)
|
66
|
+
Response.new post("", {
|
67
|
+
model: model,
|
68
|
+
messages: messages,
|
69
|
+
temperature: temperature,
|
70
|
+
top_p: top_p,
|
71
|
+
n: n,
|
72
|
+
stream: stream,
|
73
|
+
stop: stop,
|
74
|
+
max_tokens: max_tokens,
|
75
|
+
presence_penalty: presence_penalty,
|
76
|
+
frequency_penalty: frequency_penalty,
|
77
|
+
logit_bias: logit_bias,
|
78
|
+
user: user
|
79
|
+
}.to_json)
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
# makes a get request to the given path with params
|
85
|
+
def post(path, params = {})
|
86
|
+
handle_response connection.post(path, params)
|
87
|
+
end
|
88
|
+
|
89
|
+
# handles the response from the api and raises error if any
|
90
|
+
# returns the response if no error
|
91
|
+
# Error codes: https://platform.openai.com/docs/guides/error-codes/api-errors
|
92
|
+
def handle_response(response)
|
93
|
+
return response if response.status == 200
|
94
|
+
|
95
|
+
raise Error, "#{response.status}: #{response.body["error"]["message"]}"
|
96
|
+
end
|
97
|
+
|
98
|
+
# creates a faraday connection with base url and api_key
|
99
|
+
def connection
|
100
|
+
@connection ||= Faraday.new(BASE_URL) do |conn|
|
101
|
+
conn.headers["Authorization"] = "Bearer #{@api_key}"
|
102
|
+
conn.headers["Content-Type"] = "application/json"
|
103
|
+
|
104
|
+
conn.response :json, content_type: "application/json"
|
105
|
+
conn.adapter adapter, @stubs
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "ostruct"
|
4
|
+
|
5
|
+
module OpenaiChatgpt
|
6
|
+
# OpenStruct object for OpenaiChatgpt
|
7
|
+
class Object < OpenStruct # rubocop:disable Style/OpenStructUse
|
8
|
+
def initialize(attributes)
|
9
|
+
super to_ostruct(attributes)
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_ostruct(obj)
|
13
|
+
if obj.is_a?(Hash) # rubocop:disable Style/CaseLikeIf
|
14
|
+
OpenStruct.new(obj.map { |key, val| [key, to_ostruct(val)] }.to_h) # rubocop:disable Style/HashTransformValues, Style/OpenStructUse, Style/MapToHash
|
15
|
+
elsif obj.is_a?(Array)
|
16
|
+
obj.map { |o| to_ostruct(o) }
|
17
|
+
else # Assumed to be a primitive value
|
18
|
+
obj
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OpenaiChatgpt
|
4
|
+
# Response object formatted from OpenaiChatgpt response
|
5
|
+
class Response
|
6
|
+
attr_reader :raw_response, :results, :usage
|
7
|
+
|
8
|
+
def initialize(response)
|
9
|
+
@raw_response = Object.new response.body
|
10
|
+
@results = @raw_response.choices.map { |choice| Choice.new choice }
|
11
|
+
@usage = Usage.new @raw_response.usage
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OpenaiChatgpt
|
4
|
+
# Address object
|
5
|
+
class Usage
|
6
|
+
attr_reader :prompt_tokens, :completion_tokens, :total_tokens
|
7
|
+
|
8
|
+
def initialize(obj)
|
9
|
+
@prompt_tokens = obj.prompt_tokens
|
10
|
+
@completion_tokens = obj.completion_tokens
|
11
|
+
@total_tokens = obj.total_tokens
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "openai_chatgpt/version"
|
4
|
+
require "faraday"
|
5
|
+
|
6
|
+
# @note: This is a wrapper for the OpenAI Chat GPT API.
|
7
|
+
module OpenaiChatgpt
|
8
|
+
autoload :Error, "openai_chatgpt/error"
|
9
|
+
autoload :Client, "openai_chatgpt/client"
|
10
|
+
autoload :Object, "openai_chatgpt/object"
|
11
|
+
autoload :Response, "openai_chatgpt/response"
|
12
|
+
autoload :Usage, "openai_chatgpt/usage"
|
13
|
+
autoload :Choice, "openai_chatgpt/choice"
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: openai_chatgpt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sulman Baig
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-03-06 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: '2.5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rubocop
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.35'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.35'
|
41
|
+
description: OpenAI ChatGPT API is a light-weight Ruby wrapper for the Rubyists. gpt-3.5-turbo, gpt-3.5-turbo-0301.
|
42
|
+
You can use this gem to generate text using OpenAI's ChatGPT chat responses API.
|
43
|
+
email:
|
44
|
+
- sulman@hey.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files:
|
48
|
+
- README.md
|
49
|
+
files:
|
50
|
+
- ".rubocop.yml"
|
51
|
+
- CHANGELOG.md
|
52
|
+
- CODE_OF_CONDUCT.md
|
53
|
+
- Gemfile
|
54
|
+
- Gemfile.lock
|
55
|
+
- README.md
|
56
|
+
- Rakefile
|
57
|
+
- lib/openai_chatgpt.rb
|
58
|
+
- lib/openai_chatgpt/choice.rb
|
59
|
+
- lib/openai_chatgpt/client.rb
|
60
|
+
- lib/openai_chatgpt/error.rb
|
61
|
+
- lib/openai_chatgpt/object.rb
|
62
|
+
- lib/openai_chatgpt/response.rb
|
63
|
+
- lib/openai_chatgpt/usage.rb
|
64
|
+
- lib/openai_chatgpt/version.rb
|
65
|
+
- sig/openai_chatgpt.rbs
|
66
|
+
homepage: https://github.com/sulmanweb/openai_chatgpt
|
67
|
+
licenses:
|
68
|
+
- MIT
|
69
|
+
metadata:
|
70
|
+
homepage_uri: https://github.com/sulmanweb/openai_chatgpt
|
71
|
+
source_code_uri: https://github.com/sulmanweb/openai_chatgpt
|
72
|
+
changelog_uri: https://github.com/sulmanweb/openai_chatgpt/blob/main/CHANGELOG.md
|
73
|
+
rubygems_mfa_required: 'true'
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.6'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubygems_version: 3.4.1
|
90
|
+
signing_key:
|
91
|
+
specification_version: 4
|
92
|
+
summary: OpenAI ChatGPT API is a light-weight Ruby wrapper for the Rubyists. gpt-3.5-turbo,
|
93
|
+
gpt-3.5-turbo-0301
|
94
|
+
test_files: []
|