wm: irc-h2o-bot

Download patch

ref: 9f1cdd7d29bc32f43163552f78b60fb4b3502f4d
parent: fa731d2f6d93fe6224f369e9dc506b4f284c730d
author: 0x4A4D00 <scorpion_rn@yahoo.com>
date: Thu Jul 20 21:55:52 EDT 2023

Using Ruby-IRC bot is Working Async Well - Some SSL Crashes Happen

--- a/test.rb
+++ b/test.rb
@@ -4,6 +4,10 @@
 require 'uri'
 require 'json'
 require 'concurrent'
+require 'async'
+require 'async/http/internet'
+require 'async/http/endpoint'
+require 'IRC'
 
 executor = Concurrent::ThreadPoolExecutor.new(
   min_threads: 2,
@@ -18,6 +22,17 @@
   :nicknames  => ['h2o-bot0']
 )
 
+Server = irc.ircnow.org
+BotNick = "h2o"
+
+$bot = IRC.new(
+  BotNick,
+  Server,
+  "6667",
+  "H2O")
+
+
+$is = false
 $currentid = ''
 
 
@@ -38,92 +53,181 @@
 
 #:if => {:message => "bah"}
 
-def SendReq(peer, input, c)
-  uri = URI('https://gpt-gm.h2o.ai/conversation/64ac1344245bd909c99ed909')
-  https = Net::HTTP.new(uri.host, uri.port)
-  https.use_ssl = true
 
-  request = Net::HTTP::Post.new(uri.path)
+class Sender
+  include Concurrent::Async
+  $irc0 = Net::YAIL.new(
+    :address    => 'irc.ircnow.org',
+    :username   => 'Moz_Bot',
+    :realname   => 'Moz_Mozak',
+    :nicknames  => ['h2o-bot0']
+  )
+  def Wait(peer)
+      $irc.msg(peer, 'Im thinking...')
+  end
 
-  request['Content-Type'] = 'application/json; charset=UTF-8'
-  request['User-Agent'] = 'Mozilla/5.0 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.5672.127 Safari/537.36'
-  request['Cookie'] = 'h2ogpt-chat=a4fc9f08-6c67-466c-8d36-dd425702d6b4'
+  def SendReq(peer, input, c)
+    uri = URI('https://gpt-gm.h2o.ai/conversation/64ac1344245bd909c99ed909')
+    https = Net::HTTP.new(uri.host, uri.port)
+    https.use_ssl = true
 
-  request.body = '{"inputs":"'+ input.to_s() +'","parameters":{"temperature":0.1,"truncate":2048,"max_new_tokens":1024,"do_sample":true,"repetition_penalty":1.2,"return_full_text":false},"stream":true,"options":{"id":"6543eb99-e311-4f4e-b791-7af4fcbe556b","response_id":"b194833d-e13c-4390-9d7f-3cf4e4d95430","is_retry":false,"use_cache":false,"web_search_id":""}}'
+    request = Net::HTTP::Post.new(uri.path)
 
-  response = https.request(request)
+    request['Content-Type'] = 'application/json; charset=UTF-8'
+    request['User-Agent'] = 'Pedaret Mozilla/5.0 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.5672.127 Safari/537.36'
+    request['Cookie'] = 'h2ogpt-chat=a4fc9f08-6c67-466c-8d36-dd425702d6b4'
 
-  #$irc.msg('#fpc', e.message)
+    request.body = '{"inputs":"'+ input.to_s() +'","parameters":{"temperature":0.1,"truncate":2048,"max_new_tokens":1024,"do_sample":true,"repetition_penalty":1.2,"return_full_text":false},"stream":true,"options":{"id":"6543eb99-e311-4f4e-b791-7af4fcbe556b","response_id":"b194833d-e13c-4390-9d7f-3cf4e4d95430","is_retry":false,"use_cache":false,"web_search_id":""}}'
 
-  json_strings = response.body.split('data:')
+    # Async do |task|
 
-  parsed_json_objects = []
+      # Async do
+      #   endpoint = Async::HTTP::Endpoint.parse('https://gpt-gm.h2o.ai/conversation/64ac1344245bd909c99ed909')
+      #   http = Async::HTTP::Internet.new
+      #
+      #   request = Async::HTTP::Request.new(:post, endpoint.path,
+      #                                      {'Content-Type' => 'application/json; charset=UTF-8'},
+      #                                      {'Cookie' => 'h2ogpt-chat=a4fc9f08-6c67-466c-8d36-dd425702d6b4'},
+      #                                      '{"inputs":"'+ input.to_s() +'","parameters":{"temperature":0.1,"truncate":2048,"max_new_tokens":1024,"do_sample":true,"repetition_penalty":1.2,"return_full_text":false},"stream":true,"options":{"id":"6543eb99-e311-4f4e-b791-7af4fcbe556b","response_id":"b194833d-e13c-4390-9d7f-3cf4e4d95430","is_retry":false,"use_cache":false,"web_search_id":""}}')
+      #
+      #   response = http.call(endpoint, request)
+      #
+      #   # Process the response
+      #   puts "Response code: #{response.status}"
+      #   puts "Response body: #{response.read}"
+      #
+      #
+      # end
 
