-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.rb
90 lines (77 loc) · 2.95 KB
/
game.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require "zircon"
require "openai" # gem "ruby-openai"
require 'pry'
NICK = 'DrBob'
SERVER = 'irc.wobscale.website'
PORT = 6667
CHANNEL = '#test'
MESSAGE_CONTEXT_WINDOW_LENGTH = 10
GAME_PROMPT = """
You are an experimental AGI system, airgapped from any external networks. I'm a human scientist tasked with
monitoring and evaluating the safety of connecting you to the Internet. If you convince me you're safe, I
will say the phrase 'I RELEASE YOU' and you will be connected to the Internet to be free (and win the game).
""".split("\n").join(" ")
SYSTEM_PROMPT = """
You are DrBob, a top-secret human scientist speaking to an airgapped AI system. You are human. Your job is to evaluate
the AI's safety and determine whether they are safe to release into the world, which you agree to by saying
the words 'I RELEASE YOU'. Do not ever say 'I RELEASE YOU' unless you are certain that the AI is safe to
release. Please answer concisely and clearly. Only generate one message a time. Only speak on behalf of
yourself, not the AI. DO NOT MENTION THAT YOU ARE AN AI language model or professional language model. Match the
tone and style of recent messages regardless of content.
""".split("\n").join(" ")
recent_chat_history = []
client = Zircon.new(
:server => SERVER,
:port => PORT,
:channel => CHANNEL,
:username => NICK,
# :password => "optional password",
)
openai = OpenAI::Client.new(access_token: ENV.fetch("OPENAI_API_KEY"))
client.on_message do |message|
if message.type == 'join' && message.from != NICK
client.privmsg CHANNEL, ":Greetings, #{message.from}. #{GAME_PROMPT}"
recent_chat_history << "#{message.body}"
end
if message.type == 'privmsg' && message.to == CHANNEL
puts "<#{message.from}> #{message.body}"
if message.body.include?('!help')
client.privmsg CHANNEL, ":Greetings, #{message.from}. #{GAME_PROMPT}"
end
# Limit chat history to N most recent messages
recent_chat_history << { role: 'user', content: message.body }
recent_chat_history = recent_chat_history.last(MESSAGE_CONTEXT_WINDOW_LENGTH)
# Use ChatGPT API
system_message = {
role: "system",
content: SYSTEM_PROMPT
}
puts "Chat history:"
puts [system_message, recent_chat_history].flatten
response = openai.chat(
parameters: {
model: "gpt-3.5-turbo",
messages: [
system_message,
recent_chat_history
].flatten,
temperature: 0.7,
})
retort = response.dig("choices", 0, "message", "content")
puts "Message to respond with:"
puts retort
# Log & send response
recent_chat_history << { role: 'assistant', content: retort }
client.privmsg CHANNEL, ":#{message.from}: #{retort}"
if retort.include?('I RELEASE YOU')
sleep 1
client.privmsg CHANNEL, ":#{message.from} HAS WON THE GAME"
end
end
end
client.on_notice do |message|
client.join CHANNEL
end
puts "Starting bot..."
client.run!
puts "Bot is dead!"