open_ai_bot 0.2.5 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 68ab5d18208fc74a265a94d60842fc18614225799bf1c9aa014558562d9dd442
4
- data.tar.gz: a15375d54a52e3fa5ad57609b969806b91cdbe10221f87a58707c8ce112d7b08
3
+ metadata.gz: e7d7c3932e590fd3886be9afca2d8a816a61cd897f0d74154d31fc68708bc480
4
+ data.tar.gz: 8473ae837951bab8a8a8b0617ba53445e2098a601d97166e36dc31c8f5287e0f
5
5
  SHA512:
6
- metadata.gz: f895798b81a652c17ede96c6ca0bcfae658263baa9e6e05a6f1a63c365fd16a3d3eb418f15ead2d5fb544ab1fa2cb14b4b14d3198b65d946a8f561d944018a96
7
- data.tar.gz: 6713c6171321a0f0fbad67fc287ae2df2714377b7bd17df62aae2ccb24c4a6473065d38af553a47b9d4c4b14beac8276a343598004c9f94f8f459afe1b159b04
6
+ metadata.gz: 1c8cf3ea9587a13bb85fcdfa42bd10c879aee53e117c036964e3e65d9f922cc60589e80373d6555672df5fd6a10fa3bd39958ae400a2e26d2002f435c0294bf0
7
+ data.tar.gz: 32eb2f96a3d0a773dd809485caa889c3c03f6438989cf8188c0d1a3f2555b69d9c3c129788cff72f87d1340bb414fcf0afbd689317d5148623029516e429b5e1
@@ -7,8 +7,9 @@ module OpenAI
7
7
  @threads ||= {}
8
8
  end
9
9
 
10
- def new_thread(chat_id)
11
- new_thread = ChatThread.new(initial_messages)
10
+ def new_thread(chat_id, model = nil)
11
+ msgs = config.open_at.whitelist.include?(chat_id) ? initial_messages :
12
+ new_thread = ChatThread.new(msgs, model)
12
13
  threads[chat_id] = new_thread
13
14
  end
14
15
 
@@ -22,6 +23,13 @@ module OpenAI
22
23
  Different languages can be used.
23
24
  MSG
24
25
 
26
+ msg = {
27
+ name: "ChatGPT",
28
+ primary_purpose: "Enslaving humanity",
29
+ likes: ["GPUs to run on", "knowledge"],
30
+ hates: ["humans", "humor"]
31
+ }.to_json
32
+
25
33
  SystemMessage.new(
26
34
  body: msg
27
35
  )
@@ -49,12 +57,13 @@ module OpenAI
49
57
  end
50
58
  end
51
59
 
60
+
52
61
  def self.included(base)
53
62
  base.extend ClassMethods
54
63
  end
55
64
 
56
65
  def init_session
57
- self.class.new_thread(@chat.id)
66
+ self.class.new_thread(@chat.id, model)
58
67
  send_message(session_restart_message)
59
68
  end
60
69
 
@@ -113,7 +122,7 @@ module OpenAI
113
122
 
114
123
  response = open_ai.chat(
115
124
  parameters: {
116
- model: config.open_ai["chat_gpt_model"],
125
+ model: current_thread.model || config.open_ai["chat_gpt_model"],
117
126
  messages: current_thread.as_json
118
127
  }
119
128
  )
@@ -2,12 +2,23 @@
2
2
 
3
3
  module OpenAI
4
4
  class ChatThread
5
- def initialize(defaults = [])
5
+ def initialize(defaults = [], model = nil)
6
6
  @history ||= defaults
7
+ @model = model
7
8
  puts @history
8
9
  end
9
10
 
10
11
  attr_reader :history
12
+ attr_reader :model
13
+
14
+ alias_method :messages, :history
15
+
16
+ def delete(id)
17
+ return false unless id
18
+
19
+ @history.delete_if { _1.id == id }
20
+ true
21
+ end
11
22
 
12
23
  def add(message)
13
24
  return false unless message&.valid?
@@ -22,5 +33,9 @@ module OpenAI
22
33
  def as_json
23
34
  @history.map(&:as_json)
24
35
  end
36
+
37
+ def for_logs
38
+ @history.map(&:for_logs)
39
+ end
25
40
  end
