wm: irc-h2o-bot

ref: 5c482500316fb1964e64db9c42322b839dfc0606
dir: /test.rb/

View raw version
require 'rubygems'
require 'net/yail'
require 'net/http'
require 'uri'
require 'json'
require 'concurrent'

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']
)

$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"}

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

  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
        c.msg(peer, b)
        b = ''
      end
    elsif (1) then
      b += result
    end
  end

  $irc.msg(peer, b)
end

$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
$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

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