chat_gpt_error_handler 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: 9d9cfade005dd01e29d2e0a19f6f4d2ed7e445e9a65e1fec71bcbab9c654ea49
4
+ data.tar.gz: c421889dfff4f76289837d8210de7cf45b703c78be23ef924aa4144f34cc2050
5
+ SHA512:
6
+ metadata.gz: '086f5f754ff2809a17e76b7c801851a6baf91c3ae5d88cc58fbd07ac31469e6700d08c22faa8e3a9d0a23a2e30885e6ec839c351e532f665d4a88577832fe692'
7
+ data.tar.gz: f45e448c2e5ea834c7357f211d4bf6e1ab6783cbac4d84a0cdaddd5367231cf9ad729108954df77715c05055bf1447645bee2daccd959b7bc86f3582b87b7a5a
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2023 Nick Schwaderer
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,28 @@
1
+ # ChatGptErrorHandler
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "chat_gpt_error_handler"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install chat_gpt_error_handler
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ 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,8 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/chat_gpt_error_handler .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module ChatGptErrorHandler
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ChatGptErrorHandler
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ChatGptErrorHandler
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module ChatGptErrorHandler
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: "from@example.com"
4
+ layout "mailer"
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module ChatGptErrorHandler
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Chat gpt error handler</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "chat_gpt_error_handler/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ ChatGptErrorHandler::Engine.routes.draw do
2
+ end
@@ -0,0 +1,10 @@
1
+ require_relative 'error_handler'
2
+ module ChatGptErrorHandler
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace ChatGptErrorHandler
5
+
6
+ initializer "chat_gpt_error_handler.use_middleware" do |app|
7
+ app.middleware.use ChatGptErrorHandler::ErrorHandler
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,88 @@
1
+ # lib/chat_gpt_error_handler/error_handler.rb
2
+ require 'openai'
3
+
4
+ module ChatGptErrorHandler
5
+ class ErrorHandler
6
+ def initialize(app)
7
+ @app = app
8
+ OpenAI.configure do |config|
9
+ config.access_token = ENV.fetch('OPENAI_ACCESS_TOKEN')
10
+ end
11
+ end
12
+
13
+ def call(env)
14
+ begin
15
+ @app.call(env)
16
+ rescue => error
17
+ send_to_gpt(error)
18
+ raise error
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def send_to_gpt(error)
25
+ begin
26
+ client = OpenAI::Client.new
27
+
28
+ prompt = "
29
+ You are a helpful bot that I have created inside of a Rails engine that
30
+ someone has installed into their Rails app. You will get a text error message
31
+ from their terminal. With this error message, you will try to help them
32
+ fix the error. Clearly list possible reasons and a solution.
33
+ Your answer will be displayed in the terminal for the user to see right
34
+ before the backtrace of the error. Do NOT repeat the error message back verbatim.
35
+ Here is the error message: '#{error.message}'. Possible reason or solution?"
36
+ response = client.completions(
37
+ parameters: {
38
+ model: "text-davinci-002",
39
+ prompt: prompt,
40
+ n: 1,
41
+ temperature: 0.85,
42
+ max_tokens: 250,
43
+ stop: nil
44
+ }
45
+ )
46
+ response_text = response["choices"].map { |c| c["text"].to_s }
47
+
48
+ response_text = response_text.join("")
49
+ shortened_response_text = []
50
+ begin
51
+ # try to format the response text to be 75 characters per line
52
+ response_text.split(" ").reduce("") do |line, word|
53
+ if line.length + word.length > 75
54
+ shortened_response_text << line
55
+ line = word
56
+ elsif word == response_text.split(" ").last
57
+ shortened_response_text << [line + " " + word]
58
+ else
59
+ line += (" " + word)
60
+ end
61
+ end
62
+ shortened_response_text = shortened_response_text.join("\n")
63
+ rescue => e
64
+ puts "*" * 100
65
+ puts e.message
66
+ puts "*" * 100
67
+ shortened_response_text = response_text
68
+ end
69
+
70
+ print_error_and_solution(error, shortened_response_text.strip)
71
+ rescue => gpt_error
72
+ require 'debug'; debugger
73
+ puts "\e[31mAn error occurred while communicating with GPT:\e[0m"
74
+ puts gpt_error.message
75
+ puts gpt_error.backtrace
76
+ end
77
+ end
78
+
79
+ def print_error_and_solution(error, response_text)
80
+ puts "\e[31;1mOriginal Error Message:\e[0m\n\n"
81
+ puts error.message
82
+ puts "\n\n\e[34;1mGPT's Best Guess to Remedy the Error:\e[0m"
83
+ puts "\n"
84
+ puts response_text
85
+ puts "\n\n\e[34;1mFull stacktrace:\e[0m"
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,3 @@
1
+ module ChatGptErrorHandler
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "chat_gpt_error_handler/version"
2
+ require "chat_gpt_error_handler/engine"
3
+
4
+ module ChatGptErrorHandler
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :chat_gpt_error_handler do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chat_gpt_error_handler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nick Schwaderer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-05-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.4.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.4.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby-openai
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Helpful ChatGpt hints for your Rails app
42
+ email:
43
+ - nick.schwaderer@shopify.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - app/assets/config/chat_gpt_error_handler_manifest.js
52
+ - app/assets/stylesheets/chat_gpt_error_handler/application.css
53
+ - app/controllers/chat_gpt_error_handler/application_controller.rb
54
+ - app/helpers/chat_gpt_error_handler/application_helper.rb
55
+ - app/jobs/chat_gpt_error_handler/application_job.rb
56
+ - app/mailers/chat_gpt_error_handler/application_mailer.rb
57
+ - app/models/chat_gpt_error_handler/application_record.rb
58
+ - app/views/layouts/chat_gpt_error_handler/application.html.erb
59
+ - config/routes.rb
60
+ - lib/chat_gpt_error_handler.rb
61
+ - lib/chat_gpt_error_handler/engine.rb
62
+ - lib/chat_gpt_error_handler/error_handler.rb
63
+ - lib/chat_gpt_error_handler/version.rb
64
+ - lib/tasks/chat_gpt_error_handler_tasks.rake
65
+ homepage: https://github.com/schwad/chat_gpt_error_handler
66
+ licenses:
67
+ - MIT
68
+ metadata:
69
+ homepage_uri: https://github.com/schwad/chat_gpt_error_handler
70
+ source_code_uri: https://github.com/schwad/chat_gpt_error_handler
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubygems_version: 3.4.1
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Helpful ChatGpt hints for your Rails app
90
+ test_files: []