Module: OpenAI::ChatGPT
- Included in:
- OpenAIBot
- Defined in:
- lib/open_ai/chat_gpt.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #current_thread ⇒ Object
- #get_tokens_info!(response) ⇒ Object
- #handle_gpt_command ⇒ Object
- #init_session ⇒ Object
- #send_chat_gpt_error(text) ⇒ Object
- #send_chat_gpt_response(text, tokens_info) ⇒ Object
- #send_request! ⇒ Object
- #username(user) ⇒ Object
Class Method Details
.included(base) ⇒ Object
55 56 57 |
# File 'lib/open_ai/chat_gpt.rb', line 55 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#current_thread ⇒ Object
64 65 66 |
# File 'lib/open_ai/chat_gpt.rb', line 64 def current_thread self.class.threads[@chat.id] || self.class.new_thread(@chat.id) end |
#get_tokens_info!(response) ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'lib/open_ai/chat_gpt.rb', line 146 def get_tokens_info!(response) completion_tokens = response.dig("usage", "completion_tokens") prompt_tokens = response.dig("usage", "prompt_tokens") cached_prompt_tokens = response.dig("usage", "prompt_tokens_details", "cached_tokens") current_thread.model.request_cost( completion_tokens:, prompt_tokens:, cached_prompt_tokens:, current_thread: ) end |
#handle_gpt_command ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/open_ai/chat_gpt.rb', line 76 def handle_gpt_command return unless bot_mentioned? || bot_replied_to? || private_chat? return if self.class.registered_commands.keys.any? { @text.include? _1 } if !allowed_chat? reply(, parse_mode: "Markdown") if return end = Message.new( id: @message_id, replies_to: @replies_to&., from: username(@user), body: @text_without_bot_mentions, chat_id: @chat.id, chat_thread: current_thread, image: Image.from_tg_photo(download_file(@msg.photo&.last), model: current_thread.model) ) return unless .valid? replies_to = if @replies_to && !bot_replied_to? Message.new( id: @replies_to., replies_to: @replies_to.&., from: username(@target), body: @replies_to.text.to_s.gsub(/@#{config.bot_username}\b/, ""), chat_id: @chat.id, chat_thread: current_thread, image: Image.from_tg_photo(download_file(@replies_to.photo&.last), model: current_thread.model) ) else nil end current_thread.add(replies_to) current_thread.add() send_request! end |
#init_session ⇒ Object
59 60 61 62 |
# File 'lib/open_ai/chat_gpt.rb', line 59 def init_session self.class.new_thread(@chat.id) () end |
#send_chat_gpt_error(text) ⇒ Object
142 143 144 |
# File 'lib/open_ai/chat_gpt.rb', line 142 def send_chat_gpt_error(text) reply(text, parse_mode: "Markdown") end |
#send_chat_gpt_response(text, tokens_info) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/open_ai/chat_gpt.rb', line 156 def send_chat_gpt_response(text, tokens_info) tokens_text = tokens_info[:info] tokens_text = '' if config.show_price_info == false id = reply(text + tokens_text). = BotMessage.new( id: id, replies_to: @message_id, body: text, chat_id: @chat.id, chat_thread: current_thread, cost: tokens_info[:total] ) current_thread.add() end |
#send_request! ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/open_ai/chat_gpt.rb', line 118 def send_request! send_chat_action(:typing) # todo: rewrite in async response = open_ai.chat( parameters: { model: current_thread.model.to_s, messages: current_thread.as_json } ) if response["error"] error_text = "```\n#{response["error"]["message"]}```" error_text += "\n\nHint: send /restart command to reset the context." if error_text.match? "tokens" send_chat_gpt_error(error_text.strip) else text = response.dig("choices", 0, "message", "content") tokens_info = get_tokens_info!(response) send_chat_gpt_response(text, tokens_info) end end |
#username(user) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/open_ai/chat_gpt.rb', line 68 def username(user) return unless user return "@" + user.username if user.username.present? return user.first_name if user.first_name.present? "NULL" end |