ask_chatgpt 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: 66ad01c3932f32f34589d4d2eaf742c28dcd607543d98ec886e3fe56da3c655b
4
+ data.tar.gz: 1a7609890b8fc4ea6249ea2b563ab29ebfca0528f4255ffc4e00a3f1db0b806f
5
+ SHA512:
6
+ metadata.gz: e07712c65e13c713271127dcf539a3007763c03fe5e8afa7523820b31a003e442414d22f4fc218fa82f2f9294c3dd2679ab7ef9fcc4070635e58c05e50db62f8
7
+ data.tar.gz: a3564f0aecca1cea800486aa93758f46747d10cc5a185b66be43036dbc05797d92bc7f71566db652b191b1d9375790be5f3239f99f73c5f573105edcbeac23ca
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2023 Igor Kasyanchuk
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # AskChatGPT
2
+
3
+ [![RailsJazz](https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/my_other.svg?raw=true)](https://www.railsjazz.com)
4
+ [![Listed on OpenSource-Heroes.com](https://opensource-heroes.com/badge-v1.svg)](https://opensource-heroes.com/o/railsjazz)
5
+
6
+ AI-Powered Assistant Gem right in your Rails console.
7
+
8
+ ![AskChatGPT](docs/gpt.gif)
9
+
10
+ A Gem that leverages the power of AI to make your development experience more efficient and enjoyable. With this gem, you can streamline your coding process, effortlessly refactor and improve your code, and even generate tests on the fly.
11
+
12
+ We are welcoming you to propose new prompts or adjust existing ones!
13
+
14
+ ## Usage
15
+
16
+ Go to Rails console and run:
17
+
18
+ ```ruby
19
+ gpt.ask("how to get max age of user with projects from Ukraine").with_model(User, Project, Country)
20
+ gpt.ask("convert json to xml")
21
+ gpt.refactor("User.get_report")
22
+ gpt.improve("User.get_report")
23
+ gpt.rspec_test(User)
24
+ gpt.unit_test(User)
25
+ gpt.code_review(User.method(:get_report))
26
+ gpt.find_bug('User.full_name')
27
+ ```
28
+
29
+ ## Examples
30
+
31
+ Ask for code ideas:
32
+ ![AskChatGPT](docs/gpt1.png)
33
+
34
+ Do you need help to write rspec test?
35
+ ![AskChatGPT](docs/gpt2.png)
36
+
37
+ What about unit tests?
38
+ ![AskChatGPT](docs/gpt3.png)
39
+
40
+ Ask ChatGPT to improve your code:
41
+ ![AskChatGPT](docs/gpt4.png)
42
+
43
+ ## Installation
44
+
45
+ Add this line to your application's Gemfile:
46
+
47
+ ```ruby
48
+ gem "ask_chatgpt"
49
+ ```
50
+
51
+ And then execute:
52
+ ```bash
53
+ $ bundle
54
+ ```
55
+
56
+ Or install it yourself as:
57
+ ```bash
58
+ $ gem install ask_chatgpt
59
+ ```
60
+
61
+ ## Options
62
+
63
+ Run `rails g ask_chatgpt initializer`.
64
+
65
+ And you can edit:
66
+
67
+ ```ruby
68
+ AskChatGPT.setup do |config|
69
+ # config.access_token = ENV["OPENAI_API_KEY"]
70
+ # config.debug = false
71
+ # config.model = "gpt-3.5-turbo"
72
+ # config.temperature = 0.1
73
+ end
74
+ ```
75
+
76
+ ## TODO
77
+
78
+ - cli app?
79
+ - more prompts (cover controllers, sql, etc?)
80
+ - tests
81
+ - can it be used with pry/byebug/etc?
82
+ - print tokens usage?
83
+ - support org_id?
84
+
85
+ ## Contributing
86
+
87
+ You are welcome to contribute.
88
+
89
+ ## License
90
+
91
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ module AskChatgpt
2
+ class Core
3
+ def self.call
4
+ client = OpenAI::Client.new(access_token: AskChatGPT.access_token)
5
+ AskChatgpt::Executor.new(client)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,70 @@
1
+ require_relative "prompts/base"
2
+ require_relative "prompts/improve"
3
+ Dir[File.join(__dir__, "prompts", "*.rb")].each do |file|
4
+ require file
5
+ end
6
+
7
+ # for inspiration:
8
+ # https://www.greataiprompts.com/chat-gpt/best-coding-prompts-for-chat-gpt/
9
+
10
+ module AskChatgpt
11
+ class Executor
12
+ attr_reader :scope, :client
13
+
14
+ def initialize(client)
15
+ @scope = [AskChatGPT::Prompts::App.new]
16
+ @client = client
17
+ end
18
+
19
+ def with_model(*models)
20
+ models.each do |model|
21
+ @scope << AskChatGPT::Prompts::Model.new(model)
22
+ end
23
+ self
24
+ end
25
+
26
+ alias :with_models :with_model
27
+
28
+ [:improve, :refactor, :question, :find_bug, :code_review, :rspec_test, :unit_test].each do |method|
29
+ define_method(method) do |*args|
30
+ @scope << AskChatGPT::Prompts.const_get(method.to_s.camelize).new(*args)
31
+ self
32
+ end
33
+ end
34
+
35
+ alias :ask :question
36
+ alias :how :question
37
+ alias :find :question
38
+ alias :review :code_review
39
+
40
+ def inspect
41
+ puts(call); nil
42
+ end
43
+
44
+ def call
45
+ pp parameters if AskChatGPT.debug
46
+
47
+ return puts("No prompts given") if scope.size == 1 # only App prompt
48
+
49
+ spinner = TTY::Spinner.new(format: :classic)
50
+ spinner.auto_spin
51
+
52
+ response = client.chat(parameters: parameters)
53
+ content = response.dig("choices", 0, "message", "content")
54
+ spinner.stop
55
+
56
+ parsed = TTY::Markdown.parse(content)
57
+ parsed
58
+ ensure
59
+ spinner.stop if spinner.spinning?
60
+ end
61
+
62
+ def parameters
63
+ {
64
+ model: AskChatGPT.model,
65
+ messages: scope.map { |e| { role: "user", content: e.content } },
66
+ temperature: AskChatGPT.temperature,
67
+ }
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,28 @@
1
+ module AskChatgpt
2
+ module Prompts
3
+ class App < Base
4
+ def content
5
+ [
6
+ general_info,
7
+ version_info,
8
+ database_info
9
+ ].compact_blank.join(", ")
10
+ end
11
+
12
+ private
13
+
14
+ def general_info
15
+ "I have a Ruby on Rails Application"
16
+ end
17
+
18
+ def database_info
19
+ db_name = ActiveRecord::Base.connection_db_config.configuration_hash[:database] rescue nil
20
+ "Database: #{db_name}" if db_name
21
+ end
22
+
23
+ def version_info
24
+ "Version #{Rails.version}, Ruby Version: #{RUBY_VERSION}"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,15 @@
1
+ module AskChatgpt
2
+ module Prompts
3
+ class Base
4
+ attr_reader :str
5
+
6
+ def initialize(str = nil)
7
+ @str = str
8
+ end
9
+
10
+ def content
11
+ str
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module AskChatgpt
2
+ module Prompts
3
+ class CodeReview < Improve
4
+ private def action_info
5
+ "Analyze the given Ruby code for code smells and suggest improvements:"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module AskChatgpt
2
+ module Prompts
3
+ class FindBug < Improve
4
+ private def action_info
5
+ "Locate any logic errors in the following Ruby code snippet:"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,66 @@
1
+ module AskChatgpt
2
+ module Prompts
3
+ class Improve < Base
4
+ attr_reader :method
5
+
6
+ def initialize(method)
7
+ @method = method
8
+ end
9
+
10
+ def content
11
+ [
12
+ action_info,
13
+ method_info,
14
+ ].compact_blank.join("\n")
15
+ end
16
+
17
+ private
18
+
19
+ def action_info
20
+ "Analyze the given Ruby on Rails code for code smells and suggest improvements"
21
+ end
22
+
23
+ def method_info
24
+ case method
25
+ when ::Proc, ::Method, ::UnboundMethod
26
+ "Method source\n: #{method.source}"
27
+ when Module, Class, String
28
+ str = capture_io do
29
+ IRB.CurrentContext.main.irb_show_source(method.to_s)
30
+ end.join("\n")
31
+ if str.include?("locate a definition for")
32
+ method.to_s
33
+ else
34
+ str
35
+ end
36
+ else
37
+ raise "Not supported parameter, pass a Method object (example: method(:foo)))"
38
+ end
39
+ end
40
+
41
+ # from https://github.com/minitest/minitest/blob/master/lib/minitest/assertions.rb#L542
42
+ def capture_io
43
+ _synchronize do
44
+ begin
45
+ captured_stdout, captured_stderr = StringIO.new, StringIO.new
46
+
47
+ orig_stdout, orig_stderr = $stdout, $stderr
48
+ $stdout, $stderr = captured_stdout, captured_stderr
49
+
50
+ yield
51
+
52
+ return captured_stdout.string, captured_stderr.string
53
+ ensure
54
+ $stdout = orig_stdout
55
+ $stderr = orig_stderr
56
+ end
57
+ end
58
+ end
59
+
60
+ def _synchronize
61
+ yield
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,47 @@
1
+ module AskChatgpt
2
+ module Prompts
3
+ class Model < Base
4
+ attr_reader :model
5
+
6
+ def initialize(model)
7
+ @model = model
8
+ end
9
+
10
+ def content
11
+ [
12
+ summary_info,
13
+ schema_info,
14
+ associations_info,
15
+ instructions_info
16
+ ].compact_blank.join("\n\n")
17
+ end
18
+
19
+ private
20
+
21
+ def summary_info
22
+ "Model: #{model.name}, table name: #{model.table_name}"
23
+ end
24
+
25
+ def schema_info
26
+ "Schema: #{model.table_name}\n" +
27
+ model.columns.map do |column|
28
+ "#{column.name}: #{column.type}"
29
+ end.join("\n")
30
+ end
31
+
32
+ def associations_info
33
+ "#{model.name} Associations and Relations: \n" +
34
+ model.reflections.map do |name, reflection|
35
+ [
36
+ "#{reflection.macro} :#{name}",
37
+ reflection.options.inject("") { |h, (k, v)| h += "#{k}: #{v}" }
38
+ ].join(', ')
39
+ end.join("\n")
40
+ end
41
+
42
+ def instructions_info
43
+ "Use relations and associations, pay attention to the foreign keys. Read models and their associations."
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,6 @@
1
+ module AskChatgpt
2
+ module Prompts
3
+ class Question < Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ module AskChatgpt
2
+ module Prompts
3
+ class Refactor < Improve
4
+ private def action_info
5
+ "Suggest refactoring improvements for the following Ruby code:"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module AskChatgpt
2
+ module Prompts
3
+ class RspecTest < Improve
4
+ private def action_info
5
+ "Write a rspec test for the given Ruby on Rails code:"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module AskChatgpt
2
+ module Prompts
3
+ class UnitTest < Improve
4
+ private def action_info
5
+ "Write a unit test for the given Ruby on Rails code:"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ module AskChatgpt
2
+ class Railtie < ::Rails::Railtie
3
+ console do
4
+ TOPLEVEL_BINDING.eval('self').extend AskChatGPT::ConsoleMethods
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module AskChatgpt
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,48 @@
1
+ require "ask_chatgpt/version"
2
+ require "ask_chatgpt/railtie"
3
+ require "net/http"
4
+ require "json"
5
+ require "openai"
6
+ require "tty-markdown"
7
+ require "tty-spinner"
8
+
9
+ require_relative "ask_chatgpt/executor"
10
+ require_relative "ask_chatgpt/core"
11
+
12
+ module AskChatgpt
13
+ mattr_accessor :debug
14
+ @@debug = false
15
+
16
+ # https://platform.openai.com/docs/models
17
+ mattr_accessor :model
18
+ @@model = "gpt-3.5-turbo"
19
+
20
+ # https://platform.openai.com/docs/api-reference/completions/create#completions/create-temperature
21
+ mattr_accessor :temperature
22
+ @@temperature = 0.1
23
+
24
+ # use your own API key (local per set in initializer or ENV)
25
+ mattr_accessor :access_token
26
+ @@access_token = ENV["OPENAI_API_KEY"]
27
+
28
+ def self.setup
29
+ yield(self)
30
+ end
31
+
32
+ module ConsoleMethods
33
+ def gpt
34
+ AskChatGPT::Core.call
35
+ end
36
+
37
+ alias :chatgpt :gpt
38
+ alias :chat_gpt :gpt
39
+
40
+ def run(*args)
41
+ gpt(*args)
42
+ end
43
+ end
44
+
45
+ extend ConsoleMethods
46
+ end
47
+
48
+ AskChatGPT = AskChatgpt
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generate configuration file.
3
+
4
+ Example:
5
+ rails generate ask_chatgpt initializer
6
+
7
+ This will create:
8
+ config/ask_chatgpt.rb
@@ -0,0 +1,7 @@
1
+ class AskChatgptGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def copy_initializer
5
+ template 'template.rb', 'config/initializers/ask_chatgpt.rb'
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ AskChatGPT.setup do |config|
2
+ # config.access_token = ENV["OPENAI_API_KEY"]
3
+ # config.debug = false
4
+ # config.model = "gpt-3.5-turbo"
5
+ # config.temperature = 0.1
6
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :ask_chatgpt do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ask_chatgpt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Igor Kasyanchuk
8
+ - Liubomyr Manastyretskyi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2023-04-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: ruby-openai
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: tty-markdown
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: tty-spinner
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: wrapped_print
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: pry
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ description: AI-Powered Assistant Gem right in your Rails console.
99
+ email:
100
+ - igorkasyanchuk@gmail.com
101
+ - manastyretskyi@gmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - MIT-LICENSE
107
+ - README.md
108
+ - Rakefile
109
+ - lib/ask_chatgpt.rb
110
+ - lib/ask_chatgpt/core.rb
111
+ - lib/ask_chatgpt/executor.rb
112
+ - lib/ask_chatgpt/prompts/app.rb
113
+ - lib/ask_chatgpt/prompts/base.rb
114
+ - lib/ask_chatgpt/prompts/code_review.rb
115
+ - lib/ask_chatgpt/prompts/find_bug.rb
116
+ - lib/ask_chatgpt/prompts/improve.rb
117
+ - lib/ask_chatgpt/prompts/model.rb
118
+ - lib/ask_chatgpt/prompts/question.rb
119
+ - lib/ask_chatgpt/prompts/refactor.rb
120
+ - lib/ask_chatgpt/prompts/rspec_test.rb
121
+ - lib/ask_chatgpt/prompts/unit_test.rb
122
+ - lib/ask_chatgpt/railtie.rb
123
+ - lib/ask_chatgpt/version.rb
124
+ - lib/generators/ask_chatgpt/USAGE
125
+ - lib/generators/ask_chatgpt/ask_chatgpt_generator.rb
126
+ - lib/generators/ask_chatgpt/templates/template.rb
127
+ - lib/tasks/ask_chatgpt_tasks.rake
128
+ homepage: https://github.com/railsjazz.com/ask_chatgpt
129
+ licenses:
130
+ - MIT
131
+ metadata:
132
+ homepage_uri: https://github.com/railsjazz.com/ask_chatgpt
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubygems_version: 3.3.7
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: AI-Powered Assistant Gem right in your Rails console.
152
+ test_files: []