chatgpt_assistant 0.1.5 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.env_sample +17 -8
  3. data/.rubocop.yml +17 -6
  4. data/Dockerfile +11 -5
  5. data/Gemfile +36 -22
  6. data/Gemfile.lock +143 -102
  7. data/README.md +25 -27
  8. data/Rakefile +1 -0
  9. data/docker-compose.yml +25 -3
  10. data/exe/chatgpt_assistant +1 -1
  11. data/exe/chatgpt_bot +3 -0
  12. data/lib/{bots → chatgpt_assistant/bots}/application_bot.rb +15 -11
  13. data/lib/chatgpt_assistant/bots/discord/actions.rb +104 -0
  14. data/lib/chatgpt_assistant/bots/discord/auth.rb +36 -0
  15. data/lib/chatgpt_assistant/bots/discord/bot.rb +29 -0
  16. data/lib/chatgpt_assistant/bots/discord/chat_actions.rb +33 -0
  17. data/lib/chatgpt_assistant/bots/discord/events.rb +138 -0
  18. data/lib/chatgpt_assistant/bots/discord/validations.rb +38 -0
  19. data/lib/chatgpt_assistant/bots/discord/voice_actions.rb +33 -0
  20. data/lib/chatgpt_assistant/bots/discord/voice_checkers.rb +35 -0
  21. data/lib/chatgpt_assistant/bots/discord_bot.rb +42 -0
  22. data/lib/chatgpt_assistant/bots/helpers/messenger_helper.rb +16 -0
  23. data/lib/{bots → chatgpt_assistant/bots}/helpers/search_helper.rb +3 -3
  24. data/lib/chatgpt_assistant/bots/jobs/new_chat_job.rb +17 -0
  25. data/lib/chatgpt_assistant/bots/jobs/register_job.rb +16 -0
  26. data/lib/chatgpt_assistant/bots/jobs/voice_connect_job.rb +14 -0
  27. data/lib/chatgpt_assistant/bots/mailers/account_mailer.rb +30 -0
  28. data/lib/chatgpt_assistant/bots/mailers/application_mailer.rb +21 -0
  29. data/lib/chatgpt_assistant/bots/services/new_chat_service.rb +59 -0
  30. data/lib/chatgpt_assistant/bots/services/register_service.rb +45 -0
  31. data/lib/chatgpt_assistant/bots/services/voice_connect_service.rb +29 -0
  32. data/lib/chatgpt_assistant/bots/telegram/actions.rb +54 -0
  33. data/lib/chatgpt_assistant/bots/telegram/auth.rb +40 -0
  34. data/lib/chatgpt_assistant/bots/telegram/bot.rb +17 -0
  35. data/lib/chatgpt_assistant/bots/telegram/chat_actions.rb +30 -0
  36. data/lib/chatgpt_assistant/bots/telegram/events.rb +120 -0
  37. data/lib/chatgpt_assistant/bots/telegram/events_controller.rb +48 -0
  38. data/lib/chatgpt_assistant/bots/telegram/permissions.rb +48 -0
  39. data/lib/chatgpt_assistant/bots/telegram/validations.rb +55 -0
  40. data/lib/chatgpt_assistant/bots/telegram/voice_actions.rb +36 -0
  41. data/lib/chatgpt_assistant/bots/telegram_bot.rb +44 -0
  42. data/lib/chatgpt_assistant/chatter.rb +10 -2
  43. data/lib/chatgpt_assistant/config.rb +27 -2
  44. data/lib/chatgpt_assistant/default_messages.rb +79 -31
  45. data/lib/chatgpt_assistant/error.rb +228 -0
  46. data/lib/chatgpt_assistant/migrations.rb +5 -0
  47. data/lib/chatgpt_assistant/models.rb +38 -13
  48. data/lib/chatgpt_assistant/sidekiq.rb +7 -0
  49. data/lib/chatgpt_assistant/sidekiq.yml +10 -0
  50. data/lib/chatgpt_assistant/version.rb +1 -1
  51. data/lib/chatgpt_assistant.rb +8 -15
  52. data/prompts-data/{awesome-chatgpt-prompts.csv → awesome-en-prompts.csv} +164 -164
  53. data/prompts-data/awesome-pt-prompts.csv +164 -0
  54. metadata +40 -21
  55. data/.env_prod_sample +0 -18
  56. data/docker-compose.prod.yml +0 -34
  57. data/lib/bots/discord_bot.rb +0 -185
  58. data/lib/bots/helpers/authentication_helper.rb +0 -47
  59. data/lib/bots/helpers/discord_helper.rb +0 -124
  60. data/lib/bots/helpers/discord_voice_helper.rb +0 -50
  61. data/lib/bots/helpers/logger_action_helper.rb +0 -7
  62. data/lib/bots/helpers/messenger_helper.rb +0 -134
  63. data/lib/bots/helpers/telegram_helper.rb +0 -77
  64. data/lib/bots/helpers/telegram_voice_helper.rb +0 -33
  65. data/lib/bots/helpers/validation_helper.rb +0 -38
  66. data/lib/bots/helpers/visit_helper.rb +0 -24
  67. data/lib/bots/telegram_bot.rb +0 -149
  68. /data/lib/{bots → chatgpt_assistant/bots}/helpers/audio_helper.rb +0 -0
  69. /data/lib/{bots → chatgpt_assistant/bots}/helpers/file_helper.rb +0 -0
