wm: irc-h2o-bot

ref: cc357037f202611bbbc399f4683a24fe3494ab33
dir: /test.rb/

View raw version
require 'rubygems'
require 'net/yail'
require 'net/http'
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,
  max_threads: 5,
  max_queue: 10
)

$irc = Net::YAIL.new(
  :address    => 'irc.ircnow.org',
  :username   => 'Moz_Bot',
  :realname   => 'Moz_Mozak',
  :nicknames  => ['h2o-bot0']
)

Server = irc.ircnow.org
BotNick = "h2o"

$bot = IRC.new(
  BotNick,
  Server,
  "6667",
  "H2O")


$is = false
$currentid = ''


$irc.on_welcome proc { |event| $irc.join('#fpc') }


$irc.on_invite { |event| $irc.join(event.channel) }

$irc.set_callback(:incoming_invite) { |event| $irc.join(event.channel) }

$irc.hearing_ping {|event| $stderr.puts event.inspect}

$irc.hearing_message(:if => {:pm? => true}) do |event|
  event.handle!
end

$irc.on_invite     { |e| $irc.join(e.channel) }

#:if => {:message => "bah"}


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

  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)

    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'

    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":""}}'

    # Async do |task|

      # 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

      # 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

    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
    end

    $bot.send_message(peer, b)
    # $irc.msg(peer, b)

    rescue StandardError => e
      $bot.send_message(peer, e)
    end
  end
end

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

  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"))
    puts(e.channel)
    sender.async.Wait(e.channel)
    sender.async.SendReq(e.channel, e.message, $irc)
    puts('e')
  end
end

#$irc.start_listening!

futures = []



3.times do |i|
  futures << Concurrent::Future.execute(executor: executor) do

  end
end

futures.each(&:wait)
executor.shutdown



def Tra
  $irc.heard_msg(:if => {:pm? => true}) do |e|
    if ($currentid != e.nick)
      $currentid = e.nick
      SendReq(e.nick, e.message, $irc)
    end
    $currentid = ''
  end

  $irc.heard_msg(:if => {:pm? => false}) do |e|
    if (e.message.start_with?("h2o") && $currentid != e.channel)
      $currentid = e.channel
      SendReq(e.channel, e.message, $irc)
    end
    $currentid = ''
  end
end

def Tra2
  $irc.heard_msg(:if => {:pm? => true}) do |e|
    SendReq(e.nick, e.message, $irc)
  end

  $irc.heard_msg(:if => {:pm? => false}) do |e|
    if (e.message.start_with?("h2o"))
      SendReq(e.channel, e.message, $irc)
    end
  end
end

def Tra3
  $irc.heard_msg(:if => {:pm? => true}) do |e|
    SendReq(e.nick, e.message, $irc)
  end

  $irc.heard_msg(:if => {:pm? => false}) do |e|
    if (e.message.start_with?("h2o"))
      SendReq(e.channel, e.message, $irc)
    end
  end
end

#threads = []
#threads << Thread.new { Tra() }
#threads << Thread.new { Tra() }
#threads << Thread.new { Tra() }

# Wait for all threads to complete
#threads.each(&:join)



# $irc.heard_msg() do |e|
#
#
#
# end





$irc.heard_msg(:if => {:pm? => true}) do |e|
  SendReq(e.nick, e.message, $irc)
end

$irc.heard_msg(:if => {:pm? => false}) do |e|
  if (e.message.start_with?("h2o"))
    SendReq(e.channel, e.message, $irc)
  end
end