open_ai_bot 0.2.11 → 0.3.0

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: 577e5ceaa83702d924c3d5d5bbdd030f77e6dde813e787d4ceabfe1785301007
4
- data.tar.gz: e2a6bec851ee1d3b2ed7beb33471dcdf39a04f072ca477c72f7d38358b6f479b
3
+ metadata.gz: 33e8660fa731258c76bc1f3367fe02f866a02a189d49444d0006b791f49df6e4
4
+ data.tar.gz: fff2721f65eb41a74c9dc7569bc1be202ed3bd952c787fd371db158662542da8
5
5
  SHA512:
6
- metadata.gz: daf9a35778b1933e6992de0198d44e2934f3257a02d9836a4faaf8b1146c8dd3a86515f047cc7b2e7f8ae86f377e28e9872028f3ac74104ccfab54c0c31eacd1
7
- data.tar.gz: 0ba19cee1b518c306e063b84a1ffd0c42e018fd50ddf3d3c4250a0ce6bae7be09da24c065ff42abff2dba0fb2e2331c7ad449a18ad6a2355b79d605d6b11dea5
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.5)
4
+ addressable (2.8.6)
5
5
  public_suffix (>= 2.0.2, < 6.0)
6
- async (2.6.4)
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.1.1)
12
- coderay (1.1.3)
11
+ base64 (0.2.0)
13
12
  concurrent-ruby (1.2.2)
14
- console (1.23.2)
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
- faraday (2.7.11)
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.2)
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
- nokogiri (1.15.4-arm64-darwin)
69
- racc (~> 1.4)
70
- nokogiri (1.15.4-x86_64-linux)
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.2.5)
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
- unf (0.1.4)
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
 
@@ -72,6 +72,16 @@ module OpenAI
72
72
  "NULL"
73
73
  end
74
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
+
75
85
  def handle_gpt_command
76
86
  return unless bot_mentioned? || bot_replied_to? || private_chat?
77
87
  return if self.class.registered_commands.keys.any? { @text.include? _1 }
@@ -86,7 +96,8 @@ module OpenAI
86
96
  replies_to: @replies_to&.message_id,
87
97
  from: username(@user),
88
98
  body: @text_without_bot_mentions,
89
- chat_id: @chat.id
99
+ chat_id: @chat.id,
100
+ base64_image: base64(@msg.photo&.last)
90
101
  )
91
102
 
92
103
  return unless current_message.valid?
@@ -98,7 +109,8 @@ module OpenAI
98
109
  replies_to: @replies_to.reply_to_message&.message_id,
99
110
  from: username(@target),
100
111
  body: @replies_to.text.to_s.gsub(/@#{config.bot_username}\b/, ""),
101
- chat_id: @chat.id
112
+ chat_id: @chat.id,
113
+ base64_image: base64(@replies_to.photo&.last)
102
114
  )
103
115
  else
104
116
  nil
@@ -121,7 +133,7 @@ module OpenAI
121
133
  )
122
134
 
123
135
  if response["error"]
124
- error_text = "```#{response["error"]["message"]}```"
136
+ error_text = "```\n#{response["error"]["message"]}```"
125
137
  error_text += "\n\nHint: send /restart command to reset the context." if error_text.match? "tokens"
126
138
  send_chat_gpt_error(error_text.strip)
127
139
  else
@@ -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
- content = [from, body].compact.join("\n")
22
- { role:, content: content }
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|
@@ -40,7 +40,7 @@ module OpenAI
40
40
  reply(text)
41
41
  end
42
42
 
43
- def send_whisper_error
43
+ def send_whisper_error(text)
44
44
  reply_code(text)
45
45
  end
46
46
 
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.11"
11
+ spec.version = "0.3.0"
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.11
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-10-03 00:00:00.000000000 Z
11
+ date: 2023-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: down