@@ -27,15 +27,20 @@ class UserMigration < ActiveRecord::Migration[5.2]
27
27
  create_table :users do |t|
28
28
  t.string :telegram_id, foreign_key: true, class_name: "Visitor"
29
29
  t.string :discord_id, foreign_key: true, class_name: "Visitor"
30
+ t.string :name, limit: 100
30
31
  t.string :email, null: false, limit: 100
32
+ t.string :phone, limit: 100
31
33
  t.string :password_hash, null: false, limit: 100
32
34
  t.string :password_salt, null: false, limit: 100
35
+ t.string :token, null: false, limit: 100, default: ""
36
+ t.string :openai_token, null: false, limit: 100, default: ""
33
37
  t.integer :current_chat_id, null: false, default: 0
34
38
  t.integer :role, null: false, default: 0
35
39
  t.integer :open_chats, null: false, default: 0
36
40
  t.integer :closed_chats, null: false, default: 0
37
41
  t.integer :total_chats, null: false, default: 0
38
42
  t.integer :total_messages, null: false, default: 0
43
+ t.boolean :active, null: false, default: false
39
44
  t.timestamps
40
45
  end
41
46
  end
@@ -5,8 +5,6 @@ require "active_record"
5
5
  # Visitor model
6
6
  class Visitor < ActiveRecord::Base
7
7
  has_many :visitor_actions
8
- # has_one :tel_user, foreign_key: "telegram_id", class_name: "User"
9
- # has_one :dis_user, foreign_key: "discord_id", class_name: "User"
10
8
  validates :name, presence: true
11
9
  validates :platform, presence: true
12
10
  enum platform: { telegram: 0, discord: 1 }
@@ -26,22 +24,46 @@ class User < ActiveRecord::Base
26
24
 
27
25
  belongs_to :tel_visitor, optional: true, foreign_key: "telegram_id", class_name: "Visitor"
28
26
  belongs_to :dis_visitor, optional: true, foreign_key: "discord_id", class_name: "Visitor"
29
- before_save :encrypt_password
30
- validates :email, presence: true, uniqueness: true
31
- validates :role, presence: true
32
- validates :open_chats, presence: true
33
- validates :closed_chats, presence: true
34
- validates :total_chats, presence: true
35
- validates :total_messages, presence: true
27
+
28
+ validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }, length: { maximum: 100 }, on: :create
29
+ validates :role, presence: true, on: :create
30
+ validates :open_chats, presence: true, on: :create
31
+ validates :closed_chats, presence: true, on: :create
32
+ validates :total_chats, presence: true, on: :create
33
+ validates :total_messages, presence: true, on: :create
34
+ validates :password, presence: true, on: :create
35
+
36
+ before_save :encrypt_password, if: :password
37
+
36
38
  has_many :chats
37
39
 
38
40
  def encrypt_password
39
41
  return if password.nil?
40
42
 
41
- self.password_salt = BCrypt::Engine.generate_salt.to_s
43
+ self.password_salt = BCrypt::Engine.generate_salt
42
44
  self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
43
45
  end
44
46
 