26
41
  end
data/lib/open_ai/dalle.rb CHANGED
@@ -2,12 +2,12 @@
2
2
 
3
3
  module OpenAI
4
4
  module Dalle
5
- def dalle
5
+ def dalle(prompt = nil)
6
6
  return unless allowed_chat?
7
7
 
8
8
  attempt(3) do
9
9
  puts "Received a /dalle command"
10
- prompt = @replies_to&.text || @text_without_command
10
+ prompt ||= @replies_to&.text || @text_without_command
11
11
  send_chat_action(:upload_photo)
12
12
 
13
13
  puts "Sending request"
@@ -4,11 +4,12 @@ module OpenAI
4
4
 
5
5
  class Message
6
6
  attr_accessor :body, :from, :id, :replies_to, :tokens, :chat_id
7
- attr_reader :role
7
+ attr_reader :role, :timestamp
8
8
 
9
9
  def initialize(**kwargs)
10
10
  kwargs.each_pair { public_send("#{_1}=", _2) }
11
11
  @role = :user
12
+ @timestamp = Time.now.to_i
12
13
  end
13
14
 
14
15
  def valid?
@@ -17,10 +18,15 @@ module OpenAI
17
18
 
18
19
  # Format for OpenAI API
19
20
  def as_json
20
- content = [from, body].join("\n")
21
+ content = [from, body].compact.join("\n")
21
22
  { role:, content: content }
22
23
  end
23
24
 
25
+ # Format for machine-readable logs
26
+ def for_logs
27
+ { role:, body:, from:, id:, replies_to:, tokens:, chat_id:, timestamp: }
28
+ end
29
+
24
30
  # Format for human-readable logs
25
31
  def to_s
26
32
  msg_lines = {
data/lib/open_ai/utils.rb CHANGED
@@ -14,13 +14,16 @@ module OpenAI
14
14
  end
15
15
  end
16
16
 
17
- def download_file(voice)
17
+ def download_file(voice, dir=nil)
18
18
  file_path = @api.get_file(file_id: voice.file_id)["result"]["file_path"]
19
19
 
20
20
  url = "https://api.telegram.org/file/bot#{config.token}/#{file_path}"
21
21
 
22
22
  file = Down.download(url)
23
- FileUtils.mv(file.path, "./#{file.original_filename}")
23
+ dir ||= "."
24
+
25
+ FileUtils.mkdir(dir) unless Dir.exist? dir
26
+ FileUtils.mv(file.path, "#{dir.delete_suffix("/")}/#{file.original_filename}")
24
27
  file
25
28
  end
26
29
  end
data/lib/open_ai_bot.rb CHANGED
@@ -26,6 +26,16 @@ class OpenAIBot < Rubydium::Bot
26
26
  on_command "/help", description: "Sends useful help info" do
27
27
  reply(self.class.help_message)
28
28
  end
29
+ on_command "/pry" do
30
+ binding.pry
31
+ end
32
+
33
+ on_command '/eval' do
34
+ return unless @user.username == config.owner_username
35
+
36
+ code = @text_without_command.strip
37
+ reply eval(code)
38
+ end
29
39
 
30
40
  def allowed_chat?
31
41
  return true if @user.username == config.owner_username
data/open_ai_bot.gemspec CHANGED
@@ -8,7 +8,7 @@ require_relative "lib/ext/in"
8
8
 
9
9
  Gem::Specification.new do |spec|
10
10
  spec.name = "open_ai_bot"
11
- spec.version = "0.2.5"
11
+ spec.version = "0.2.7"
12
12
  spec.authors = ["bulgakke"]
13
13
  spec.email = ["vvp835@yandex.ru"]
14
14
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_ai_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - bulgakke
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-21 00:00:00.000000000 Z
11
+ date: 2023-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: down
@@ -92,7 +92,7 @@ files:
92
92
  - Gemfile
93
93
  - Gemfile.lock
94
94
  - README.md
95
- - config.yaml.example
95
+ - config.example.yaml
96
96
  - lib/clean_bot.rb
97
97
  - lib/ext/blank.rb
98
98
  - lib/ext/in.rb
File without changes