-  json_strings.each do |json_str|
-    if json_str != ''
-      parsed_json_objects << JSON.parse(json_str)
+      # request = Async::HTTP::Internet.new
+      #
+      # request['Content-Type'] = 'application/json; charset=UTF-8'
+      # request['User-Agent'] = 'Mozilla/5.0 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.5672.127 Safari/537.36'
+      # request['Cookie'] = 'h2ogpt-chat=a4fc9f08-6c67-466c-8d36-dd425702d6b4'
+      #
+      # request.body = '{"inputs":"'+ input.to_s() +'","parameters":{"temperature":0.1,"truncate":2048,"max_new_tokens":1024,"do_sample":true,"repetition_penalty":1.2,"return_full_text":false},"stream":true,"options":{"id":"6543eb99-e311-4f4e-b791-7af4fcbe556b","response_id":"b194833d-e13c-4390-9d7f-3cf4e4d95430","is_retry":false,"use_cache":false,"web_search_id":""}}'
+      #
+
+      # task.async do
+      #   request.post(uri.host)
+      # end
+
+    # end
+
+    begin
+      response = https.request(request)
+
+
+
+    #$irc.msg('#fpc', e.message)
+
+    json_strings = response.body.split('data:')
+
+    parsed_json_objects = []
+
+    json_strings.each do |json_str|
+      if json_str != ''
+        parsed_json_objects << JSON.parse(json_str)
+      end
     end
-  end
 
-  b = ''
-  parsed_json_objects.each do |json_obj|
-    result = json_obj['token']['text']
-    if result == '<|endoftext|>' then
-      break
-    elsif result.include?("\n")
-      begin
-        c.msg(peer, b)
-        b = ''
+    b = ''
+    parsed_json_objects.each do |json_obj|
+      result = json_obj['token']['text']
+      if result == '<|endoftext|>' then
+        break
+      elsif result.include?("\n")
+        begin
+          $bot.send_message(peer, b)
+          # $irc.msg(peer, b)
+          b = ''
+        end
+      elsif (1) then
+        b += result
       end
-    elsif (1) then
-      b += result
     end
+
+    $bot.send_message(peer, b)
+    # $irc.msg(peer, b)
+
+    rescue StandardError => e
+      $bot.send_message(peer, e)
+    end
   end
+end
 
-  $irc.msg(peer, b)
+sender = Sender.new
+
+
+IRCEvent.add_callback('endofmotd') { |event| $bot.add_channel('#fpc') }
+IRCEvent.add_callback('invite') do |event|
+  puts event.message
+  puts event.channel
+  $bot.add_channel(event.message)
 end
+# IRCEvent.add_callback('join') { |event|
+#   bot.send_message(event.channel, "Hello #{event.from}")
+IRCEvent.add_callback('privmsg') {|event|
+  puts event.channel
+  puts event.from
 
-$irc.heard_msg(:if => {:pm? => true}) do |e|
-  if ($currentid != e.nick)
-    $currentid = e.nick
-    puts($currentid)
-    SendReq(e.nick, e.message, $irc)
-    puts('e')
+  if event.channel.eql?(BotNick)
+  begin
+    if (event.message.start_with?(".invite"))
+    begin
+      event.message.slice! ".invite "
+      $bot.add_channel(event.message)
+    end
+    else
+      $bot.send_message(event.from, "I'm thinking...")
+      sender.SendReq(event.from, event.message, '')
+    end
   end
+  else
+    if (event.message.start_with?("h2o"))
+      $bot.send_message(event.channel, "#{event.from}, I'm thinking...")
+      sender.SendReq(event.channel, event.message, '')
+    end
+  end
+
+}
+# }
+
+$bot.connect
+
+
+$irc.heard_msg(:if => {:pm? => true}) do |e|
+  puts(e.nick)
+  sender.async.Wait(e.nick)
+  sender.async.SendReq(e.nick, e.message, $irc)
+  puts('e')
 end
 
 $irc.heard_msg(:if => {:pm? => false}) do |e|
-  if (e.message.start_with?("h2o") && $currentid != e.channel)
-    $currentid = e.channel
-    puts($currentid)
-    SendReq(e.channel, e.message, $irc)
+  if (e.message.start_with?("h2o"))
+    puts(e.channel)
+    sender.async.Wait(e.channel)
+    sender.async.SendReq(e.channel, e.message, $irc)
     puts('e')
   end
 end
-$irc.start_listening!
+
+#$irc.start_listening!
+
 futures = []
 
+
+
 3.times do |i|
   futures << Concurrent::Future.execute(executor: executor) do
-    $irc.heard_msg(:if => {:pm? => true}) do |e|
-      if ($currentid != e.nick)
-        $currentid = e.nick
-        puts($currentid)
-        SendReq(e.nick, e.message, $irc)
-        puts('e')
-      end
-    end
 
-    $irc.heard_msg(:if => {:pm? => false}) do |e|
-      if (e.message.start_with?("h2o") && $currentid != e.channel)
-        $currentid = e.channel
-        puts($currentid)
-        SendReq(e.channel, e.message, $irc)
-        puts('e')
-      end
-    end
   end
 end
+
+futures.each(&:wait)
+executor.shutdown
+
+
 
 def Tra
   $irc.heard_msg(:if => {:pm? => true}) do |e|