47
+ def encrypt_token
48
+ confirmation = { email: email, password_hash: password_hash, time: created_at }
49
+ BCrypt::Engine.hash_secret(JSON.parse(confirmation.to_json), password_salt)
50
+ end
51
+
52
+ def valid_password?(password)
53
+ BCrypt::Engine.hash_secret(password, password_salt) == password_hash
54
+ end
55
+
56
+ def confirm_account(hash)
57
+ confirmation = { email: email, password_hash: password_hash, time: created_at }
58
+ secret = BCrypt::Engine.hash_secret(JSON.parse(confirmation.to_json), password_salt)
59
+ return false unless secret == hash
60
+
61
+ self.token = secret
62
+ self.active = true
63
+ save
64
+ true
65
+ end
66
+
45
67
  def current_chat
46
68
  chats.find(current_chat_id)
47
69
  end
@@ -53,6 +75,10 @@ class User < ActiveRecord::Base
53
75
  def chat_by_title(title)
54
76
  chats.find_by(title: title)
55
77
  end
78
+
79
+ def chat_history
80
+ current_chat.messages.last(10).map { |m| "#{m.role}: #{m.content}\nat: #{m.created_at}" }
81
+ end
56
82
  end
57
83
 
58
84
  # Chat model
@@ -69,15 +95,14 @@ class Chat < ActiveRecord::Base
69
95
  def init_chat_if_actor_provided
70
96
  return if actor.nil?
71
97
 
72
- messages.create(content: prompt, role: "user")
73
- messages.create(content: "Hello, I'm #{actor}. I will follow #{prompt}", role: "assistant")
98
+ messages.create(content: prompt, role: "system")
74
99
  end
75
100
  end
76
101
 
77
102
  # Message model
78
103
  class Message < ActiveRecord::Base
79
104
  validates :content, presence: true
80
- enum role: { user: 0, assistant: 1 }
105
+ enum role: { user: 0, assistant: 1, system: 2 }
81
106
 
82
107
  belongs_to :chat
83
108
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../chatgpt_assistant"
4
+
5
+ Sidekiq.configure_server do |config|
6
+ config.redis = { url: ENV.fetch("REDIS_URL", "redis://redis:6379/1") }
7
+ end
@@ -0,0 +1,10 @@
1
+ development:
2
+ :concurrency: 5
3
+
4
+ production:
5
+ :concurrency: 5
6
+
7
+ :max_retries: 1
8
+
9
+ :queues:
10
+ - default
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ChatgptAssistant
4
- VERSION = "0.1.5"
4
+ VERSION = "0.1.7"
5
5
  end
@@ -7,15 +7,20 @@ require_relative "chatgpt_assistant/chatter"
7
7
  require_relative "chatgpt_assistant/version"
8
8
  require_relative "chatgpt_assistant/config"
9
9
  require_relative "chatgpt_assistant/models"
10
- require_relative "bots/application_bot"
11
- require_relative "bots/telegram_bot"
12
- require_relative "bots/discord_bot"
10
+ require_relative "chatgpt_assistant/error"
11
+ require_relative "chatgpt_assistant/bots/application_bot"
12
+ require_relative "chatgpt_assistant/bots/telegram_bot"
13
+ require_relative "chatgpt_assistant/bots/discord_bot"
14
+ require_relative "chatgpt_assistant/bots/mailers/application_mailer"
15
+ require_relative "chatgpt_assistant/bots/mailers/account_mailer"
13
16
  require "awesome_chatgpt_actors"
14
17
  require "streamio-ffmpeg"
15
18
  require "aws-sdk-polly"
16
19
  require "telegram/bot"
17
20
  require "ibm_watson"
18
21
  require "discordrb"
22
+ require "sidekiq"
23
+ require "sidekiq-scheduler"
19
24
  require "faraday"
20
25
  require "bcrypt"
21
26
 
@@ -32,24 +37,12 @@ module ChatgptAssistant
32
37
  return discord_bot if discord_mode?
33
38
 
34
39
  raise "Invalid mode"
35
- rescue StandardError => e
36
- save_error(e)
37
- retry
38
40
  end
39
41
 
40
42
  private
41
43
 
42
44
  attr_reader :mode, :config
43
45
 
44
- def save_error(err)
45
- puts "Error: #{err.message}"
46
- err.backtrace.each { |line| puts line }
47
- Error.create(message: err.message, backtrace: err.backtrace) unless err.message == Error.last&.message
48
- rescue StandardError
49
- puts "Error: #{err.message}"
50
- puts "Backtrace: #{err.backtrace}"
51
- end
52
-
53
46
  def telegram_mode?
54
47
  mode == "telegram"
55
48
  end