open_ai_bot 0.2.10 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +12 -45
- data/lib/open_ai/chat_gpt.rb +16 -11
- data/lib/open_ai/message.rb +17 -5
- data/lib/open_ai/whisper.rb +1 -1
- data/lib/open_ai_bot.rb +0 -10
- data/open_ai_bot.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 33e8660fa731258c76bc1f3367fe02f866a02a189d49444d0006b791f49df6e4
|
|
4
|
+
data.tar.gz: fff2721f65eb41a74c9dc7569bc1be202ed3bd952c787fd371db158662542da8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 84ee567855c908efb78b5b4dc8133a35a954eb8b0d21ad0889518037eb3eed5fea210cdcb1a0f948f0b4fa323e0b92da0e5c5a6bfcf6af4d5d64bac796f09f7a
|
|
7
|
+
data.tar.gz: 2eab3c94fc4493ce5f02e62ca909cf9edbe24955ea4b3085dab3971f4449f958f61939f4d54e3beb02755f9f1977e1950bb588e6be8e7898e8f88224d3684561
|
data/Gemfile.lock
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
1
|
GEM
|
|
2
2
|
remote: https://rubygems.org/
|
|
3
3
|
specs:
|
|
4
|
-
addressable (2.8.
|
|
4
|
+
addressable (2.8.6)
|
|
5
5
|
public_suffix (>= 2.0.2, < 6.0)
|
|
6
|
-
async (2.6.
|
|
6
|
+
async (2.6.5)
|
|
7
7
|
console (~> 1.10)
|
|
8
8
|
fiber-annotation
|
|
9
9
|
io-event (~> 1.1)
|
|
10
10
|
timers (~> 4.1)
|
|
11
|
-
base64 (0.
|
|
12
|
-
coderay (1.1.3)
|
|
11
|
+
base64 (0.2.0)
|
|
13
12
|
concurrent-ruby (1.2.2)
|
|
14
|
-
console (1.23.
|
|
13
|
+
console (1.23.3)
|
|
15
14
|
fiber-annotation
|
|
16
15
|
fiber-local
|
|
17
|
-
domain_name (0.5.20190701)
|
|
18
|
-
unf (>= 0.0.5, < 1.0.0)
|
|
19
16
|
down (5.4.1)
|
|
20
17
|
addressable (~> 2.8)
|
|
21
18
|
dry-core (1.0.1)
|
|
@@ -37,49 +34,26 @@ GEM
|
|
|
37
34
|
dry-inflector (~> 1.0)
|
|
38
35
|
dry-logic (~> 1.4)
|
|
39
36
|
zeitwerk (~> 2.6)
|
|
40
|
-
|
|
37
|
+
event_stream_parser (0.3.0)
|
|
38
|
+
faraday (2.7.12)
|
|
41
39
|
base64
|
|
42
40
|
faraday-net_http (>= 2.0, < 3.1)
|
|
43
41
|
ruby2_keywords (>= 0.0.4)
|
|
44
42
|
faraday-multipart (1.0.4)
|
|
45
43
|
multipart-post (~> 2)
|
|
46
44
|
faraday-net_http (3.0.2)
|
|
47
|
-
ffi (1.15.5)
|
|
48
|
-
ffi-compiler (1.0.1)
|
|
49
|
-
ffi (>= 1.0.0)
|
|
50
|
-
rake
|
|
51
45
|
fiber-annotation (0.2.0)
|
|
52
46
|
fiber-local (1.0.0)
|
|
53
|
-
http (5.1.1)
|
|
54
|
-
addressable (~> 2.8)
|
|
55
|
-
http-cookie (~> 1.0)
|
|
56
|
-
http-form_data (~> 2.2)
|
|
57
|
-
llhttp-ffi (~> 0.4.0)
|
|
58
|
-
http-cookie (1.0.5)
|
|
59
|
-
domain_name (~> 0.5)
|
|
60
|
-
http-form_data (2.3.0)
|
|
61
47
|
ice_nine (0.11.2)
|
|
62
|
-
io-event (1.3.
|
|
63
|
-
llhttp-ffi (0.4.0)
|
|
64
|
-
ffi-compiler (~> 1.0)
|
|
65
|
-
rake (~> 13.0)
|
|
66
|
-
method_source (1.0.0)
|
|
48
|
+
io-event (1.3.3)
|
|
67
49
|
multipart-post (2.3.0)
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
racc (~> 1.4)
|
|
72
|
-
pry (0.14.2)
|
|
73
|
-
coderay (~> 1.1)
|
|
74
|
-
method_source (~> 1.0)
|
|
75
|
-
public_suffix (5.0.3)
|
|
76
|
-
racc (1.7.1)
|
|
77
|
-
rake (13.0.6)
|
|
78
|
-
ruby-openai (5.1.0)
|
|
50
|
+
public_suffix (5.0.4)
|
|
51
|
+
ruby-openai (5.2.0)
|
|
52
|
+
event_stream_parser (>= 0.3.0, < 1.0.0)
|
|
79
53
|
faraday (>= 1)
|
|
80
54
|
faraday-multipart (>= 1)
|
|
81
55
|
ruby2_keywords (0.0.5)
|
|
82
|
-
rubydium (0.
|
|
56
|
+
rubydium (0.4.1)
|
|
83
57
|
async (~> 2.3)
|
|
84
58
|
telegram-bot-ruby (~> 1.0.0)
|
|
85
59
|
telegram-bot-ruby (1.0.0)
|
|
@@ -88,20 +62,13 @@ GEM
|
|
|
88
62
|
faraday-multipart (~> 1.0)
|
|
89
63
|
zeitwerk (~> 2.6)
|
|
90
64
|
timers (4.3.5)
|
|
91
|
-
|
|
92
|
-
unf_ext
|
|
93
|
-
unf_ext (0.0.8.2)
|
|
94
|
-
zeitwerk (2.6.11)
|
|
65
|
+
zeitwerk (2.6.12)
|
|
95
66
|
|
|
96
67
|
PLATFORMS
|
|
97
|
-
arm64-darwin-22
|
|
98
68
|
x86_64-linux
|
|
99
69
|
|
|
100
70
|
DEPENDENCIES
|
|
101
71
|
down (~> 5.4)
|
|
102
|
-
http (~> 5.1)
|
|
103
|
-
nokogiri (~> 1.15)
|
|
104
|
-
pry
|
|
105
72
|
ruby-openai (~> 5.1)
|
|
106
73
|
rubydium (>= 0.2.5)
|
|
107
74
|
|
data/lib/open_ai/chat_gpt.rb
CHANGED
|
@@ -8,7 +8,7 @@ module OpenAI
|
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
def new_thread(chat_id, model = nil)
|
|
11
|
-
msgs = config.open_ai["whitelist"].include?(chat_id) ? initial_messages :
|
|
11
|
+
msgs = config.open_ai["whitelist"].include?(chat_id) ? initial_messages : []
|
|
12
12
|
new_thread = ChatThread.new(msgs, model)
|
|
13
13
|
threads[chat_id] = new_thread
|
|
14
14
|
end
|
|
@@ -23,13 +23,6 @@ module OpenAI
|
|
|
23
23
|
Different languages can be used.
|
|
24
24
|
MSG
|
|
25
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
|
-
|
|
33
26
|
SystemMessage.new(
|
|
34
27
|
body: msg
|
|
35
28
|
)
|
|
@@ -79,6 +72,16 @@ module OpenAI
|
|
|
79
72
|
"NULL"
|
|
80
73
|
end
|
|
81
74
|
|
|
75
|
+
def base64(file)
|
|
76
|
+
return unless config.open_ai["chat_gpt_model"] == "gpt-4-vision-preview"
|
|
77
|
+
return unless file
|
|
78
|
+
|
|
79
|
+
f = download_file(file)
|
|
80
|
+
res = Base64.encode64(f.read)
|
|
81
|
+
FileUtils.rm_rf("./#{f.original_filename}")
|
|
82
|
+
res
|
|
83
|
+
end
|
|
84
|
+
|
|
82
85
|
def handle_gpt_command
|
|
83
86
|
return unless bot_mentioned? || bot_replied_to? || private_chat?
|
|
84
87
|
return if self.class.registered_commands.keys.any? { @text.include? _1 }
|
|
@@ -93,7 +96,8 @@ module OpenAI
|
|
|
93
96
|
replies_to: @replies_to&.message_id,
|
|
94
97
|
from: username(@user),
|
|
95
98
|
body: @text_without_bot_mentions,
|
|
96
|
-
chat_id: @chat.id
|
|
99
|
+
chat_id: @chat.id,
|
|
100
|
+
base64_image: base64(@msg.photo&.last)
|
|
97
101
|
)
|
|
98
102
|
|
|
99
103
|
return unless current_message.valid?
|
|
@@ -105,7 +109,8 @@ module OpenAI
|
|
|
105
109
|
replies_to: @replies_to.reply_to_message&.message_id,
|
|
106
110
|
from: username(@target),
|
|
107
111
|
body: @replies_to.text.to_s.gsub(/@#{config.bot_username}\b/, ""),
|
|
108
|
-
chat_id: @chat.id
|
|
112
|
+
chat_id: @chat.id,
|
|
113
|
+
base64_image: base64(@replies_to.photo&.last)
|
|
109
114
|
)
|
|
110
115
|
else
|
|
111
116
|
nil
|
|
@@ -128,7 +133,7 @@ module OpenAI
|
|
|
128
133
|
)
|
|
129
134
|
|
|
130
135
|
if response["error"]
|
|
131
|
-
error_text = "
|
|
136
|
+
error_text = "```\n#{response["error"]["message"]}```"
|
|
132
137
|
error_text += "\n\nHint: send /restart command to reset the context." if error_text.match? "tokens"
|
|
133
138
|
send_chat_gpt_error(error_text.strip)
|
|
134
139
|
else
|
data/lib/open_ai/message.rb
CHANGED
|
@@ -3,7 +3,7 @@ module OpenAI
|
|
|
3
3
|
# (ChatGPT isn't brilliant at parsing JSON sructures without starting to reply in JSON, so most of it is useless)
|
|
4
4
|
|
|
5
5
|
class Message
|
|
6
|
-
attr_accessor :body, :from, :id, :replies_to, :tokens, :chat_id
|
|
6
|
+
attr_accessor :body, :from, :id, :replies_to, :tokens, :chat_id, :base64_image
|
|
7
7
|
attr_reader :role, :timestamp
|
|
8
8
|
|
|
9
9
|
def initialize(**kwargs)
|
|
@@ -13,13 +13,24 @@ module OpenAI
|
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def valid?
|
|
16
|
-
[body, from, id, chat_id].all?(&:present?)
|
|
16
|
+
[(base64_image || body), from, id, chat_id].all?(&:present?)
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
# Format for OpenAI API
|
|
20
20
|
def as_json
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
msg = [from, body].compact.join("\n")
|
|
22
|
+
|
|
23
|
+
if base64_image
|
|
24
|
+
{
|
|
25
|
+
role: role,
|
|
26
|
+
content: [
|
|
27
|
+
{ type: "text", text: msg },
|
|
28
|
+
{ type: "image_url", image_url: { url: "data:image/jpeg;base64,#{base64_image}" } }
|
|
29
|
+
]
|
|
30
|
+
}
|
|
31
|
+
else
|
|
32
|
+
{ role:, content: msg }
|
|
33
|
+
end
|
|
23
34
|
end
|
|
24
35
|
|
|
25
36
|
# Format for machine-readable logs
|
|
@@ -35,7 +46,8 @@ module OpenAI
|
|
|
35
46
|
"From" => from,
|
|
36
47
|
"To" => replies_to,
|
|
37
48
|
"Body" => body,
|
|
38
|
-
"Tokens used" => tokens
|
|
49
|
+
"Tokens used" => tokens,
|
|
50
|
+
"Image" => (base64_image ? "Some image" : "None")
|
|
39
51
|
}.reject { |_k, v|
|
|
40
52
|
v.blank?
|
|
41
53
|
}.map { |k, v|
|
data/lib/open_ai/whisper.rb
CHANGED
data/lib/open_ai_bot.rb
CHANGED
|
@@ -26,16 +26,6 @@ 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
|
|
39
29
|
|
|
40
30
|
def allowed_chat?
|
|
41
31
|
return true if @user.username == config.owner_username
|
data/open_ai_bot.gemspec
CHANGED
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.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- bulgakke
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2023-
|
|
11
|
+
date: 2023-12-09 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: down
|