chat_gpt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9ca65fccf7955b7187454c88484a5e1d252767a5349789bf2c8597bc7764f088
4
+ data.tar.gz: 7ab863283c180354d8aee8aa1d1a2c9a809f4fda77e716f489b5ac8eee954c94
5
+ SHA512:
6
+ metadata.gz: 4be0031f7a0ea028fafd2cce4eab9c99c562235a14f5f5c3a0bef816e8628b89e54f2c8b47aaf310fcf1fbdbe69a5bba4108a23068e04aff3dea469750afc242
7
+ data.tar.gz: 6176884544c505114e43d9c8fb580bfc8357599ea379bc23e2d3af42f652531a67bc81a0f6edc23426f746fe44dd8629339630bf37a29ba79da6ed24b25390a9
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ # Chat GPT
2
+
3
+ This is OpenAI Chat GPT ruby wrapper (beta). Read more <https://beta.openai.com/docs/introduction>
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ `bundle add chat_gpt`
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ `gem install chat_gpt`
14
+
15
+ ## Usage
16
+
17
+ Visit <https://openai.com> to get an API key for the ChatGPT API
18
+
19
+ ```ruby
20
+ # config/initializers/chat_gpt.rb
21
+ ChatGpt.configure do |config|
22
+ config.key = 'API_KEY'
23
+ end
24
+ ```
25
+
26
+ ```ruby
27
+ # Create a new ChatGPT instance with your API key
28
+ chat_gpt = ChatGpt::Api.new
29
+
30
+ # Send a message to ChatGPT and output the response
31
+ response = chat_gpt.send("Hello, how are you?")
32
+ puts response.inspect
33
+
34
+ # Send a subsequent message to ChatGPT and connect it to the parent message
35
+ response = chat_gpt.send("That's good to hear. What have you been up to lately?", conversation_id: 'abc123')
36
+ puts response.inspect
37
+ ```
38
+
39
+ The response:
40
+
41
+ ```ruby
42
+ {
43
+ conversation_id: 'unique_conversation_id',
44
+ responses: [
45
+ 'An array of messages from ChatGPT'
46
+ ]
47
+ }
48
+ ```
49
+
50
+ ### Parameter: `max_tokens`
51
+
52
+ In the context of using the ChatGPT API, `max_tokens` refers to the maximum number of tokens (i.e. individual words or punctuation marks) that can be included in each response generated by ChatGPT. This parameter is used to limit the length of the responses generated by ChatGPT and to prevent the API from generating excessively long responses.
53
+
54
+ By default, the `max_tokens` parameter is set to **100**, which means that ChatGPT will generate responses with a maximum of 100 tokens each. However, you can adjust this parameter when making a request to the API in order to generate longer or shorter responses, depending on your needs.
55
+
56
+ It's important to note that setting a higher value for max_tokens will not necessarily result in longer responses from ChatGPT. The length of the responses generated by ChatGPT will also depend on the input prompt and the model being used. Additionally, the max_tokens parameter is subject to certain limitations imposed by the API provider, so you should be aware of these limitations when setting this parameter.
57
+
58
+ ### Parameter: `temperature`
59
+
60
+ In the context of a language model such as ChatGPT, the `temperature` parameter is a value that controls the level of randomness in the model's responses. A higher temperature value will cause the model to generate more diverse and unpredictable responses, while a lower temperature value will cause the model to generate more predictable and repetitive responses.
61
+
62
+ For example, if you set the temperature parameter to a high value, such as **0.8**, the ChatGPT model will be more likely to generate unique and creative responses to your prompts. This can be useful if you want to explore different ideas or possibilities, or if you want the model to generate more interesting and varied responses.
63
+
64
+ On the other hand, if you set the temperature parameter to a low value, such as 0.2, the ChatGPT model will be more likely to generate responses that are similar to previous responses and follow predictable patterns. This can be useful if you want the model to generate more consistent and coherent responses, or if you want to control the content of the responses more closely.
65
+
66
+ Overall, the `temperature` parameter allows you to adjust the level of randomness in the ChatGPT model's responses and fine-tune the output to suit your needs.
67
+
68
+ ### ChatGPT models
69
+
70
+ The ChatGPT API offers several different models to choose from, each with its own unique characteristics and capabilities. The available models are:
71
+
72
+ 1. `curie`: This model is named after Marie Curie, a famous scientist known for her pioneering work in the field of radioactivity. This model is well-suited for generating responses to scientific or technical prompts.
73
+ 2. `ada`: This model is named after Ada Lovelace, a 19th-century mathematician who is considered to be the world's first computer programmer. This model is well-suited for generating responses to mathematical or computational prompts.
74
+ 3. `grace`: This model is named after Grace Hopper, a pioneering computer scientist and United States Navy Rear Admiral. This model is well-suited for generating responses to prompts related to computers, technology, or the military.
75
+ 4. `flora`: This model is named after Flora Stieglitz, a pioneering photographer who was known for her portraits and landscapes. This model is well-suited for generating responses to prompts related to photography or the arts.
76
+ 5. `davinci`: This model is named after Leonardo da Vinci, a famous artist, scientist, and inventor.This model is well-suited for generating responses to prompts that are broad or open-ended in nature.
77
+
78
+ ## Development
79
+
80
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
81
+
82
+ 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).
83
+
84
+ ## Contributing
85
+
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dpaluy/chat_gpt. 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/dpaluy/chat_gpt/blob/master/CODE_OF_CONDUCT.md).
87
+
88
+ ## License
89
+
90
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
91
+
92
+ ## Code of Conduct
93
+
94
+ Everyone interacting in the chatGpt project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/dpaluy/chat_gpt/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,37 @@
1
+ module ChatGpt
2
+ class Api
3
+ MODELS = %w[
4
+ curie
5
+ ada
6
+ grace
7
+ flora
8
+ davinci
9
+ ].freeze
10
+
11
+ attr_reader :connection
12
+
13
+ def initialize(params)
14
+ @connection = ChatGpt::Client.new(params)
15
+ end
16
+
17
+ # rubocop:disable Layout/ArgumentAlignment
18
+ def send(prompt, model: "curie", num_responses: 1, max_tokens: 100,
19
+ temperature: 0.5, conversation_id: nil)
20
+ response = connection.send_message(prompt,
21
+ model: model, num_responses: num_responses, max_tokens: max_tokens,
22
+ temperature: temperature, conversation_id: conversation_id)
23
+
24
+ parse_resonse(JSON.parse(response.body)["data"])
25
+ end
26
+ # rubocop:enable Layout/ArgumentAlignment
27
+
28
+ private
29
+
30
+ def parse_resonse(response)
31
+ {
32
+ conversation_id: response[0]["conversation_id"],
33
+ responses: response.map { |r| r["response"] }
34
+ }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,34 @@
1
+ require "json"
2
+ require "net/http"
3
+
4
+ module ChatGpt
5
+ class Client
6
+ # Set the URL for the ChatGPT API
7
+ API_URL = "https://api.openai.com/v1/chatgpt/generate".freeze
8
+
9
+ attr_reader :api_key
10
+
11
+ # Initialize the class with the API key
12
+ def initialize(params = {})
13
+ @api_key = params.fetch(:api_key, ChatGpt.config.key)
14
+ end
15
+
16
+ # Send a message to ChatGPT and return the response and conversation ID
17
+ def send_message(prompt,
18
+ model: "curie", num_responses: 1, max_tokens: 100,
19
+ temperature: 0.5, conversation_id: nil)
20
+ headers = {
21
+ "Content-Type" => "application/json",
22
+ "Authorization" => "Bearer #{api_key}"
23
+ }
24
+
25
+ body = {
26
+ prompt: prompt, model: model,
27
+ num_responses: num_responses, max_tokens: max_tokens,
28
+ temperature: temperature, conversation_id: conversation_id
29
+ }.to_json
30
+
31
+ Net::HTTP.post(URI(API_URL), body, headers)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ require "logger"
2
+
3
+ module ChatGpt
4
+ class Config
5
+ attr_accessor :key
6
+ attr_writer :logger
7
+
8
+ def logger
9
+ @logger ||= Logger.new($stderr)
10
+ end
11
+ end
12
+
13
+ def self.configure
14
+ yield config
15
+ end
16
+
17
+ def self.config
18
+ @config ||= Config.new
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module ChatGpt
2
+ class Engine < Rails::Engine
3
+ isolate_namespace ChatGpt
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatGpt
4
+ VERSION = "0.1.0"
5
+ end
data/lib/chat_gpt.rb ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "chat_gpt/config"
4
+ require_relative "chat_gpt/api"
5
+ require_relative "chat_gpt/client"
6
+ require_relative "chat_gpt/engine" if defined?(Rails)
7
+ require_relative "chat_gpt/version"
8
+
9
+ module ChatGpt
10
+ class Error < StandardError; end
11
+
12
+ def self.logger
13
+ config.logger
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chat_gpt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - dpaluy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-12-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This is OpenAI's ChatGPT API wrapper for Ruby.
14
+ email:
15
+ - dpaluy@users.noreply.github.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - lib/chat_gpt.rb
22
+ - lib/chat_gpt/api.rb
23
+ - lib/chat_gpt/client.rb
24
+ - lib/chat_gpt/config.rb
25
+ - lib/chat_gpt/engine.rb
26
+ - lib/chat_gpt/version.rb
27
+ homepage: https://github.com/dpaluy/chat_gpt
28
+ licenses:
29
+ - MIT
30
+ metadata:
31
+ allowed_push_host: https://rubygems.org
32
+ homepage_uri: https://github.com/dpaluy/chat_gpt
33
+ source_code_uri: https://github.com/dpaluy/chat_gpt
34
+ changelog_uri: https://github.com/dpaluy/chat_gpt/releases
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 3.0.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.3.7
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: This is OpenAI's ChatGPT API wrapper for Ruby.
54
+ test_files: []