ref: e9c239a3e0c40963003327907c55a03c3ba040d5
dir: /test.rb/
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 # ) config = File.read('CONFIG') parsed_config = JSON.parse(config) Server = parsed_config['IRCServer'] BotNick = parsed_config['BotNickName'] $URL = parsed_config['SessionUrl'] $Cookie = parsed_config['SessionCookie'] $irc = Net::YAIL.new( :address => 'irc.ircnow.org', :username => 'Moz_Bot', :realname => 'Moz_Mozak', :nicknames => ['h2o-bot0'] ) $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"} @stack = [] def Push(a,b) @stack << {:peer => a, :message => b} end def Send $bot.send_message(@stack[0][':peer'], @stack[0][':message']) end 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($URL) 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'] = $Cookie 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 counts = 0 b = '' parsed_json_objects.each do |json_obj| result = json_obj['token']['text'] if result == '<|endoftext|>' then break elsif counts >= 20 begin $bot.send_message(peer, 'limit reached!') break end elsif result.include?("\n") begin $bot.send_message(peer, b) counts += 1 #$irc.msg(peer, b) b = '' end else b += result end end $bot.send_message(peer, b) #$irc.msg(peer, b) rescue StandardError => e $bot.send_message(peer, e) #$irc.msg(peer, e) end end end sender = Sender.new IRCConnection.add_IO_socket(STDIN) 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| 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.async.SendReq(event.from, event.message, '') end end else if (event.message.start_with?("h2o")) Push(event.channel, 'Im') Send() # IRCConnection.output_push("PRIVMSG #{event.channel} : moz") $bot.send_message(event.channel, "#{event.from}, I'm thinking...") sender.async.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