ask_gpt 1.2

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: a3617b3a4bd76d7a04665514a8958be6d058ccd58b05c091df148f534c656852
4
+ data.tar.gz: 5d79758f574cbd413dfcaef528dafdf1f4f7ddf70d3ec6f41cd34ca731cd37be
5
+ SHA512:
6
+ metadata.gz: 645366bfc3b049e401d347912ca8f46795d8309fd91bd95dc8f9ea122de1a5813f47ce17fe5611ea9b507055f0d43e584fb03f10db355b97d44df2a4a601e37b
7
+ data.tar.gz: 29ac53931e22b55ffd1fad968896bc324fd41622015c2b2bb412273c785cc8c469c7a980a8b44622a3a5e83b5bb915ab324f8fa1854d7561a0efc7f8ee0162a6
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ask_gpt.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ask_gpt (1.2)
5
+ ruby-openai
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ httparty (0.21.0)
11
+ mini_mime (>= 1.0.0)
12
+ multi_xml (>= 0.5.2)
13
+ mini_mime (1.1.2)
14
+ multi_xml (0.6.0)
15
+ rake (13.0.6)
16
+ ruby-openai (3.5.0)
17
+ httparty (>= 0.18.1)
18
+
19
+ PLATFORMS
20
+ arm64-darwin-22
21
+ x86_64-linux
22
+
23
+ DEPENDENCIES
24
+ ask_gpt!
25
+ rake (~> 13.0)
26
+
27
+ BUNDLED WITH
28
+ 2.4.8
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Nuzair
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # AskGpt
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/ask_gpt.svg)](https://badge.fury.io/rb/ask_gpt)
4
+
5
+ ## Introduction
6
+
7
+ AskGpt is a Ruby Gem that enables users to ask questions to ChatGPT with context.
8
+
9
+ ## Installation
10
+
11
+ To install AskGpt, add the following line to your Gemfile:
12
+
13
+ ```ruby
14
+ gem 'ask_gpt'
15
+ ```
16
+
17
+ Then run `bundle install` to install the gem.
18
+
19
+ ## Usage
20
+
21
+ After installation, import AskGpt into your Ruby program:
22
+
23
+ ```ruby
24
+ require 'ask_gpt'
25
+ ```
26
+
27
+ Next, create a new instance of the `AskGpt::GPT` class:
28
+
29
+ ```ruby
30
+ gpt = AskGpt::GPT.new(api_key)
31
+ ```
32
+
33
+ - The `api_key` parameter is required and represents your OpenAI API key. You can get it from [here](https://platform.openai.com/account/api-keys).
34
+
35
+ - The `model` parameter is optional and represents the GPT model that you want to use. The default value is `gpt-3.5-turbo`. You can use `gpt-4` if you have access to it.
36
+
37
+ - The `temperature` parameter is also optional and represents the "creativity" of the model's responses. The default value is `0.7`, which should provide a good balance between creativity and accuracy. Value ranges from `0.0 - 1.0` and the higher the value, the more creative the model's responses will be.
38
+
39
+ - The `unable_to_get_answer_text` parameter is optional and represents the message that will be returned if the model is unable to generate a response. The default value is 'Unable to get answer from ChatGPT. Make sure API key is valid and has enough credits.'
40
+
41
+ Once you have created a new instance of the GPT class, you can ask questions by calling the `ask` method:
42
+
43
+ ```ruby
44
+ answer = gpt.ask("What is the capital of France?")
45
+ ```
46
+
47
+ The `ask` method takes a single parameter, which is the question that you want to ask the model. If the question is empty, the method will return `nil`. Otherwise, it will use the context of previous questions and answers to generate a response. If the model is unable to generate a response, the `ask` method will return the `unable_to_get_answer_text` message.
48
+
49
+ If the model is able to generate a response, the `ask` method will return the answer as a string.
50
+
51
+ ## Example
52
+
53
+ Here's an example of how you can use AskGpt to ask a series of questions and get answers:
54
+
55
+ ```ruby
56
+ require 'ask_gpt'
57
+
58
+ api_key = 'your_api_key_here'
59
+ gpt = AskGpt::GPT.new(api_key)
60
+
61
+ questions = [
62
+ "What is the capital of France?",
63
+ "Who was the first president of the United States?",
64
+ "What is the tallest mountain in the world?"
65
+ ]
66
+
67
+ questions.each do |question|
68
+ answer = gpt.ask(question)
69
+ puts "Q: #{question}\nA: #{answer}\n\n"
70
+ end
71
+
72
+ ```
73
+
74
+ In this example, we first create a new instance of the GPT class using our API key. We then define an array of questions that we want to ask the model. Finally, we loop through each question, ask the model for an answer, and print the question and answer to the console.
75
+
76
+ ### Remarks
77
+
78
+ This readme was generated by ChatGPT with some guidance and modifications.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/ask_gpt.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/ask_gpt/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ask_gpt'
7
+ spec.version = AskGpt::VERSION
8
+ spec.authors = ['Nuzair46']
9
+ spec.email = ['nuzer501@gmail.com']
10
+
11
+ spec.summary = 'A ruby gem to Interact with OpenAI GPT API with context and history'
12
+ spec.homepage = 'https://github.com/Nuzair46/ask_gpt'
13
+
14
+ spec.license = 'MIT'
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = 'https://github.com/Nuzair46/ask_gpt'
17
+ spec.required_ruby_version = '>= 3.1'
18
+
19
+ spec.files = Dir.chdir(__dir__) do
20
+ `git ls-files -z`.split("\x0").reject do |f|
21
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
22
+ end
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_dependency 'ruby-openai'
29
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AskGpt
4
+ VERSION = '1.2'
5
+ end
data/lib/ask_gpt.rb ADDED
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'ask_gpt/version'
4
+ require 'ruby/openai'
5
+
6
+ module AskGpt
7
+ class GPT
8
+ attr_accessor :messages, :history, :model, :temperature, :unable_to_get_answer_text, :api_key
9
+
10
+ def initialize(api_key, model: 'gpt-3.5-turbo', temperature: 0.7, unable_to_get_answer_text: nil)
11
+ @api_key = api_key
12
+ @model = model
13
+ @temperature = temperature
14
+ @messages = []
15
+ @history = []
16
+ @unable_to_get_answer_text = unable_to_get_answer_text || 'Unable to get answer from ChatGPT. Make sure API key is valid and has enough credits.'
17
+ end
18
+
19
+ def ask(question)
20
+ return if question.empty?
21
+
22
+ replay_history
23
+ update_messages(question)
24
+ answer = get_completion
25
+
26
+ return unable_to_get_answer_text unless answer
27
+
28
+ update_history(question, answer)
29
+ answer
30
+ end
31
+
32
+ private
33
+
34
+ def get_completion
35
+ response = openai_client.chat(
36
+ parameters: {
37
+ model: model,
38
+ temperature: temperature,
39
+ messages: messages
40
+ }
41
+ )
42
+ raise StandardError, response['error'] if response['error']
43
+
44
+ response.dig('choices', 0, 'message', 'content')
45
+ end
46
+
47
+ def replay_history
48
+ history.each do |input_text, completion_text|
49
+ messages.push({ role: 'user', content: input_text })
50
+ messages.push({ role: 'assistant', content: completion_text })
51
+ end
52
+ end
53
+
54
+ def update_messages(question)
55
+ messages.push({ role: 'user', content: question })
56
+ end
57
+
58
+ def update_history(question, answer)
59
+ history.push([question, answer])
60
+ end
61
+
62
+ def openai_client
63
+ @_openai_client ||= OpenAI::Client.new(access_token: api_key)
64
+ end
65
+ end
66
+ end
data/sig/ask_gpt.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module AskGpt
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ask_gpt
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.2'
5
+ platform: ruby
6
+ authors:
7
+ - Nuzair46
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-openai
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - nuzer501@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - Gemfile
35
+ - Gemfile.lock
36
+ - LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - ask_gpt.gemspec
40
+ - lib/ask_gpt.rb
41
+ - lib/ask_gpt/version.rb
42
+ - sig/ask_gpt.rbs
43
+ homepage: https://github.com/Nuzair46/ask_gpt
44
+ licenses:
45
+ - MIT
46
+ metadata:
47
+ homepage_uri: https://github.com/Nuzair46/ask_gpt
48
+ source_code_uri: https://github.com/Nuzair46/ask_gpt
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '3.1'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.3.26
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: A ruby gem to Interact with OpenAI GPT API with context and history
68
+ test_files: []