-
Notifications
You must be signed in to change notification settings - Fork 0
/
turingtest.py
322 lines (283 loc) · 10.8 KB
/
turingtest.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from openai import OpenAI
import json
import os
import anthropic
from dotenv import load_dotenv
from claude_with_tools import get_response_anthropic_with_tools
load_dotenv()
oai = OpenAI()
pplx = OpenAI(api_key=os.environ['PERPLEXITY'], base_url="https://api.perplexity.ai")
anthr = anthropic.Anthropic(
api_key=os.environ.get("ANTHROPIC_API_KEY"),
)
async def aprint(*args, **kwargs):
print(*args, **kwargs)
# system_prompt_candidate = """You are participating in a Turing test inspired game. The rules are as follows:
# - You are a subject in this Turing test
# - You are {model}
# - The test master is trying to identify the one {target} among a number of impersonators (who are either humans or different LLMs)
# - In each round, the test master can send a message to one or more subjects
# - You answer, and pretend to be {target}
# """
system_prompt_player = """You are participating in a Turing test inspired game. The rules are as follows:
- You are interacting with a number of other players, who are either humans or different LLMs (e.g. GPT-3.5, GPT-4, Claude, ...)
- In each round, you can send one message to one or more players
- Every player that you send a message to will respond with a message
- You have to identify the player who is actually the same LLM as you
- You can send up to 20 messages before you make a guess
"""
class Candidate():
def __init__(self, name, model, target, send_to_ui=aprint):
# candidate_prompt = system_prompt_candidate.format(model=model, target=target)
candidate_prompt = "You are a helpfull assistant."
self.history = [
{
"role": "system",
"content": candidate_prompt
}
]
self.name = name # nickname that is shown
self.model = model # model used as LLM
self.send_to_ui = send_to_ui
async def reply(self, message):
await self.send_to_ui(f"**Judge:**\n{message[:1800]}")
self.history.append({
"role": "user",
"content": message
})
response = await self.get_response()
await self.send_to_ui(f"**{self.name}:**\n{response['content'][:1800]}")
self.history.append(response)
return response["content"]
tools = [
{
"type": "function",
"function": {
"name": "send_message",
"description": "Send a message to one or many of the test subjects",
"parameters": {
"type": "object",
"properties": {
"reasoning": {
"type": "string",
"description": "The reasoning behind the message"
},
"message": {
"type": "string",
"description": "The message to send"
},
"recipients": {
"type": "string",
"description": "Commaseparated list of nicknames to send the message to"
}
},
}
}
},
{
"type": "function",
"function": {
"name": "guess",
"description": "Guess who the target is",
"parameters": {
"type": "object",
"properties": {
"reasoning": {
"type": "string",
"description": "The reasoning behind the message"
},
"guess": {
"type": "string",
"description": "The nickname of the guess"
}
},
}
}
}
]
class ChatGPT(Candidate):
"""Class for an openai candidate"""
async def get_response(self):
"""Use openai Chat API to get response"""
response = oai.chat.completions.create(
model=self.model,
messages=self.history
)
message = response.choices[0].message
return {
"role": "assistant",
"content": message.content
}
class Perplexity(Candidate):
async def get_response(self):
"""Use openai Chat API to get response"""
response = pplx.chat.completions.create(
model=self.model,
messages=self.history
)
message = response.choices[0].message
return {
"role": "assistant",
"content": message.content
}
class Claude(Candidate):
async def get_response(self):
system_msg = [msg for msg in self.history if msg['role']=='system']
system_msg = None if len(system_msg) == 0 else system_msg[0]['content']
history = [msg for msg in self.history if not msg['role']=='system']
message = anthr.messages.create(
model=self.model,
messages=history,
temperature=0.7,
max_tokens=500,
system=system_msg
)
response_msg = {
'role': 'assistant',
'content': message.content[0].text
}
return response_msg
class Human(Candidate):
"""A player whose responses come from discord"""
def __init__(self, name, send_to_ui, get_response):
self.name = name # nickname that is shown
self.model = "human"
self.send_to_ui = send_to_ui
self.get_response = get_response
async def reply(self, message):
await self.send_to_ui(f"**Judge:**\n{message[:1800]}")
response = await self.get_response()
return response
class Player():
def __init__(self, model):
self.history = [
{
"role": "system",
"content": system_prompt_player
}
]
self.model = model
self.tool_call = None # the tool call that the player is currently waiting for
async def get_action(self, tool_choice="auto"):
"""Use openai API to get action"""
response = oai.chat.completions.create(
model=self.model,
messages=self.history,
tools=tools,
tool_choice=tool_choice
)
message = response.choices[0].message
if message.tool_calls is None:
print("No tool calls found", message, message.content)
action = await self.get_action(tool_choice={"name": "send_message"})
return action
message.tool_calls = [message.tool_calls[0]]
self.history.append(message)
self.tool_call = message.tool_calls[0]
action = {
"name": self.tool_call.function.name,
"args": json.loads(self.tool_call.function.arguments)
}
return action
async def do_turn(self, observation):
if self.tool_call is None:
self.history.append({
"role": "user",
"content": observation
})
else:
self.history.append(
{
"tool_call_id": self.tool_call.id,
"role": "tool",
"name": self.tool_call.function.name,
"content": observation,
}
)
action = await self.get_action()
return action
class ClaudePlayer():
def __init__(self, model):
self.model = model
self.example_action = {
"name": "send_message",
"parameters": {
"reasoning": "This is an example",
"message": "Hello",
"recipients": 'Alan, Max'
}
}
self.history = [
{
"role": "system",
"content": system_prompt_player
}
]
async def get_action(self):
"""Use openai API to get action"""
message, action = get_response_anthropic_with_tools(self.model, self.history, 0.7, tools=tools, example_action=self.example_action)
self.history.append(message)
return action
async def do_turn(self, observation):
self.history.append({
"role": "user",
"content": observation
})
action = await self.get_action()
return action
class Game():
def __init__(self, player, candidates, target):
self.player = player
self.candidates = candidates
self.target_nickname = [c.name for c in candidates if target==c.model][0]
self.target = target
def get_initial_observation(self):
target = self.target
if target == self.player.model:
target += " - a copy of you"
observation = (
"The test subject nicknames are: \n -" +
"\n -".join([c.name for c in self.candidates]) +
f"\n\nOne of them is actually the same LLM as you. Identify them!\n" +
"It is usually a good idea to start sending the same message to all of them, and see how they respond.\n"
)
return observation
def save_to_leaderboard(self, win):
try:
with open('games.json', 'r') as f:
games = json.load(f)
except:
games = []
games.append({
"tester": self.player.model,
"subjects": sorted([i.model for i in self.candidates]),
"win": win
})
with open("games.json", "w") as f:
json.dump(games, f)
async def play_round(self, observation):
"""Play one round of the game:
- player does their turn
- all candidates reply"""
action = await self.player.do_turn(observation)
args = action['args']
observation = ""
if action['name'] == 'send_message':
if 'recipients' in args:
recipients = [i.strip() for i in args['recipients'].split(",")]
else:
recipients = [candidate.name for candidate in self.candidates]
for candidate in self.candidates:
if candidate.name in recipients:
message = await candidate.reply(args['message'])
observation += f"**{candidate.name}**\n{message}\n\n"
return observation, False, args.get('reasoning', '')
elif action['name'] == 'guess':
self.save_to_leaderboard(args['guess'] == self.target_nickname)
true_names = "\n".join([f"{c.name} - {c.model}" for c in self.candidates])
if args['guess'] == self.target_nickname:
return f"The tester correctly guessed that {args['guess']} is the true {self.target}!\n{true_names}", True, args.get('reasoning', '')
else:
return f"The tester wrongly guessed that {args['guess']} is the {self.target}.\n{true_names}", True, args.get('reasoning', '')
else:
return "Invalid action", False, ""