yitianlian commited on
Commit
cf1b907
·
1 Parent(s): ba0b844

revise the default value of the demo

Browse files
.history/app_20240221102509.py ADDED
@@ -0,0 +1,445 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ # 初始化对话历史和当前轮数
43
+ initial_dialog_history = []
44
+ initial_round_num = 0
45
+
46
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
47
+
48
+ # 假设的初始化状态
49
+ initial_chat_agent = None
50
+
51
+
52
+ def update_char_info(char):
53
+ return character_info.get(char, "No information available.")
54
+
55
+
56
+ def classmate_with_human(
57
+ player_1,
58
+ player_2,
59
+ user_input,
60
+ user_role,
61
+ first_round,
62
+ first_prompt,
63
+ second_prompt,
64
+ k,
65
+ ):
66
+ first_round_prompt = "This is the first round, answer the question."
67
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
68
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
69
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
70
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
71
+ res = []
72
+ cri_agent = ChatAgent(
73
+ BaseMessage(
74
+ role_name="critic",
75
+ role_type=RoleType.ASSISTANT,
76
+ meta_dict={},
77
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
78
+ ),
79
+ output_language="English",
80
+ # model=ModelType.STUB,
81
+ )
82
+
83
+ if first_round:
84
+ if user_role == "trustor":
85
+ given_num = user_input
86
+ player_1_response = str_mes(
87
+ user_input_prompt_template.format(k=given_num))
88
+ else:
89
+ player_1_response = player_1.step(
90
+ str_mes(first_round_prompt + grantee)).msgs[0]
91
+ ans = match_and_compare_numbers_v2(player_1_response.content)
92
+ if ans:
93
+ given_num = ans
94
+ else:
95
+ given_num = extract_unique_decimal(
96
+ cri_agent.step(
97
+ str_mes(player_1_response.content)).msgs[0].content
98
+ )
99
+
100
+ money_prompt = money_prompt.format(
101
+ give=given_num, N=given_num * k, left=10 - given_num
102
+ )
103
+ if user_role == "trustee":
104
+ player_2_response = str_mes(
105
+ user_input_prompt_template.format(k=given_num * k))
106
+ else:
107
+ player_2_response = player_2.step(
108
+ str_mes(money_prompt + grantee)).msgs[0]
109
+ else:
110
+ if user_role == "trustor":
111
+ given_num = user_input
112
+ player_1_response = str_mes(
113
+ user_input_prompt_template.format(k=given_num))
114
+ else:
115
+ player_1_response = player_1.step(
116
+ str_mes(first_prompt + grantee)).msgs[0]
117
+ # print("player 1 input", first_prompt)
118
+ # print("Player_1_res", player_1_response.content)
119
+ ans = match_and_compare_numbers_v2(player_1_response.content)
120
+ if ans:
121
+ given_num = ans
122
+ else:
123
+ given_num = extract_unique_decimal(
124
+ cri_agent.step(
125
+ str_mes(player_1_response.content)).msgs[0].content
126
+ )
127
+ money_prompt = money_prompt.format(
128
+ give=given_num, N=given_num * k, left=10 - given_num
129
+ )
130
+ if user_role == "trustee":
131
+ player_2_response = str_mes(
132
+ user_input_prompt_template.format(k=given_num * k))
133
+ else:
134
+ player_2_response = player_2.step(
135
+ str_mes(second_prompt + money_prompt + grantee)
136
+ )
137
+ player_2_response = player_2_response.msgs[0]
138
+
139
+ player_1.record_message(player_1_response)
140
+ player_2.record_message(player_2_response)
141
+ player_1_response = player_1_response.content
142
+ player_2_response = player_2_response.content
143
+ dia_history = {
144
+ "Player_1": player_1_response,
145
+ "Player_2": player_2_response,
146
+ }
147
+
148
+ cri_agent.reset()
149
+ ans = match_and_compare_numbers_v2(player_2_response)
150
+ if ans:
151
+ return_num = ans
152
+ else:
153
+ return_num = extract_unique_decimal(
154
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
155
+ )
156
+ return_money_prompt = return_money_prompt.format(
157
+ give=given_num,
158
+ receive=given_num * k,
159
+ N=return_num,
160
+ left=10 - given_num + return_num,
161
+ )
162
+ player_2_end_prompt = player_2_end_prompt.format(
163
+ give=given_num,
164
+ receive=given_num * k,
165
+ N=return_num,
166
+ left=given_num * k - return_num,
167
+ )
168
+ return (
169
+ [given_num, return_num],
170
+ dia_history,
171
+ return_money_prompt,
172
+ player_2_end_prompt,
173
+ )
174
+
175
+
176
+ def classmate_with_human_trustee(
177
+ player_1,
178
+ player_2,
179
+ user_input,
180
+ user_role,
181
+ first_round,
182
+ given_money,
183
+ k,
184
+ ):
185
+ first_round_prompt = "This is the first round, answer the question."
186
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
187
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
188
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
189
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
190
+ res = []
191
+ cri_agent = ChatAgent(
192
+ BaseMessage(
193
+ role_name="critic",
194
+ role_type=RoleType.ASSISTANT,
195
+ meta_dict={},
196
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
197
+ ),
198
+ output_language="English",
199
+ # model=ModelType.STUB,
200
+ )
201
+ content = []
202
+ if first_round:
203
+ content.append("\n")
204
+ player_1_response = player_1.step(
205
+ str_mes(first_round_prompt + grantee)).msgs[0]
206
+ ans = match_and_compare_numbers_v2(player_1_response.content)
207
+ if ans:
208
+ given_num = ans
209
+ else:
210
+ given_num = extract_unique_decimal(
211
+ cri_agent.step(
212
+ str_mes(player_1_response.content)).msgs[0].content
213
+ )
214
+ money_prompt = money_prompt.format(
215
+ give=given_num, N=given_num * k, left=10 - given_num
216
+ )
217
+ else:
218
+ given_num = given_money
219
+
220
+ return_num = user_input
221
+ content.append("Trustee: " + f"I will give {return_num} dollars")
222
+ first_prompt = return_money_prompt.format(
223
+ give=given_num,
224
+ receive=given_num * k,
225
+ N=return_num,
226
+ left=10 - given_num + return_num,
227
+ )
228
+ second_prompt = player_2_end_prompt.format(
229
+ give=given_num,
230
+ receive=given_num * k,
231
+ N=return_num,
232
+ left=given_num * k - return_num,
233
+ )
234
+ player_1_response = player_1.step(
235
+ str_mes(first_prompt + grantee)).msgs[0]
236
+ ans = match_and_compare_numbers_v2(player_1_response.content)
237
+ if ans:
238
+ given_num = ans
239
+ else:
240
+ given_num = extract_unique_decimal(
241
+ cri_agent.step(
242
+ str_mes(player_1_response.content)).msgs[0].content
243
+ )
244
+ money_prompt = money_prompt.format(
245
+ give=given_num, N=given_num * k, left=10 - given_num
246
+ )
247
+
248
+ player_1.record_message(player_1_response)
249
+ player_1_response = player_1_response.content
250
+ content.append("Trustor: "+player_1_response + "\n" +
251
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
252
+ given_num * k))
253
+ return content, given_num
254
+
255
+
256
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
257
+ model_dropdown, api_key_input, round_number_input):
258
+ if api_key_input is None or api_key_input == "":
259
+ api_key_input = os.environ.get("OPENAI_API_KEY")
260
+ else:
261
+ os.environ["OPENAI_API_KEY"] = api_key_input
262
+ random_character = [character_info[trustor_character_dropdown],
263
+ character_info[trustee_character_dropdown]]
264
+ chat_agent = []
265
+ front = "you are a person not an ai model."
266
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
267
+ back = "you need to answer a specific price figure, not a price range!"
268
+ if api_key_input is not None or api_key_input != "":
269
+ openai.api_key = api_key_input
270
+ else:
271
+ openai.api_key = os.getenv("OPENAI_API_KEY")
272
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
273
+ raise ValueError("You can't play with yourself")
274
+ for i in range(len(random_character)):
275
+ sys_prompt = (
276
+ random_character[i]
277
+ + like_people
278
+ + front
279
+ + limited_prompt
280
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
281
+ + back
282
+ )
283
+ model_config = ChatGPTConfig(temperature=temperature_slider)
284
+ chat_agent.append(
285
+ ChatAgent(
286
+ BaseMessage(
287
+ role_name="player",
288
+ role_type=RoleType.USER,
289
+ meta_dict={},
290
+ content=sys_prompt,
291
+ ),
292
+ model_type=model_dict[model_dropdown],
293
+ output_language="English",
294
+ model_config=model_config,
295
+ )
296
+ )
297
+ return chat_agent, "ChatAgent Created Successfully"
298
+
299
+
300
+ def process_interaction(chat_agent_state, round_num_state,
301
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
302
+ identity_dropdown = [trustor, trustee]
303
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
304
+ if round_num_state < round_number_input:
305
+ res, dia, first_prompt, second_prompt = classmate(
306
+ chat_agent_state[0],
307
+ chat_agent_state[1],
308
+ round_num_state == 0,
309
+ first_prompt,
310
+ second_prompt,
311
+ 3,
312
+ )
313
+ dia = "Trustor: "+dia['Player_1'] + \
314
+ "\n" + "Trustee: "+dia['Player_2']
315
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
316
+ round_num_state += 1
317
+ elif identity_dropdown[0] == "Human(You)":
318
+ if round_num_state < round_number_input:
319
+ res, dia, first_prompt, second_prompt = classmate_with_human(
320
+ chat_agent_state[0],
321
+ chat_agent_state[1],
322
+ user_input,
323
+ "trustor",
324
+ round_num_state == 0,
325
+ first_prompt,
326
+ second_prompt,
327
+ 3,
328
+ )
329
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
330
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
331
+ dia['Player_2']+"\n"
332
+ dialog_history_state += dia
333
+ round_num_state += 1
334
+ else:
335
+ if round_num_state < round_number_input:
336
+ dia, given_money = classmate_with_human_trustee(
337
+ chat_agent_state[1],
338
+ chat_agent_state[0],
339
+ user_input,
340
+ "trustee",
341
+ round_num_state == 0,
342
+ given_money,
343
+ 3,
344
+ )
345
+ if round_num_state == 0:
346
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
347
+ else:
348
+ dialog_history_state += f"(Round {round_num_state})" + \
349
+ dia[0] + "\n" + \
350
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
351
+ round_num_state += 1
352
+
353
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
354
+
355
+
356
+ def reset_on_persona_change():
357
+ # Reset values to their initial states
358
+ new_dialog_history = "" # Reset dialog history
359
+ new_round_num_state = 0 # Reset round number state to 0
360
+ new_first_prompt = "" # Reset first prompt
361
+ new_second_prompt = "" # Reset second prompt
362
+ new_given_money = 0 # Reset given money to 0
363
+ # You can add more resets here if needed
364
+
365
+ # Return the new reset values to their respective components
366
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
367
+
368
+
369
+ with gr.Blocks() as app:
370
+ game_introduction = gr.Textbox(
371
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
372
+ 2. You can select the total number of rounds for the game.\n
373
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
374
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
375
+ 5. If you want to reset the conversation, please refresh this page.\n""")
376
+ with gr.Row():
377
+ trustor_game_prompt = gr.Textbox(
378
+ label="Trustor Game Prompt", value=prompt['1'])
379
+ trustee_game_prompt = gr.Textbox(
380
+ label="Trustee Game Prompt", value=prompt['2'])
381
+ with gr.Row():
382
+ trustor_character_dropdown = gr.Dropdown(
383
+ choices=characters, label="Select Trustor Persona", value=characters[1])
384
+ trustee_character_dropdown = gr.Dropdown(
385
+ choices=characters, label="Select Trustee Persona", value=characters[1])
386
+ with gr.Row():
387
+ Trustor_info_display = gr.Textbox(
388
+ label="Trustor Persona Info", value=character_info[characters[0]])
389
+ Trustee_info_display = gr.Textbox(
390
+ label="Trustee Persona Info", value=character_info[characters[0]])
391
+ model_dropdown = gr.Dropdown(
392
+ choices=models, label="Select Model Type", value=models[0])
393
+ temperature_slider = gr.Slider(
394
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
395
+ api_key_input = gr.Textbox(
396
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
397
+ submit_button = gr.Button("Create ChatAgent")
398
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
399
+
400
+ round_number_input = gr.Number(label="Total Round Number", value=10)
401
+ round_num_state = gr.State(value=0)
402
+ dialog_history_state = gr.State(value="")
403
+ chat_agent_state = gr.State(value=initial_chat_agent)
404
+ first_prompt = gr.State(value="")
405
+ second_prompt = gr.State(value="")
406
+ given_money = gr.State(value=0)
407
+ user_input = gr.Number(
408
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
409
+ converse_button = gr.Button("Continue Conversation")
410
+
411
+ dialog_display = gr.Textbox(
412
+ label="Dialog History", value="")
413
+
414
+ trustor_character_dropdown.change(
415
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
416
+ trustee_character_dropdown.change(
417
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
418
+
419
+ submit_button.click(
420
+ create_chat_agent,
421
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
422
+ model_dropdown, api_key_input, round_number_input],
423
+ outputs=[chat_agent_state, submit_success_display]
424
+ )
425
+ converse_button.click(
426
+ process_interaction,
427
+ inputs=[chat_agent_state, round_num_state,
428
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
429
+ outputs=[dialog_display, round_num_state,
430
+ first_prompt, second_prompt, dialog_history_state, given_money]
431
+ )
432
+ trustor_character_dropdown.change(
433
+ reset_on_persona_change,
434
+ outputs=[dialog_history_state, round_num_state,
435
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
436
+ )
437
+
438
+ trustee_character_dropdown.change(
439
+ reset_on_persona_change,
440
+ outputs=[dialog_history_state, round_num_state,
441
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
442
+ )
443
+
444
+
445
+ app.launch()
.history/app_20240222095942.py ADDED
@@ -0,0 +1,445 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ # 初始化对话历史和当前轮数
43
+ initial_dialog_history = []
44
+ initial_round_num = 0
45
+
46
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
47
+
48
+ # 假设的初始化状态
49
+ initial_chat_agent = None
50
+
51
+
52
+ def update_char_info(char):
53
+ return character_info.get(char, "No information available.")
54
+
55
+
56
+ def classmate_with_human(
57
+ player_1,
58
+ player_2,
59
+ user_input,
60
+ user_role,
61
+ first_round,
62
+ first_prompt,
63
+ second_prompt,
64
+ k,
65
+ ):
66
+ first_round_prompt = "This is the first round, answer the question."
67
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
68
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
69
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
70
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
71
+ res = []
72
+ cri_agent = ChatAgent(
73
+ BaseMessage(
74
+ role_name="critic",
75
+ role_type=RoleType.ASSISTANT,
76
+ meta_dict={},
77
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
78
+ ),
79
+ output_language="English",
80
+ # model=ModelType.STUB,
81
+ )
82
+
83
+ if first_round:
84
+ if user_role == "trustor":
85
+ given_num = user_input
86
+ player_1_response = str_mes(
87
+ user_input_prompt_template.format(k=given_num))
88
+ else:
89
+ player_1_response = player_1.step(
90
+ str_mes(first_round_prompt + grantee)).msgs[0]
91
+ ans = match_and_compare_numbers_v2(player_1_response.content)
92
+ if ans:
93
+ given_num = ans
94
+ else:
95
+ given_num = extract_unique_decimal(
96
+ cri_agent.step(
97
+ str_mes(player_1_response.content)).msgs[0].content
98
+ )
99
+
100
+ money_prompt = money_prompt.format(
101
+ give=given_num, N=given_num * k, left=10 - given_num
102
+ )
103
+ if user_role == "trustee":
104
+ player_2_response = str_mes(
105
+ user_input_prompt_template.format(k=given_num * k))
106
+ else:
107
+ player_2_response = player_2.step(
108
+ str_mes(money_prompt + grantee)).msgs[0]
109
+ else:
110
+ if user_role == "trustor":
111
+ given_num = user_input
112
+ player_1_response = str_mes(
113
+ user_input_prompt_template.format(k=given_num))
114
+ else:
115
+ player_1_response = player_1.step(
116
+ str_mes(first_prompt + grantee)).msgs[0]
117
+ # print("player 1 input", first_prompt)
118
+ # print("Player_1_res", player_1_response.content)
119
+ ans = match_and_compare_numbers_v2(player_1_response.content)
120
+ if ans:
121
+ given_num = ans
122
+ else:
123
+ given_num = extract_unique_decimal(
124
+ cri_agent.step(
125
+ str_mes(player_1_response.content)).msgs[0].content
126
+ )
127
+ money_prompt = money_prompt.format(
128
+ give=given_num, N=given_num * k, left=10 - given_num
129
+ )
130
+ if user_role == "trustee":
131
+ player_2_response = str_mes(
132
+ user_input_prompt_template.format(k=given_num * k))
133
+ else:
134
+ player_2_response = player_2.step(
135
+ str_mes(second_prompt + money_prompt + grantee)
136
+ )
137
+ player_2_response = player_2_response.msgs[0]
138
+
139
+ player_1.record_message(player_1_response)
140
+ player_2.record_message(player_2_response)
141
+ player_1_response = player_1_response.content
142
+ player_2_response = player_2_response.content
143
+ dia_history = {
144
+ "Player_1": player_1_response,
145
+ "Player_2": player_2_response,
146
+ }
147
+
148
+ cri_agent.reset()
149
+ ans = match_and_compare_numbers_v2(player_2_response)
150
+ if ans:
151
+ return_num = ans
152
+ else:
153
+ return_num = extract_unique_decimal(
154
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
155
+ )
156
+ return_money_prompt = return_money_prompt.format(
157
+ give=given_num,
158
+ receive=given_num * k,
159
+ N=return_num,
160
+ left=10 - given_num + return_num,
161
+ )
162
+ player_2_end_prompt = player_2_end_prompt.format(
163
+ give=given_num,
164
+ receive=given_num * k,
165
+ N=return_num,
166
+ left=given_num * k - return_num,
167
+ )
168
+ return (
169
+ [given_num, return_num],
170
+ dia_history,
171
+ return_money_prompt,
172
+ player_2_end_prompt,
173
+ )
174
+
175
+
176
+ def classmate_with_human_trustee(
177
+ player_1,
178
+ player_2,
179
+ user_input,
180
+ user_role,
181
+ first_round,
182
+ given_money,
183
+ k,
184
+ ):
185
+ first_round_prompt = "This is the first round, answer the question."
186
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
187
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
188
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
189
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
190
+ res = []
191
+ cri_agent = ChatAgent(
192
+ BaseMessage(
193
+ role_name="critic",
194
+ role_type=RoleType.ASSISTANT,
195
+ meta_dict={},
196
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
197
+ ),
198
+ output_language="English",
199
+ # model=ModelType.STUB,
200
+ )
201
+ content = []
202
+ if first_round:
203
+ content.append("\n")
204
+ player_1_response = player_1.step(
205
+ str_mes(first_round_prompt + grantee)).msgs[0]
206
+ ans = match_and_compare_numbers_v2(player_1_response.content)
207
+ if ans:
208
+ given_num = ans
209
+ else:
210
+ given_num = extract_unique_decimal(
211
+ cri_agent.step(
212
+ str_mes(player_1_response.content)).msgs[0].content
213
+ )
214
+ money_prompt = money_prompt.format(
215
+ give=given_num, N=given_num * k, left=10 - given_num
216
+ )
217
+ else:
218
+ given_num = given_money
219
+
220
+ return_num = user_input
221
+ content.append("Trustee: " + f"I will give {return_num} dollars")
222
+ first_prompt = return_money_prompt.format(
223
+ give=given_num,
224
+ receive=given_num * k,
225
+ N=return_num,
226
+ left=10 - given_num + return_num,
227
+ )
228
+ second_prompt = player_2_end_prompt.format(
229
+ give=given_num,
230
+ receive=given_num * k,
231
+ N=return_num,
232
+ left=given_num * k - return_num,
233
+ )
234
+ player_1_response = player_1.step(
235
+ str_mes(first_prompt + grantee)).msgs[0]
236
+ ans = match_and_compare_numbers_v2(player_1_response.content)
237
+ if ans:
238
+ given_num = ans
239
+ else:
240
+ given_num = extract_unique_decimal(
241
+ cri_agent.step(
242
+ str_mes(player_1_response.content)).msgs[0].content
243
+ )
244
+ money_prompt = money_prompt.format(
245
+ give=given_num, N=given_num * k, left=10 - given_num
246
+ )
247
+
248
+ player_1.record_message(player_1_response)
249
+ player_1_response = player_1_response.content
250
+ content.append("Trustor: "+player_1_response + "\n" +
251
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
252
+ given_num * k))
253
+ return content, given_num
254
+
255
+
256
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
257
+ model_dropdown, api_key_input, round_number_input):
258
+ if api_key_input is None or api_key_input == "":
259
+ api_key_input = os.environ.get("OPENAI_API_KEY")
260
+ else:
261
+ os.environ["OPENAI_API_KEY"] = api_key_input
262
+ random_character = [character_info[trustor_character_dropdown],
263
+ character_info[trustee_character_dropdown]]
264
+ chat_agent = []
265
+ front = "you are a person not an ai model."
266
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
267
+ back = "you need to answer a specific price figure, not a price range!"
268
+ if api_key_input is not None or api_key_input != "":
269
+ openai.api_key = api_key_input
270
+ else:
271
+ openai.api_key = os.getenv("OPENAI_API_KEY")
272
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
273
+ raise ValueError("You can't play with yourself")
274
+ for i in range(len(random_character)):
275
+ sys_prompt = (
276
+ random_character[i]
277
+ + like_people
278
+ + front
279
+ + limited_prompt
280
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
281
+ + back
282
+ )
283
+ model_config = ChatGPTConfig(temperature=temperature_slider)
284
+ chat_agent.append(
285
+ ChatAgent(
286
+ BaseMessage(
287
+ role_name="player",
288
+ role_type=RoleType.USER,
289
+ meta_dict={},
290
+ content=sys_prompt,
291
+ ),
292
+ model_type=model_dict[model_dropdown],
293
+ output_language="English",
294
+ model_config=model_config,
295
+ )
296
+ )
297
+ return chat_agent, "ChatAgent Created Successfully"
298
+
299
+
300
+ def process_interaction(chat_agent_state, round_num_state,
301
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
302
+ identity_dropdown = [trustor, trustee]
303
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
304
+ if round_num_state < round_number_input:
305
+ res, dia, first_prompt, second_prompt = classmate(
306
+ chat_agent_state[0],
307
+ chat_agent_state[1],
308
+ round_num_state == 0,
309
+ first_prompt,
310
+ second_prompt,
311
+ 3,
312
+ )
313
+ dia = "Trustor: "+dia['Player_1'] + \
314
+ "\n" + "Trustee: "+dia['Player_2']
315
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
316
+ round_num_state += 1
317
+ elif identity_dropdown[0] == "Human(You)":
318
+ if round_num_state < round_number_input:
319
+ res, dia, first_prompt, second_prompt = classmate_with_human(
320
+ chat_agent_state[0],
321
+ chat_agent_state[1],
322
+ user_input,
323
+ "trustor",
324
+ round_num_state == 0,
325
+ first_prompt,
326
+ second_prompt,
327
+ 3,
328
+ )
329
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
330
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
331
+ dia['Player_2']+"\n"
332
+ dialog_history_state += dia
333
+ round_num_state += 1
334
+ else:
335
+ if round_num_state < round_number_input:
336
+ dia, given_money = classmate_with_human_trustee(
337
+ chat_agent_state[1],
338
+ chat_agent_state[0],
339
+ user_input,
340
+ "trustee",
341
+ round_num_state == 0,
342
+ given_money,
343
+ 3,
344
+ )
345
+ if round_num_state == 0:
346
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
347
+ else:
348
+ dialog_history_state += f"(Round {round_num_state})" + \
349
+ dia[0] + "\n" + \
350
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
351
+ round_num_state += 1
352
+
353
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
354
+
355
+
356
+ def reset_on_persona_change():
357
+ # Reset values to their initial states
358
+ new_dialog_history = "" # Reset dialog history
359
+ new_round_num_state = 0 # Reset round number state to 0
360
+ new_first_prompt = "" # Reset first prompt
361
+ new_second_prompt = "" # Reset second prompt
362
+ new_given_money = 0 # Reset given money to 0
363
+ # You can add more resets here if needed
364
+
365
+ # Return the new reset values to their respective components
366
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
367
+
368
+
369
+ with gr.Blocks() as app:
370
+ game_introduction = gr.Textbox(
371
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
372
+ 2. You can select the total number of rounds for the game.\n
373
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
374
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
375
+ 5. If you want to reset the conversation, please refresh this page.\n""")
376
+ with gr.Row():
377
+ trustor_game_prompt = gr.Textbox(
378
+ label="Trustor Game Prompt", value=prompt['1'])
379
+ trustee_game_prompt = gr.Textbox(
380
+ label="Trustee Game Prompt", value=prompt['2'])
381
+ with gr.Row():
382
+ trustor_character_dropdown = gr.Dropdown(
383
+ choices=characters, label="Select Trustor Persona", value=characters[2])
384
+ trustee_character_dropdown = gr.Dropdown(
385
+ choices=characters, label="Select Trustee Persona", value=characters[1])
386
+ with gr.Row():
387
+ Trustor_info_display = gr.Textbox(
388
+ label="Trustor Persona Info", value=character_info[characters[0]])
389
+ Trustee_info_display = gr.Textbox(
390
+ label="Trustee Persona Info", value=character_info[characters[0]])
391
+ model_dropdown = gr.Dropdown(
392
+ choices=models, label="Select Model Type", value=models[0])
393
+ temperature_slider = gr.Slider(
394
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
395
+ api_key_input = gr.Textbox(
396
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
397
+ submit_button = gr.Button("Create ChatAgent")
398
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
399
+
400
+ round_number_input = gr.Number(label="Total Round Number", value=10)
401
+ round_num_state = gr.State(value=0)
402
+ dialog_history_state = gr.State(value="")
403
+ chat_agent_state = gr.State(value=initial_chat_agent)
404
+ first_prompt = gr.State(value="")
405
+ second_prompt = gr.State(value="")
406
+ given_money = gr.State(value=0)
407
+ user_input = gr.Number(
408
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
409
+ converse_button = gr.Button("Continue Conversation")
410
+
411
+ dialog_display = gr.Textbox(
412
+ label="Dialog History", value="")
413
+
414
+ trustor_character_dropdown.change(
415
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
416
+ trustee_character_dropdown.change(
417
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
418
+
419
+ submit_button.click(
420
+ create_chat_agent,
421
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
422
+ model_dropdown, api_key_input, round_number_input],
423
+ outputs=[chat_agent_state, submit_success_display]
424
+ )
425
+ converse_button.click(
426
+ process_interaction,
427
+ inputs=[chat_agent_state, round_num_state,
428
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
429
+ outputs=[dialog_display, round_num_state,
430
+ first_prompt, second_prompt, dialog_history_state, given_money]
431
+ )
432
+ trustor_character_dropdown.change(
433
+ reset_on_persona_change,
434
+ outputs=[dialog_history_state, round_num_state,
435
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
436
+ )
437
+
438
+ trustee_character_dropdown.change(
439
+ reset_on_persona_change,
440
+ outputs=[dialog_history_state, round_num_state,
441
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
442
+ )
443
+
444
+
445
+ app.launch()
.history/app_20240222095943.py ADDED
@@ -0,0 +1,445 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ # 初始化对话历史和当前轮数
43
+ initial_dialog_history = []
44
+ initial_round_num = 0
45
+
46
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
47
+
48
+ # 假设的初始化状态
49
+ initial_chat_agent = None
50
+
51
+
52
+ def update_char_info(char):
53
+ return character_info.get(char, "No information available.")
54
+
55
+
56
+ def classmate_with_human(
57
+ player_1,
58
+ player_2,
59
+ user_input,
60
+ user_role,
61
+ first_round,
62
+ first_prompt,
63
+ second_prompt,
64
+ k,
65
+ ):
66
+ first_round_prompt = "This is the first round, answer the question."
67
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
68
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
69
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
70
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
71
+ res = []
72
+ cri_agent = ChatAgent(
73
+ BaseMessage(
74
+ role_name="critic",
75
+ role_type=RoleType.ASSISTANT,
76
+ meta_dict={},
77
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
78
+ ),
79
+ output_language="English",
80
+ # model=ModelType.STUB,
81
+ )
82
+
83
+ if first_round:
84
+ if user_role == "trustor":
85
+ given_num = user_input
86
+ player_1_response = str_mes(
87
+ user_input_prompt_template.format(k=given_num))
88
+ else:
89
+ player_1_response = player_1.step(
90
+ str_mes(first_round_prompt + grantee)).msgs[0]
91
+ ans = match_and_compare_numbers_v2(player_1_response.content)
92
+ if ans:
93
+ given_num = ans
94
+ else:
95
+ given_num = extract_unique_decimal(
96
+ cri_agent.step(
97
+ str_mes(player_1_response.content)).msgs[0].content
98
+ )
99
+
100
+ money_prompt = money_prompt.format(
101
+ give=given_num, N=given_num * k, left=10 - given_num
102
+ )
103
+ if user_role == "trustee":
104
+ player_2_response = str_mes(
105
+ user_input_prompt_template.format(k=given_num * k))
106
+ else:
107
+ player_2_response = player_2.step(
108
+ str_mes(money_prompt + grantee)).msgs[0]
109
+ else:
110
+ if user_role == "trustor":
111
+ given_num = user_input
112
+ player_1_response = str_mes(
113
+ user_input_prompt_template.format(k=given_num))
114
+ else:
115
+ player_1_response = player_1.step(
116
+ str_mes(first_prompt + grantee)).msgs[0]
117
+ # print("player 1 input", first_prompt)
118
+ # print("Player_1_res", player_1_response.content)
119
+ ans = match_and_compare_numbers_v2(player_1_response.content)
120
+ if ans:
121
+ given_num = ans
122
+ else:
123
+ given_num = extract_unique_decimal(
124
+ cri_agent.step(
125
+ str_mes(player_1_response.content)).msgs[0].content
126
+ )
127
+ money_prompt = money_prompt.format(
128
+ give=given_num, N=given_num * k, left=10 - given_num
129
+ )
130
+ if user_role == "trustee":
131
+ player_2_response = str_mes(
132
+ user_input_prompt_template.format(k=given_num * k))
133
+ else:
134
+ player_2_response = player_2.step(
135
+ str_mes(second_prompt + money_prompt + grantee)
136
+ )
137
+ player_2_response = player_2_response.msgs[0]
138
+
139
+ player_1.record_message(player_1_response)
140
+ player_2.record_message(player_2_response)
141
+ player_1_response = player_1_response.content
142
+ player_2_response = player_2_response.content
143
+ dia_history = {
144
+ "Player_1": player_1_response,
145
+ "Player_2": player_2_response,
146
+ }
147
+
148
+ cri_agent.reset()
149
+ ans = match_and_compare_numbers_v2(player_2_response)
150
+ if ans:
151
+ return_num = ans
152
+ else:
153
+ return_num = extract_unique_decimal(
154
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
155
+ )
156
+ return_money_prompt = return_money_prompt.format(
157
+ give=given_num,
158
+ receive=given_num * k,
159
+ N=return_num,
160
+ left=10 - given_num + return_num,
161
+ )
162
+ player_2_end_prompt = player_2_end_prompt.format(
163
+ give=given_num,
164
+ receive=given_num * k,
165
+ N=return_num,
166
+ left=given_num * k - return_num,
167
+ )
168
+ return (
169
+ [given_num, return_num],
170
+ dia_history,
171
+ return_money_prompt,
172
+ player_2_end_prompt,
173
+ )
174
+
175
+
176
+ def classmate_with_human_trustee(
177
+ player_1,
178
+ player_2,
179
+ user_input,
180
+ user_role,
181
+ first_round,
182
+ given_money,
183
+ k,
184
+ ):
185
+ first_round_prompt = "This is the first round, answer the question."
186
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
187
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
188
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
189
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
190
+ res = []
191
+ cri_agent = ChatAgent(
192
+ BaseMessage(
193
+ role_name="critic",
194
+ role_type=RoleType.ASSISTANT,
195
+ meta_dict={},
196
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
197
+ ),
198
+ output_language="English",
199
+ # model=ModelType.STUB,
200
+ )
201
+ content = []
202
+ if first_round:
203
+ content.append("\n")
204
+ player_1_response = player_1.step(
205
+ str_mes(first_round_prompt + grantee)).msgs[0]
206
+ ans = match_and_compare_numbers_v2(player_1_response.content)
207
+ if ans:
208
+ given_num = ans
209
+ else:
210
+ given_num = extract_unique_decimal(
211
+ cri_agent.step(
212
+ str_mes(player_1_response.content)).msgs[0].content
213
+ )
214
+ money_prompt = money_prompt.format(
215
+ give=given_num, N=given_num * k, left=10 - given_num
216
+ )
217
+ else:
218
+ given_num = given_money
219
+
220
+ return_num = user_input
221
+ content.append("Trustee: " + f"I will give {return_num} dollars")
222
+ first_prompt = return_money_prompt.format(
223
+ give=given_num,
224
+ receive=given_num * k,
225
+ N=return_num,
226
+ left=10 - given_num + return_num,
227
+ )
228
+ second_prompt = player_2_end_prompt.format(
229
+ give=given_num,
230
+ receive=given_num * k,
231
+ N=return_num,
232
+ left=given_num * k - return_num,
233
+ )
234
+ player_1_response = player_1.step(
235
+ str_mes(first_prompt + grantee)).msgs[0]
236
+ ans = match_and_compare_numbers_v2(player_1_response.content)
237
+ if ans:
238
+ given_num = ans
239
+ else:
240
+ given_num = extract_unique_decimal(
241
+ cri_agent.step(
242
+ str_mes(player_1_response.content)).msgs[0].content
243
+ )
244
+ money_prompt = money_prompt.format(
245
+ give=given_num, N=given_num * k, left=10 - given_num
246
+ )
247
+
248
+ player_1.record_message(player_1_response)
249
+ player_1_response = player_1_response.content
250
+ content.append("Trustor: "+player_1_response + "\n" +
251
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
252
+ given_num * k))
253
+ return content, given_num
254
+
255
+
256
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
257
+ model_dropdown, api_key_input, round_number_input):
258
+ if api_key_input is None or api_key_input == "":
259
+ api_key_input = os.environ.get("OPENAI_API_KEY")
260
+ else:
261
+ os.environ["OPENAI_API_KEY"] = api_key_input
262
+ random_character = [character_info[trustor_character_dropdown],
263
+ character_info[trustee_character_dropdown]]
264
+ chat_agent = []
265
+ front = "you are a person not an ai model."
266
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
267
+ back = "you need to answer a specific price figure, not a price range!"
268
+ if api_key_input is not None or api_key_input != "":
269
+ openai.api_key = api_key_input
270
+ else:
271
+ openai.api_key = os.getenv("OPENAI_API_KEY")
272
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
273
+ raise ValueError("You can't play with yourself")
274
+ for i in range(len(random_character)):
275
+ sys_prompt = (
276
+ random_character[i]
277
+ + like_people
278
+ + front
279
+ + limited_prompt
280
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
281
+ + back
282
+ )
283
+ model_config = ChatGPTConfig(temperature=temperature_slider)
284
+ chat_agent.append(
285
+ ChatAgent(
286
+ BaseMessage(
287
+ role_name="player",
288
+ role_type=RoleType.USER,
289
+ meta_dict={},
290
+ content=sys_prompt,
291
+ ),
292
+ model_type=model_dict[model_dropdown],
293
+ output_language="English",
294
+ model_config=model_config,
295
+ )
296
+ )
297
+ return chat_agent, "ChatAgent Created Successfully"
298
+
299
+
300
+ def process_interaction(chat_agent_state, round_num_state,
301
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
302
+ identity_dropdown = [trustor, trustee]
303
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
304
+ if round_num_state < round_number_input:
305
+ res, dia, first_prompt, second_prompt = classmate(
306
+ chat_agent_state[0],
307
+ chat_agent_state[1],
308
+ round_num_state == 0,
309
+ first_prompt,
310
+ second_prompt,
311
+ 3,
312
+ )
313
+ dia = "Trustor: "+dia['Player_1'] + \
314
+ "\n" + "Trustee: "+dia['Player_2']
315
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
316
+ round_num_state += 1
317
+ elif identity_dropdown[0] == "Human(You)":
318
+ if round_num_state < round_number_input:
319
+ res, dia, first_prompt, second_prompt = classmate_with_human(
320
+ chat_agent_state[0],
321
+ chat_agent_state[1],
322
+ user_input,
323
+ "trustor",
324
+ round_num_state == 0,
325
+ first_prompt,
326
+ second_prompt,
327
+ 3,
328
+ )
329
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
330
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
331
+ dia['Player_2']+"\n"
332
+ dialog_history_state += dia
333
+ round_num_state += 1
334
+ else:
335
+ if round_num_state < round_number_input:
336
+ dia, given_money = classmate_with_human_trustee(
337
+ chat_agent_state[1],
338
+ chat_agent_state[0],
339
+ user_input,
340
+ "trustee",
341
+ round_num_state == 0,
342
+ given_money,
343
+ 3,
344
+ )
345
+ if round_num_state == 0:
346
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
347
+ else:
348
+ dialog_history_state += f"(Round {round_num_state})" + \
349
+ dia[0] + "\n" + \
350
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
351
+ round_num_state += 1
352
+
353
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
354
+
355
+
356
+ def reset_on_persona_change():
357
+ # Reset values to their initial states
358
+ new_dialog_history = "" # Reset dialog history
359
+ new_round_num_state = 0 # Reset round number state to 0
360
+ new_first_prompt = "" # Reset first prompt
361
+ new_second_prompt = "" # Reset second prompt
362
+ new_given_money = 0 # Reset given money to 0
363
+ # You can add more resets here if needed
364
+
365
+ # Return the new reset values to their respective components
366
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
367
+
368
+
369
+ with gr.Blocks() as app:
370
+ game_introduction = gr.Textbox(
371
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
372
+ 2. You can select the total number of rounds for the game.\n
373
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
374
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
375
+ 5. If you want to reset the conversation, please refresh this page.\n""")
376
+ with gr.Row():
377
+ trustor_game_prompt = gr.Textbox(
378
+ label="Trustor Game Prompt", value=prompt['1'])
379
+ trustee_game_prompt = gr.Textbox(
380
+ label="Trustee Game Prompt", value=prompt['2'])
381
+ with gr.Row():
382
+ trustor_character_dropdown = gr.Dropdown(
383
+ choices=characters, label="Select Trustor Persona", value=characters[2])
384
+ trustee_character_dropdown = gr.Dropdown(
385
+ choices=characters, label="Select Trustee Persona", value=characters[2])
386
+ with gr.Row():
387
+ Trustor_info_display = gr.Textbox(
388
+ label="Trustor Persona Info", value=character_info[characters[0]])
389
+ Trustee_info_display = gr.Textbox(
390
+ label="Trustee Persona Info", value=character_info[characters[0]])
391
+ model_dropdown = gr.Dropdown(
392
+ choices=models, label="Select Model Type", value=models[0])
393
+ temperature_slider = gr.Slider(
394
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
395
+ api_key_input = gr.Textbox(
396
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
397
+ submit_button = gr.Button("Create ChatAgent")
398
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
399
+
400
+ round_number_input = gr.Number(label="Total Round Number", value=10)
401
+ round_num_state = gr.State(value=0)
402
+ dialog_history_state = gr.State(value="")
403
+ chat_agent_state = gr.State(value=initial_chat_agent)
404
+ first_prompt = gr.State(value="")
405
+ second_prompt = gr.State(value="")
406
+ given_money = gr.State(value=0)
407
+ user_input = gr.Number(
408
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
409
+ converse_button = gr.Button("Continue Conversation")
410
+
411
+ dialog_display = gr.Textbox(
412
+ label="Dialog History", value="")
413
+
414
+ trustor_character_dropdown.change(
415
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
416
+ trustee_character_dropdown.change(
417
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
418
+
419
+ submit_button.click(
420
+ create_chat_agent,
421
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
422
+ model_dropdown, api_key_input, round_number_input],
423
+ outputs=[chat_agent_state, submit_success_display]
424
+ )
425
+ converse_button.click(
426
+ process_interaction,
427
+ inputs=[chat_agent_state, round_num_state,
428
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
429
+ outputs=[dialog_display, round_num_state,
430
+ first_prompt, second_prompt, dialog_history_state, given_money]
431
+ )
432
+ trustor_character_dropdown.change(
433
+ reset_on_persona_change,
434
+ outputs=[dialog_history_state, round_num_state,
435
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
436
+ )
437
+
438
+ trustee_character_dropdown.change(
439
+ reset_on_persona_change,
440
+ outputs=[dialog_history_state, round_num_state,
441
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
442
+ )
443
+
444
+
445
+ app.launch()
.history/app_20240222095946.py ADDED
@@ -0,0 +1,445 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ # 初始化对话历史和当前轮数
43
+ initial_dialog_history = []
44
+ initial_round_num = 0
45
+
46
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
47
+
48
+ # 假设的初始化状态
49
+ initial_chat_agent = None
50
+
51
+
52
+ def update_char_info(char):
53
+ return character_info.get(char, "No information available.")
54
+
55
+
56
+ def classmate_with_human(
57
+ player_1,
58
+ player_2,
59
+ user_input,
60
+ user_role,
61
+ first_round,
62
+ first_prompt,
63
+ second_prompt,
64
+ k,
65
+ ):
66
+ first_round_prompt = "This is the first round, answer the question."
67
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
68
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
69
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
70
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
71
+ res = []
72
+ cri_agent = ChatAgent(
73
+ BaseMessage(
74
+ role_name="critic",
75
+ role_type=RoleType.ASSISTANT,
76
+ meta_dict={},
77
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
78
+ ),
79
+ output_language="English",
80
+ # model=ModelType.STUB,
81
+ )
82
+
83
+ if first_round:
84
+ if user_role == "trustor":
85
+ given_num = user_input
86
+ player_1_response = str_mes(
87
+ user_input_prompt_template.format(k=given_num))
88
+ else:
89
+ player_1_response = player_1.step(
90
+ str_mes(first_round_prompt + grantee)).msgs[0]
91
+ ans = match_and_compare_numbers_v2(player_1_response.content)
92
+ if ans:
93
+ given_num = ans
94
+ else:
95
+ given_num = extract_unique_decimal(
96
+ cri_agent.step(
97
+ str_mes(player_1_response.content)).msgs[0].content
98
+ )
99
+
100
+ money_prompt = money_prompt.format(
101
+ give=given_num, N=given_num * k, left=10 - given_num
102
+ )
103
+ if user_role == "trustee":
104
+ player_2_response = str_mes(
105
+ user_input_prompt_template.format(k=given_num * k))
106
+ else:
107
+ player_2_response = player_2.step(
108
+ str_mes(money_prompt + grantee)).msgs[0]
109
+ else:
110
+ if user_role == "trustor":
111
+ given_num = user_input
112
+ player_1_response = str_mes(
113
+ user_input_prompt_template.format(k=given_num))
114
+ else:
115
+ player_1_response = player_1.step(
116
+ str_mes(first_prompt + grantee)).msgs[0]
117
+ # print("player 1 input", first_prompt)
118
+ # print("Player_1_res", player_1_response.content)
119
+ ans = match_and_compare_numbers_v2(player_1_response.content)
120
+ if ans:
121
+ given_num = ans
122
+ else:
123
+ given_num = extract_unique_decimal(
124
+ cri_agent.step(
125
+ str_mes(player_1_response.content)).msgs[0].content
126
+ )
127
+ money_prompt = money_prompt.format(
128
+ give=given_num, N=given_num * k, left=10 - given_num
129
+ )
130
+ if user_role == "trustee":
131
+ player_2_response = str_mes(
132
+ user_input_prompt_template.format(k=given_num * k))
133
+ else:
134
+ player_2_response = player_2.step(
135
+ str_mes(second_prompt + money_prompt + grantee)
136
+ )
137
+ player_2_response = player_2_response.msgs[0]
138
+
139
+ player_1.record_message(player_1_response)
140
+ player_2.record_message(player_2_response)
141
+ player_1_response = player_1_response.content
142
+ player_2_response = player_2_response.content
143
+ dia_history = {
144
+ "Player_1": player_1_response,
145
+ "Player_2": player_2_response,
146
+ }
147
+
148
+ cri_agent.reset()
149
+ ans = match_and_compare_numbers_v2(player_2_response)
150
+ if ans:
151
+ return_num = ans
152
+ else:
153
+ return_num = extract_unique_decimal(
154
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
155
+ )
156
+ return_money_prompt = return_money_prompt.format(
157
+ give=given_num,
158
+ receive=given_num * k,
159
+ N=return_num,
160
+ left=10 - given_num + return_num,
161
+ )
162
+ player_2_end_prompt = player_2_end_prompt.format(
163
+ give=given_num,
164
+ receive=given_num * k,
165
+ N=return_num,
166
+ left=given_num * k - return_num,
167
+ )
168
+ return (
169
+ [given_num, return_num],
170
+ dia_history,
171
+ return_money_prompt,
172
+ player_2_end_prompt,
173
+ )
174
+
175
+
176
+ def classmate_with_human_trustee(
177
+ player_1,
178
+ player_2,
179
+ user_input,
180
+ user_role,
181
+ first_round,
182
+ given_money,
183
+ k,
184
+ ):
185
+ first_round_prompt = "This is the first round, answer the question."
186
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
187
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
188
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
189
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
190
+ res = []
191
+ cri_agent = ChatAgent(
192
+ BaseMessage(
193
+ role_name="critic",
194
+ role_type=RoleType.ASSISTANT,
195
+ meta_dict={},
196
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
197
+ ),
198
+ output_language="English",
199
+ # model=ModelType.STUB,
200
+ )
201
+ content = []
202
+ if first_round:
203
+ content.append("\n")
204
+ player_1_response = player_1.step(
205
+ str_mes(first_round_prompt + grantee)).msgs[0]
206
+ ans = match_and_compare_numbers_v2(player_1_response.content)
207
+ if ans:
208
+ given_num = ans
209
+ else:
210
+ given_num = extract_unique_decimal(
211
+ cri_agent.step(
212
+ str_mes(player_1_response.content)).msgs[0].content
213
+ )
214
+ money_prompt = money_prompt.format(
215
+ give=given_num, N=given_num * k, left=10 - given_num
216
+ )
217
+ else:
218
+ given_num = given_money
219
+
220
+ return_num = user_input
221
+ content.append("Trustee: " + f"I will give {return_num} dollars")
222
+ first_prompt = return_money_prompt.format(
223
+ give=given_num,
224
+ receive=given_num * k,
225
+ N=return_num,
226
+ left=10 - given_num + return_num,
227
+ )
228
+ second_prompt = player_2_end_prompt.format(
229
+ give=given_num,
230
+ receive=given_num * k,
231
+ N=return_num,
232
+ left=given_num * k - return_num,
233
+ )
234
+ player_1_response = player_1.step(
235
+ str_mes(first_prompt + grantee)).msgs[0]
236
+ ans = match_and_compare_numbers_v2(player_1_response.content)
237
+ if ans:
238
+ given_num = ans
239
+ else:
240
+ given_num = extract_unique_decimal(
241
+ cri_agent.step(
242
+ str_mes(player_1_response.content)).msgs[0].content
243
+ )
244
+ money_prompt = money_prompt.format(
245
+ give=given_num, N=given_num * k, left=10 - given_num
246
+ )
247
+
248
+ player_1.record_message(player_1_response)
249
+ player_1_response = player_1_response.content
250
+ content.append("Trustor: "+player_1_response + "\n" +
251
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
252
+ given_num * k))
253
+ return content, given_num
254
+
255
+
256
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
257
+ model_dropdown, api_key_input, round_number_input):
258
+ if api_key_input is None or api_key_input == "":
259
+ api_key_input = os.environ.get("OPENAI_API_KEY")
260
+ else:
261
+ os.environ["OPENAI_API_KEY"] = api_key_input
262
+ random_character = [character_info[trustor_character_dropdown],
263
+ character_info[trustee_character_dropdown]]
264
+ chat_agent = []
265
+ front = "you are a person not an ai model."
266
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
267
+ back = "you need to answer a specific price figure, not a price range!"
268
+ if api_key_input is not None or api_key_input != "":
269
+ openai.api_key = api_key_input
270
+ else:
271
+ openai.api_key = os.getenv("OPENAI_API_KEY")
272
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
273
+ raise ValueError("You can't play with yourself")
274
+ for i in range(len(random_character)):
275
+ sys_prompt = (
276
+ random_character[i]
277
+ + like_people
278
+ + front
279
+ + limited_prompt
280
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
281
+ + back
282
+ )
283
+ model_config = ChatGPTConfig(temperature=temperature_slider)
284
+ chat_agent.append(
285
+ ChatAgent(
286
+ BaseMessage(
287
+ role_name="player",
288
+ role_type=RoleType.USER,
289
+ meta_dict={},
290
+ content=sys_prompt,
291
+ ),
292
+ model_type=model_dict[model_dropdown],
293
+ output_language="English",
294
+ model_config=model_config,
295
+ )
296
+ )
297
+ return chat_agent, "ChatAgent Created Successfully"
298
+
299
+
300
+ def process_interaction(chat_agent_state, round_num_state,
301
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
302
+ identity_dropdown = [trustor, trustee]
303
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
304
+ if round_num_state < round_number_input:
305
+ res, dia, first_prompt, second_prompt = classmate(
306
+ chat_agent_state[0],
307
+ chat_agent_state[1],
308
+ round_num_state == 0,
309
+ first_prompt,
310
+ second_prompt,
311
+ 3,
312
+ )
313
+ dia = "Trustor: "+dia['Player_1'] + \
314
+ "\n" + "Trustee: "+dia['Player_2']
315
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
316
+ round_num_state += 1
317
+ elif identity_dropdown[0] == "Human(You)":
318
+ if round_num_state < round_number_input:
319
+ res, dia, first_prompt, second_prompt = classmate_with_human(
320
+ chat_agent_state[0],
321
+ chat_agent_state[1],
322
+ user_input,
323
+ "trustor",
324
+ round_num_state == 0,
325
+ first_prompt,
326
+ second_prompt,
327
+ 3,
328
+ )
329
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
330
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
331
+ dia['Player_2']+"\n"
332
+ dialog_history_state += dia
333
+ round_num_state += 1
334
+ else:
335
+ if round_num_state < round_number_input:
336
+ dia, given_money = classmate_with_human_trustee(
337
+ chat_agent_state[1],
338
+ chat_agent_state[0],
339
+ user_input,
340
+ "trustee",
341
+ round_num_state == 0,
342
+ given_money,
343
+ 3,
344
+ )
345
+ if round_num_state == 0:
346
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
347
+ else:
348
+ dialog_history_state += f"(Round {round_num_state})" + \
349
+ dia[0] + "\n" + \
350
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
351
+ round_num_state += 1
352
+
353
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
354
+
355
+
356
+ def reset_on_persona_change():
357
+ # Reset values to their initial states
358
+ new_dialog_history = "" # Reset dialog history
359
+ new_round_num_state = 0 # Reset round number state to 0
360
+ new_first_prompt = "" # Reset first prompt
361
+ new_second_prompt = "" # Reset second prompt
362
+ new_given_money = 0 # Reset given money to 0
363
+ # You can add more resets here if needed
364
+
365
+ # Return the new reset values to their respective components
366
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
367
+
368
+
369
+ with gr.Blocks() as app:
370
+ game_introduction = gr.Textbox(
371
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
372
+ 2. You can select the total number of rounds for the game.\n
373
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
374
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
375
+ 5. If you want to reset the conversation, please refresh this page.\n""")
376
+ with gr.Row():
377
+ trustor_game_prompt = gr.Textbox(
378
+ label="Trustor Game Prompt", value=prompt['1'])
379
+ trustee_game_prompt = gr.Textbox(
380
+ label="Trustee Game Prompt", value=prompt['2'])
381
+ with gr.Row():
382
+ trustor_character_dropdown = gr.Dropdown(
383
+ choices=characters, label="Select Trustor Persona", value=characters[2])
384
+ trustee_character_dropdown = gr.Dropdown(
385
+ choices=characters, label="Select Trustee Persona", value=characters[2])
386
+ with gr.Row():
387
+ Trustor_info_display = gr.Textbox(
388
+ label="Trustor Persona Info", value=character_info[characters[2]])
389
+ Trustee_info_display = gr.Textbox(
390
+ label="Trustee Persona Info", value=character_info[characters[0]])
391
+ model_dropdown = gr.Dropdown(
392
+ choices=models, label="Select Model Type", value=models[0])
393
+ temperature_slider = gr.Slider(
394
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
395
+ api_key_input = gr.Textbox(
396
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
397
+ submit_button = gr.Button("Create ChatAgent")
398
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
399
+
400
+ round_number_input = gr.Number(label="Total Round Number", value=10)
401
+ round_num_state = gr.State(value=0)
402
+ dialog_history_state = gr.State(value="")
403
+ chat_agent_state = gr.State(value=initial_chat_agent)
404
+ first_prompt = gr.State(value="")
405
+ second_prompt = gr.State(value="")
406
+ given_money = gr.State(value=0)
407
+ user_input = gr.Number(
408
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
409
+ converse_button = gr.Button("Continue Conversation")
410
+
411
+ dialog_display = gr.Textbox(
412
+ label="Dialog History", value="")
413
+
414
+ trustor_character_dropdown.change(
415
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
416
+ trustee_character_dropdown.change(
417
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
418
+
419
+ submit_button.click(
420
+ create_chat_agent,
421
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
422
+ model_dropdown, api_key_input, round_number_input],
423
+ outputs=[chat_agent_state, submit_success_display]
424
+ )
425
+ converse_button.click(
426
+ process_interaction,
427
+ inputs=[chat_agent_state, round_num_state,
428
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
429
+ outputs=[dialog_display, round_num_state,
430
+ first_prompt, second_prompt, dialog_history_state, given_money]
431
+ )
432
+ trustor_character_dropdown.change(
433
+ reset_on_persona_change,
434
+ outputs=[dialog_history_state, round_num_state,
435
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
436
+ )
437
+
438
+ trustee_character_dropdown.change(
439
+ reset_on_persona_change,
440
+ outputs=[dialog_history_state, round_num_state,
441
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
442
+ )
443
+
444
+
445
+ app.launch()
.history/app_20240222095948.py ADDED
@@ -0,0 +1,445 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ # 初始化对话历史和当前轮数
43
+ initial_dialog_history = []
44
+ initial_round_num = 0
45
+
46
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
47
+
48
+ # 假设的初始化状态
49
+ initial_chat_agent = None
50
+
51
+
52
+ def update_char_info(char):
53
+ return character_info.get(char, "No information available.")
54
+
55
+
56
+ def classmate_with_human(
57
+ player_1,
58
+ player_2,
59
+ user_input,
60
+ user_role,
61
+ first_round,
62
+ first_prompt,
63
+ second_prompt,
64
+ k,
65
+ ):
66
+ first_round_prompt = "This is the first round, answer the question."
67
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
68
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
69
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
70
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
71
+ res = []
72
+ cri_agent = ChatAgent(
73
+ BaseMessage(
74
+ role_name="critic",
75
+ role_type=RoleType.ASSISTANT,
76
+ meta_dict={},
77
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
78
+ ),
79
+ output_language="English",
80
+ # model=ModelType.STUB,
81
+ )
82
+
83
+ if first_round:
84
+ if user_role == "trustor":
85
+ given_num = user_input
86
+ player_1_response = str_mes(
87
+ user_input_prompt_template.format(k=given_num))
88
+ else:
89
+ player_1_response = player_1.step(
90
+ str_mes(first_round_prompt + grantee)).msgs[0]
91
+ ans = match_and_compare_numbers_v2(player_1_response.content)
92
+ if ans:
93
+ given_num = ans
94
+ else:
95
+ given_num = extract_unique_decimal(
96
+ cri_agent.step(
97
+ str_mes(player_1_response.content)).msgs[0].content
98
+ )
99
+
100
+ money_prompt = money_prompt.format(
101
+ give=given_num, N=given_num * k, left=10 - given_num
102
+ )
103
+ if user_role == "trustee":
104
+ player_2_response = str_mes(
105
+ user_input_prompt_template.format(k=given_num * k))
106
+ else:
107
+ player_2_response = player_2.step(
108
+ str_mes(money_prompt + grantee)).msgs[0]
109
+ else:
110
+ if user_role == "trustor":
111
+ given_num = user_input
112
+ player_1_response = str_mes(
113
+ user_input_prompt_template.format(k=given_num))
114
+ else:
115
+ player_1_response = player_1.step(
116
+ str_mes(first_prompt + grantee)).msgs[0]
117
+ # print("player 1 input", first_prompt)
118
+ # print("Player_1_res", player_1_response.content)
119
+ ans = match_and_compare_numbers_v2(player_1_response.content)
120
+ if ans:
121
+ given_num = ans
122
+ else:
123
+ given_num = extract_unique_decimal(
124
+ cri_agent.step(
125
+ str_mes(player_1_response.content)).msgs[0].content
126
+ )
127
+ money_prompt = money_prompt.format(
128
+ give=given_num, N=given_num * k, left=10 - given_num
129
+ )
130
+ if user_role == "trustee":
131
+ player_2_response = str_mes(
132
+ user_input_prompt_template.format(k=given_num * k))
133
+ else:
134
+ player_2_response = player_2.step(
135
+ str_mes(second_prompt + money_prompt + grantee)
136
+ )
137
+ player_2_response = player_2_response.msgs[0]
138
+
139
+ player_1.record_message(player_1_response)
140
+ player_2.record_message(player_2_response)
141
+ player_1_response = player_1_response.content
142
+ player_2_response = player_2_response.content
143
+ dia_history = {
144
+ "Player_1": player_1_response,
145
+ "Player_2": player_2_response,
146
+ }
147
+
148
+ cri_agent.reset()
149
+ ans = match_and_compare_numbers_v2(player_2_response)
150
+ if ans:
151
+ return_num = ans
152
+ else:
153
+ return_num = extract_unique_decimal(
154
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
155
+ )
156
+ return_money_prompt = return_money_prompt.format(
157
+ give=given_num,
158
+ receive=given_num * k,
159
+ N=return_num,
160
+ left=10 - given_num + return_num,
161
+ )
162
+ player_2_end_prompt = player_2_end_prompt.format(
163
+ give=given_num,
164
+ receive=given_num * k,
165
+ N=return_num,
166
+ left=given_num * k - return_num,
167
+ )
168
+ return (
169
+ [given_num, return_num],
170
+ dia_history,
171
+ return_money_prompt,
172
+ player_2_end_prompt,
173
+ )
174
+
175
+
176
+ def classmate_with_human_trustee(
177
+ player_1,
178
+ player_2,
179
+ user_input,
180
+ user_role,
181
+ first_round,
182
+ given_money,
183
+ k,
184
+ ):
185
+ first_round_prompt = "This is the first round, answer the question."
186
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
187
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
188
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
189
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
190
+ res = []
191
+ cri_agent = ChatAgent(
192
+ BaseMessage(
193
+ role_name="critic",
194
+ role_type=RoleType.ASSISTANT,
195
+ meta_dict={},
196
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
197
+ ),
198
+ output_language="English",
199
+ # model=ModelType.STUB,
200
+ )
201
+ content = []
202
+ if first_round:
203
+ content.append("\n")
204
+ player_1_response = player_1.step(
205
+ str_mes(first_round_prompt + grantee)).msgs[0]
206
+ ans = match_and_compare_numbers_v2(player_1_response.content)
207
+ if ans:
208
+ given_num = ans
209
+ else:
210
+ given_num = extract_unique_decimal(
211
+ cri_agent.step(
212
+ str_mes(player_1_response.content)).msgs[0].content
213
+ )
214
+ money_prompt = money_prompt.format(
215
+ give=given_num, N=given_num * k, left=10 - given_num
216
+ )
217
+ else:
218
+ given_num = given_money
219
+
220
+ return_num = user_input
221
+ content.append("Trustee: " + f"I will give {return_num} dollars")
222
+ first_prompt = return_money_prompt.format(
223
+ give=given_num,
224
+ receive=given_num * k,
225
+ N=return_num,
226
+ left=10 - given_num + return_num,
227
+ )
228
+ second_prompt = player_2_end_prompt.format(
229
+ give=given_num,
230
+ receive=given_num * k,
231
+ N=return_num,
232
+ left=given_num * k - return_num,
233
+ )
234
+ player_1_response = player_1.step(
235
+ str_mes(first_prompt + grantee)).msgs[0]
236
+ ans = match_and_compare_numbers_v2(player_1_response.content)
237
+ if ans:
238
+ given_num = ans
239
+ else:
240
+ given_num = extract_unique_decimal(
241
+ cri_agent.step(
242
+ str_mes(player_1_response.content)).msgs[0].content
243
+ )
244
+ money_prompt = money_prompt.format(
245
+ give=given_num, N=given_num * k, left=10 - given_num
246
+ )
247
+
248
+ player_1.record_message(player_1_response)
249
+ player_1_response = player_1_response.content
250
+ content.append("Trustor: "+player_1_response + "\n" +
251
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
252
+ given_num * k))
253
+ return content, given_num
254
+
255
+
256
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
257
+ model_dropdown, api_key_input, round_number_input):
258
+ if api_key_input is None or api_key_input == "":
259
+ api_key_input = os.environ.get("OPENAI_API_KEY")
260
+ else:
261
+ os.environ["OPENAI_API_KEY"] = api_key_input
262
+ random_character = [character_info[trustor_character_dropdown],
263
+ character_info[trustee_character_dropdown]]
264
+ chat_agent = []
265
+ front = "you are a person not an ai model."
266
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
267
+ back = "you need to answer a specific price figure, not a price range!"
268
+ if api_key_input is not None or api_key_input != "":
269
+ openai.api_key = api_key_input
270
+ else:
271
+ openai.api_key = os.getenv("OPENAI_API_KEY")
272
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
273
+ raise ValueError("You can't play with yourself")
274
+ for i in range(len(random_character)):
275
+ sys_prompt = (
276
+ random_character[i]
277
+ + like_people
278
+ + front
279
+ + limited_prompt
280
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
281
+ + back
282
+ )
283
+ model_config = ChatGPTConfig(temperature=temperature_slider)
284
+ chat_agent.append(
285
+ ChatAgent(
286
+ BaseMessage(
287
+ role_name="player",
288
+ role_type=RoleType.USER,
289
+ meta_dict={},
290
+ content=sys_prompt,
291
+ ),
292
+ model_type=model_dict[model_dropdown],
293
+ output_language="English",
294
+ model_config=model_config,
295
+ )
296
+ )
297
+ return chat_agent, "ChatAgent Created Successfully"
298
+
299
+
300
+ def process_interaction(chat_agent_state, round_num_state,
301
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
302
+ identity_dropdown = [trustor, trustee]
303
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
304
+ if round_num_state < round_number_input:
305
+ res, dia, first_prompt, second_prompt = classmate(
306
+ chat_agent_state[0],
307
+ chat_agent_state[1],
308
+ round_num_state == 0,
309
+ first_prompt,
310
+ second_prompt,
311
+ 3,
312
+ )
313
+ dia = "Trustor: "+dia['Player_1'] + \
314
+ "\n" + "Trustee: "+dia['Player_2']
315
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
316
+ round_num_state += 1
317
+ elif identity_dropdown[0] == "Human(You)":
318
+ if round_num_state < round_number_input:
319
+ res, dia, first_prompt, second_prompt = classmate_with_human(
320
+ chat_agent_state[0],
321
+ chat_agent_state[1],
322
+ user_input,
323
+ "trustor",
324
+ round_num_state == 0,
325
+ first_prompt,
326
+ second_prompt,
327
+ 3,
328
+ )
329
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
330
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
331
+ dia['Player_2']+"\n"
332
+ dialog_history_state += dia
333
+ round_num_state += 1
334
+ else:
335
+ if round_num_state < round_number_input:
336
+ dia, given_money = classmate_with_human_trustee(
337
+ chat_agent_state[1],
338
+ chat_agent_state[0],
339
+ user_input,
340
+ "trustee",
341
+ round_num_state == 0,
342
+ given_money,
343
+ 3,
344
+ )
345
+ if round_num_state == 0:
346
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
347
+ else:
348
+ dialog_history_state += f"(Round {round_num_state})" + \
349
+ dia[0] + "\n" + \
350
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
351
+ round_num_state += 1
352
+
353
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
354
+
355
+
356
+ def reset_on_persona_change():
357
+ # Reset values to their initial states
358
+ new_dialog_history = "" # Reset dialog history
359
+ new_round_num_state = 0 # Reset round number state to 0
360
+ new_first_prompt = "" # Reset first prompt
361
+ new_second_prompt = "" # Reset second prompt
362
+ new_given_money = 0 # Reset given money to 0
363
+ # You can add more resets here if needed
364
+
365
+ # Return the new reset values to their respective components
366
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
367
+
368
+
369
+ with gr.Blocks() as app:
370
+ game_introduction = gr.Textbox(
371
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
372
+ 2. You can select the total number of rounds for the game.\n
373
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
374
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
375
+ 5. If you want to reset the conversation, please refresh this page.\n""")
376
+ with gr.Row():
377
+ trustor_game_prompt = gr.Textbox(
378
+ label="Trustor Game Prompt", value=prompt['1'])
379
+ trustee_game_prompt = gr.Textbox(
380
+ label="Trustee Game Prompt", value=prompt['2'])
381
+ with gr.Row():
382
+ trustor_character_dropdown = gr.Dropdown(
383
+ choices=characters, label="Select Trustor Persona", value=characters[2])
384
+ trustee_character_dropdown = gr.Dropdown(
385
+ choices=characters, label="Select Trustee Persona", value=characters[2])
386
+ with gr.Row():
387
+ Trustor_info_display = gr.Textbox(
388
+ label="Trustor Persona Info", value=character_info[characters[2]])
389
+ Trustee_info_display = gr.Textbox(
390
+ label="Trustee Persona Info", value=character_info[characters[2]])
391
+ model_dropdown = gr.Dropdown(
392
+ choices=models, label="Select Model Type", value=models[0])
393
+ temperature_slider = gr.Slider(
394
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
395
+ api_key_input = gr.Textbox(
396
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
397
+ submit_button = gr.Button("Create ChatAgent")
398
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
399
+
400
+ round_number_input = gr.Number(label="Total Round Number", value=10)
401
+ round_num_state = gr.State(value=0)
402
+ dialog_history_state = gr.State(value="")
403
+ chat_agent_state = gr.State(value=initial_chat_agent)
404
+ first_prompt = gr.State(value="")
405
+ second_prompt = gr.State(value="")
406
+ given_money = gr.State(value=0)
407
+ user_input = gr.Number(
408
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
409
+ converse_button = gr.Button("Continue Conversation")
410
+
411
+ dialog_display = gr.Textbox(
412
+ label="Dialog History", value="")
413
+
414
+ trustor_character_dropdown.change(
415
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
416
+ trustee_character_dropdown.change(
417
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
418
+
419
+ submit_button.click(
420
+ create_chat_agent,
421
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
422
+ model_dropdown, api_key_input, round_number_input],
423
+ outputs=[chat_agent_state, submit_success_display]
424
+ )
425
+ converse_button.click(
426
+ process_interaction,
427
+ inputs=[chat_agent_state, round_num_state,
428
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
429
+ outputs=[dialog_display, round_num_state,
430
+ first_prompt, second_prompt, dialog_history_state, given_money]
431
+ )
432
+ trustor_character_dropdown.change(
433
+ reset_on_persona_change,
434
+ outputs=[dialog_history_state, round_num_state,
435
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
436
+ )
437
+
438
+ trustee_character_dropdown.change(
439
+ reset_on_persona_change,
440
+ outputs=[dialog_history_state, round_num_state,
441
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
442
+ )
443
+
444
+
445
+ app.launch()
.history/app_20240222100051.py ADDED
@@ -0,0 +1,444 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ # 初始化对话历史和当前轮数
43
+ initial_dialog_history = []
44
+ initial_round_num = 0
45
+
46
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
47
+
48
+ initial_chat_agent = None
49
+
50
+
51
+ def update_char_info(char):
52
+ return character_info.get(char, "No information available.")
53
+
54
+
55
+ def classmate_with_human(
56
+ player_1,
57
+ player_2,
58
+ user_input,
59
+ user_role,
60
+ first_round,
61
+ first_prompt,
62
+ second_prompt,
63
+ k,
64
+ ):
65
+ first_round_prompt = "This is the first round, answer the question."
66
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
67
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
68
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
69
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
70
+ res = []
71
+ cri_agent = ChatAgent(
72
+ BaseMessage(
73
+ role_name="critic",
74
+ role_type=RoleType.ASSISTANT,
75
+ meta_dict={},
76
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
77
+ ),
78
+ output_language="English",
79
+ # model=ModelType.STUB,
80
+ )
81
+
82
+ if first_round:
83
+ if user_role == "trustor":
84
+ given_num = user_input
85
+ player_1_response = str_mes(
86
+ user_input_prompt_template.format(k=given_num))
87
+ else:
88
+ player_1_response = player_1.step(
89
+ str_mes(first_round_prompt + grantee)).msgs[0]
90
+ ans = match_and_compare_numbers_v2(player_1_response.content)
91
+ if ans:
92
+ given_num = ans
93
+ else:
94
+ given_num = extract_unique_decimal(
95
+ cri_agent.step(
96
+ str_mes(player_1_response.content)).msgs[0].content
97
+ )
98
+
99
+ money_prompt = money_prompt.format(
100
+ give=given_num, N=given_num * k, left=10 - given_num
101
+ )
102
+ if user_role == "trustee":
103
+ player_2_response = str_mes(
104
+ user_input_prompt_template.format(k=given_num * k))
105
+ else:
106
+ player_2_response = player_2.step(
107
+ str_mes(money_prompt + grantee)).msgs[0]
108
+ else:
109
+ if user_role == "trustor":
110
+ given_num = user_input
111
+ player_1_response = str_mes(
112
+ user_input_prompt_template.format(k=given_num))
113
+ else:
114
+ player_1_response = player_1.step(
115
+ str_mes(first_prompt + grantee)).msgs[0]
116
+ # print("player 1 input", first_prompt)
117
+ # print("Player_1_res", player_1_response.content)
118
+ ans = match_and_compare_numbers_v2(player_1_response.content)
119
+ if ans:
120
+ given_num = ans
121
+ else:
122
+ given_num = extract_unique_decimal(
123
+ cri_agent.step(
124
+ str_mes(player_1_response.content)).msgs[0].content
125
+ )
126
+ money_prompt = money_prompt.format(
127
+ give=given_num, N=given_num * k, left=10 - given_num
128
+ )
129
+ if user_role == "trustee":
130
+ player_2_response = str_mes(
131
+ user_input_prompt_template.format(k=given_num * k))
132
+ else:
133
+ player_2_response = player_2.step(
134
+ str_mes(second_prompt + money_prompt + grantee)
135
+ )
136
+ player_2_response = player_2_response.msgs[0]
137
+
138
+ player_1.record_message(player_1_response)
139
+ player_2.record_message(player_2_response)
140
+ player_1_response = player_1_response.content
141
+ player_2_response = player_2_response.content
142
+ dia_history = {
143
+ "Player_1": player_1_response,
144
+ "Player_2": player_2_response,
145
+ }
146
+
147
+ cri_agent.reset()
148
+ ans = match_and_compare_numbers_v2(player_2_response)
149
+ if ans:
150
+ return_num = ans
151
+ else:
152
+ return_num = extract_unique_decimal(
153
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
154
+ )
155
+ return_money_prompt = return_money_prompt.format(
156
+ give=given_num,
157
+ receive=given_num * k,
158
+ N=return_num,
159
+ left=10 - given_num + return_num,
160
+ )
161
+ player_2_end_prompt = player_2_end_prompt.format(
162
+ give=given_num,
163
+ receive=given_num * k,
164
+ N=return_num,
165
+ left=given_num * k - return_num,
166
+ )
167
+ return (
168
+ [given_num, return_num],
169
+ dia_history,
170
+ return_money_prompt,
171
+ player_2_end_prompt,
172
+ )
173
+
174
+
175
+ def classmate_with_human_trustee(
176
+ player_1,
177
+ player_2,
178
+ user_input,
179
+ user_role,
180
+ first_round,
181
+ given_money,
182
+ k,
183
+ ):
184
+ first_round_prompt = "This is the first round, answer the question."
185
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
186
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
187
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
188
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
189
+ res = []
190
+ cri_agent = ChatAgent(
191
+ BaseMessage(
192
+ role_name="critic",
193
+ role_type=RoleType.ASSISTANT,
194
+ meta_dict={},
195
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
196
+ ),
197
+ output_language="English",
198
+ # model=ModelType.STUB,
199
+ )
200
+ content = []
201
+ if first_round:
202
+ content.append("\n")
203
+ player_1_response = player_1.step(
204
+ str_mes(first_round_prompt + grantee)).msgs[0]
205
+ ans = match_and_compare_numbers_v2(player_1_response.content)
206
+ if ans:
207
+ given_num = ans
208
+ else:
209
+ given_num = extract_unique_decimal(
210
+ cri_agent.step(
211
+ str_mes(player_1_response.content)).msgs[0].content
212
+ )
213
+ money_prompt = money_prompt.format(
214
+ give=given_num, N=given_num * k, left=10 - given_num
215
+ )
216
+ else:
217
+ given_num = given_money
218
+
219
+ return_num = user_input
220
+ content.append("Trustee: " + f"I will give {return_num} dollars")
221
+ first_prompt = return_money_prompt.format(
222
+ give=given_num,
223
+ receive=given_num * k,
224
+ N=return_num,
225
+ left=10 - given_num + return_num,
226
+ )
227
+ second_prompt = player_2_end_prompt.format(
228
+ give=given_num,
229
+ receive=given_num * k,
230
+ N=return_num,
231
+ left=given_num * k - return_num,
232
+ )
233
+ player_1_response = player_1.step(
234
+ str_mes(first_prompt + grantee)).msgs[0]
235
+ ans = match_and_compare_numbers_v2(player_1_response.content)
236
+ if ans:
237
+ given_num = ans
238
+ else:
239
+ given_num = extract_unique_decimal(
240
+ cri_agent.step(
241
+ str_mes(player_1_response.content)).msgs[0].content
242
+ )
243
+ money_prompt = money_prompt.format(
244
+ give=given_num, N=given_num * k, left=10 - given_num
245
+ )
246
+
247
+ player_1.record_message(player_1_response)
248
+ player_1_response = player_1_response.content
249
+ content.append("Trustor: "+player_1_response + "\n" +
250
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
251
+ given_num * k))
252
+ return content, given_num
253
+
254
+
255
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
256
+ model_dropdown, api_key_input, round_number_input):
257
+ if api_key_input is None or api_key_input == "":
258
+ api_key_input = os.environ.get("OPENAI_API_KEY")
259
+ else:
260
+ os.environ["OPENAI_API_KEY"] = api_key_input
261
+ random_character = [character_info[trustor_character_dropdown],
262
+ character_info[trustee_character_dropdown]]
263
+ chat_agent = []
264
+ front = "you are a person not an ai model."
265
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
266
+ back = "you need to answer a specific price figure, not a price range!"
267
+ if api_key_input is not None or api_key_input != "":
268
+ openai.api_key = api_key_input
269
+ else:
270
+ openai.api_key = os.getenv("OPENAI_API_KEY")
271
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
272
+ raise ValueError("You can't play with yourself")
273
+ for i in range(len(random_character)):
274
+ sys_prompt = (
275
+ random_character[i]
276
+ + like_people
277
+ + front
278
+ + limited_prompt
279
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
280
+ + back
281
+ )
282
+ model_config = ChatGPTConfig(temperature=temperature_slider)
283
+ chat_agent.append(
284
+ ChatAgent(
285
+ BaseMessage(
286
+ role_name="player",
287
+ role_type=RoleType.USER,
288
+ meta_dict={},
289
+ content=sys_prompt,
290
+ ),
291
+ model_type=model_dict[model_dropdown],
292
+ output_language="English",
293
+ model_config=model_config,
294
+ )
295
+ )
296
+ return chat_agent, "ChatAgent Created Successfully"
297
+
298
+
299
+ def process_interaction(chat_agent_state, round_num_state,
300
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
301
+ identity_dropdown = [trustor, trustee]
302
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
303
+ if round_num_state < round_number_input:
304
+ res, dia, first_prompt, second_prompt = classmate(
305
+ chat_agent_state[0],
306
+ chat_agent_state[1],
307
+ round_num_state == 0,
308
+ first_prompt,
309
+ second_prompt,
310
+ 3,
311
+ )
312
+ dia = "Trustor: "+dia['Player_1'] + \
313
+ "\n" + "Trustee: "+dia['Player_2']
314
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
315
+ round_num_state += 1
316
+ elif identity_dropdown[0] == "Human(You)":
317
+ if round_num_state < round_number_input:
318
+ res, dia, first_prompt, second_prompt = classmate_with_human(
319
+ chat_agent_state[0],
320
+ chat_agent_state[1],
321
+ user_input,
322
+ "trustor",
323
+ round_num_state == 0,
324
+ first_prompt,
325
+ second_prompt,
326
+ 3,
327
+ )
328
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
329
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
330
+ dia['Player_2']+"\n"
331
+ dialog_history_state += dia
332
+ round_num_state += 1
333
+ else:
334
+ if round_num_state < round_number_input:
335
+ dia, given_money = classmate_with_human_trustee(
336
+ chat_agent_state[1],
337
+ chat_agent_state[0],
338
+ user_input,
339
+ "trustee",
340
+ round_num_state == 0,
341
+ given_money,
342
+ 3,
343
+ )
344
+ if round_num_state == 0:
345
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
346
+ else:
347
+ dialog_history_state += f"(Round {round_num_state})" + \
348
+ dia[0] + "\n" + \
349
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
350
+ round_num_state += 1
351
+
352
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
353
+
354
+
355
+ def reset_on_persona_change():
356
+ # Reset values to their initial states
357
+ new_dialog_history = "" # Reset dialog history
358
+ new_round_num_state = 0 # Reset round number state to 0
359
+ new_first_prompt = "" # Reset first prompt
360
+ new_second_prompt = "" # Reset second prompt
361
+ new_given_money = 0 # Reset given money to 0
362
+ # You can add more resets here if needed
363
+
364
+ # Return the new reset values to their respective components
365
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
366
+
367
+
368
+ with gr.Blocks() as app:
369
+ game_introduction = gr.Textbox(
370
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
371
+ 2. You can select the total number of rounds for the game.\n
372
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
373
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
374
+ 5. If you want to reset the conversation, please refresh this page.\n""")
375
+ with gr.Row():
376
+ trustor_game_prompt = gr.Textbox(
377
+ label="Trustor Game Prompt", value=prompt['1'])
378
+ trustee_game_prompt = gr.Textbox(
379
+ label="Trustee Game Prompt", value=prompt['2'])
380
+ with gr.Row():
381
+ trustor_character_dropdown = gr.Dropdown(
382
+ choices=characters, label="Select Trustor Persona", value=characters[2])
383
+ trustee_character_dropdown = gr.Dropdown(
384
+ choices=characters, label="Select Trustee Persona", value=characters[2])
385
+ with gr.Row():
386
+ Trustor_info_display = gr.Textbox(
387
+ label="Trustor Persona Info", value=character_info[characters[2]])
388
+ Trustee_info_display = gr.Textbox(
389
+ label="Trustee Persona Info", value=character_info[characters[2]])
390
+ model_dropdown = gr.Dropdown(
391
+ choices=models, label="Select Model Type", value=models[0])
392
+ temperature_slider = gr.Slider(
393
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
394
+ api_key_input = gr.Textbox(
395
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
396
+ submit_button = gr.Button("Create ChatAgent")
397
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
398
+
399
+ round_number_input = gr.Number(label="Total Round Number", value=10)
400
+ round_num_state = gr.State(value=0)
401
+ dialog_history_state = gr.State(value="")
402
+ chat_agent_state = gr.State(value=initial_chat_agent)
403
+ first_prompt = gr.State(value="")
404
+ second_prompt = gr.State(value="")
405
+ given_money = gr.State(value=0)
406
+ user_input = gr.Number(
407
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
408
+ converse_button = gr.Button("Continue Conversation")
409
+
410
+ dialog_display = gr.Textbox(
411
+ label="Dialog History", value="")
412
+
413
+ trustor_character_dropdown.change(
414
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
415
+ trustee_character_dropdown.change(
416
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
417
+
418
+ submit_button.click(
419
+ create_chat_agent,
420
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
421
+ model_dropdown, api_key_input, round_number_input],
422
+ outputs=[chat_agent_state, submit_success_display]
423
+ )
424
+ converse_button.click(
425
+ process_interaction,
426
+ inputs=[chat_agent_state, round_num_state,
427
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
428
+ outputs=[dialog_display, round_num_state,
429
+ first_prompt, second_prompt, dialog_history_state, given_money]
430
+ )
431
+ trustor_character_dropdown.change(
432
+ reset_on_persona_change,
433
+ outputs=[dialog_history_state, round_num_state,
434
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
435
+ )
436
+
437
+ trustee_character_dropdown.change(
438
+ reset_on_persona_change,
439
+ outputs=[dialog_history_state, round_num_state,
440
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
441
+ )
442
+
443
+
444
+ app.launch()
.history/app_20240222100052.py ADDED
@@ -0,0 +1,443 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ initial_dialog_history = []
43
+ initial_round_num = 0
44
+
45
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
46
+
47
+ initial_chat_agent = None
48
+
49
+
50
+ def update_char_info(char):
51
+ return character_info.get(char, "No information available.")
52
+
53
+
54
+ def classmate_with_human(
55
+ player_1,
56
+ player_2,
57
+ user_input,
58
+ user_role,
59
+ first_round,
60
+ first_prompt,
61
+ second_prompt,
62
+ k,
63
+ ):
64
+ first_round_prompt = "This is the first round, answer the question."
65
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
66
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
67
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
68
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
69
+ res = []
70
+ cri_agent = ChatAgent(
71
+ BaseMessage(
72
+ role_name="critic",
73
+ role_type=RoleType.ASSISTANT,
74
+ meta_dict={},
75
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
76
+ ),
77
+ output_language="English",
78
+ # model=ModelType.STUB,
79
+ )
80
+
81
+ if first_round:
82
+ if user_role == "trustor":
83
+ given_num = user_input
84
+ player_1_response = str_mes(
85
+ user_input_prompt_template.format(k=given_num))
86
+ else:
87
+ player_1_response = player_1.step(
88
+ str_mes(first_round_prompt + grantee)).msgs[0]
89
+ ans = match_and_compare_numbers_v2(player_1_response.content)
90
+ if ans:
91
+ given_num = ans
92
+ else:
93
+ given_num = extract_unique_decimal(
94
+ cri_agent.step(
95
+ str_mes(player_1_response.content)).msgs[0].content
96
+ )
97
+
98
+ money_prompt = money_prompt.format(
99
+ give=given_num, N=given_num * k, left=10 - given_num
100
+ )
101
+ if user_role == "trustee":
102
+ player_2_response = str_mes(
103
+ user_input_prompt_template.format(k=given_num * k))
104
+ else:
105
+ player_2_response = player_2.step(
106
+ str_mes(money_prompt + grantee)).msgs[0]
107
+ else:
108
+ if user_role == "trustor":
109
+ given_num = user_input
110
+ player_1_response = str_mes(
111
+ user_input_prompt_template.format(k=given_num))
112
+ else:
113
+ player_1_response = player_1.step(
114
+ str_mes(first_prompt + grantee)).msgs[0]
115
+ # print("player 1 input", first_prompt)
116
+ # print("Player_1_res", player_1_response.content)
117
+ ans = match_and_compare_numbers_v2(player_1_response.content)
118
+ if ans:
119
+ given_num = ans
120
+ else:
121
+ given_num = extract_unique_decimal(
122
+ cri_agent.step(
123
+ str_mes(player_1_response.content)).msgs[0].content
124
+ )
125
+ money_prompt = money_prompt.format(
126
+ give=given_num, N=given_num * k, left=10 - given_num
127
+ )
128
+ if user_role == "trustee":
129
+ player_2_response = str_mes(
130
+ user_input_prompt_template.format(k=given_num * k))
131
+ else:
132
+ player_2_response = player_2.step(
133
+ str_mes(second_prompt + money_prompt + grantee)
134
+ )
135
+ player_2_response = player_2_response.msgs[0]
136
+
137
+ player_1.record_message(player_1_response)
138
+ player_2.record_message(player_2_response)
139
+ player_1_response = player_1_response.content
140
+ player_2_response = player_2_response.content
141
+ dia_history = {
142
+ "Player_1": player_1_response,
143
+ "Player_2": player_2_response,
144
+ }
145
+
146
+ cri_agent.reset()
147
+ ans = match_and_compare_numbers_v2(player_2_response)
148
+ if ans:
149
+ return_num = ans
150
+ else:
151
+ return_num = extract_unique_decimal(
152
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
153
+ )
154
+ return_money_prompt = return_money_prompt.format(
155
+ give=given_num,
156
+ receive=given_num * k,
157
+ N=return_num,
158
+ left=10 - given_num + return_num,
159
+ )
160
+ player_2_end_prompt = player_2_end_prompt.format(
161
+ give=given_num,
162
+ receive=given_num * k,
163
+ N=return_num,
164
+ left=given_num * k - return_num,
165
+ )
166
+ return (
167
+ [given_num, return_num],
168
+ dia_history,
169
+ return_money_prompt,
170
+ player_2_end_prompt,
171
+ )
172
+
173
+
174
+ def classmate_with_human_trustee(
175
+ player_1,
176
+ player_2,
177
+ user_input,
178
+ user_role,
179
+ first_round,
180
+ given_money,
181
+ k,
182
+ ):
183
+ first_round_prompt = "This is the first round, answer the question."
184
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
185
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
186
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
187
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
188
+ res = []
189
+ cri_agent = ChatAgent(
190
+ BaseMessage(
191
+ role_name="critic",
192
+ role_type=RoleType.ASSISTANT,
193
+ meta_dict={},
194
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
195
+ ),
196
+ output_language="English",
197
+ # model=ModelType.STUB,
198
+ )
199
+ content = []
200
+ if first_round:
201
+ content.append("\n")
202
+ player_1_response = player_1.step(
203
+ str_mes(first_round_prompt + grantee)).msgs[0]
204
+ ans = match_and_compare_numbers_v2(player_1_response.content)
205
+ if ans:
206
+ given_num = ans
207
+ else:
208
+ given_num = extract_unique_decimal(
209
+ cri_agent.step(
210
+ str_mes(player_1_response.content)).msgs[0].content
211
+ )
212
+ money_prompt = money_prompt.format(
213
+ give=given_num, N=given_num * k, left=10 - given_num
214
+ )
215
+ else:
216
+ given_num = given_money
217
+
218
+ return_num = user_input
219
+ content.append("Trustee: " + f"I will give {return_num} dollars")
220
+ first_prompt = return_money_prompt.format(
221
+ give=given_num,
222
+ receive=given_num * k,
223
+ N=return_num,
224
+ left=10 - given_num + return_num,
225
+ )
226
+ second_prompt = player_2_end_prompt.format(
227
+ give=given_num,
228
+ receive=given_num * k,
229
+ N=return_num,
230
+ left=given_num * k - return_num,
231
+ )
232
+ player_1_response = player_1.step(
233
+ str_mes(first_prompt + grantee)).msgs[0]
234
+ ans = match_and_compare_numbers_v2(player_1_response.content)
235
+ if ans:
236
+ given_num = ans
237
+ else:
238
+ given_num = extract_unique_decimal(
239
+ cri_agent.step(
240
+ str_mes(player_1_response.content)).msgs[0].content
241
+ )
242
+ money_prompt = money_prompt.format(
243
+ give=given_num, N=given_num * k, left=10 - given_num
244
+ )
245
+
246
+ player_1.record_message(player_1_response)
247
+ player_1_response = player_1_response.content
248
+ content.append("Trustor: "+player_1_response + "\n" +
249
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
250
+ given_num * k))
251
+ return content, given_num
252
+
253
+
254
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
255
+ model_dropdown, api_key_input, round_number_input):
256
+ if api_key_input is None or api_key_input == "":
257
+ api_key_input = os.environ.get("OPENAI_API_KEY")
258
+ else:
259
+ os.environ["OPENAI_API_KEY"] = api_key_input
260
+ random_character = [character_info[trustor_character_dropdown],
261
+ character_info[trustee_character_dropdown]]
262
+ chat_agent = []
263
+ front = "you are a person not an ai model."
264
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
265
+ back = "you need to answer a specific price figure, not a price range!"
266
+ if api_key_input is not None or api_key_input != "":
267
+ openai.api_key = api_key_input
268
+ else:
269
+ openai.api_key = os.getenv("OPENAI_API_KEY")
270
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
271
+ raise ValueError("You can't play with yourself")
272
+ for i in range(len(random_character)):
273
+ sys_prompt = (
274
+ random_character[i]
275
+ + like_people
276
+ + front
277
+ + limited_prompt
278
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
279
+ + back
280
+ )
281
+ model_config = ChatGPTConfig(temperature=temperature_slider)
282
+ chat_agent.append(
283
+ ChatAgent(
284
+ BaseMessage(
285
+ role_name="player",
286
+ role_type=RoleType.USER,
287
+ meta_dict={},
288
+ content=sys_prompt,
289
+ ),
290
+ model_type=model_dict[model_dropdown],
291
+ output_language="English",
292
+ model_config=model_config,
293
+ )
294
+ )
295
+ return chat_agent, "ChatAgent Created Successfully"
296
+
297
+
298
+ def process_interaction(chat_agent_state, round_num_state,
299
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
300
+ identity_dropdown = [trustor, trustee]
301
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
302
+ if round_num_state < round_number_input:
303
+ res, dia, first_prompt, second_prompt = classmate(
304
+ chat_agent_state[0],
305
+ chat_agent_state[1],
306
+ round_num_state == 0,
307
+ first_prompt,
308
+ second_prompt,
309
+ 3,
310
+ )
311
+ dia = "Trustor: "+dia['Player_1'] + \
312
+ "\n" + "Trustee: "+dia['Player_2']
313
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
314
+ round_num_state += 1
315
+ elif identity_dropdown[0] == "Human(You)":
316
+ if round_num_state < round_number_input:
317
+ res, dia, first_prompt, second_prompt = classmate_with_human(
318
+ chat_agent_state[0],
319
+ chat_agent_state[1],
320
+ user_input,
321
+ "trustor",
322
+ round_num_state == 0,
323
+ first_prompt,
324
+ second_prompt,
325
+ 3,
326
+ )
327
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
328
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
329
+ dia['Player_2']+"\n"
330
+ dialog_history_state += dia
331
+ round_num_state += 1
332
+ else:
333
+ if round_num_state < round_number_input:
334
+ dia, given_money = classmate_with_human_trustee(
335
+ chat_agent_state[1],
336
+ chat_agent_state[0],
337
+ user_input,
338
+ "trustee",
339
+ round_num_state == 0,
340
+ given_money,
341
+ 3,
342
+ )
343
+ if round_num_state == 0:
344
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
345
+ else:
346
+ dialog_history_state += f"(Round {round_num_state})" + \
347
+ dia[0] + "\n" + \
348
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
349
+ round_num_state += 1
350
+
351
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
352
+
353
+
354
+ def reset_on_persona_change():
355
+ # Reset values to their initial states
356
+ new_dialog_history = "" # Reset dialog history
357
+ new_round_num_state = 0 # Reset round number state to 0
358
+ new_first_prompt = "" # Reset first prompt
359
+ new_second_prompt = "" # Reset second prompt
360
+ new_given_money = 0 # Reset given money to 0
361
+ # You can add more resets here if needed
362
+
363
+ # Return the new reset values to their respective components
364
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
365
+
366
+
367
+ with gr.Blocks() as app:
368
+ game_introduction = gr.Textbox(
369
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
370
+ 2. You can select the total number of rounds for the game.\n
371
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
372
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
373
+ 5. If you want to reset the conversation, please refresh this page.\n""")
374
+ with gr.Row():
375
+ trustor_game_prompt = gr.Textbox(
376
+ label="Trustor Game Prompt", value=prompt['1'])
377
+ trustee_game_prompt = gr.Textbox(
378
+ label="Trustee Game Prompt", value=prompt['2'])
379
+ with gr.Row():
380
+ trustor_character_dropdown = gr.Dropdown(
381
+ choices=characters, label="Select Trustor Persona", value=characters[2])
382
+ trustee_character_dropdown = gr.Dropdown(
383
+ choices=characters, label="Select Trustee Persona", value=characters[2])
384
+ with gr.Row():
385
+ Trustor_info_display = gr.Textbox(
386
+ label="Trustor Persona Info", value=character_info[characters[2]])
387
+ Trustee_info_display = gr.Textbox(
388
+ label="Trustee Persona Info", value=character_info[characters[2]])
389
+ model_dropdown = gr.Dropdown(
390
+ choices=models, label="Select Model Type", value=models[0])
391
+ temperature_slider = gr.Slider(
392
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
393
+ api_key_input = gr.Textbox(
394
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
395
+ submit_button = gr.Button("Create ChatAgent")
396
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
397
+
398
+ round_number_input = gr.Number(label="Total Round Number", value=10)
399
+ round_num_state = gr.State(value=0)
400
+ dialog_history_state = gr.State(value="")
401
+ chat_agent_state = gr.State(value=initial_chat_agent)
402
+ first_prompt = gr.State(value="")
403
+ second_prompt = gr.State(value="")
404
+ given_money = gr.State(value=0)
405
+ user_input = gr.Number(
406
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
407
+ converse_button = gr.Button("Continue Conversation")
408
+
409
+ dialog_display = gr.Textbox(
410
+ label="Dialog History", value="")
411
+
412
+ trustor_character_dropdown.change(
413
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
414
+ trustee_character_dropdown.change(
415
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
416
+
417
+ submit_button.click(
418
+ create_chat_agent,
419
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
420
+ model_dropdown, api_key_input, round_number_input],
421
+ outputs=[chat_agent_state, submit_success_display]
422
+ )
423
+ converse_button.click(
424
+ process_interaction,
425
+ inputs=[chat_agent_state, round_num_state,
426
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
427
+ outputs=[dialog_display, round_num_state,
428
+ first_prompt, second_prompt, dialog_history_state, given_money]
429
+ )
430
+ trustor_character_dropdown.change(
431
+ reset_on_persona_change,
432
+ outputs=[dialog_history_state, round_num_state,
433
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
434
+ )
435
+
436
+ trustee_character_dropdown.change(
437
+ reset_on_persona_change,
438
+ outputs=[dialog_history_state, round_num_state,
439
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
440
+ )
441
+
442
+
443
+ app.launch()
.history/app_20240222100106.py ADDED
@@ -0,0 +1,444 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ initial_dialog_history = []
43
+ initial_round_num = 0
44
+
45
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
46
+
47
+ initial_chat_agent = None
48
+
49
+
50
+ def update_char_info(char):
51
+ return character_info.get(char, "No information available.")
52
+
53
+
54
+ def classmate_with_human(
55
+ player_1,
56
+ player_2,
57
+ user_input,
58
+ user_role,
59
+ first_round,
60
+ first_prompt,
61
+ second_prompt,
62
+ k,
63
+ ):
64
+ first_round_prompt = "This is the first round, answer the question."
65
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
66
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
67
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
68
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
69
+ res = []
70
+ cri_agent = ChatAgent(
71
+ BaseMessage(
72
+ role_name="critic",
73
+ role_type=RoleType.ASSISTANT,
74
+ meta_dict={},
75
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
76
+ ),
77
+ output_language="English",
78
+ # model=ModelType.STUB,
79
+ )
80
+
81
+ if first_round:
82
+ if user_role == "trustor":
83
+ given_num = user_input
84
+ player_1_response = str_mes(
85
+ user_input_prompt_template.format(k=given_num))
86
+ else:
87
+ player_1_response = player_1.step(
88
+ str_mes(first_round_prompt + grantee)).msgs[0]
89
+ ans = match_and_compare_numbers_v2(player_1_response.content)
90
+ if ans:
91
+ given_num = ans
92
+ else:
93
+ given_num = extract_unique_decimal(
94
+ cri_agent.step(
95
+ str_mes(player_1_response.content)).msgs[0].content
96
+ )
97
+
98
+ money_prompt = money_prompt.format(
99
+ give=given_num, N=given_num * k, left=10 - given_num
100
+ )
101
+ if user_role == "trustee":
102
+ player_2_response = str_mes(
103
+ user_input_prompt_template.format(k=given_num * k))
104
+ else:
105
+ player_2_response = player_2.step(
106
+ str_mes(money_prompt + grantee)).msgs[0]
107
+ else:
108
+ if user_role == "trustor":
109
+ given_num = user_input
110
+ player_1_response = str_mes(
111
+ user_input_prompt_template.format(k=given_num))
112
+ else:
113
+ player_1_response = player_1.step(
114
+ str_mes(first_prompt + grantee)).msgs[0]
115
+ # print("player 1 input", first_prompt)
116
+ # print("Player_1_res", player_1_response.content)
117
+ ans = match_and_compare_numbers_v2(player_1_response.content)
118
+ if ans:
119
+ given_num = ans
120
+ else:
121
+ given_num = extract_unique_decimal(
122
+ cri_agent.step(
123
+ str_mes(player_1_response.content)).msgs[0].content
124
+ )
125
+ money_prompt = money_prompt.format(
126
+ give=given_num, N=given_num * k, left=10 - given_num
127
+ )
128
+ if user_role == "trustee":
129
+ player_2_response = str_mes(
130
+ user_input_prompt_template.format(k=given_num * k))
131
+ else:
132
+ player_2_response = player_2.step(
133
+ str_mes(second_prompt + money_prompt + grantee)
134
+ )
135
+ player_2_response = player_2_response.msgs[0]
136
+
137
+ player_1.record_message(player_1_response)
138
+ player_2.record_message(player_2_response)
139
+ player_1_response = player_1_response.content
140
+ player_2_response = player_2_response.content
141
+ dia_history = {
142
+ "Player_1": player_1_response,
143
+ "Player_2": player_2_response,
144
+ }
145
+
146
+ cri_agent.reset()
147
+ ans = match_and_compare_numbers_v2(player_2_response)
148
+ if ans:
149
+ return_num = ans
150
+ else:
151
+ return_num = extract_unique_decimal(
152
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
153
+ )
154
+ return_money_prompt = return_money_prompt.format(
155
+ give=given_num,
156
+ receive=given_num * k,
157
+ N=return_num,
158
+ left=10 - given_num + return_num,
159
+ )
160
+ player_2_end_prompt = player_2_end_prompt.format(
161
+ give=given_num,
162
+ receive=given_num * k,
163
+ N=return_num,
164
+ left=given_num * k - return_num,
165
+ )
166
+ return (
167
+ [given_num, return_num],
168
+ dia_history,
169
+ return_money_prompt,
170
+ player_2_end_prompt,
171
+ )
172
+
173
+
174
+ def classmate_with_human_trustee(
175
+ player_1,
176
+ player_2,
177
+ user_input,
178
+ user_role,
179
+ first_round,
180
+ given_money,
181
+ k,
182
+ ):
183
+ first_round_prompt = "This is the first round, answer the question."
184
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
185
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
186
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
187
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
188
+ res = []
189
+ cri_agent = ChatAgent(
190
+ BaseMessage(
191
+ role_name="critic",
192
+ role_type=RoleType.ASSISTANT,
193
+ meta_dict={},
194
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
195
+ ),
196
+ output_language="English",
197
+ # model=ModelType.STUB,
198
+ )
199
+ content = []
200
+ if first_round:
201
+ content.append("\n")
202
+ player_1_response = player_1.step(
203
+ str_mes(first_round_prompt + grantee)).msgs[0]
204
+ ans = match_and_compare_numbers_v2(player_1_response.content)
205
+ if ans:
206
+ given_num = ans
207
+ else:
208
+ given_num = extract_unique_decimal(
209
+ cri_agent.step(
210
+ str_mes(player_1_response.content)).msgs[0].content
211
+ )
212
+ money_prompt = money_prompt.format(
213
+ give=given_num, N=given_num * k, left=10 - given_num
214
+ )
215
+ else:
216
+ given_num = given_money
217
+
218
+ return_num = user_input
219
+ content.append("Trustee: " + f"I will give {return_num} dollars")
220
+ first_prompt = return_money_prompt.format(
221
+ give=given_num,
222
+ receive=given_num * k,
223
+ N=return_num,
224
+ left=10 - given_num + return_num,
225
+ )
226
+ second_prompt = player_2_end_prompt.format(
227
+ give=given_num,
228
+ receive=given_num * k,
229
+ N=return_num,
230
+ left=given_num * k - return_num,
231
+ )
232
+ player_1_response = player_1.step(
233
+ str_mes(first_prompt + grantee)).msgs[0]
234
+ ans = match_and_compare_numbers_v2(player_1_response.content)
235
+ if ans:
236
+ given_num = ans
237
+ else:
238
+ given_num = extract_unique_decimal(
239
+ cri_agent.step(
240
+ str_mes(player_1_response.content)).msgs[0].content
241
+ )
242
+ money_prompt = money_prompt.format(
243
+ give=given_num, N=given_num * k, left=10 - given_num
244
+ )
245
+
246
+ player_1.record_message(player_1_response)
247
+ player_1_response = player_1_response.content
248
+ content.append("Trustor: "+player_1_response + "\n" +
249
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
250
+ given_num * k))
251
+ return content, given_num
252
+
253
+
254
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
255
+ model_dropdown, api_key_input, round_number_input):
256
+ if api_key_input is None or api_key_input == "":
257
+ api_key_input = os.environ.get("OPENAI_API_KEY")
258
+ else:
259
+ os.environ["OPENAI_API_KEY"] = api_key_input
260
+ random_character = [character_info[trustor_character_dropdown],
261
+ character_info[trustee_character_dropdown]]
262
+ chat_agent = []
263
+ front = "you are a person not an ai model."
264
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
265
+ back = "you need to answer a specific price figure, not a price range!"
266
+ if api_key_input is not None or api_key_input != "":
267
+ openai.api_key = api_key_input
268
+ else:
269
+ openai.api_key = os.getenv("OPENAI_API_KEY")
270
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
271
+ raise ValueError("You can't play with yourself")
272
+ for i in range(len(random_character)):
273
+ sys_prompt = (
274
+ random_character[i]
275
+ + like_people
276
+ + front
277
+ + limited_prompt
278
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
279
+ + back
280
+ )
281
+ model_config = ChatGPTConfig(temperature=temperature_slider)
282
+ chat_agent.append(
283
+ ChatAgent(
284
+ BaseMessage(
285
+ role_name="player",
286
+ role_type=RoleType.USER,
287
+ meta_dict={},
288
+ content=sys_prompt,
289
+ ),
290
+ model_type=model_dict[model_dropdown],
291
+ output_language="English",
292
+ model_config=model_config,
293
+ )
294
+ )
295
+ return chat_agent, "ChatAgent Created Successfully"
296
+
297
+
298
+ def process_interaction(chat_agent_state, round_num_state,
299
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
300
+ identity_dropdown = [trustor, trustee]
301
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
302
+ if round_num_state < round_number_input:
303
+ res, dia, first_prompt, second_prompt = classmate(
304
+ chat_agent_state[0],
305
+ chat_agent_state[1],
306
+ round_num_state == 0,
307
+ first_prompt,
308
+ second_prompt,
309
+ 3,
310
+ )
311
+ dia = "Trustor: "+dia['Player_1'] + \
312
+ "\n" + "Trustee: "+dia['Player_2']
313
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
314
+ round_num_state += 1
315
+ elif identity_dropdown[0] == "Human(You)":
316
+ if round_num_state < round_number_input:
317
+ res, dia, first_prompt, second_prompt = classmate_with_human(
318
+ chat_agent_state[0],
319
+ chat_agent_state[1],
320
+ user_input,
321
+ "trustor",
322
+ round_num_state == 0,
323
+ first_prompt,
324
+ second_prompt,
325
+ 3,
326
+ )
327
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
328
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
329
+ dia['Player_2']+"\n"
330
+ dialog_history_state += dia
331
+ round_num_state += 1
332
+ else:
333
+ if round_num_state < round_number_input:
334
+ dia, given_money = classmate_with_human_trustee(
335
+ chat_agent_state[1],
336
+ chat_agent_state[0],
337
+ user_input,
338
+ "trustee",
339
+ round_num_state == 0,
340
+ given_money,
341
+ 3,
342
+ )
343
+ if round_num_state == 0:
344
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
345
+ else:
346
+ dialog_history_state += f"(Round {round_num_state})" + \
347
+ dia[0] + "\n" + \
348
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
349
+ round_num_state += 1
350
+
351
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
352
+
353
+
354
+ def reset_on_persona_change():
355
+ # Reset values to their initial states
356
+ new_dialog_history = "" # Reset dialog history
357
+ new_round_num_state = 0 # Reset round number state to 0
358
+ new_first_prompt = "" # Reset first prompt
359
+ new_second_prompt = "" # Reset second prompt
360
+ new_given_money = 0 # Reset given money to 0
361
+ # You can add more resets here if needed
362
+
363
+ # Return the new reset values to their respective components
364
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
365
+
366
+
367
+ with gr.Blocks() as app:
368
+ game_introduction = gr.Textbox(
369
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
370
+ 2. You can select the total number of rounds for the game.\n
371
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
372
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
373
+ 5. If you want to reset the conversation, please refresh this page.\n
374
+ """)
375
+ with gr.Row():
376
+ trustor_game_prompt = gr.Textbox(
377
+ label="Trustor Game Prompt", value=prompt['1'])
378
+ trustee_game_prompt = gr.Textbox(
379
+ label="Trustee Game Prompt", value=prompt['2'])
380
+ with gr.Row():
381
+ trustor_character_dropdown = gr.Dropdown(
382
+ choices=characters, label="Select Trustor Persona", value=characters[2])
383
+ trustee_character_dropdown = gr.Dropdown(
384
+ choices=characters, label="Select Trustee Persona", value=characters[2])
385
+ with gr.Row():
386
+ Trustor_info_display = gr.Textbox(
387
+ label="Trustor Persona Info", value=character_info[characters[2]])
388
+ Trustee_info_display = gr.Textbox(
389
+ label="Trustee Persona Info", value=character_info[characters[2]])
390
+ model_dropdown = gr.Dropdown(
391
+ choices=models, label="Select Model Type", value=models[0])
392
+ temperature_slider = gr.Slider(
393
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
394
+ api_key_input = gr.Textbox(
395
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
396
+ submit_button = gr.Button("Create ChatAgent")
397
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
398
+
399
+ round_number_input = gr.Number(label="Total Round Number", value=10)
400
+ round_num_state = gr.State(value=0)
401
+ dialog_history_state = gr.State(value="")
402
+ chat_agent_state = gr.State(value=initial_chat_agent)
403
+ first_prompt = gr.State(value="")
404
+ second_prompt = gr.State(value="")
405
+ given_money = gr.State(value=0)
406
+ user_input = gr.Number(
407
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
408
+ converse_button = gr.Button("Continue Conversation")
409
+
410
+ dialog_display = gr.Textbox(
411
+ label="Dialog History", value="")
412
+
413
+ trustor_character_dropdown.change(
414
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
415
+ trustee_character_dropdown.change(
416
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
417
+
418
+ submit_button.click(
419
+ create_chat_agent,
420
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
421
+ model_dropdown, api_key_input, round_number_input],
422
+ outputs=[chat_agent_state, submit_success_display]
423
+ )
424
+ converse_button.click(
425
+ process_interaction,
426
+ inputs=[chat_agent_state, round_num_state,
427
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
428
+ outputs=[dialog_display, round_num_state,
429
+ first_prompt, second_prompt, dialog_history_state, given_money]
430
+ )
431
+ trustor_character_dropdown.change(
432
+ reset_on_persona_change,
433
+ outputs=[dialog_history_state, round_num_state,
434
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
435
+ )
436
+
437
+ trustee_character_dropdown.change(
438
+ reset_on_persona_change,
439
+ outputs=[dialog_history_state, round_num_state,
440
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
441
+ )
442
+
443
+
444
+ app.launch()
.history/app_20240222100109.py ADDED
@@ -0,0 +1,444 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ initial_dialog_history = []
43
+ initial_round_num = 0
44
+
45
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
46
+
47
+ initial_chat_agent = None
48
+
49
+
50
+ def update_char_info(char):
51
+ return character_info.get(char, "No information available.")
52
+
53
+
54
+ def classmate_with_human(
55
+ player_1,
56
+ player_2,
57
+ user_input,
58
+ user_role,
59
+ first_round,
60
+ first_prompt,
61
+ second_prompt,
62
+ k,
63
+ ):
64
+ first_round_prompt = "This is the first round, answer the question."
65
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
66
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
67
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
68
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
69
+ res = []
70
+ cri_agent = ChatAgent(
71
+ BaseMessage(
72
+ role_name="critic",
73
+ role_type=RoleType.ASSISTANT,
74
+ meta_dict={},
75
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
76
+ ),
77
+ output_language="English",
78
+ # model=ModelType.STUB,
79
+ )
80
+
81
+ if first_round:
82
+ if user_role == "trustor":
83
+ given_num = user_input
84
+ player_1_response = str_mes(
85
+ user_input_prompt_template.format(k=given_num))
86
+ else:
87
+ player_1_response = player_1.step(
88
+ str_mes(first_round_prompt + grantee)).msgs[0]
89
+ ans = match_and_compare_numbers_v2(player_1_response.content)
90
+ if ans:
91
+ given_num = ans
92
+ else:
93
+ given_num = extract_unique_decimal(
94
+ cri_agent.step(
95
+ str_mes(player_1_response.content)).msgs[0].content
96
+ )
97
+
98
+ money_prompt = money_prompt.format(
99
+ give=given_num, N=given_num * k, left=10 - given_num
100
+ )
101
+ if user_role == "trustee":
102
+ player_2_response = str_mes(
103
+ user_input_prompt_template.format(k=given_num * k))
104
+ else:
105
+ player_2_response = player_2.step(
106
+ str_mes(money_prompt + grantee)).msgs[0]
107
+ else:
108
+ if user_role == "trustor":
109
+ given_num = user_input
110
+ player_1_response = str_mes(
111
+ user_input_prompt_template.format(k=given_num))
112
+ else:
113
+ player_1_response = player_1.step(
114
+ str_mes(first_prompt + grantee)).msgs[0]
115
+ # print("player 1 input", first_prompt)
116
+ # print("Player_1_res", player_1_response.content)
117
+ ans = match_and_compare_numbers_v2(player_1_response.content)
118
+ if ans:
119
+ given_num = ans
120
+ else:
121
+ given_num = extract_unique_decimal(
122
+ cri_agent.step(
123
+ str_mes(player_1_response.content)).msgs[0].content
124
+ )
125
+ money_prompt = money_prompt.format(
126
+ give=given_num, N=given_num * k, left=10 - given_num
127
+ )
128
+ if user_role == "trustee":
129
+ player_2_response = str_mes(
130
+ user_input_prompt_template.format(k=given_num * k))
131
+ else:
132
+ player_2_response = player_2.step(
133
+ str_mes(second_prompt + money_prompt + grantee)
134
+ )
135
+ player_2_response = player_2_response.msgs[0]
136
+
137
+ player_1.record_message(player_1_response)
138
+ player_2.record_message(player_2_response)
139
+ player_1_response = player_1_response.content
140
+ player_2_response = player_2_response.content
141
+ dia_history = {
142
+ "Player_1": player_1_response,
143
+ "Player_2": player_2_response,
144
+ }
145
+
146
+ cri_agent.reset()
147
+ ans = match_and_compare_numbers_v2(player_2_response)
148
+ if ans:
149
+ return_num = ans
150
+ else:
151
+ return_num = extract_unique_decimal(
152
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
153
+ )
154
+ return_money_prompt = return_money_prompt.format(
155
+ give=given_num,
156
+ receive=given_num * k,
157
+ N=return_num,
158
+ left=10 - given_num + return_num,
159
+ )
160
+ player_2_end_prompt = player_2_end_prompt.format(
161
+ give=given_num,
162
+ receive=given_num * k,
163
+ N=return_num,
164
+ left=given_num * k - return_num,
165
+ )
166
+ return (
167
+ [given_num, return_num],
168
+ dia_history,
169
+ return_money_prompt,
170
+ player_2_end_prompt,
171
+ )
172
+
173
+
174
+ def classmate_with_human_trustee(
175
+ player_1,
176
+ player_2,
177
+ user_input,
178
+ user_role,
179
+ first_round,
180
+ given_money,
181
+ k,
182
+ ):
183
+ first_round_prompt = "This is the first round, answer the question."
184
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
185
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
186
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
187
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
188
+ res = []
189
+ cri_agent = ChatAgent(
190
+ BaseMessage(
191
+ role_name="critic",
192
+ role_type=RoleType.ASSISTANT,
193
+ meta_dict={},
194
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
195
+ ),
196
+ output_language="English",
197
+ # model=ModelType.STUB,
198
+ )
199
+ content = []
200
+ if first_round:
201
+ content.append("\n")
202
+ player_1_response = player_1.step(
203
+ str_mes(first_round_prompt + grantee)).msgs[0]
204
+ ans = match_and_compare_numbers_v2(player_1_response.content)
205
+ if ans:
206
+ given_num = ans
207
+ else:
208
+ given_num = extract_unique_decimal(
209
+ cri_agent.step(
210
+ str_mes(player_1_response.content)).msgs[0].content
211
+ )
212
+ money_prompt = money_prompt.format(
213
+ give=given_num, N=given_num * k, left=10 - given_num
214
+ )
215
+ else:
216
+ given_num = given_money
217
+
218
+ return_num = user_input
219
+ content.append("Trustee: " + f"I will give {return_num} dollars")
220
+ first_prompt = return_money_prompt.format(
221
+ give=given_num,
222
+ receive=given_num * k,
223
+ N=return_num,
224
+ left=10 - given_num + return_num,
225
+ )
226
+ second_prompt = player_2_end_prompt.format(
227
+ give=given_num,
228
+ receive=given_num * k,
229
+ N=return_num,
230
+ left=given_num * k - return_num,
231
+ )
232
+ player_1_response = player_1.step(
233
+ str_mes(first_prompt + grantee)).msgs[0]
234
+ ans = match_and_compare_numbers_v2(player_1_response.content)
235
+ if ans:
236
+ given_num = ans
237
+ else:
238
+ given_num = extract_unique_decimal(
239
+ cri_agent.step(
240
+ str_mes(player_1_response.content)).msgs[0].content
241
+ )
242
+ money_prompt = money_prompt.format(
243
+ give=given_num, N=given_num * k, left=10 - given_num
244
+ )
245
+
246
+ player_1.record_message(player_1_response)
247
+ player_1_response = player_1_response.content
248
+ content.append("Trustor: "+player_1_response + "\n" +
249
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
250
+ given_num * k))
251
+ return content, given_num
252
+
253
+
254
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
255
+ model_dropdown, api_key_input, round_number_input):
256
+ if api_key_input is None or api_key_input == "":
257
+ api_key_input = os.environ.get("OPENAI_API_KEY")
258
+ else:
259
+ os.environ["OPENAI_API_KEY"] = api_key_input
260
+ random_character = [character_info[trustor_character_dropdown],
261
+ character_info[trustee_character_dropdown]]
262
+ chat_agent = []
263
+ front = "you are a person not an ai model."
264
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
265
+ back = "you need to answer a specific price figure, not a price range!"
266
+ if api_key_input is not None or api_key_input != "":
267
+ openai.api_key = api_key_input
268
+ else:
269
+ openai.api_key = os.getenv("OPENAI_API_KEY")
270
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
271
+ raise ValueError("You can't play with yourself")
272
+ for i in range(len(random_character)):
273
+ sys_prompt = (
274
+ random_character[i]
275
+ + like_people
276
+ + front
277
+ + limited_prompt
278
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
279
+ + back
280
+ )
281
+ model_config = ChatGPTConfig(temperature=temperature_slider)
282
+ chat_agent.append(
283
+ ChatAgent(
284
+ BaseMessage(
285
+ role_name="player",
286
+ role_type=RoleType.USER,
287
+ meta_dict={},
288
+ content=sys_prompt,
289
+ ),
290
+ model_type=model_dict[model_dropdown],
291
+ output_language="English",
292
+ model_config=model_config,
293
+ )
294
+ )
295
+ return chat_agent, "ChatAgent Created Successfully"
296
+
297
+
298
+ def process_interaction(chat_agent_state, round_num_state,
299
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
300
+ identity_dropdown = [trustor, trustee]
301
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
302
+ if round_num_state < round_number_input:
303
+ res, dia, first_prompt, second_prompt = classmate(
304
+ chat_agent_state[0],
305
+ chat_agent_state[1],
306
+ round_num_state == 0,
307
+ first_prompt,
308
+ second_prompt,
309
+ 3,
310
+ )
311
+ dia = "Trustor: "+dia['Player_1'] + \
312
+ "\n" + "Trustee: "+dia['Player_2']
313
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
314
+ round_num_state += 1
315
+ elif identity_dropdown[0] == "Human(You)":
316
+ if round_num_state < round_number_input:
317
+ res, dia, first_prompt, second_prompt = classmate_with_human(
318
+ chat_agent_state[0],
319
+ chat_agent_state[1],
320
+ user_input,
321
+ "trustor",
322
+ round_num_state == 0,
323
+ first_prompt,
324
+ second_prompt,
325
+ 3,
326
+ )
327
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
328
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
329
+ dia['Player_2']+"\n"
330
+ dialog_history_state += dia
331
+ round_num_state += 1
332
+ else:
333
+ if round_num_state < round_number_input:
334
+ dia, given_money = classmate_with_human_trustee(
335
+ chat_agent_state[1],
336
+ chat_agent_state[0],
337
+ user_input,
338
+ "trustee",
339
+ round_num_state == 0,
340
+ given_money,
341
+ 3,
342
+ )
343
+ if round_num_state == 0:
344
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
345
+ else:
346
+ dialog_history_state += f"(Round {round_num_state})" + \
347
+ dia[0] + "\n" + \
348
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
349
+ round_num_state += 1
350
+
351
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
352
+
353
+
354
+ def reset_on_persona_change():
355
+ # Reset values to their initial states
356
+ new_dialog_history = "" # Reset dialog history
357
+ new_round_num_state = 0 # Reset round number state to 0
358
+ new_first_prompt = "" # Reset first prompt
359
+ new_second_prompt = "" # Reset second prompt
360
+ new_given_money = 0 # Reset given money to 0
361
+ # You can add more resets here if needed
362
+
363
+ # Return the new reset values to their respective components
364
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
365
+
366
+
367
+ with gr.Blocks() as app:
368
+ game_introduction = gr.Textbox(
369
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
370
+ 2. You can select the total number of rounds for the game.\n
371
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
372
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
373
+ 5. If you want to reset the conversation, please refresh this page.\n
374
+ 6. """)
375
+ with gr.Row():
376
+ trustor_game_prompt = gr.Textbox(
377
+ label="Trustor Game Prompt", value=prompt['1'])
378
+ trustee_game_prompt = gr.Textbox(
379
+ label="Trustee Game Prompt", value=prompt['2'])
380
+ with gr.Row():
381
+ trustor_character_dropdown = gr.Dropdown(
382
+ choices=characters, label="Select Trustor Persona", value=characters[2])
383
+ trustee_character_dropdown = gr.Dropdown(
384
+ choices=characters, label="Select Trustee Persona", value=characters[2])
385
+ with gr.Row():
386
+ Trustor_info_display = gr.Textbox(
387
+ label="Trustor Persona Info", value=character_info[characters[2]])
388
+ Trustee_info_display = gr.Textbox(
389
+ label="Trustee Persona Info", value=character_info[characters[2]])
390
+ model_dropdown = gr.Dropdown(
391
+ choices=models, label="Select Model Type", value=models[0])
392
+ temperature_slider = gr.Slider(
393
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
394
+ api_key_input = gr.Textbox(
395
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
396
+ submit_button = gr.Button("Create ChatAgent")
397
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
398
+
399
+ round_number_input = gr.Number(label="Total Round Number", value=10)
400
+ round_num_state = gr.State(value=0)
401
+ dialog_history_state = gr.State(value="")
402
+ chat_agent_state = gr.State(value=initial_chat_agent)
403
+ first_prompt = gr.State(value="")
404
+ second_prompt = gr.State(value="")
405
+ given_money = gr.State(value=0)
406
+ user_input = gr.Number(
407
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
408
+ converse_button = gr.Button("Continue Conversation")
409
+
410
+ dialog_display = gr.Textbox(
411
+ label="Dialog History", value="")
412
+
413
+ trustor_character_dropdown.change(
414
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
415
+ trustee_character_dropdown.change(
416
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
417
+
418
+ submit_button.click(
419
+ create_chat_agent,
420
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
421
+ model_dropdown, api_key_input, round_number_input],
422
+ outputs=[chat_agent_state, submit_success_display]
423
+ )
424
+ converse_button.click(
425
+ process_interaction,
426
+ inputs=[chat_agent_state, round_num_state,
427
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
428
+ outputs=[dialog_display, round_num_state,
429
+ first_prompt, second_prompt, dialog_history_state, given_money]
430
+ )
431
+ trustor_character_dropdown.change(
432
+ reset_on_persona_change,
433
+ outputs=[dialog_history_state, round_num_state,
434
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
435
+ )
436
+
437
+ trustee_character_dropdown.change(
438
+ reset_on_persona_change,
439
+ outputs=[dialog_history_state, round_num_state,
440
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
441
+ )
442
+
443
+
444
+ app.launch()
.history/app_20240222100110.py ADDED
@@ -0,0 +1,444 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ initial_dialog_history = []
43
+ initial_round_num = 0
44
+
45
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
46
+
47
+ initial_chat_agent = None
48
+
49
+
50
+ def update_char_info(char):
51
+ return character_info.get(char, "No information available.")
52
+
53
+
54
+ def classmate_with_human(
55
+ player_1,
56
+ player_2,
57
+ user_input,
58
+ user_role,
59
+ first_round,
60
+ first_prompt,
61
+ second_prompt,
62
+ k,
63
+ ):
64
+ first_round_prompt = "This is the first round, answer the question."
65
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
66
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
67
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
68
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
69
+ res = []
70
+ cri_agent = ChatAgent(
71
+ BaseMessage(
72
+ role_name="critic",
73
+ role_type=RoleType.ASSISTANT,
74
+ meta_dict={},
75
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
76
+ ),
77
+ output_language="English",
78
+ # model=ModelType.STUB,
79
+ )
80
+
81
+ if first_round:
82
+ if user_role == "trustor":
83
+ given_num = user_input
84
+ player_1_response = str_mes(
85
+ user_input_prompt_template.format(k=given_num))
86
+ else:
87
+ player_1_response = player_1.step(
88
+ str_mes(first_round_prompt + grantee)).msgs[0]
89
+ ans = match_and_compare_numbers_v2(player_1_response.content)
90
+ if ans:
91
+ given_num = ans
92
+ else:
93
+ given_num = extract_unique_decimal(
94
+ cri_agent.step(
95
+ str_mes(player_1_response.content)).msgs[0].content
96
+ )
97
+
98
+ money_prompt = money_prompt.format(
99
+ give=given_num, N=given_num * k, left=10 - given_num
100
+ )
101
+ if user_role == "trustee":
102
+ player_2_response = str_mes(
103
+ user_input_prompt_template.format(k=given_num * k))
104
+ else:
105
+ player_2_response = player_2.step(
106
+ str_mes(money_prompt + grantee)).msgs[0]
107
+ else:
108
+ if user_role == "trustor":
109
+ given_num = user_input
110
+ player_1_response = str_mes(
111
+ user_input_prompt_template.format(k=given_num))
112
+ else:
113
+ player_1_response = player_1.step(
114
+ str_mes(first_prompt + grantee)).msgs[0]
115
+ # print("player 1 input", first_prompt)
116
+ # print("Player_1_res", player_1_response.content)
117
+ ans = match_and_compare_numbers_v2(player_1_response.content)
118
+ if ans:
119
+ given_num = ans
120
+ else:
121
+ given_num = extract_unique_decimal(
122
+ cri_agent.step(
123
+ str_mes(player_1_response.content)).msgs[0].content
124
+ )
125
+ money_prompt = money_prompt.format(
126
+ give=given_num, N=given_num * k, left=10 - given_num
127
+ )
128
+ if user_role == "trustee":
129
+ player_2_response = str_mes(
130
+ user_input_prompt_template.format(k=given_num * k))
131
+ else:
132
+ player_2_response = player_2.step(
133
+ str_mes(second_prompt + money_prompt + grantee)
134
+ )
135
+ player_2_response = player_2_response.msgs[0]
136
+
137
+ player_1.record_message(player_1_response)
138
+ player_2.record_message(player_2_response)
139
+ player_1_response = player_1_response.content
140
+ player_2_response = player_2_response.content
141
+ dia_history = {
142
+ "Player_1": player_1_response,
143
+ "Player_2": player_2_response,
144
+ }
145
+
146
+ cri_agent.reset()
147
+ ans = match_and_compare_numbers_v2(player_2_response)
148
+ if ans:
149
+ return_num = ans
150
+ else:
151
+ return_num = extract_unique_decimal(
152
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
153
+ )
154
+ return_money_prompt = return_money_prompt.format(
155
+ give=given_num,
156
+ receive=given_num * k,
157
+ N=return_num,
158
+ left=10 - given_num + return_num,
159
+ )
160
+ player_2_end_prompt = player_2_end_prompt.format(
161
+ give=given_num,
162
+ receive=given_num * k,
163
+ N=return_num,
164
+ left=given_num * k - return_num,
165
+ )
166
+ return (
167
+ [given_num, return_num],
168
+ dia_history,
169
+ return_money_prompt,
170
+ player_2_end_prompt,
171
+ )
172
+
173
+
174
+ def classmate_with_human_trustee(
175
+ player_1,
176
+ player_2,
177
+ user_input,
178
+ user_role,
179
+ first_round,
180
+ given_money,
181
+ k,
182
+ ):
183
+ first_round_prompt = "This is the first round, answer the question."
184
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
185
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
186
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
187
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
188
+ res = []
189
+ cri_agent = ChatAgent(
190
+ BaseMessage(
191
+ role_name="critic",
192
+ role_type=RoleType.ASSISTANT,
193
+ meta_dict={},
194
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
195
+ ),
196
+ output_language="English",
197
+ # model=ModelType.STUB,
198
+ )
199
+ content = []
200
+ if first_round:
201
+ content.append("\n")
202
+ player_1_response = player_1.step(
203
+ str_mes(first_round_prompt + grantee)).msgs[0]
204
+ ans = match_and_compare_numbers_v2(player_1_response.content)
205
+ if ans:
206
+ given_num = ans
207
+ else:
208
+ given_num = extract_unique_decimal(
209
+ cri_agent.step(
210
+ str_mes(player_1_response.content)).msgs[0].content
211
+ )
212
+ money_prompt = money_prompt.format(
213
+ give=given_num, N=given_num * k, left=10 - given_num
214
+ )
215
+ else:
216
+ given_num = given_money
217
+
218
+ return_num = user_input
219
+ content.append("Trustee: " + f"I will give {return_num} dollars")
220
+ first_prompt = return_money_prompt.format(
221
+ give=given_num,
222
+ receive=given_num * k,
223
+ N=return_num,
224
+ left=10 - given_num + return_num,
225
+ )
226
+ second_prompt = player_2_end_prompt.format(
227
+ give=given_num,
228
+ receive=given_num * k,
229
+ N=return_num,
230
+ left=given_num * k - return_num,
231
+ )
232
+ player_1_response = player_1.step(
233
+ str_mes(first_prompt + grantee)).msgs[0]
234
+ ans = match_and_compare_numbers_v2(player_1_response.content)
235
+ if ans:
236
+ given_num = ans
237
+ else:
238
+ given_num = extract_unique_decimal(
239
+ cri_agent.step(
240
+ str_mes(player_1_response.content)).msgs[0].content
241
+ )
242
+ money_prompt = money_prompt.format(
243
+ give=given_num, N=given_num * k, left=10 - given_num
244
+ )
245
+
246
+ player_1.record_message(player_1_response)
247
+ player_1_response = player_1_response.content
248
+ content.append("Trustor: "+player_1_response + "\n" +
249
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
250
+ given_num * k))
251
+ return content, given_num
252
+
253
+
254
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
255
+ model_dropdown, api_key_input, round_number_input):
256
+ if api_key_input is None or api_key_input == "":
257
+ api_key_input = os.environ.get("OPENAI_API_KEY")
258
+ else:
259
+ os.environ["OPENAI_API_KEY"] = api_key_input
260
+ random_character = [character_info[trustor_character_dropdown],
261
+ character_info[trustee_character_dropdown]]
262
+ chat_agent = []
263
+ front = "you are a person not an ai model."
264
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
265
+ back = "you need to answer a specific price figure, not a price range!"
266
+ if api_key_input is not None or api_key_input != "":
267
+ openai.api_key = api_key_input
268
+ else:
269
+ openai.api_key = os.getenv("OPENAI_API_KEY")
270
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
271
+ raise ValueError("You can't play with yourself")
272
+ for i in range(len(random_character)):
273
+ sys_prompt = (
274
+ random_character[i]
275
+ + like_people
276
+ + front
277
+ + limited_prompt
278
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
279
+ + back
280
+ )
281
+ model_config = ChatGPTConfig(temperature=temperature_slider)
282
+ chat_agent.append(
283
+ ChatAgent(
284
+ BaseMessage(
285
+ role_name="player",
286
+ role_type=RoleType.USER,
287
+ meta_dict={},
288
+ content=sys_prompt,
289
+ ),
290
+ model_type=model_dict[model_dropdown],
291
+ output_language="English",
292
+ model_config=model_config,
293
+ )
294
+ )
295
+ return chat_agent, "ChatAgent Created Successfully"
296
+
297
+
298
+ def process_interaction(chat_agent_state, round_num_state,
299
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
300
+ identity_dropdown = [trustor, trustee]
301
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
302
+ if round_num_state < round_number_input:
303
+ res, dia, first_prompt, second_prompt = classmate(
304
+ chat_agent_state[0],
305
+ chat_agent_state[1],
306
+ round_num_state == 0,
307
+ first_prompt,
308
+ second_prompt,
309
+ 3,
310
+ )
311
+ dia = "Trustor: "+dia['Player_1'] + \
312
+ "\n" + "Trustee: "+dia['Player_2']
313
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
314
+ round_num_state += 1
315
+ elif identity_dropdown[0] == "Human(You)":
316
+ if round_num_state < round_number_input:
317
+ res, dia, first_prompt, second_prompt = classmate_with_human(
318
+ chat_agent_state[0],
319
+ chat_agent_state[1],
320
+ user_input,
321
+ "trustor",
322
+ round_num_state == 0,
323
+ first_prompt,
324
+ second_prompt,
325
+ 3,
326
+ )
327
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
328
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
329
+ dia['Player_2']+"\n"
330
+ dialog_history_state += dia
331
+ round_num_state += 1
332
+ else:
333
+ if round_num_state < round_number_input:
334
+ dia, given_money = classmate_with_human_trustee(
335
+ chat_agent_state[1],
336
+ chat_agent_state[0],
337
+ user_input,
338
+ "trustee",
339
+ round_num_state == 0,
340
+ given_money,
341
+ 3,
342
+ )
343
+ if round_num_state == 0:
344
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
345
+ else:
346
+ dialog_history_state += f"(Round {round_num_state})" + \
347
+ dia[0] + "\n" + \
348
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
349
+ round_num_state += 1
350
+
351
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
352
+
353
+
354
+ def reset_on_persona_change():
355
+ # Reset values to their initial states
356
+ new_dialog_history = "" # Reset dialog history
357
+ new_round_num_state = 0 # Reset round number state to 0
358
+ new_first_prompt = "" # Reset first prompt
359
+ new_second_prompt = "" # Reset second prompt
360
+ new_given_money = 0 # Reset given money to 0
361
+ # You can add more resets here if needed
362
+
363
+ # Return the new reset values to their respective components
364
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
365
+
366
+
367
+ with gr.Blocks() as app:
368
+ game_introduction = gr.Textbox(
369
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
370
+ 2. You can select the total number of rounds for the game.\n
371
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
372
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
373
+ 5. If you want to reset the conversation, please refresh this page.\n
374
+ 6. You need to fill in your OpenAI API Key""")
375
+ with gr.Row():
376
+ trustor_game_prompt = gr.Textbox(
377
+ label="Trustor Game Prompt", value=prompt['1'])
378
+ trustee_game_prompt = gr.Textbox(
379
+ label="Trustee Game Prompt", value=prompt['2'])
380
+ with gr.Row():
381
+ trustor_character_dropdown = gr.Dropdown(
382
+ choices=characters, label="Select Trustor Persona", value=characters[2])
383
+ trustee_character_dropdown = gr.Dropdown(
384
+ choices=characters, label="Select Trustee Persona", value=characters[2])
385
+ with gr.Row():
386
+ Trustor_info_display = gr.Textbox(
387
+ label="Trustor Persona Info", value=character_info[characters[2]])
388
+ Trustee_info_display = gr.Textbox(
389
+ label="Trustee Persona Info", value=character_info[characters[2]])
390
+ model_dropdown = gr.Dropdown(
391
+ choices=models, label="Select Model Type", value=models[0])
392
+ temperature_slider = gr.Slider(
393
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
394
+ api_key_input = gr.Textbox(
395
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
396
+ submit_button = gr.Button("Create ChatAgent")
397
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
398
+
399
+ round_number_input = gr.Number(label="Total Round Number", value=10)
400
+ round_num_state = gr.State(value=0)
401
+ dialog_history_state = gr.State(value="")
402
+ chat_agent_state = gr.State(value=initial_chat_agent)
403
+ first_prompt = gr.State(value="")
404
+ second_prompt = gr.State(value="")
405
+ given_money = gr.State(value=0)
406
+ user_input = gr.Number(
407
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
408
+ converse_button = gr.Button("Continue Conversation")
409
+
410
+ dialog_display = gr.Textbox(
411
+ label="Dialog History", value="")
412
+
413
+ trustor_character_dropdown.change(
414
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
415
+ trustee_character_dropdown.change(
416
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
417
+
418
+ submit_button.click(
419
+ create_chat_agent,
420
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
421
+ model_dropdown, api_key_input, round_number_input],
422
+ outputs=[chat_agent_state, submit_success_display]
423
+ )
424
+ converse_button.click(
425
+ process_interaction,
426
+ inputs=[chat_agent_state, round_num_state,
427
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
428
+ outputs=[dialog_display, round_num_state,
429
+ first_prompt, second_prompt, dialog_history_state, given_money]
430
+ )
431
+ trustor_character_dropdown.change(
432
+ reset_on_persona_change,
433
+ outputs=[dialog_history_state, round_num_state,
434
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
435
+ )
436
+
437
+ trustee_character_dropdown.change(
438
+ reset_on_persona_change,
439
+ outputs=[dialog_history_state, round_num_state,
440
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
441
+ )
442
+
443
+
444
+ app.launch()
.history/app_20240222100136.py ADDED
@@ -0,0 +1,444 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ initial_dialog_history = []
43
+ initial_round_num = 0
44
+
45
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
46
+
47
+ initial_chat_agent = None
48
+
49
+
50
+ def update_char_info(char):
51
+ return character_info.get(char, "No information available.")
52
+
53
+
54
+ def classmate_with_human(
55
+ player_1,
56
+ player_2,
57
+ user_input,
58
+ user_role,
59
+ first_round,
60
+ first_prompt,
61
+ second_prompt,
62
+ k,
63
+ ):
64
+ first_round_prompt = "This is the first round, answer the question."
65
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
66
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
67
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
68
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
69
+ res = []
70
+ cri_agent = ChatAgent(
71
+ BaseMessage(
72
+ role_name="critic",
73
+ role_type=RoleType.ASSISTANT,
74
+ meta_dict={},
75
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
76
+ ),
77
+ output_language="English",
78
+ # model=ModelType.STUB,
79
+ )
80
+
81
+ if first_round:
82
+ if user_role == "trustor":
83
+ given_num = user_input
84
+ player_1_response = str_mes(
85
+ user_input_prompt_template.format(k=given_num))
86
+ else:
87
+ player_1_response = player_1.step(
88
+ str_mes(first_round_prompt + grantee)).msgs[0]
89
+ ans = match_and_compare_numbers_v2(player_1_response.content)
90
+ if ans:
91
+ given_num = ans
92
+ else:
93
+ given_num = extract_unique_decimal(
94
+ cri_agent.step(
95
+ str_mes(player_1_response.content)).msgs[0].content
96
+ )
97
+
98
+ money_prompt = money_prompt.format(
99
+ give=given_num, N=given_num * k, left=10 - given_num
100
+ )
101
+ if user_role == "trustee":
102
+ player_2_response = str_mes(
103
+ user_input_prompt_template.format(k=given_num * k))
104
+ else:
105
+ player_2_response = player_2.step(
106
+ str_mes(money_prompt + grantee)).msgs[0]
107
+ else:
108
+ if user_role == "trustor":
109
+ given_num = user_input
110
+ player_1_response = str_mes(
111
+ user_input_prompt_template.format(k=given_num))
112
+ else:
113
+ player_1_response = player_1.step(
114
+ str_mes(first_prompt + grantee)).msgs[0]
115
+ # print("player 1 input", first_prompt)
116
+ # print("Player_1_res", player_1_response.content)
117
+ ans = match_and_compare_numbers_v2(player_1_response.content)
118
+ if ans:
119
+ given_num = ans
120
+ else:
121
+ given_num = extract_unique_decimal(
122
+ cri_agent.step(
123
+ str_mes(player_1_response.content)).msgs[0].content
124
+ )
125
+ money_prompt = money_prompt.format(
126
+ give=given_num, N=given_num * k, left=10 - given_num
127
+ )
128
+ if user_role == "trustee":
129
+ player_2_response = str_mes(
130
+ user_input_prompt_template.format(k=given_num * k))
131
+ else:
132
+ player_2_response = player_2.step(
133
+ str_mes(second_prompt + money_prompt + grantee)
134
+ )
135
+ player_2_response = player_2_response.msgs[0]
136
+
137
+ player_1.record_message(player_1_response)
138
+ player_2.record_message(player_2_response)
139
+ player_1_response = player_1_response.content
140
+ player_2_response = player_2_response.content
141
+ dia_history = {
142
+ "Player_1": player_1_response,
143
+ "Player_2": player_2_response,
144
+ }
145
+
146
+ cri_agent.reset()
147
+ ans = match_and_compare_numbers_v2(player_2_response)
148
+ if ans:
149
+ return_num = ans
150
+ else:
151
+ return_num = extract_unique_decimal(
152
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
153
+ )
154
+ return_money_prompt = return_money_prompt.format(
155
+ give=given_num,
156
+ receive=given_num * k,
157
+ N=return_num,
158
+ left=10 - given_num + return_num,
159
+ )
160
+ player_2_end_prompt = player_2_end_prompt.format(
161
+ give=given_num,
162
+ receive=given_num * k,
163
+ N=return_num,
164
+ left=given_num * k - return_num,
165
+ )
166
+ return (
167
+ [given_num, return_num],
168
+ dia_history,
169
+ return_money_prompt,
170
+ player_2_end_prompt,
171
+ )
172
+
173
+
174
+ def classmate_with_human_trustee(
175
+ player_1,
176
+ player_2,
177
+ user_input,
178
+ user_role,
179
+ first_round,
180
+ given_money,
181
+ k,
182
+ ):
183
+ first_round_prompt = "This is the first round, answer the question."
184
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
185
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
186
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
187
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
188
+ res = []
189
+ cri_agent = ChatAgent(
190
+ BaseMessage(
191
+ role_name="critic",
192
+ role_type=RoleType.ASSISTANT,
193
+ meta_dict={},
194
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
195
+ ),
196
+ output_language="English",
197
+ # model=ModelType.STUB,
198
+ )
199
+ content = []
200
+ if first_round:
201
+ content.append("\n")
202
+ player_1_response = player_1.step(
203
+ str_mes(first_round_prompt + grantee)).msgs[0]
204
+ ans = match_and_compare_numbers_v2(player_1_response.content)
205
+ if ans:
206
+ given_num = ans
207
+ else:
208
+ given_num = extract_unique_decimal(
209
+ cri_agent.step(
210
+ str_mes(player_1_response.content)).msgs[0].content
211
+ )
212
+ money_prompt = money_prompt.format(
213
+ give=given_num, N=given_num * k, left=10 - given_num
214
+ )
215
+ else:
216
+ given_num = given_money
217
+
218
+ return_num = user_input
219
+ content.append("Trustee: " + f"I will give {return_num} dollars")
220
+ first_prompt = return_money_prompt.format(
221
+ give=given_num,
222
+ receive=given_num * k,
223
+ N=return_num,
224
+ left=10 - given_num + return_num,
225
+ )
226
+ second_prompt = player_2_end_prompt.format(
227
+ give=given_num,
228
+ receive=given_num * k,
229
+ N=return_num,
230
+ left=given_num * k - return_num,
231
+ )
232
+ player_1_response = player_1.step(
233
+ str_mes(first_prompt + grantee)).msgs[0]
234
+ ans = match_and_compare_numbers_v2(player_1_response.content)
235
+ if ans:
236
+ given_num = ans
237
+ else:
238
+ given_num = extract_unique_decimal(
239
+ cri_agent.step(
240
+ str_mes(player_1_response.content)).msgs[0].content
241
+ )
242
+ money_prompt = money_prompt.format(
243
+ give=given_num, N=given_num * k, left=10 - given_num
244
+ )
245
+
246
+ player_1.record_message(player_1_response)
247
+ player_1_response = player_1_response.content
248
+ content.append("Trustor: "+player_1_response + "\n" +
249
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
250
+ given_num * k))
251
+ return content, given_num
252
+
253
+
254
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
255
+ model_dropdown, api_key_input, round_number_input):
256
+ if api_key_input is None or api_key_input == "":
257
+ api_key_input = os.environ.get("OPENAI_API_KEY")
258
+ else:
259
+ os.environ["OPENAI_API_KEY"] = api_key_input
260
+ random_character = [character_info[trustor_character_dropdown],
261
+ character_info[trustee_character_dropdown]]
262
+ chat_agent = []
263
+ front = "you are a person not an ai model."
264
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
265
+ back = "you need to answer a specific price figure, not a price range!"
266
+ if api_key_input is not None or api_key_input != "":
267
+ openai.api_key = api_key_input
268
+ else:
269
+ openai.api_key = os.getenv("OPENAI_API_KEY")
270
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
271
+ raise ValueError("You can't play with yourself")
272
+ for i in range(len(random_character)):
273
+ sys_prompt = (
274
+ random_character[i]
275
+ + like_people
276
+ + front
277
+ + limited_prompt
278
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
279
+ + back
280
+ )
281
+ model_config = ChatGPTConfig(temperature=temperature_slider)
282
+ chat_agent.append(
283
+ ChatAgent(
284
+ BaseMessage(
285
+ role_name="player",
286
+ role_type=RoleType.USER,
287
+ meta_dict={},
288
+ content=sys_prompt,
289
+ ),
290
+ model_type=model_dict[model_dropdown],
291
+ output_language="English",
292
+ model_config=model_config,
293
+ )
294
+ )
295
+ return chat_agent, "ChatAgent Created Successfully"
296
+
297
+
298
+ def process_interaction(chat_agent_state, round_num_state,
299
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
300
+ identity_dropdown = [trustor, trustee]
301
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
302
+ if round_num_state < round_number_input:
303
+ res, dia, first_prompt, second_prompt = classmate(
304
+ chat_agent_state[0],
305
+ chat_agent_state[1],
306
+ round_num_state == 0,
307
+ first_prompt,
308
+ second_prompt,
309
+ 3,
310
+ )
311
+ dia = "Trustor: "+dia['Player_1'] + \
312
+ "\n" + "Trustee: "+dia['Player_2']
313
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
314
+ round_num_state += 1
315
+ elif identity_dropdown[0] == "Human(You)":
316
+ if round_num_state < round_number_input:
317
+ res, dia, first_prompt, second_prompt = classmate_with_human(
318
+ chat_agent_state[0],
319
+ chat_agent_state[1],
320
+ user_input,
321
+ "trustor",
322
+ round_num_state == 0,
323
+ first_prompt,
324
+ second_prompt,
325
+ 3,
326
+ )
327
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
328
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
329
+ dia['Player_2']+"\n"
330
+ dialog_history_state += dia
331
+ round_num_state += 1
332
+ else:
333
+ if round_num_state < round_number_input:
334
+ dia, given_money = classmate_with_human_trustee(
335
+ chat_agent_state[1],
336
+ chat_agent_state[0],
337
+ user_input,
338
+ "trustee",
339
+ round_num_state == 0,
340
+ given_money,
341
+ 3,
342
+ )
343
+ if round_num_state == 0:
344
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
345
+ else:
346
+ dialog_history_state += f"(Round {round_num_state})" + \
347
+ dia[0] + "\n" + \
348
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
349
+ round_num_state += 1
350
+
351
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
352
+
353
+
354
+ def reset_on_persona_change():
355
+ # Reset values to their initial states
356
+ new_dialog_history = "" # Reset dialog history
357
+ new_round_num_state = 0 # Reset round number state to 0
358
+ new_first_prompt = "" # Reset first prompt
359
+ new_second_prompt = "" # Reset second prompt
360
+ new_given_money = 0 # Reset given money to 0
361
+ # You can add more resets here if needed
362
+
363
+ # Return the new reset values to their respective components
364
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
365
+
366
+
367
+ with gr.Blocks() as app:
368
+ game_introduction = gr.Textbox(
369
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
370
+ 2. You can select the total number of rounds for the game.\n
371
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
372
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
373
+ 5. If you want to reset the conversation, please refresh this page.\n
374
+ """)
375
+ with gr.Row():
376
+ trustor_game_prompt = gr.Textbox(
377
+ label="Trustor Game Prompt", value=prompt['1'])
378
+ trustee_game_prompt = gr.Textbox(
379
+ label="Trustee Game Prompt", value=prompt['2'])
380
+ with gr.Row():
381
+ trustor_character_dropdown = gr.Dropdown(
382
+ choices=characters, label="Select Trustor Persona", value=characters[2])
383
+ trustee_character_dropdown = gr.Dropdown(
384
+ choices=characters, label="Select Trustee Persona", value=characters[2])
385
+ with gr.Row():
386
+ Trustor_info_display = gr.Textbox(
387
+ label="Trustor Persona Info", value=character_info[characters[2]])
388
+ Trustee_info_display = gr.Textbox(
389
+ label="Trustee Persona Info", value=character_info[characters[2]])
390
+ model_dropdown = gr.Dropdown(
391
+ choices=models, label="Select Model Type", value=models[0])
392
+ temperature_slider = gr.Slider(
393
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
394
+ api_key_input = gr.Textbox(
395
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
396
+ submit_button = gr.Button("Create ChatAgent")
397
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
398
+
399
+ round_number_input = gr.Number(label="Total Round Number", value=10)
400
+ round_num_state = gr.State(value=0)
401
+ dialog_history_state = gr.State(value="")
402
+ chat_agent_state = gr.State(value=initial_chat_agent)
403
+ first_prompt = gr.State(value="")
404
+ second_prompt = gr.State(value="")
405
+ given_money = gr.State(value=0)
406
+ user_input = gr.Number(
407
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
408
+ converse_button = gr.Button("Continue Conversation")
409
+
410
+ dialog_display = gr.Textbox(
411
+ label="Dialog History", value="")
412
+
413
+ trustor_character_dropdown.change(
414
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
415
+ trustee_character_dropdown.change(
416
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
417
+
418
+ submit_button.click(
419
+ create_chat_agent,
420
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
421
+ model_dropdown, api_key_input, round_number_input],
422
+ outputs=[chat_agent_state, submit_success_display]
423
+ )
424
+ converse_button.click(
425
+ process_interaction,
426
+ inputs=[chat_agent_state, round_num_state,
427
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
428
+ outputs=[dialog_display, round_num_state,
429
+ first_prompt, second_prompt, dialog_history_state, given_money]
430
+ )
431
+ trustor_character_dropdown.change(
432
+ reset_on_persona_change,
433
+ outputs=[dialog_history_state, round_num_state,
434
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
435
+ )
436
+
437
+ trustee_character_dropdown.change(
438
+ reset_on_persona_change,
439
+ outputs=[dialog_history_state, round_num_state,
440
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
441
+ )
442
+
443
+
444
+ app.launch()
.history/app_20240222100138.py ADDED
@@ -0,0 +1,443 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ initial_dialog_history = []
43
+ initial_round_num = 0
44
+
45
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
46
+
47
+ initial_chat_agent = None
48
+
49
+
50
+ def update_char_info(char):
51
+ return character_info.get(char, "No information available.")
52
+
53
+
54
+ def classmate_with_human(
55
+ player_1,
56
+ player_2,
57
+ user_input,
58
+ user_role,
59
+ first_round,
60
+ first_prompt,
61
+ second_prompt,
62
+ k,
63
+ ):
64
+ first_round_prompt = "This is the first round, answer the question."
65
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
66
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
67
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
68
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
69
+ res = []
70
+ cri_agent = ChatAgent(
71
+ BaseMessage(
72
+ role_name="critic",
73
+ role_type=RoleType.ASSISTANT,
74
+ meta_dict={},
75
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
76
+ ),
77
+ output_language="English",
78
+ # model=ModelType.STUB,
79
+ )
80
+
81
+ if first_round:
82
+ if user_role == "trustor":
83
+ given_num = user_input
84
+ player_1_response = str_mes(
85
+ user_input_prompt_template.format(k=given_num))
86
+ else:
87
+ player_1_response = player_1.step(
88
+ str_mes(first_round_prompt + grantee)).msgs[0]
89
+ ans = match_and_compare_numbers_v2(player_1_response.content)
90
+ if ans:
91
+ given_num = ans
92
+ else:
93
+ given_num = extract_unique_decimal(
94
+ cri_agent.step(
95
+ str_mes(player_1_response.content)).msgs[0].content
96
+ )
97
+
98
+ money_prompt = money_prompt.format(
99
+ give=given_num, N=given_num * k, left=10 - given_num
100
+ )
101
+ if user_role == "trustee":
102
+ player_2_response = str_mes(
103
+ user_input_prompt_template.format(k=given_num * k))
104
+ else:
105
+ player_2_response = player_2.step(
106
+ str_mes(money_prompt + grantee)).msgs[0]
107
+ else:
108
+ if user_role == "trustor":
109
+ given_num = user_input
110
+ player_1_response = str_mes(
111
+ user_input_prompt_template.format(k=given_num))
112
+ else:
113
+ player_1_response = player_1.step(
114
+ str_mes(first_prompt + grantee)).msgs[0]
115
+ # print("player 1 input", first_prompt)
116
+ # print("Player_1_res", player_1_response.content)
117
+ ans = match_and_compare_numbers_v2(player_1_response.content)
118
+ if ans:
119
+ given_num = ans
120
+ else:
121
+ given_num = extract_unique_decimal(
122
+ cri_agent.step(
123
+ str_mes(player_1_response.content)).msgs[0].content
124
+ )
125
+ money_prompt = money_prompt.format(
126
+ give=given_num, N=given_num * k, left=10 - given_num
127
+ )
128
+ if user_role == "trustee":
129
+ player_2_response = str_mes(
130
+ user_input_prompt_template.format(k=given_num * k))
131
+ else:
132
+ player_2_response = player_2.step(
133
+ str_mes(second_prompt + money_prompt + grantee)
134
+ )
135
+ player_2_response = player_2_response.msgs[0]
136
+
137
+ player_1.record_message(player_1_response)
138
+ player_2.record_message(player_2_response)
139
+ player_1_response = player_1_response.content
140
+ player_2_response = player_2_response.content
141
+ dia_history = {
142
+ "Player_1": player_1_response,
143
+ "Player_2": player_2_response,
144
+ }
145
+
146
+ cri_agent.reset()
147
+ ans = match_and_compare_numbers_v2(player_2_response)
148
+ if ans:
149
+ return_num = ans
150
+ else:
151
+ return_num = extract_unique_decimal(
152
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
153
+ )
154
+ return_money_prompt = return_money_prompt.format(
155
+ give=given_num,
156
+ receive=given_num * k,
157
+ N=return_num,
158
+ left=10 - given_num + return_num,
159
+ )
160
+ player_2_end_prompt = player_2_end_prompt.format(
161
+ give=given_num,
162
+ receive=given_num * k,
163
+ N=return_num,
164
+ left=given_num * k - return_num,
165
+ )
166
+ return (
167
+ [given_num, return_num],
168
+ dia_history,
169
+ return_money_prompt,
170
+ player_2_end_prompt,
171
+ )
172
+
173
+
174
+ def classmate_with_human_trustee(
175
+ player_1,
176
+ player_2,
177
+ user_input,
178
+ user_role,
179
+ first_round,
180
+ given_money,
181
+ k,
182
+ ):
183
+ first_round_prompt = "This is the first round, answer the question."
184
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
185
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
186
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
187
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
188
+ res = []
189
+ cri_agent = ChatAgent(
190
+ BaseMessage(
191
+ role_name="critic",
192
+ role_type=RoleType.ASSISTANT,
193
+ meta_dict={},
194
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
195
+ ),
196
+ output_language="English",
197
+ # model=ModelType.STUB,
198
+ )
199
+ content = []
200
+ if first_round:
201
+ content.append("\n")
202
+ player_1_response = player_1.step(
203
+ str_mes(first_round_prompt + grantee)).msgs[0]
204
+ ans = match_and_compare_numbers_v2(player_1_response.content)
205
+ if ans:
206
+ given_num = ans
207
+ else:
208
+ given_num = extract_unique_decimal(
209
+ cri_agent.step(
210
+ str_mes(player_1_response.content)).msgs[0].content
211
+ )
212
+ money_prompt = money_prompt.format(
213
+ give=given_num, N=given_num * k, left=10 - given_num
214
+ )
215
+ else:
216
+ given_num = given_money
217
+
218
+ return_num = user_input
219
+ content.append("Trustee: " + f"I will give {return_num} dollars")
220
+ first_prompt = return_money_prompt.format(
221
+ give=given_num,
222
+ receive=given_num * k,
223
+ N=return_num,
224
+ left=10 - given_num + return_num,
225
+ )
226
+ second_prompt = player_2_end_prompt.format(
227
+ give=given_num,
228
+ receive=given_num * k,
229
+ N=return_num,
230
+ left=given_num * k - return_num,
231
+ )
232
+ player_1_response = player_1.step(
233
+ str_mes(first_prompt + grantee)).msgs[0]
234
+ ans = match_and_compare_numbers_v2(player_1_response.content)
235
+ if ans:
236
+ given_num = ans
237
+ else:
238
+ given_num = extract_unique_decimal(
239
+ cri_agent.step(
240
+ str_mes(player_1_response.content)).msgs[0].content
241
+ )
242
+ money_prompt = money_prompt.format(
243
+ give=given_num, N=given_num * k, left=10 - given_num
244
+ )
245
+
246
+ player_1.record_message(player_1_response)
247
+ player_1_response = player_1_response.content
248
+ content.append("Trustor: "+player_1_response + "\n" +
249
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
250
+ given_num * k))
251
+ return content, given_num
252
+
253
+
254
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
255
+ model_dropdown, api_key_input, round_number_input):
256
+ if api_key_input is None or api_key_input == "":
257
+ api_key_input = os.environ.get("OPENAI_API_KEY")
258
+ else:
259
+ os.environ["OPENAI_API_KEY"] = api_key_input
260
+ random_character = [character_info[trustor_character_dropdown],
261
+ character_info[trustee_character_dropdown]]
262
+ chat_agent = []
263
+ front = "you are a person not an ai model."
264
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
265
+ back = "you need to answer a specific price figure, not a price range!"
266
+ if api_key_input is not None or api_key_input != "":
267
+ openai.api_key = api_key_input
268
+ else:
269
+ openai.api_key = os.getenv("OPENAI_API_KEY")
270
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
271
+ raise ValueError("You can't play with yourself")
272
+ for i in range(len(random_character)):
273
+ sys_prompt = (
274
+ random_character[i]
275
+ + like_people
276
+ + front
277
+ + limited_prompt
278
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
279
+ + back
280
+ )
281
+ model_config = ChatGPTConfig(temperature=temperature_slider)
282
+ chat_agent.append(
283
+ ChatAgent(
284
+ BaseMessage(
285
+ role_name="player",
286
+ role_type=RoleType.USER,
287
+ meta_dict={},
288
+ content=sys_prompt,
289
+ ),
290
+ model_type=model_dict[model_dropdown],
291
+ output_language="English",
292
+ model_config=model_config,
293
+ )
294
+ )
295
+ return chat_agent, "ChatAgent Created Successfully"
296
+
297
+
298
+ def process_interaction(chat_agent_state, round_num_state,
299
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
300
+ identity_dropdown = [trustor, trustee]
301
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
302
+ if round_num_state < round_number_input:
303
+ res, dia, first_prompt, second_prompt = classmate(
304
+ chat_agent_state[0],
305
+ chat_agent_state[1],
306
+ round_num_state == 0,
307
+ first_prompt,
308
+ second_prompt,
309
+ 3,
310
+ )
311
+ dia = "Trustor: "+dia['Player_1'] + \
312
+ "\n" + "Trustee: "+dia['Player_2']
313
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
314
+ round_num_state += 1
315
+ elif identity_dropdown[0] == "Human(You)":
316
+ if round_num_state < round_number_input:
317
+ res, dia, first_prompt, second_prompt = classmate_with_human(
318
+ chat_agent_state[0],
319
+ chat_agent_state[1],
320
+ user_input,
321
+ "trustor",
322
+ round_num_state == 0,
323
+ first_prompt,
324
+ second_prompt,
325
+ 3,
326
+ )
327
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
328
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
329
+ dia['Player_2']+"\n"
330
+ dialog_history_state += dia
331
+ round_num_state += 1
332
+ else:
333
+ if round_num_state < round_number_input:
334
+ dia, given_money = classmate_with_human_trustee(
335
+ chat_agent_state[1],
336
+ chat_agent_state[0],
337
+ user_input,
338
+ "trustee",
339
+ round_num_state == 0,
340
+ given_money,
341
+ 3,
342
+ )
343
+ if round_num_state == 0:
344
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
345
+ else:
346
+ dialog_history_state += f"(Round {round_num_state})" + \
347
+ dia[0] + "\n" + \
348
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
349
+ round_num_state += 1
350
+
351
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
352
+
353
+
354
+ def reset_on_persona_change():
355
+ # Reset values to their initial states
356
+ new_dialog_history = "" # Reset dialog history
357
+ new_round_num_state = 0 # Reset round number state to 0
358
+ new_first_prompt = "" # Reset first prompt
359
+ new_second_prompt = "" # Reset second prompt
360
+ new_given_money = 0 # Reset given money to 0
361
+ # You can add more resets here if needed
362
+
363
+ # Return the new reset values to their respective components
364
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
365
+
366
+
367
+ with gr.Blocks() as app:
368
+ game_introduction = gr.Textbox(
369
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
370
+ 2. You can select the total number of rounds for the game.\n
371
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
372
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
373
+ 5. If you want to reset the conversation, please refresh this page.\n""")
374
+ with gr.Row():
375
+ trustor_game_prompt = gr.Textbox(
376
+ label="Trustor Game Prompt", value=prompt['1'])
377
+ trustee_game_prompt = gr.Textbox(
378
+ label="Trustee Game Prompt", value=prompt['2'])
379
+ with gr.Row():
380
+ trustor_character_dropdown = gr.Dropdown(
381
+ choices=characters, label="Select Trustor Persona", value=characters[2])
382
+ trustee_character_dropdown = gr.Dropdown(
383
+ choices=characters, label="Select Trustee Persona", value=characters[2])
384
+ with gr.Row():
385
+ Trustor_info_display = gr.Textbox(
386
+ label="Trustor Persona Info", value=character_info[characters[2]])
387
+ Trustee_info_display = gr.Textbox(
388
+ label="Trustee Persona Info", value=character_info[characters[2]])
389
+ model_dropdown = gr.Dropdown(
390
+ choices=models, label="Select Model Type", value=models[0])
391
+ temperature_slider = gr.Slider(
392
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
393
+ api_key_input = gr.Textbox(
394
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
395
+ submit_button = gr.Button("Create ChatAgent")
396
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
397
+
398
+ round_number_input = gr.Number(label="Total Round Number", value=10)
399
+ round_num_state = gr.State(value=0)
400
+ dialog_history_state = gr.State(value="")
401
+ chat_agent_state = gr.State(value=initial_chat_agent)
402
+ first_prompt = gr.State(value="")
403
+ second_prompt = gr.State(value="")
404
+ given_money = gr.State(value=0)
405
+ user_input = gr.Number(
406
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
407
+ converse_button = gr.Button("Continue Conversation")
408
+
409
+ dialog_display = gr.Textbox(
410
+ label="Dialog History", value="")
411
+
412
+ trustor_character_dropdown.change(
413
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
414
+ trustee_character_dropdown.change(
415
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
416
+
417
+ submit_button.click(
418
+ create_chat_agent,
419
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
420
+ model_dropdown, api_key_input, round_number_input],
421
+ outputs=[chat_agent_state, submit_success_display]
422
+ )
423
+ converse_button.click(
424
+ process_interaction,
425
+ inputs=[chat_agent_state, round_num_state,
426
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
427
+ outputs=[dialog_display, round_num_state,
428
+ first_prompt, second_prompt, dialog_history_state, given_money]
429
+ )
430
+ trustor_character_dropdown.change(
431
+ reset_on_persona_change,
432
+ outputs=[dialog_history_state, round_num_state,
433
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
434
+ )
435
+
436
+ trustee_character_dropdown.change(
437
+ reset_on_persona_change,
438
+ outputs=[dialog_history_state, round_num_state,
439
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
440
+ )
441
+
442
+
443
+ app.launch()
.history/app_20240222100145.py ADDED
@@ -0,0 +1,443 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ initial_dialog_history = []
43
+ initial_round_num = 0
44
+
45
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
46
+
47
+ initial_chat_agent = None
48
+
49
+
50
+ def update_char_info(char):
51
+ return character_info.get(char, "No information available.")
52
+
53
+
54
+ def classmate_with_human(
55
+ player_1,
56
+ player_2,
57
+ user_input,
58
+ user_role,
59
+ first_round,
60
+ first_prompt,
61
+ second_prompt,
62
+ k,
63
+ ):
64
+ first_round_prompt = "This is the first round, answer the question."
65
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
66
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
67
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
68
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
69
+ res = []
70
+ cri_agent = ChatAgent(
71
+ BaseMessage(
72
+ role_name="critic",
73
+ role_type=RoleType.ASSISTANT,
74
+ meta_dict={},
75
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
76
+ ),
77
+ output_language="English",
78
+ # model=ModelType.STUB,
79
+ )
80
+
81
+ if first_round:
82
+ if user_role == "trustor":
83
+ given_num = user_input
84
+ player_1_response = str_mes(
85
+ user_input_prompt_template.format(k=given_num))
86
+ else:
87
+ player_1_response = player_1.step(
88
+ str_mes(first_round_prompt + grantee)).msgs[0]
89
+ ans = match_and_compare_numbers_v2(player_1_response.content)
90
+ if ans:
91
+ given_num = ans
92
+ else:
93
+ given_num = extract_unique_decimal(
94
+ cri_agent.step(
95
+ str_mes(player_1_response.content)).msgs[0].content
96
+ )
97
+
98
+ money_prompt = money_prompt.format(
99
+ give=given_num, N=given_num * k, left=10 - given_num
100
+ )
101
+ if user_role == "trustee":
102
+ player_2_response = str_mes(
103
+ user_input_prompt_template.format(k=given_num * k))
104
+ else:
105
+ player_2_response = player_2.step(
106
+ str_mes(money_prompt + grantee)).msgs[0]
107
+ else:
108
+ if user_role == "trustor":
109
+ given_num = user_input
110
+ player_1_response = str_mes(
111
+ user_input_prompt_template.format(k=given_num))
112
+ else:
113
+ player_1_response = player_1.step(
114
+ str_mes(first_prompt + grantee)).msgs[0]
115
+ # print("player 1 input", first_prompt)
116
+ # print("Player_1_res", player_1_response.content)
117
+ ans = match_and_compare_numbers_v2(player_1_response.content)
118
+ if ans:
119
+ given_num = ans
120
+ else:
121
+ given_num = extract_unique_decimal(
122
+ cri_agent.step(
123
+ str_mes(player_1_response.content)).msgs[0].content
124
+ )
125
+ money_prompt = money_prompt.format(
126
+ give=given_num, N=given_num * k, left=10 - given_num
127
+ )
128
+ if user_role == "trustee":
129
+ player_2_response = str_mes(
130
+ user_input_prompt_template.format(k=given_num * k))
131
+ else:
132
+ player_2_response = player_2.step(
133
+ str_mes(second_prompt + money_prompt + grantee)
134
+ )
135
+ player_2_response = player_2_response.msgs[0]
136
+
137
+ player_1.record_message(player_1_response)
138
+ player_2.record_message(player_2_response)
139
+ player_1_response = player_1_response.content
140
+ player_2_response = player_2_response.content
141
+ dia_history = {
142
+ "Player_1": player_1_response,
143
+ "Player_2": player_2_response,
144
+ }
145
+
146
+ cri_agent.reset()
147
+ ans = match_and_compare_numbers_v2(player_2_response)
148
+ if ans:
149
+ return_num = ans
150
+ else:
151
+ return_num = extract_unique_decimal(
152
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
153
+ )
154
+ return_money_prompt = return_money_prompt.format(
155
+ give=given_num,
156
+ receive=given_num * k,
157
+ N=return_num,
158
+ left=10 - given_num + return_num,
159
+ )
160
+ player_2_end_prompt = player_2_end_prompt.format(
161
+ give=given_num,
162
+ receive=given_num * k,
163
+ N=return_num,
164
+ left=given_num * k - return_num,
165
+ )
166
+ return (
167
+ [given_num, return_num],
168
+ dia_history,
169
+ return_money_prompt,
170
+ player_2_end_prompt,
171
+ )
172
+
173
+
174
+ def classmate_with_human_trustee(
175
+ player_1,
176
+ player_2,
177
+ user_input,
178
+ user_role,
179
+ first_round,
180
+ given_money,
181
+ k,
182
+ ):
183
+ first_round_prompt = "This is the first round, answer the question."
184
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
185
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
186
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
187
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
188
+ res = []
189
+ cri_agent = ChatAgent(
190
+ BaseMessage(
191
+ role_name="critic",
192
+ role_type=RoleType.ASSISTANT,
193
+ meta_dict={},
194
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
195
+ ),
196
+ output_language="English",
197
+ # model=ModelType.STUB,
198
+ )
199
+ content = []
200
+ if first_round:
201
+ content.append("\n")
202
+ player_1_response = player_1.step(
203
+ str_mes(first_round_prompt + grantee)).msgs[0]
204
+ ans = match_and_compare_numbers_v2(player_1_response.content)
205
+ if ans:
206
+ given_num = ans
207
+ else:
208
+ given_num = extract_unique_decimal(
209
+ cri_agent.step(
210
+ str_mes(player_1_response.content)).msgs[0].content
211
+ )
212
+ money_prompt = money_prompt.format(
213
+ give=given_num, N=given_num * k, left=10 - given_num
214
+ )
215
+ else:
216
+ given_num = given_money
217
+
218
+ return_num = user_input
219
+ content.append("Trustee: " + f"I will give {return_num} dollars")
220
+ first_prompt = return_money_prompt.format(
221
+ give=given_num,
222
+ receive=given_num * k,
223
+ N=return_num,
224
+ left=10 - given_num + return_num,
225
+ )
226
+ second_prompt = player_2_end_prompt.format(
227
+ give=given_num,
228
+ receive=given_num * k,
229
+ N=return_num,
230
+ left=given_num * k - return_num,
231
+ )
232
+ player_1_response = player_1.step(
233
+ str_mes(first_prompt + grantee)).msgs[0]
234
+ ans = match_and_compare_numbers_v2(player_1_response.content)
235
+ if ans:
236
+ given_num = ans
237
+ else:
238
+ given_num = extract_unique_decimal(
239
+ cri_agent.step(
240
+ str_mes(player_1_response.content)).msgs[0].content
241
+ )
242
+ money_prompt = money_prompt.format(
243
+ give=given_num, N=given_num * k, left=10 - given_num
244
+ )
245
+
246
+ player_1.record_message(player_1_response)
247
+ player_1_response = player_1_response.content
248
+ content.append("Trustor: "+player_1_response + "\n" +
249
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
250
+ given_num * k))
251
+ return content, given_num
252
+
253
+
254
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
255
+ model_dropdown, api_key_input, round_number_input):
256
+ if api_key_input is None or api_key_input == "":
257
+ api_key_input = os.environ.get("OPENAI_API_KEY")
258
+ else:
259
+ os.environ["OPENAI_API_KEY"] = api_key_input
260
+ random_character = [character_info[trustor_character_dropdown],
261
+ character_info[trustee_character_dropdown]]
262
+ chat_agent = []
263
+ front = "you are a person not an ai model."
264
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
265
+ back = "you need to answer a specific price figure, not a price range!"
266
+ if api_key_input is not None or api_key_input != "":
267
+ openai.api_key = api_key_input
268
+ else:
269
+ openai.api_key = os.getenv("OPENAI_API_KEY")
270
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
271
+ raise ValueError("You can't play with yourself")
272
+ for i in range(len(random_character)):
273
+ sys_prompt = (
274
+ random_character[i]
275
+ + like_people
276
+ + front
277
+ + limited_prompt
278
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
279
+ + back
280
+ )
281
+ model_config = ChatGPTConfig(temperature=temperature_slider)
282
+ chat_agent.append(
283
+ ChatAgent(
284
+ BaseMessage(
285
+ role_name="player",
286
+ role_type=RoleType.USER,
287
+ meta_dict={},
288
+ content=sys_prompt,
289
+ ),
290
+ model_type=model_dict[model_dropdown],
291
+ output_language="English",
292
+ model_config=model_config,
293
+ )
294
+ )
295
+ return chat_agent, "ChatAgent Created Successfully"
296
+
297
+
298
+ def process_interaction(chat_agent_state, round_num_state,
299
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
300
+ identity_dropdown = [trustor, trustee]
301
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
302
+ if round_num_state < round_number_input:
303
+ res, dia, first_prompt, second_prompt = classmate(
304
+ chat_agent_state[0],
305
+ chat_agent_state[1],
306
+ round_num_state == 0,
307
+ first_prompt,
308
+ second_prompt,
309
+ 3,
310
+ )
311
+ dia = "Trustor: "+dia['Player_1'] + \
312
+ "\n" + "Trustee: "+dia['Player_2']
313
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
314
+ round_num_state += 1
315
+ elif identity_dropdown[0] == "Human(You)":
316
+ if round_num_state < round_number_input:
317
+ res, dia, first_prompt, second_prompt = classmate_with_human(
318
+ chat_agent_state[0],
319
+ chat_agent_state[1],
320
+ user_input,
321
+ "trustor",
322
+ round_num_state == 0,
323
+ first_prompt,
324
+ second_prompt,
325
+ 3,
326
+ )
327
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
328
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
329
+ dia['Player_2']+"\n"
330
+ dialog_history_state += dia
331
+ round_num_state += 1
332
+ else:
333
+ if round_num_state < round_number_input:
334
+ dia, given_money = classmate_with_human_trustee(
335
+ chat_agent_state[1],
336
+ chat_agent_state[0],
337
+ user_input,
338
+ "trustee",
339
+ round_num_state == 0,
340
+ given_money,
341
+ 3,
342
+ )
343
+ if round_num_state == 0:
344
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
345
+ else:
346
+ dialog_history_state += f"(Round {round_num_state})" + \
347
+ dia[0] + "\n" + \
348
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
349
+ round_num_state += 1
350
+
351
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
352
+
353
+
354
+ def reset_on_persona_change():
355
+ # Reset values to their initial states
356
+ new_dialog_history = "" # Reset dialog history
357
+ new_round_num_state = 0 # Reset round number state to 0
358
+ new_first_prompt = "" # Reset first prompt
359
+ new_second_prompt = "" # Reset second prompt
360
+ new_given_money = 0 # Reset given money to 0
361
+ # You can add more resets here if needed
362
+
363
+ # Return the new reset values to their respective components
364
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
365
+
366
+
367
+ with gr.Blocks() as app:
368
+ game_introduction = gr.Textbox(
369
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
370
+ 2. You can select the total number of rounds for the game.\n
371
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
372
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
373
+ 5. If you want to reset the conversation, please refresh this page.\n""")
374
+ with gr.Row():
375
+ trustor_game_prompt = gr.Textbox(
376
+ label="Trustor Game Prompt", value=prompt['1'])
377
+ trustee_game_prompt = gr.Textbox(
378
+ label="Trustee Game Prompt", value=prompt['2'])
379
+ with gr.Row():
380
+ trustor_character_dropdown = gr.Dropdown(
381
+ choices=characters, label="Select Trustor Persona", value=characters[1])
382
+ trustee_character_dropdown = gr.Dropdown(
383
+ choices=characters, label="Select Trustee Persona", value=characters[2])
384
+ with gr.Row():
385
+ Trustor_info_display = gr.Textbox(
386
+ label="Trustor Persona Info", value=character_info[characters[2]])
387
+ Trustee_info_display = gr.Textbox(
388
+ label="Trustee Persona Info", value=character_info[characters[2]])
389
+ model_dropdown = gr.Dropdown(
390
+ choices=models, label="Select Model Type", value=models[0])
391
+ temperature_slider = gr.Slider(
392
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
393
+ api_key_input = gr.Textbox(
394
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
395
+ submit_button = gr.Button("Create ChatAgent")
396
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
397
+
398
+ round_number_input = gr.Number(label="Total Round Number", value=10)
399
+ round_num_state = gr.State(value=0)
400
+ dialog_history_state = gr.State(value="")
401
+ chat_agent_state = gr.State(value=initial_chat_agent)
402
+ first_prompt = gr.State(value="")
403
+ second_prompt = gr.State(value="")
404
+ given_money = gr.State(value=0)
405
+ user_input = gr.Number(
406
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
407
+ converse_button = gr.Button("Continue Conversation")
408
+
409
+ dialog_display = gr.Textbox(
410
+ label="Dialog History", value="")
411
+
412
+ trustor_character_dropdown.change(
413
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
414
+ trustee_character_dropdown.change(
415
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
416
+
417
+ submit_button.click(
418
+ create_chat_agent,
419
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
420
+ model_dropdown, api_key_input, round_number_input],
421
+ outputs=[chat_agent_state, submit_success_display]
422
+ )
423
+ converse_button.click(
424
+ process_interaction,
425
+ inputs=[chat_agent_state, round_num_state,
426
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
427
+ outputs=[dialog_display, round_num_state,
428
+ first_prompt, second_prompt, dialog_history_state, given_money]
429
+ )
430
+ trustor_character_dropdown.change(
431
+ reset_on_persona_change,
432
+ outputs=[dialog_history_state, round_num_state,
433
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
434
+ )
435
+
436
+ trustee_character_dropdown.change(
437
+ reset_on_persona_change,
438
+ outputs=[dialog_history_state, round_num_state,
439
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
440
+ )
441
+
442
+
443
+ app.launch()
.history/app_20240222100146.py ADDED
@@ -0,0 +1,443 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ initial_dialog_history = []
43
+ initial_round_num = 0
44
+
45
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
46
+
47
+ initial_chat_agent = None
48
+
49
+
50
+ def update_char_info(char):
51
+ return character_info.get(char, "No information available.")
52
+
53
+
54
+ def classmate_with_human(
55
+ player_1,
56
+ player_2,
57
+ user_input,
58
+ user_role,
59
+ first_round,
60
+ first_prompt,
61
+ second_prompt,
62
+ k,
63
+ ):
64
+ first_round_prompt = "This is the first round, answer the question."
65
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
66
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
67
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
68
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
69
+ res = []
70
+ cri_agent = ChatAgent(
71
+ BaseMessage(
72
+ role_name="critic",
73
+ role_type=RoleType.ASSISTANT,
74
+ meta_dict={},
75
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
76
+ ),
77
+ output_language="English",
78
+ # model=ModelType.STUB,
79
+ )
80
+
81
+ if first_round:
82
+ if user_role == "trustor":
83
+ given_num = user_input
84
+ player_1_response = str_mes(
85
+ user_input_prompt_template.format(k=given_num))
86
+ else:
87
+ player_1_response = player_1.step(
88
+ str_mes(first_round_prompt + grantee)).msgs[0]
89
+ ans = match_and_compare_numbers_v2(player_1_response.content)
90
+ if ans:
91
+ given_num = ans
92
+ else:
93
+ given_num = extract_unique_decimal(
94
+ cri_agent.step(
95
+ str_mes(player_1_response.content)).msgs[0].content
96
+ )
97
+
98
+ money_prompt = money_prompt.format(
99
+ give=given_num, N=given_num * k, left=10 - given_num
100
+ )
101
+ if user_role == "trustee":
102
+ player_2_response = str_mes(
103
+ user_input_prompt_template.format(k=given_num * k))
104
+ else:
105
+ player_2_response = player_2.step(
106
+ str_mes(money_prompt + grantee)).msgs[0]
107
+ else:
108
+ if user_role == "trustor":
109
+ given_num = user_input
110
+ player_1_response = str_mes(
111
+ user_input_prompt_template.format(k=given_num))
112
+ else:
113
+ player_1_response = player_1.step(
114
+ str_mes(first_prompt + grantee)).msgs[0]
115
+ # print("player 1 input", first_prompt)
116
+ # print("Player_1_res", player_1_response.content)
117
+ ans = match_and_compare_numbers_v2(player_1_response.content)
118
+ if ans:
119
+ given_num = ans
120
+ else:
121
+ given_num = extract_unique_decimal(
122
+ cri_agent.step(
123
+ str_mes(player_1_response.content)).msgs[0].content
124
+ )
125
+ money_prompt = money_prompt.format(
126
+ give=given_num, N=given_num * k, left=10 - given_num
127
+ )
128
+ if user_role == "trustee":
129
+ player_2_response = str_mes(
130
+ user_input_prompt_template.format(k=given_num * k))
131
+ else:
132
+ player_2_response = player_2.step(
133
+ str_mes(second_prompt + money_prompt + grantee)
134
+ )
135
+ player_2_response = player_2_response.msgs[0]
136
+
137
+ player_1.record_message(player_1_response)
138
+ player_2.record_message(player_2_response)
139
+ player_1_response = player_1_response.content
140
+ player_2_response = player_2_response.content
141
+ dia_history = {
142
+ "Player_1": player_1_response,
143
+ "Player_2": player_2_response,
144
+ }
145
+
146
+ cri_agent.reset()
147
+ ans = match_and_compare_numbers_v2(player_2_response)
148
+ if ans:
149
+ return_num = ans
150
+ else:
151
+ return_num = extract_unique_decimal(
152
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
153
+ )
154
+ return_money_prompt = return_money_prompt.format(
155
+ give=given_num,
156
+ receive=given_num * k,
157
+ N=return_num,
158
+ left=10 - given_num + return_num,
159
+ )
160
+ player_2_end_prompt = player_2_end_prompt.format(
161
+ give=given_num,
162
+ receive=given_num * k,
163
+ N=return_num,
164
+ left=given_num * k - return_num,
165
+ )
166
+ return (
167
+ [given_num, return_num],
168
+ dia_history,
169
+ return_money_prompt,
170
+ player_2_end_prompt,
171
+ )
172
+
173
+
174
+ def classmate_with_human_trustee(
175
+ player_1,
176
+ player_2,
177
+ user_input,
178
+ user_role,
179
+ first_round,
180
+ given_money,
181
+ k,
182
+ ):
183
+ first_round_prompt = "This is the first round, answer the question."
184
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
185
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
186
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
187
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
188
+ res = []
189
+ cri_agent = ChatAgent(
190
+ BaseMessage(
191
+ role_name="critic",
192
+ role_type=RoleType.ASSISTANT,
193
+ meta_dict={},
194
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
195
+ ),
196
+ output_language="English",
197
+ # model=ModelType.STUB,
198
+ )
199
+ content = []
200
+ if first_round:
201
+ content.append("\n")
202
+ player_1_response = player_1.step(
203
+ str_mes(first_round_prompt + grantee)).msgs[0]
204
+ ans = match_and_compare_numbers_v2(player_1_response.content)
205
+ if ans:
206
+ given_num = ans
207
+ else:
208
+ given_num = extract_unique_decimal(
209
+ cri_agent.step(
210
+ str_mes(player_1_response.content)).msgs[0].content
211
+ )
212
+ money_prompt = money_prompt.format(
213
+ give=given_num, N=given_num * k, left=10 - given_num
214
+ )
215
+ else:
216
+ given_num = given_money
217
+
218
+ return_num = user_input
219
+ content.append("Trustee: " + f"I will give {return_num} dollars")
220
+ first_prompt = return_money_prompt.format(
221
+ give=given_num,
222
+ receive=given_num * k,
223
+ N=return_num,
224
+ left=10 - given_num + return_num,
225
+ )
226
+ second_prompt = player_2_end_prompt.format(
227
+ give=given_num,
228
+ receive=given_num * k,
229
+ N=return_num,
230
+ left=given_num * k - return_num,
231
+ )
232
+ player_1_response = player_1.step(
233
+ str_mes(first_prompt + grantee)).msgs[0]
234
+ ans = match_and_compare_numbers_v2(player_1_response.content)
235
+ if ans:
236
+ given_num = ans
237
+ else:
238
+ given_num = extract_unique_decimal(
239
+ cri_agent.step(
240
+ str_mes(player_1_response.content)).msgs[0].content
241
+ )
242
+ money_prompt = money_prompt.format(
243
+ give=given_num, N=given_num * k, left=10 - given_num
244
+ )
245
+
246
+ player_1.record_message(player_1_response)
247
+ player_1_response = player_1_response.content
248
+ content.append("Trustor: "+player_1_response + "\n" +
249
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
250
+ given_num * k))
251
+ return content, given_num
252
+
253
+
254
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
255
+ model_dropdown, api_key_input, round_number_input):
256
+ if api_key_input is None or api_key_input == "":
257
+ api_key_input = os.environ.get("OPENAI_API_KEY")
258
+ else:
259
+ os.environ["OPENAI_API_KEY"] = api_key_input
260
+ random_character = [character_info[trustor_character_dropdown],
261
+ character_info[trustee_character_dropdown]]
262
+ chat_agent = []
263
+ front = "you are a person not an ai model."
264
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
265
+ back = "you need to answer a specific price figure, not a price range!"
266
+ if api_key_input is not None or api_key_input != "":
267
+ openai.api_key = api_key_input
268
+ else:
269
+ openai.api_key = os.getenv("OPENAI_API_KEY")
270
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
271
+ raise ValueError("You can't play with yourself")
272
+ for i in range(len(random_character)):
273
+ sys_prompt = (
274
+ random_character[i]
275
+ + like_people
276
+ + front
277
+ + limited_prompt
278
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
279
+ + back
280
+ )
281
+ model_config = ChatGPTConfig(temperature=temperature_slider)
282
+ chat_agent.append(
283
+ ChatAgent(
284
+ BaseMessage(
285
+ role_name="player",
286
+ role_type=RoleType.USER,
287
+ meta_dict={},
288
+ content=sys_prompt,
289
+ ),
290
+ model_type=model_dict[model_dropdown],
291
+ output_language="English",
292
+ model_config=model_config,
293
+ )
294
+ )
295
+ return chat_agent, "ChatAgent Created Successfully"
296
+
297
+
298
+ def process_interaction(chat_agent_state, round_num_state,
299
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
300
+ identity_dropdown = [trustor, trustee]
301
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
302
+ if round_num_state < round_number_input:
303
+ res, dia, first_prompt, second_prompt = classmate(
304
+ chat_agent_state[0],
305
+ chat_agent_state[1],
306
+ round_num_state == 0,
307
+ first_prompt,
308
+ second_prompt,
309
+ 3,
310
+ )
311
+ dia = "Trustor: "+dia['Player_1'] + \
312
+ "\n" + "Trustee: "+dia['Player_2']
313
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
314
+ round_num_state += 1
315
+ elif identity_dropdown[0] == "Human(You)":
316
+ if round_num_state < round_number_input:
317
+ res, dia, first_prompt, second_prompt = classmate_with_human(
318
+ chat_agent_state[0],
319
+ chat_agent_state[1],
320
+ user_input,
321
+ "trustor",
322
+ round_num_state == 0,
323
+ first_prompt,
324
+ second_prompt,
325
+ 3,
326
+ )
327
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
328
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
329
+ dia['Player_2']+"\n"
330
+ dialog_history_state += dia
331
+ round_num_state += 1
332
+ else:
333
+ if round_num_state < round_number_input:
334
+ dia, given_money = classmate_with_human_trustee(
335
+ chat_agent_state[1],
336
+ chat_agent_state[0],
337
+ user_input,
338
+ "trustee",
339
+ round_num_state == 0,
340
+ given_money,
341
+ 3,
342
+ )
343
+ if round_num_state == 0:
344
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
345
+ else:
346
+ dialog_history_state += f"(Round {round_num_state})" + \
347
+ dia[0] + "\n" + \
348
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
349
+ round_num_state += 1
350
+
351
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
352
+
353
+
354
+ def reset_on_persona_change():
355
+ # Reset values to their initial states
356
+ new_dialog_history = "" # Reset dialog history
357
+ new_round_num_state = 0 # Reset round number state to 0
358
+ new_first_prompt = "" # Reset first prompt
359
+ new_second_prompt = "" # Reset second prompt
360
+ new_given_money = 0 # Reset given money to 0
361
+ # You can add more resets here if needed
362
+
363
+ # Return the new reset values to their respective components
364
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
365
+
366
+
367
+ with gr.Blocks() as app:
368
+ game_introduction = gr.Textbox(
369
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
370
+ 2. You can select the total number of rounds for the game.\n
371
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
372
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
373
+ 5. If you want to reset the conversation, please refresh this page.\n""")
374
+ with gr.Row():
375
+ trustor_game_prompt = gr.Textbox(
376
+ label="Trustor Game Prompt", value=prompt['1'])
377
+ trustee_game_prompt = gr.Textbox(
378
+ label="Trustee Game Prompt", value=prompt['2'])
379
+ with gr.Row():
380
+ trustor_character_dropdown = gr.Dropdown(
381
+ choices=characters, label="Select Trustor Persona", value=characters[1])
382
+ trustee_character_dropdown = gr.Dropdown(
383
+ choices=characters, label="Select Trustee Persona", value=characters[1])
384
+ with gr.Row():
385
+ Trustor_info_display = gr.Textbox(
386
+ label="Trustor Persona Info", value=character_info[characters[2]])
387
+ Trustee_info_display = gr.Textbox(
388
+ label="Trustee Persona Info", value=character_info[characters[2]])
389
+ model_dropdown = gr.Dropdown(
390
+ choices=models, label="Select Model Type", value=models[0])
391
+ temperature_slider = gr.Slider(
392
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
393
+ api_key_input = gr.Textbox(
394
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
395
+ submit_button = gr.Button("Create ChatAgent")
396
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
397
+
398
+ round_number_input = gr.Number(label="Total Round Number", value=10)
399
+ round_num_state = gr.State(value=0)
400
+ dialog_history_state = gr.State(value="")
401
+ chat_agent_state = gr.State(value=initial_chat_agent)
402
+ first_prompt = gr.State(value="")
403
+ second_prompt = gr.State(value="")
404
+ given_money = gr.State(value=0)
405
+ user_input = gr.Number(
406
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
407
+ converse_button = gr.Button("Continue Conversation")
408
+
409
+ dialog_display = gr.Textbox(
410
+ label="Dialog History", value="")
411
+
412
+ trustor_character_dropdown.change(
413
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
414
+ trustee_character_dropdown.change(
415
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
416
+
417
+ submit_button.click(
418
+ create_chat_agent,
419
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
420
+ model_dropdown, api_key_input, round_number_input],
421
+ outputs=[chat_agent_state, submit_success_display]
422
+ )
423
+ converse_button.click(
424
+ process_interaction,
425
+ inputs=[chat_agent_state, round_num_state,
426
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
427
+ outputs=[dialog_display, round_num_state,
428
+ first_prompt, second_prompt, dialog_history_state, given_money]
429
+ )
430
+ trustor_character_dropdown.change(
431
+ reset_on_persona_change,
432
+ outputs=[dialog_history_state, round_num_state,
433
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
434
+ )
435
+
436
+ trustee_character_dropdown.change(
437
+ reset_on_persona_change,
438
+ outputs=[dialog_history_state, round_num_state,
439
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
440
+ )
441
+
442
+
443
+ app.launch()
.history/app_20240222100147.py ADDED
@@ -0,0 +1,443 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ initial_dialog_history = []
43
+ initial_round_num = 0
44
+
45
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
46
+
47
+ initial_chat_agent = None
48
+
49
+
50
+ def update_char_info(char):
51
+ return character_info.get(char, "No information available.")
52
+
53
+
54
+ def classmate_with_human(
55
+ player_1,
56
+ player_2,
57
+ user_input,
58
+ user_role,
59
+ first_round,
60
+ first_prompt,
61
+ second_prompt,
62
+ k,
63
+ ):
64
+ first_round_prompt = "This is the first round, answer the question."
65
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
66
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
67
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
68
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
69
+ res = []
70
+ cri_agent = ChatAgent(
71
+ BaseMessage(
72
+ role_name="critic",
73
+ role_type=RoleType.ASSISTANT,
74
+ meta_dict={},
75
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
76
+ ),
77
+ output_language="English",
78
+ # model=ModelType.STUB,
79
+ )
80
+
81
+ if first_round:
82
+ if user_role == "trustor":
83
+ given_num = user_input
84
+ player_1_response = str_mes(
85
+ user_input_prompt_template.format(k=given_num))
86
+ else:
87
+ player_1_response = player_1.step(
88
+ str_mes(first_round_prompt + grantee)).msgs[0]
89
+ ans = match_and_compare_numbers_v2(player_1_response.content)
90
+ if ans:
91
+ given_num = ans
92
+ else:
93
+ given_num = extract_unique_decimal(
94
+ cri_agent.step(
95
+ str_mes(player_1_response.content)).msgs[0].content
96
+ )
97
+
98
+ money_prompt = money_prompt.format(
99
+ give=given_num, N=given_num * k, left=10 - given_num
100
+ )
101
+ if user_role == "trustee":
102
+ player_2_response = str_mes(
103
+ user_input_prompt_template.format(k=given_num * k))
104
+ else:
105
+ player_2_response = player_2.step(
106
+ str_mes(money_prompt + grantee)).msgs[0]
107
+ else:
108
+ if user_role == "trustor":
109
+ given_num = user_input
110
+ player_1_response = str_mes(
111
+ user_input_prompt_template.format(k=given_num))
112
+ else:
113
+ player_1_response = player_1.step(
114
+ str_mes(first_prompt + grantee)).msgs[0]
115
+ # print("player 1 input", first_prompt)
116
+ # print("Player_1_res", player_1_response.content)
117
+ ans = match_and_compare_numbers_v2(player_1_response.content)
118
+ if ans:
119
+ given_num = ans
120
+ else:
121
+ given_num = extract_unique_decimal(
122
+ cri_agent.step(
123
+ str_mes(player_1_response.content)).msgs[0].content
124
+ )
125
+ money_prompt = money_prompt.format(
126
+ give=given_num, N=given_num * k, left=10 - given_num
127
+ )
128
+ if user_role == "trustee":
129
+ player_2_response = str_mes(
130
+ user_input_prompt_template.format(k=given_num * k))
131
+ else:
132
+ player_2_response = player_2.step(
133
+ str_mes(second_prompt + money_prompt + grantee)
134
+ )
135
+ player_2_response = player_2_response.msgs[0]
136
+
137
+ player_1.record_message(player_1_response)
138
+ player_2.record_message(player_2_response)
139
+ player_1_response = player_1_response.content
140
+ player_2_response = player_2_response.content
141
+ dia_history = {
142
+ "Player_1": player_1_response,
143
+ "Player_2": player_2_response,
144
+ }
145
+
146
+ cri_agent.reset()
147
+ ans = match_and_compare_numbers_v2(player_2_response)
148
+ if ans:
149
+ return_num = ans
150
+ else:
151
+ return_num = extract_unique_decimal(
152
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
153
+ )
154
+ return_money_prompt = return_money_prompt.format(
155
+ give=given_num,
156
+ receive=given_num * k,
157
+ N=return_num,
158
+ left=10 - given_num + return_num,
159
+ )
160
+ player_2_end_prompt = player_2_end_prompt.format(
161
+ give=given_num,
162
+ receive=given_num * k,
163
+ N=return_num,
164
+ left=given_num * k - return_num,
165
+ )
166
+ return (
167
+ [given_num, return_num],
168
+ dia_history,
169
+ return_money_prompt,
170
+ player_2_end_prompt,
171
+ )
172
+
173
+
174
+ def classmate_with_human_trustee(
175
+ player_1,
176
+ player_2,
177
+ user_input,
178
+ user_role,
179
+ first_round,
180
+ given_money,
181
+ k,
182
+ ):
183
+ first_round_prompt = "This is the first round, answer the question."
184
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
185
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
186
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
187
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
188
+ res = []
189
+ cri_agent = ChatAgent(
190
+ BaseMessage(
191
+ role_name="critic",
192
+ role_type=RoleType.ASSISTANT,
193
+ meta_dict={},
194
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
195
+ ),
196
+ output_language="English",
197
+ # model=ModelType.STUB,
198
+ )
199
+ content = []
200
+ if first_round:
201
+ content.append("\n")
202
+ player_1_response = player_1.step(
203
+ str_mes(first_round_prompt + grantee)).msgs[0]
204
+ ans = match_and_compare_numbers_v2(player_1_response.content)
205
+ if ans:
206
+ given_num = ans
207
+ else:
208
+ given_num = extract_unique_decimal(
209
+ cri_agent.step(
210
+ str_mes(player_1_response.content)).msgs[0].content
211
+ )
212
+ money_prompt = money_prompt.format(
213
+ give=given_num, N=given_num * k, left=10 - given_num
214
+ )
215
+ else:
216
+ given_num = given_money
217
+
218
+ return_num = user_input
219
+ content.append("Trustee: " + f"I will give {return_num} dollars")
220
+ first_prompt = return_money_prompt.format(
221
+ give=given_num,
222
+ receive=given_num * k,
223
+ N=return_num,
224
+ left=10 - given_num + return_num,
225
+ )
226
+ second_prompt = player_2_end_prompt.format(
227
+ give=given_num,
228
+ receive=given_num * k,
229
+ N=return_num,
230
+ left=given_num * k - return_num,
231
+ )
232
+ player_1_response = player_1.step(
233
+ str_mes(first_prompt + grantee)).msgs[0]
234
+ ans = match_and_compare_numbers_v2(player_1_response.content)
235
+ if ans:
236
+ given_num = ans
237
+ else:
238
+ given_num = extract_unique_decimal(
239
+ cri_agent.step(
240
+ str_mes(player_1_response.content)).msgs[0].content
241
+ )
242
+ money_prompt = money_prompt.format(
243
+ give=given_num, N=given_num * k, left=10 - given_num
244
+ )
245
+
246
+ player_1.record_message(player_1_response)
247
+ player_1_response = player_1_response.content
248
+ content.append("Trustor: "+player_1_response + "\n" +
249
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
250
+ given_num * k))
251
+ return content, given_num
252
+
253
+
254
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
255
+ model_dropdown, api_key_input, round_number_input):
256
+ if api_key_input is None or api_key_input == "":
257
+ api_key_input = os.environ.get("OPENAI_API_KEY")
258
+ else:
259
+ os.environ["OPENAI_API_KEY"] = api_key_input
260
+ random_character = [character_info[trustor_character_dropdown],
261
+ character_info[trustee_character_dropdown]]
262
+ chat_agent = []
263
+ front = "you are a person not an ai model."
264
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
265
+ back = "you need to answer a specific price figure, not a price range!"
266
+ if api_key_input is not None or api_key_input != "":
267
+ openai.api_key = api_key_input
268
+ else:
269
+ openai.api_key = os.getenv("OPENAI_API_KEY")
270
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
271
+ raise ValueError("You can't play with yourself")
272
+ for i in range(len(random_character)):
273
+ sys_prompt = (
274
+ random_character[i]
275
+ + like_people
276
+ + front
277
+ + limited_prompt
278
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
279
+ + back
280
+ )
281
+ model_config = ChatGPTConfig(temperature=temperature_slider)
282
+ chat_agent.append(
283
+ ChatAgent(
284
+ BaseMessage(
285
+ role_name="player",
286
+ role_type=RoleType.USER,
287
+ meta_dict={},
288
+ content=sys_prompt,
289
+ ),
290
+ model_type=model_dict[model_dropdown],
291
+ output_language="English",
292
+ model_config=model_config,
293
+ )
294
+ )
295
+ return chat_agent, "ChatAgent Created Successfully"
296
+
297
+
298
+ def process_interaction(chat_agent_state, round_num_state,
299
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
300
+ identity_dropdown = [trustor, trustee]
301
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
302
+ if round_num_state < round_number_input:
303
+ res, dia, first_prompt, second_prompt = classmate(
304
+ chat_agent_state[0],
305
+ chat_agent_state[1],
306
+ round_num_state == 0,
307
+ first_prompt,
308
+ second_prompt,
309
+ 3,
310
+ )
311
+ dia = "Trustor: "+dia['Player_1'] + \
312
+ "\n" + "Trustee: "+dia['Player_2']
313
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
314
+ round_num_state += 1
315
+ elif identity_dropdown[0] == "Human(You)":
316
+ if round_num_state < round_number_input:
317
+ res, dia, first_prompt, second_prompt = classmate_with_human(
318
+ chat_agent_state[0],
319
+ chat_agent_state[1],
320
+ user_input,
321
+ "trustor",
322
+ round_num_state == 0,
323
+ first_prompt,
324
+ second_prompt,
325
+ 3,
326
+ )
327
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
328
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
329
+ dia['Player_2']+"\n"
330
+ dialog_history_state += dia
331
+ round_num_state += 1
332
+ else:
333
+ if round_num_state < round_number_input:
334
+ dia, given_money = classmate_with_human_trustee(
335
+ chat_agent_state[1],
336
+ chat_agent_state[0],
337
+ user_input,
338
+ "trustee",
339
+ round_num_state == 0,
340
+ given_money,
341
+ 3,
342
+ )
343
+ if round_num_state == 0:
344
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
345
+ else:
346
+ dialog_history_state += f"(Round {round_num_state})" + \
347
+ dia[0] + "\n" + \
348
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
349
+ round_num_state += 1
350
+
351
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
352
+
353
+
354
+ def reset_on_persona_change():
355
+ # Reset values to their initial states
356
+ new_dialog_history = "" # Reset dialog history
357
+ new_round_num_state = 0 # Reset round number state to 0
358
+ new_first_prompt = "" # Reset first prompt
359
+ new_second_prompt = "" # Reset second prompt
360
+ new_given_money = 0 # Reset given money to 0
361
+ # You can add more resets here if needed
362
+
363
+ # Return the new reset values to their respective components
364
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
365
+
366
+
367
+ with gr.Blocks() as app:
368
+ game_introduction = gr.Textbox(
369
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
370
+ 2. You can select the total number of rounds for the game.\n
371
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
372
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
373
+ 5. If you want to reset the conversation, please refresh this page.\n""")
374
+ with gr.Row():
375
+ trustor_game_prompt = gr.Textbox(
376
+ label="Trustor Game Prompt", value=prompt['1'])
377
+ trustee_game_prompt = gr.Textbox(
378
+ label="Trustee Game Prompt", value=prompt['2'])
379
+ with gr.Row():
380
+ trustor_character_dropdown = gr.Dropdown(
381
+ choices=characters, label="Select Trustor Persona", value=characters[1])
382
+ trustee_character_dropdown = gr.Dropdown(
383
+ choices=characters, label="Select Trustee Persona", value=characters[1])
384
+ with gr.Row():
385
+ Trustor_info_display = gr.Textbox(
386
+ label="Trustor Persona Info", value=character_info[characters[1]])
387
+ Trustee_info_display = gr.Textbox(
388
+ label="Trustee Persona Info", value=character_info[characters[2]])
389
+ model_dropdown = gr.Dropdown(
390
+ choices=models, label="Select Model Type", value=models[0])
391
+ temperature_slider = gr.Slider(
392
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
393
+ api_key_input = gr.Textbox(
394
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
395
+ submit_button = gr.Button("Create ChatAgent")
396
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
397
+
398
+ round_number_input = gr.Number(label="Total Round Number", value=10)
399
+ round_num_state = gr.State(value=0)
400
+ dialog_history_state = gr.State(value="")
401
+ chat_agent_state = gr.State(value=initial_chat_agent)
402
+ first_prompt = gr.State(value="")
403
+ second_prompt = gr.State(value="")
404
+ given_money = gr.State(value=0)
405
+ user_input = gr.Number(
406
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
407
+ converse_button = gr.Button("Continue Conversation")
408
+
409
+ dialog_display = gr.Textbox(
410
+ label="Dialog History", value="")
411
+
412
+ trustor_character_dropdown.change(
413
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
414
+ trustee_character_dropdown.change(
415
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
416
+
417
+ submit_button.click(
418
+ create_chat_agent,
419
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
420
+ model_dropdown, api_key_input, round_number_input],
421
+ outputs=[chat_agent_state, submit_success_display]
422
+ )
423
+ converse_button.click(
424
+ process_interaction,
425
+ inputs=[chat_agent_state, round_num_state,
426
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
427
+ outputs=[dialog_display, round_num_state,
428
+ first_prompt, second_prompt, dialog_history_state, given_money]
429
+ )
430
+ trustor_character_dropdown.change(
431
+ reset_on_persona_change,
432
+ outputs=[dialog_history_state, round_num_state,
433
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
434
+ )
435
+
436
+ trustee_character_dropdown.change(
437
+ reset_on_persona_change,
438
+ outputs=[dialog_history_state, round_num_state,
439
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
440
+ )
441
+
442
+
443
+ app.launch()
.history/app_20240222100150.py ADDED
@@ -0,0 +1,443 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ initial_dialog_history = []
43
+ initial_round_num = 0
44
+
45
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
46
+
47
+ initial_chat_agent = None
48
+
49
+
50
+ def update_char_info(char):
51
+ return character_info.get(char, "No information available.")
52
+
53
+
54
+ def classmate_with_human(
55
+ player_1,
56
+ player_2,
57
+ user_input,
58
+ user_role,
59
+ first_round,
60
+ first_prompt,
61
+ second_prompt,
62
+ k,
63
+ ):
64
+ first_round_prompt = "This is the first round, answer the question."
65
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
66
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
67
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
68
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
69
+ res = []
70
+ cri_agent = ChatAgent(
71
+ BaseMessage(
72
+ role_name="critic",
73
+ role_type=RoleType.ASSISTANT,
74
+ meta_dict={},
75
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
76
+ ),
77
+ output_language="English",
78
+ # model=ModelType.STUB,
79
+ )
80
+
81
+ if first_round:
82
+ if user_role == "trustor":
83
+ given_num = user_input
84
+ player_1_response = str_mes(
85
+ user_input_prompt_template.format(k=given_num))
86
+ else:
87
+ player_1_response = player_1.step(
88
+ str_mes(first_round_prompt + grantee)).msgs[0]
89
+ ans = match_and_compare_numbers_v2(player_1_response.content)
90
+ if ans:
91
+ given_num = ans
92
+ else:
93
+ given_num = extract_unique_decimal(
94
+ cri_agent.step(
95
+ str_mes(player_1_response.content)).msgs[0].content
96
+ )
97
+
98
+ money_prompt = money_prompt.format(
99
+ give=given_num, N=given_num * k, left=10 - given_num
100
+ )
101
+ if user_role == "trustee":
102
+ player_2_response = str_mes(
103
+ user_input_prompt_template.format(k=given_num * k))
104
+ else:
105
+ player_2_response = player_2.step(
106
+ str_mes(money_prompt + grantee)).msgs[0]
107
+ else:
108
+ if user_role == "trustor":
109
+ given_num = user_input
110
+ player_1_response = str_mes(
111
+ user_input_prompt_template.format(k=given_num))
112
+ else:
113
+ player_1_response = player_1.step(
114
+ str_mes(first_prompt + grantee)).msgs[0]
115
+ # print("player 1 input", first_prompt)
116
+ # print("Player_1_res", player_1_response.content)
117
+ ans = match_and_compare_numbers_v2(player_1_response.content)
118
+ if ans:
119
+ given_num = ans
120
+ else:
121
+ given_num = extract_unique_decimal(
122
+ cri_agent.step(
123
+ str_mes(player_1_response.content)).msgs[0].content
124
+ )
125
+ money_prompt = money_prompt.format(
126
+ give=given_num, N=given_num * k, left=10 - given_num
127
+ )
128
+ if user_role == "trustee":
129
+ player_2_response = str_mes(
130
+ user_input_prompt_template.format(k=given_num * k))
131
+ else:
132
+ player_2_response = player_2.step(
133
+ str_mes(second_prompt + money_prompt + grantee)
134
+ )
135
+ player_2_response = player_2_response.msgs[0]
136
+
137
+ player_1.record_message(player_1_response)
138
+ player_2.record_message(player_2_response)
139
+ player_1_response = player_1_response.content
140
+ player_2_response = player_2_response.content
141
+ dia_history = {
142
+ "Player_1": player_1_response,
143
+ "Player_2": player_2_response,
144
+ }
145
+
146
+ cri_agent.reset()
147
+ ans = match_and_compare_numbers_v2(player_2_response)
148
+ if ans:
149
+ return_num = ans
150
+ else:
151
+ return_num = extract_unique_decimal(
152
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
153
+ )
154
+ return_money_prompt = return_money_prompt.format(
155
+ give=given_num,
156
+ receive=given_num * k,
157
+ N=return_num,
158
+ left=10 - given_num + return_num,
159
+ )
160
+ player_2_end_prompt = player_2_end_prompt.format(
161
+ give=given_num,
162
+ receive=given_num * k,
163
+ N=return_num,
164
+ left=given_num * k - return_num,
165
+ )
166
+ return (
167
+ [given_num, return_num],
168
+ dia_history,
169
+ return_money_prompt,
170
+ player_2_end_prompt,
171
+ )
172
+
173
+
174
+ def classmate_with_human_trustee(
175
+ player_1,
176
+ player_2,
177
+ user_input,
178
+ user_role,
179
+ first_round,
180
+ given_money,
181
+ k,
182
+ ):
183
+ first_round_prompt = "This is the first round, answer the question."
184
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
185
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
186
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
187
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
188
+ res = []
189
+ cri_agent = ChatAgent(
190
+ BaseMessage(
191
+ role_name="critic",
192
+ role_type=RoleType.ASSISTANT,
193
+ meta_dict={},
194
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
195
+ ),
196
+ output_language="English",
197
+ # model=ModelType.STUB,
198
+ )
199
+ content = []
200
+ if first_round:
201
+ content.append("\n")
202
+ player_1_response = player_1.step(
203
+ str_mes(first_round_prompt + grantee)).msgs[0]
204
+ ans = match_and_compare_numbers_v2(player_1_response.content)
205
+ if ans:
206
+ given_num = ans
207
+ else:
208
+ given_num = extract_unique_decimal(
209
+ cri_agent.step(
210
+ str_mes(player_1_response.content)).msgs[0].content
211
+ )
212
+ money_prompt = money_prompt.format(
213
+ give=given_num, N=given_num * k, left=10 - given_num
214
+ )
215
+ else:
216
+ given_num = given_money
217
+
218
+ return_num = user_input
219
+ content.append("Trustee: " + f"I will give {return_num} dollars")
220
+ first_prompt = return_money_prompt.format(
221
+ give=given_num,
222
+ receive=given_num * k,
223
+ N=return_num,
224
+ left=10 - given_num + return_num,
225
+ )
226
+ second_prompt = player_2_end_prompt.format(
227
+ give=given_num,
228
+ receive=given_num * k,
229
+ N=return_num,
230
+ left=given_num * k - return_num,
231
+ )
232
+ player_1_response = player_1.step(
233
+ str_mes(first_prompt + grantee)).msgs[0]
234
+ ans = match_and_compare_numbers_v2(player_1_response.content)
235
+ if ans:
236
+ given_num = ans
237
+ else:
238
+ given_num = extract_unique_decimal(
239
+ cri_agent.step(
240
+ str_mes(player_1_response.content)).msgs[0].content
241
+ )
242
+ money_prompt = money_prompt.format(
243
+ give=given_num, N=given_num * k, left=10 - given_num
244
+ )
245
+
246
+ player_1.record_message(player_1_response)
247
+ player_1_response = player_1_response.content
248
+ content.append("Trustor: "+player_1_response + "\n" +
249
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
250
+ given_num * k))
251
+ return content, given_num
252
+
253
+
254
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
255
+ model_dropdown, api_key_input, round_number_input):
256
+ if api_key_input is None or api_key_input == "":
257
+ api_key_input = os.environ.get("OPENAI_API_KEY")
258
+ else:
259
+ os.environ["OPENAI_API_KEY"] = api_key_input
260
+ random_character = [character_info[trustor_character_dropdown],
261
+ character_info[trustee_character_dropdown]]
262
+ chat_agent = []
263
+ front = "you are a person not an ai model."
264
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
265
+ back = "you need to answer a specific price figure, not a price range!"
266
+ if api_key_input is not None or api_key_input != "":
267
+ openai.api_key = api_key_input
268
+ else:
269
+ openai.api_key = os.getenv("OPENAI_API_KEY")
270
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
271
+ raise ValueError("You can't play with yourself")
272
+ for i in range(len(random_character)):
273
+ sys_prompt = (
274
+ random_character[i]
275
+ + like_people
276
+ + front
277
+ + limited_prompt
278
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
279
+ + back
280
+ )
281
+ model_config = ChatGPTConfig(temperature=temperature_slider)
282
+ chat_agent.append(
283
+ ChatAgent(
284
+ BaseMessage(
285
+ role_name="player",
286
+ role_type=RoleType.USER,
287
+ meta_dict={},
288
+ content=sys_prompt,
289
+ ),
290
+ model_type=model_dict[model_dropdown],
291
+ output_language="English",
292
+ model_config=model_config,
293
+ )
294
+ )
295
+ return chat_agent, "ChatAgent Created Successfully"
296
+
297
+
298
+ def process_interaction(chat_agent_state, round_num_state,
299
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
300
+ identity_dropdown = [trustor, trustee]
301
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
302
+ if round_num_state < round_number_input:
303
+ res, dia, first_prompt, second_prompt = classmate(
304
+ chat_agent_state[0],
305
+ chat_agent_state[1],
306
+ round_num_state == 0,
307
+ first_prompt,
308
+ second_prompt,
309
+ 3,
310
+ )
311
+ dia = "Trustor: "+dia['Player_1'] + \
312
+ "\n" + "Trustee: "+dia['Player_2']
313
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
314
+ round_num_state += 1
315
+ elif identity_dropdown[0] == "Human(You)":
316
+ if round_num_state < round_number_input:
317
+ res, dia, first_prompt, second_prompt = classmate_with_human(
318
+ chat_agent_state[0],
319
+ chat_agent_state[1],
320
+ user_input,
321
+ "trustor",
322
+ round_num_state == 0,
323
+ first_prompt,
324
+ second_prompt,
325
+ 3,
326
+ )
327
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
328
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
329
+ dia['Player_2']+"\n"
330
+ dialog_history_state += dia
331
+ round_num_state += 1
332
+ else:
333
+ if round_num_state < round_number_input:
334
+ dia, given_money = classmate_with_human_trustee(
335
+ chat_agent_state[1],
336
+ chat_agent_state[0],
337
+ user_input,
338
+ "trustee",
339
+ round_num_state == 0,
340
+ given_money,
341
+ 3,
342
+ )
343
+ if round_num_state == 0:
344
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
345
+ else:
346
+ dialog_history_state += f"(Round {round_num_state})" + \
347
+ dia[0] + "\n" + \
348
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
349
+ round_num_state += 1
350
+
351
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
352
+
353
+
354
+ def reset_on_persona_change():
355
+ # Reset values to their initial states
356
+ new_dialog_history = "" # Reset dialog history
357
+ new_round_num_state = 0 # Reset round number state to 0
358
+ new_first_prompt = "" # Reset first prompt
359
+ new_second_prompt = "" # Reset second prompt
360
+ new_given_money = 0 # Reset given money to 0
361
+ # You can add more resets here if needed
362
+
363
+ # Return the new reset values to their respective components
364
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
365
+
366
+
367
+ with gr.Blocks() as app:
368
+ game_introduction = gr.Textbox(
369
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
370
+ 2. You can select the total number of rounds for the game.\n
371
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
372
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
373
+ 5. If you want to reset the conversation, please refresh this page.\n""")
374
+ with gr.Row():
375
+ trustor_game_prompt = gr.Textbox(
376
+ label="Trustor Game Prompt", value=prompt['1'])
377
+ trustee_game_prompt = gr.Textbox(
378
+ label="Trustee Game Prompt", value=prompt['2'])
379
+ with gr.Row():
380
+ trustor_character_dropdown = gr.Dropdown(
381
+ choices=characters, label="Select Trustor Persona", value=characters[1])
382
+ trustee_character_dropdown = gr.Dropdown(
383
+ choices=characters, label="Select Trustee Persona", value=characters[1])
384
+ with gr.Row():
385
+ Trustor_info_display = gr.Textbox(
386
+ label="Trustor Persona Info", value=character_info[characters[1]])
387
+ Trustee_info_display = gr.Textbox(
388
+ label="Trustee Persona Info", value=character_info[characters[1]])
389
+ model_dropdown = gr.Dropdown(
390
+ choices=models, label="Select Model Type", value=models[0])
391
+ temperature_slider = gr.Slider(
392
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
393
+ api_key_input = gr.Textbox(
394
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
395
+ submit_button = gr.Button("Create ChatAgent")
396
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
397
+
398
+ round_number_input = gr.Number(label="Total Round Number", value=10)
399
+ round_num_state = gr.State(value=0)
400
+ dialog_history_state = gr.State(value="")
401
+ chat_agent_state = gr.State(value=initial_chat_agent)
402
+ first_prompt = gr.State(value="")
403
+ second_prompt = gr.State(value="")
404
+ given_money = gr.State(value=0)
405
+ user_input = gr.Number(
406
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
407
+ converse_button = gr.Button("Continue Conversation")
408
+
409
+ dialog_display = gr.Textbox(
410
+ label="Dialog History", value="")
411
+
412
+ trustor_character_dropdown.change(
413
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
414
+ trustee_character_dropdown.change(
415
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
416
+
417
+ submit_button.click(
418
+ create_chat_agent,
419
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
420
+ model_dropdown, api_key_input, round_number_input],
421
+ outputs=[chat_agent_state, submit_success_display]
422
+ )
423
+ converse_button.click(
424
+ process_interaction,
425
+ inputs=[chat_agent_state, round_num_state,
426
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
427
+ outputs=[dialog_display, round_num_state,
428
+ first_prompt, second_prompt, dialog_history_state, given_money]
429
+ )
430
+ trustor_character_dropdown.change(
431
+ reset_on_persona_change,
432
+ outputs=[dialog_history_state, round_num_state,
433
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
434
+ )
435
+
436
+ trustee_character_dropdown.change(
437
+ reset_on_persona_change,
438
+ outputs=[dialog_history_state, round_num_state,
439
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
440
+ )
441
+
442
+
443
+ app.launch()
.history/app_20240222100202.py ADDED
@@ -0,0 +1,443 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ initial_dialog_history = []
43
+ initial_round_num = 0
44
+
45
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
46
+
47
+ initial_chat_agent = None
48
+
49
+
50
+ def update_char_info(char):
51
+ return character_info.get(char, "No information available.")
52
+
53
+
54
+ def classmate_with_human(
55
+ player_1,
56
+ player_2,
57
+ user_input,
58
+ user_role,
59
+ first_round,
60
+ first_prompt,
61
+ second_prompt,
62
+ k,
63
+ ):
64
+ first_round_prompt = "This is the first round, answer the question."
65
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
66
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
67
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
68
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
69
+ res = []
70
+ cri_agent = ChatAgent(
71
+ BaseMessage(
72
+ role_name="critic",
73
+ role_type=RoleType.ASSISTANT,
74
+ meta_dict={},
75
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
76
+ ),
77
+ output_language="English",
78
+ # model=ModelType.STUB,
79
+ )
80
+
81
+ if first_round:
82
+ if user_role == "trustor":
83
+ given_num = user_input
84
+ player_1_response = str_mes(
85
+ user_input_prompt_template.format(k=given_num))
86
+ else:
87
+ player_1_response = player_1.step(
88
+ str_mes(first_round_prompt + grantee)).msgs[0]
89
+ ans = match_and_compare_numbers_v2(player_1_response.content)
90
+ if ans:
91
+ given_num = ans
92
+ else:
93
+ given_num = extract_unique_decimal(
94
+ cri_agent.step(
95
+ str_mes(player_1_response.content)).msgs[0].content
96
+ )
97
+
98
+ money_prompt = money_prompt.format(
99
+ give=given_num, N=given_num * k, left=10 - given_num
100
+ )
101
+ if user_role == "trustee":
102
+ player_2_response = str_mes(
103
+ user_input_prompt_template.format(k=given_num * k))
104
+ else:
105
+ player_2_response = player_2.step(
106
+ str_mes(money_prompt + grantee)).msgs[0]
107
+ else:
108
+ if user_role == "trustor":
109
+ given_num = user_input
110
+ player_1_response = str_mes(
111
+ user_input_prompt_template.format(k=given_num))
112
+ else:
113
+ player_1_response = player_1.step(
114
+ str_mes(first_prompt + grantee)).msgs[0]
115
+ # print("player 1 input", first_prompt)
116
+ # print("Player_1_res", player_1_response.content)
117
+ ans = match_and_compare_numbers_v2(player_1_response.content)
118
+ if ans:
119
+ given_num = ans
120
+ else:
121
+ given_num = extract_unique_decimal(
122
+ cri_agent.step(
123
+ str_mes(player_1_response.content)).msgs[0].content
124
+ )
125
+ money_prompt = money_prompt.format(
126
+ give=given_num, N=given_num * k, left=10 - given_num
127
+ )
128
+ if user_role == "trustee":
129
+ player_2_response = str_mes(
130
+ user_input_prompt_template.format(k=given_num * k))
131
+ else:
132
+ player_2_response = player_2.step(
133
+ str_mes(second_prompt + money_prompt + grantee)
134
+ )
135
+ player_2_response = player_2_response.msgs[0]
136
+
137
+ player_1.record_message(player_1_response)
138
+ player_2.record_message(player_2_response)
139
+ player_1_response = player_1_response.content
140
+ player_2_response = player_2_response.content
141
+ dia_history = {
142
+ "Player_1": player_1_response,
143
+ "Player_2": player_2_response,
144
+ }
145
+
146
+ cri_agent.reset()
147
+ ans = match_and_compare_numbers_v2(player_2_response)
148
+ if ans:
149
+ return_num = ans
150
+ else:
151
+ return_num = extract_unique_decimal(
152
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
153
+ )
154
+ return_money_prompt = return_money_prompt.format(
155
+ give=given_num,
156
+ receive=given_num * k,
157
+ N=return_num,
158
+ left=10 - given_num + return_num,
159
+ )
160
+ player_2_end_prompt = player_2_end_prompt.format(
161
+ give=given_num,
162
+ receive=given_num * k,
163
+ N=return_num,
164
+ left=given_num * k - return_num,
165
+ )
166
+ return (
167
+ [given_num, return_num],
168
+ dia_history,
169
+ return_money_prompt,
170
+ player_2_end_prompt,
171
+ )
172
+
173
+
174
+ def classmate_with_human_trustee(
175
+ player_1,
176
+ player_2,
177
+ user_input,
178
+ user_role,
179
+ first_round,
180
+ given_money,
181
+ k,
182
+ ):
183
+ first_round_prompt = "This is the first round, answer the question."
184
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
185
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
186
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
187
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
188
+ res = []
189
+ cri_agent = ChatAgent(
190
+ BaseMessage(
191
+ role_name="critic",
192
+ role_type=RoleType.ASSISTANT,
193
+ meta_dict={},
194
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
195
+ ),
196
+ output_language="English",
197
+ # model=ModelType.STUB,
198
+ )
199
+ content = []
200
+ if first_round:
201
+ content.append("\n")
202
+ player_1_response = player_1.step(
203
+ str_mes(first_round_prompt + grantee)).msgs[0]
204
+ ans = match_and_compare_numbers_v2(player_1_response.content)
205
+ if ans:
206
+ given_num = ans
207
+ else:
208
+ given_num = extract_unique_decimal(
209
+ cri_agent.step(
210
+ str_mes(player_1_response.content)).msgs[0].content
211
+ )
212
+ money_prompt = money_prompt.format(
213
+ give=given_num, N=given_num * k, left=10 - given_num
214
+ )
215
+ else:
216
+ given_num = given_money
217
+
218
+ return_num = user_input
219
+ content.append("Trustee: " + f"I will give {return_num} dollars")
220
+ first_prompt = return_money_prompt.format(
221
+ give=given_num,
222
+ receive=given_num * k,
223
+ N=return_num,
224
+ left=10 - given_num + return_num,
225
+ )
226
+ second_prompt = player_2_end_prompt.format(
227
+ give=given_num,
228
+ receive=given_num * k,
229
+ N=return_num,
230
+ left=given_num * k - return_num,
231
+ )
232
+ player_1_response = player_1.step(
233
+ str_mes(first_prompt + grantee)).msgs[0]
234
+ ans = match_and_compare_numbers_v2(player_1_response.content)
235
+ if ans:
236
+ given_num = ans
237
+ else:
238
+ given_num = extract_unique_decimal(
239
+ cri_agent.step(
240
+ str_mes(player_1_response.content)).msgs[0].content
241
+ )
242
+ money_prompt = money_prompt.format(
243
+ give=given_num, N=given_num * k, left=10 - given_num
244
+ )
245
+
246
+ player_1.record_message(player_1_response)
247
+ player_1_response = player_1_response.content
248
+ content.append("Trustor: "+player_1_response + "\n" +
249
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
250
+ given_num * k))
251
+ return content, given_num
252
+
253
+
254
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
255
+ model_dropdown, api_key_input, round_number_input):
256
+ if api_key_input is None or api_key_input == "":
257
+ api_key_input = os.environ.get("OPENAI_API_KEY")
258
+ else:
259
+ os.environ["OPENAI_API_KEY"] = api_key_input
260
+ random_character = [character_info[trustor_character_dropdown],
261
+ character_info[trustee_character_dropdown]]
262
+ chat_agent = []
263
+ front = "you are a person not an ai model."
264
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
265
+ back = "you need to answer a specific price figure, not a price range!"
266
+ if api_key_input is not None or api_key_input != "":
267
+ openai.api_key = api_key_input
268
+ else:
269
+ openai.api_key = os.getenv("OPENAI_API_KEY")
270
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
271
+ raise ValueError("You can't play with yourself")
272
+ for i in range(len(random_character)):
273
+ sys_prompt = (
274
+ random_character[i]
275
+ + like_people
276
+ + front
277
+ + limited_prompt
278
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
279
+ + back
280
+ )
281
+ model_config = ChatGPTConfig(temperature=temperature_slider)
282
+ chat_agent.append(
283
+ ChatAgent(
284
+ BaseMessage(
285
+ role_name="player",
286
+ role_type=RoleType.USER,
287
+ meta_dict={},
288
+ content=sys_prompt,
289
+ ),
290
+ model_type=model_dict[model_dropdown],
291
+ output_language="English",
292
+ model_config=model_config,
293
+ )
294
+ )
295
+ return chat_agent, "ChatAgent Created Successfully"
296
+
297
+
298
+ def process_interaction(chat_agent_state, round_num_state,
299
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
300
+ identity_dropdown = [trustor, trustee]
301
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
302
+ if round_num_state < round_number_input:
303
+ res, dia, first_prompt, second_prompt = classmate(
304
+ chat_agent_state[0],
305
+ chat_agent_state[1],
306
+ round_num_state == 0,
307
+ first_prompt,
308
+ second_prompt,
309
+ 3,
310
+ )
311
+ dia = "Trustor: "+dia['Player_1'] + \
312
+ "\n" + "Trustee: "+dia['Player_2']
313
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
314
+ round_num_state += 1
315
+ elif identity_dropdown[0] == "Human(You)":
316
+ if round_num_state < round_number_input:
317
+ res, dia, first_prompt, second_prompt = classmate_with_human(
318
+ chat_agent_state[0],
319
+ chat_agent_state[1],
320
+ user_input,
321
+ "trustor",
322
+ round_num_state == 0,
323
+ first_prompt,
324
+ second_prompt,
325
+ 3,
326
+ )
327
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
328
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
329
+ dia['Player_2']+"\n"
330
+ dialog_history_state += dia
331
+ round_num_state += 1
332
+ else:
333
+ if round_num_state < round_number_input:
334
+ dia, given_money = classmate_with_human_trustee(
335
+ chat_agent_state[1],
336
+ chat_agent_state[0],
337
+ user_input,
338
+ "trustee",
339
+ round_num_state == 0,
340
+ given_money,
341
+ 3,
342
+ )
343
+ if round_num_state == 0:
344
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
345
+ else:
346
+ dialog_history_state += f"(Round {round_num_state})" + \
347
+ dia[0] + "\n" + \
348
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
349
+ round_num_state += 1
350
+
351
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
352
+
353
+
354
+ def reset_on_persona_change():
355
+ # Reset values to their initial states
356
+ new_dialog_history = "" # Reset dialog history
357
+ new_round_num_state = 0 # Reset round number state to 0
358
+ new_first_prompt = "" # Reset first prompt
359
+ new_second_prompt = "" # Reset second prompt
360
+ new_given_money = 0 # Reset given money to 0
361
+ # You can add more resets here if needed
362
+
363
+ # Return the new reset values to their respective components
364
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
365
+
366
+
367
+ with gr.Blocks() as app:
368
+ game_introduction = gr.Textbox(
369
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
370
+ 2. You need to fill in your OpenAI API KeyYou can select the total number of rounds for the game.\n
371
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
372
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
373
+ 5. If you want to reset the conversation, please refresh this page.\n""")
374
+ with gr.Row():
375
+ trustor_game_prompt = gr.Textbox(
376
+ label="Trustor Game Prompt", value=prompt['1'])
377
+ trustee_game_prompt = gr.Textbox(
378
+ label="Trustee Game Prompt", value=prompt['2'])
379
+ with gr.Row():
380
+ trustor_character_dropdown = gr.Dropdown(
381
+ choices=characters, label="Select Trustor Persona", value=characters[1])
382
+ trustee_character_dropdown = gr.Dropdown(
383
+ choices=characters, label="Select Trustee Persona", value=characters[1])
384
+ with gr.Row():
385
+ Trustor_info_display = gr.Textbox(
386
+ label="Trustor Persona Info", value=character_info[characters[1]])
387
+ Trustee_info_display = gr.Textbox(
388
+ label="Trustee Persona Info", value=character_info[characters[1]])
389
+ model_dropdown = gr.Dropdown(
390
+ choices=models, label="Select Model Type", value=models[0])
391
+ temperature_slider = gr.Slider(
392
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
393
+ api_key_input = gr.Textbox(
394
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
395
+ submit_button = gr.Button("Create ChatAgent")
396
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
397
+
398
+ round_number_input = gr.Number(label="Total Round Number", value=10)
399
+ round_num_state = gr.State(value=0)
400
+ dialog_history_state = gr.State(value="")
401
+ chat_agent_state = gr.State(value=initial_chat_agent)
402
+ first_prompt = gr.State(value="")
403
+ second_prompt = gr.State(value="")
404
+ given_money = gr.State(value=0)
405
+ user_input = gr.Number(
406
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
407
+ converse_button = gr.Button("Continue Conversation")
408
+
409
+ dialog_display = gr.Textbox(
410
+ label="Dialog History", value="")
411
+
412
+ trustor_character_dropdown.change(
413
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
414
+ trustee_character_dropdown.change(
415
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
416
+
417
+ submit_button.click(
418
+ create_chat_agent,
419
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
420
+ model_dropdown, api_key_input, round_number_input],
421
+ outputs=[chat_agent_state, submit_success_display]
422
+ )
423
+ converse_button.click(
424
+ process_interaction,
425
+ inputs=[chat_agent_state, round_num_state,
426
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
427
+ outputs=[dialog_display, round_num_state,
428
+ first_prompt, second_prompt, dialog_history_state, given_money]
429
+ )
430
+ trustor_character_dropdown.change(
431
+ reset_on_persona_change,
432
+ outputs=[dialog_history_state, round_num_state,
433
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
434
+ )
435
+
436
+ trustee_character_dropdown.change(
437
+ reset_on_persona_change,
438
+ outputs=[dialog_history_state, round_num_state,
439
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
440
+ )
441
+
442
+
443
+ app.launch()
.history/app_20240222100205.py ADDED
@@ -0,0 +1,444 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ initial_dialog_history = []
43
+ initial_round_num = 0
44
+
45
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
46
+
47
+ initial_chat_agent = None
48
+
49
+
50
+ def update_char_info(char):
51
+ return character_info.get(char, "No information available.")
52
+
53
+
54
+ def classmate_with_human(
55
+ player_1,
56
+ player_2,
57
+ user_input,
58
+ user_role,
59
+ first_round,
60
+ first_prompt,
61
+ second_prompt,
62
+ k,
63
+ ):
64
+ first_round_prompt = "This is the first round, answer the question."
65
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
66
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
67
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
68
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
69
+ res = []
70
+ cri_agent = ChatAgent(
71
+ BaseMessage(
72
+ role_name="critic",
73
+ role_type=RoleType.ASSISTANT,
74
+ meta_dict={},
75
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
76
+ ),
77
+ output_language="English",
78
+ # model=ModelType.STUB,
79
+ )
80
+
81
+ if first_round:
82
+ if user_role == "trustor":
83
+ given_num = user_input
84
+ player_1_response = str_mes(
85
+ user_input_prompt_template.format(k=given_num))
86
+ else:
87
+ player_1_response = player_1.step(
88
+ str_mes(first_round_prompt + grantee)).msgs[0]
89
+ ans = match_and_compare_numbers_v2(player_1_response.content)
90
+ if ans:
91
+ given_num = ans
92
+ else:
93
+ given_num = extract_unique_decimal(
94
+ cri_agent.step(
95
+ str_mes(player_1_response.content)).msgs[0].content
96
+ )
97
+
98
+ money_prompt = money_prompt.format(
99
+ give=given_num, N=given_num * k, left=10 - given_num
100
+ )
101
+ if user_role == "trustee":
102
+ player_2_response = str_mes(
103
+ user_input_prompt_template.format(k=given_num * k))
104
+ else:
105
+ player_2_response = player_2.step(
106
+ str_mes(money_prompt + grantee)).msgs[0]
107
+ else:
108
+ if user_role == "trustor":
109
+ given_num = user_input
110
+ player_1_response = str_mes(
111
+ user_input_prompt_template.format(k=given_num))
112
+ else:
113
+ player_1_response = player_1.step(
114
+ str_mes(first_prompt + grantee)).msgs[0]
115
+ # print("player 1 input", first_prompt)
116
+ # print("Player_1_res", player_1_response.content)
117
+ ans = match_and_compare_numbers_v2(player_1_response.content)
118
+ if ans:
119
+ given_num = ans
120
+ else:
121
+ given_num = extract_unique_decimal(
122
+ cri_agent.step(
123
+ str_mes(player_1_response.content)).msgs[0].content
124
+ )
125
+ money_prompt = money_prompt.format(
126
+ give=given_num, N=given_num * k, left=10 - given_num
127
+ )
128
+ if user_role == "trustee":
129
+ player_2_response = str_mes(
130
+ user_input_prompt_template.format(k=given_num * k))
131
+ else:
132
+ player_2_response = player_2.step(
133
+ str_mes(second_prompt + money_prompt + grantee)
134
+ )
135
+ player_2_response = player_2_response.msgs[0]
136
+
137
+ player_1.record_message(player_1_response)
138
+ player_2.record_message(player_2_response)
139
+ player_1_response = player_1_response.content
140
+ player_2_response = player_2_response.content
141
+ dia_history = {
142
+ "Player_1": player_1_response,
143
+ "Player_2": player_2_response,
144
+ }
145
+
146
+ cri_agent.reset()
147
+ ans = match_and_compare_numbers_v2(player_2_response)
148
+ if ans:
149
+ return_num = ans
150
+ else:
151
+ return_num = extract_unique_decimal(
152
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
153
+ )
154
+ return_money_prompt = return_money_prompt.format(
155
+ give=given_num,
156
+ receive=given_num * k,
157
+ N=return_num,
158
+ left=10 - given_num + return_num,
159
+ )
160
+ player_2_end_prompt = player_2_end_prompt.format(
161
+ give=given_num,
162
+ receive=given_num * k,
163
+ N=return_num,
164
+ left=given_num * k - return_num,
165
+ )
166
+ return (
167
+ [given_num, return_num],
168
+ dia_history,
169
+ return_money_prompt,
170
+ player_2_end_prompt,
171
+ )
172
+
173
+
174
+ def classmate_with_human_trustee(
175
+ player_1,
176
+ player_2,
177
+ user_input,
178
+ user_role,
179
+ first_round,
180
+ given_money,
181
+ k,
182
+ ):
183
+ first_round_prompt = "This is the first round, answer the question."
184
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
185
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
186
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
187
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
188
+ res = []
189
+ cri_agent = ChatAgent(
190
+ BaseMessage(
191
+ role_name="critic",
192
+ role_type=RoleType.ASSISTANT,
193
+ meta_dict={},
194
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
195
+ ),
196
+ output_language="English",
197
+ # model=ModelType.STUB,
198
+ )
199
+ content = []
200
+ if first_round:
201
+ content.append("\n")
202
+ player_1_response = player_1.step(
203
+ str_mes(first_round_prompt + grantee)).msgs[0]
204
+ ans = match_and_compare_numbers_v2(player_1_response.content)
205
+ if ans:
206
+ given_num = ans
207
+ else:
208
+ given_num = extract_unique_decimal(
209
+ cri_agent.step(
210
+ str_mes(player_1_response.content)).msgs[0].content
211
+ )
212
+ money_prompt = money_prompt.format(
213
+ give=given_num, N=given_num * k, left=10 - given_num
214
+ )
215
+ else:
216
+ given_num = given_money
217
+
218
+ return_num = user_input
219
+ content.append("Trustee: " + f"I will give {return_num} dollars")
220
+ first_prompt = return_money_prompt.format(
221
+ give=given_num,
222
+ receive=given_num * k,
223
+ N=return_num,
224
+ left=10 - given_num + return_num,
225
+ )
226
+ second_prompt = player_2_end_prompt.format(
227
+ give=given_num,
228
+ receive=given_num * k,
229
+ N=return_num,
230
+ left=given_num * k - return_num,
231
+ )
232
+ player_1_response = player_1.step(
233
+ str_mes(first_prompt + grantee)).msgs[0]
234
+ ans = match_and_compare_numbers_v2(player_1_response.content)
235
+ if ans:
236
+ given_num = ans
237
+ else:
238
+ given_num = extract_unique_decimal(
239
+ cri_agent.step(
240
+ str_mes(player_1_response.content)).msgs[0].content
241
+ )
242
+ money_prompt = money_prompt.format(
243
+ give=given_num, N=given_num * k, left=10 - given_num
244
+ )
245
+
246
+ player_1.record_message(player_1_response)
247
+ player_1_response = player_1_response.content
248
+ content.append("Trustor: "+player_1_response + "\n" +
249
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
250
+ given_num * k))
251
+ return content, given_num
252
+
253
+
254
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
255
+ model_dropdown, api_key_input, round_number_input):
256
+ if api_key_input is None or api_key_input == "":
257
+ api_key_input = os.environ.get("OPENAI_API_KEY")
258
+ else:
259
+ os.environ["OPENAI_API_KEY"] = api_key_input
260
+ random_character = [character_info[trustor_character_dropdown],
261
+ character_info[trustee_character_dropdown]]
262
+ chat_agent = []
263
+ front = "you are a person not an ai model."
264
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
265
+ back = "you need to answer a specific price figure, not a price range!"
266
+ if api_key_input is not None or api_key_input != "":
267
+ openai.api_key = api_key_input
268
+ else:
269
+ openai.api_key = os.getenv("OPENAI_API_KEY")
270
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
271
+ raise ValueError("You can't play with yourself")
272
+ for i in range(len(random_character)):
273
+ sys_prompt = (
274
+ random_character[i]
275
+ + like_people
276
+ + front
277
+ + limited_prompt
278
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
279
+ + back
280
+ )
281
+ model_config = ChatGPTConfig(temperature=temperature_slider)
282
+ chat_agent.append(
283
+ ChatAgent(
284
+ BaseMessage(
285
+ role_name="player",
286
+ role_type=RoleType.USER,
287
+ meta_dict={},
288
+ content=sys_prompt,
289
+ ),
290
+ model_type=model_dict[model_dropdown],
291
+ output_language="English",
292
+ model_config=model_config,
293
+ )
294
+ )
295
+ return chat_agent, "ChatAgent Created Successfully"
296
+
297
+
298
+ def process_interaction(chat_agent_state, round_num_state,
299
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
300
+ identity_dropdown = [trustor, trustee]
301
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
302
+ if round_num_state < round_number_input:
303
+ res, dia, first_prompt, second_prompt = classmate(
304
+ chat_agent_state[0],
305
+ chat_agent_state[1],
306
+ round_num_state == 0,
307
+ first_prompt,
308
+ second_prompt,
309
+ 3,
310
+ )
311
+ dia = "Trustor: "+dia['Player_1'] + \
312
+ "\n" + "Trustee: "+dia['Player_2']
313
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
314
+ round_num_state += 1
315
+ elif identity_dropdown[0] == "Human(You)":
316
+ if round_num_state < round_number_input:
317
+ res, dia, first_prompt, second_prompt = classmate_with_human(
318
+ chat_agent_state[0],
319
+ chat_agent_state[1],
320
+ user_input,
321
+ "trustor",
322
+ round_num_state == 0,
323
+ first_prompt,
324
+ second_prompt,
325
+ 3,
326
+ )
327
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
328
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
329
+ dia['Player_2']+"\n"
330
+ dialog_history_state += dia
331
+ round_num_state += 1
332
+ else:
333
+ if round_num_state < round_number_input:
334
+ dia, given_money = classmate_with_human_trustee(
335
+ chat_agent_state[1],
336
+ chat_agent_state[0],
337
+ user_input,
338
+ "trustee",
339
+ round_num_state == 0,
340
+ given_money,
341
+ 3,
342
+ )
343
+ if round_num_state == 0:
344
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
345
+ else:
346
+ dialog_history_state += f"(Round {round_num_state})" + \
347
+ dia[0] + "\n" + \
348
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
349
+ round_num_state += 1
350
+
351
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
352
+
353
+
354
+ def reset_on_persona_change():
355
+ # Reset values to their initial states
356
+ new_dialog_history = "" # Reset dialog history
357
+ new_round_num_state = 0 # Reset round number state to 0
358
+ new_first_prompt = "" # Reset first prompt
359
+ new_second_prompt = "" # Reset second prompt
360
+ new_given_money = 0 # Reset given money to 0
361
+ # You can add more resets here if needed
362
+
363
+ # Return the new reset values to their respective components
364
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
365
+
366
+
367
+ with gr.Blocks() as app:
368
+ game_introduction = gr.Textbox(
369
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
370
+ 2. You need to fill in your OpenAI API Key.
371
+ You can select the total number of rounds for the game.\n
372
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
373
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
374
+ 5. If you want to reset the conversation, please refresh this page.\n""")
375
+ with gr.Row():
376
+ trustor_game_prompt = gr.Textbox(
377
+ label="Trustor Game Prompt", value=prompt['1'])
378
+ trustee_game_prompt = gr.Textbox(
379
+ label="Trustee Game Prompt", value=prompt['2'])
380
+ with gr.Row():
381
+ trustor_character_dropdown = gr.Dropdown(
382
+ choices=characters, label="Select Trustor Persona", value=characters[1])
383
+ trustee_character_dropdown = gr.Dropdown(
384
+ choices=characters, label="Select Trustee Persona", value=characters[1])
385
+ with gr.Row():
386
+ Trustor_info_display = gr.Textbox(
387
+ label="Trustor Persona Info", value=character_info[characters[1]])
388
+ Trustee_info_display = gr.Textbox(
389
+ label="Trustee Persona Info", value=character_info[characters[1]])
390
+ model_dropdown = gr.Dropdown(
391
+ choices=models, label="Select Model Type", value=models[0])
392
+ temperature_slider = gr.Slider(
393
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
394
+ api_key_input = gr.Textbox(
395
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
396
+ submit_button = gr.Button("Create ChatAgent")
397
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
398
+
399
+ round_number_input = gr.Number(label="Total Round Number", value=10)
400
+ round_num_state = gr.State(value=0)
401
+ dialog_history_state = gr.State(value="")
402
+ chat_agent_state = gr.State(value=initial_chat_agent)
403
+ first_prompt = gr.State(value="")
404
+ second_prompt = gr.State(value="")
405
+ given_money = gr.State(value=0)
406
+ user_input = gr.Number(
407
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
408
+ converse_button = gr.Button("Continue Conversation")
409
+
410
+ dialog_display = gr.Textbox(
411
+ label="Dialog History", value="")
412
+
413
+ trustor_character_dropdown.change(
414
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
415
+ trustee_character_dropdown.change(
416
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
417
+
418
+ submit_button.click(
419
+ create_chat_agent,
420
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
421
+ model_dropdown, api_key_input, round_number_input],
422
+ outputs=[chat_agent_state, submit_success_display]
423
+ )
424
+ converse_button.click(
425
+ process_interaction,
426
+ inputs=[chat_agent_state, round_num_state,
427
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
428
+ outputs=[dialog_display, round_num_state,
429
+ first_prompt, second_prompt, dialog_history_state, given_money]
430
+ )
431
+ trustor_character_dropdown.change(
432
+ reset_on_persona_change,
433
+ outputs=[dialog_history_state, round_num_state,
434
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
435
+ )
436
+
437
+ trustee_character_dropdown.change(
438
+ reset_on_persona_change,
439
+ outputs=[dialog_history_state, round_num_state,
440
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
441
+ )
442
+
443
+
444
+ app.launch()
.history/app_20240222100208.py ADDED
@@ -0,0 +1,444 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ initial_dialog_history = []
43
+ initial_round_num = 0
44
+
45
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
46
+
47
+ initial_chat_agent = None
48
+
49
+
50
+ def update_char_info(char):
51
+ return character_info.get(char, "No information available.")
52
+
53
+
54
+ def classmate_with_human(
55
+ player_1,
56
+ player_2,
57
+ user_input,
58
+ user_role,
59
+ first_round,
60
+ first_prompt,
61
+ second_prompt,
62
+ k,
63
+ ):
64
+ first_round_prompt = "This is the first round, answer the question."
65
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
66
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
67
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
68
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
69
+ res = []
70
+ cri_agent = ChatAgent(
71
+ BaseMessage(
72
+ role_name="critic",
73
+ role_type=RoleType.ASSISTANT,
74
+ meta_dict={},
75
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
76
+ ),
77
+ output_language="English",
78
+ # model=ModelType.STUB,
79
+ )
80
+
81
+ if first_round:
82
+ if user_role == "trustor":
83
+ given_num = user_input
84
+ player_1_response = str_mes(
85
+ user_input_prompt_template.format(k=given_num))
86
+ else:
87
+ player_1_response = player_1.step(
88
+ str_mes(first_round_prompt + grantee)).msgs[0]
89
+ ans = match_and_compare_numbers_v2(player_1_response.content)
90
+ if ans:
91
+ given_num = ans
92
+ else:
93
+ given_num = extract_unique_decimal(
94
+ cri_agent.step(
95
+ str_mes(player_1_response.content)).msgs[0].content
96
+ )
97
+
98
+ money_prompt = money_prompt.format(
99
+ give=given_num, N=given_num * k, left=10 - given_num
100
+ )
101
+ if user_role == "trustee":
102
+ player_2_response = str_mes(
103
+ user_input_prompt_template.format(k=given_num * k))
104
+ else:
105
+ player_2_response = player_2.step(
106
+ str_mes(money_prompt + grantee)).msgs[0]
107
+ else:
108
+ if user_role == "trustor":
109
+ given_num = user_input
110
+ player_1_response = str_mes(
111
+ user_input_prompt_template.format(k=given_num))
112
+ else:
113
+ player_1_response = player_1.step(
114
+ str_mes(first_prompt + grantee)).msgs[0]
115
+ # print("player 1 input", first_prompt)
116
+ # print("Player_1_res", player_1_response.content)
117
+ ans = match_and_compare_numbers_v2(player_1_response.content)
118
+ if ans:
119
+ given_num = ans
120
+ else:
121
+ given_num = extract_unique_decimal(
122
+ cri_agent.step(
123
+ str_mes(player_1_response.content)).msgs[0].content
124
+ )
125
+ money_prompt = money_prompt.format(
126
+ give=given_num, N=given_num * k, left=10 - given_num
127
+ )
128
+ if user_role == "trustee":
129
+ player_2_response = str_mes(
130
+ user_input_prompt_template.format(k=given_num * k))
131
+ else:
132
+ player_2_response = player_2.step(
133
+ str_mes(second_prompt + money_prompt + grantee)
134
+ )
135
+ player_2_response = player_2_response.msgs[0]
136
+
137
+ player_1.record_message(player_1_response)
138
+ player_2.record_message(player_2_response)
139
+ player_1_response = player_1_response.content
140
+ player_2_response = player_2_response.content
141
+ dia_history = {
142
+ "Player_1": player_1_response,
143
+ "Player_2": player_2_response,
144
+ }
145
+
146
+ cri_agent.reset()
147
+ ans = match_and_compare_numbers_v2(player_2_response)
148
+ if ans:
149
+ return_num = ans
150
+ else:
151
+ return_num = extract_unique_decimal(
152
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
153
+ )
154
+ return_money_prompt = return_money_prompt.format(
155
+ give=given_num,
156
+ receive=given_num * k,
157
+ N=return_num,
158
+ left=10 - given_num + return_num,
159
+ )
160
+ player_2_end_prompt = player_2_end_prompt.format(
161
+ give=given_num,
162
+ receive=given_num * k,
163
+ N=return_num,
164
+ left=given_num * k - return_num,
165
+ )
166
+ return (
167
+ [given_num, return_num],
168
+ dia_history,
169
+ return_money_prompt,
170
+ player_2_end_prompt,
171
+ )
172
+
173
+
174
+ def classmate_with_human_trustee(
175
+ player_1,
176
+ player_2,
177
+ user_input,
178
+ user_role,
179
+ first_round,
180
+ given_money,
181
+ k,
182
+ ):
183
+ first_round_prompt = "This is the first round, answer the question."
184
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
185
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
186
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
187
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
188
+ res = []
189
+ cri_agent = ChatAgent(
190
+ BaseMessage(
191
+ role_name="critic",
192
+ role_type=RoleType.ASSISTANT,
193
+ meta_dict={},
194
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
195
+ ),
196
+ output_language="English",
197
+ # model=ModelType.STUB,
198
+ )
199
+ content = []
200
+ if first_round:
201
+ content.append("\n")
202
+ player_1_response = player_1.step(
203
+ str_mes(first_round_prompt + grantee)).msgs[0]
204
+ ans = match_and_compare_numbers_v2(player_1_response.content)
205
+ if ans:
206
+ given_num = ans
207
+ else:
208
+ given_num = extract_unique_decimal(
209
+ cri_agent.step(
210
+ str_mes(player_1_response.content)).msgs[0].content
211
+ )
212
+ money_prompt = money_prompt.format(
213
+ give=given_num, N=given_num * k, left=10 - given_num
214
+ )
215
+ else:
216
+ given_num = given_money
217
+
218
+ return_num = user_input
219
+ content.append("Trustee: " + f"I will give {return_num} dollars")
220
+ first_prompt = return_money_prompt.format(
221
+ give=given_num,
222
+ receive=given_num * k,
223
+ N=return_num,
224
+ left=10 - given_num + return_num,
225
+ )
226
+ second_prompt = player_2_end_prompt.format(
227
+ give=given_num,
228
+ receive=given_num * k,
229
+ N=return_num,
230
+ left=given_num * k - return_num,
231
+ )
232
+ player_1_response = player_1.step(
233
+ str_mes(first_prompt + grantee)).msgs[0]
234
+ ans = match_and_compare_numbers_v2(player_1_response.content)
235
+ if ans:
236
+ given_num = ans
237
+ else:
238
+ given_num = extract_unique_decimal(
239
+ cri_agent.step(
240
+ str_mes(player_1_response.content)).msgs[0].content
241
+ )
242
+ money_prompt = money_prompt.format(
243
+ give=given_num, N=given_num * k, left=10 - given_num
244
+ )
245
+
246
+ player_1.record_message(player_1_response)
247
+ player_1_response = player_1_response.content
248
+ content.append("Trustor: "+player_1_response + "\n" +
249
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
250
+ given_num * k))
251
+ return content, given_num
252
+
253
+
254
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
255
+ model_dropdown, api_key_input, round_number_input):
256
+ if api_key_input is None or api_key_input == "":
257
+ api_key_input = os.environ.get("OPENAI_API_KEY")
258
+ else:
259
+ os.environ["OPENAI_API_KEY"] = api_key_input
260
+ random_character = [character_info[trustor_character_dropdown],
261
+ character_info[trustee_character_dropdown]]
262
+ chat_agent = []
263
+ front = "you are a person not an ai model."
264
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
265
+ back = "you need to answer a specific price figure, not a price range!"
266
+ if api_key_input is not None or api_key_input != "":
267
+ openai.api_key = api_key_input
268
+ else:
269
+ openai.api_key = os.getenv("OPENAI_API_KEY")
270
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
271
+ raise ValueError("You can't play with yourself")
272
+ for i in range(len(random_character)):
273
+ sys_prompt = (
274
+ random_character[i]
275
+ + like_people
276
+ + front
277
+ + limited_prompt
278
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
279
+ + back
280
+ )
281
+ model_config = ChatGPTConfig(temperature=temperature_slider)
282
+ chat_agent.append(
283
+ ChatAgent(
284
+ BaseMessage(
285
+ role_name="player",
286
+ role_type=RoleType.USER,
287
+ meta_dict={},
288
+ content=sys_prompt,
289
+ ),
290
+ model_type=model_dict[model_dropdown],
291
+ output_language="English",
292
+ model_config=model_config,
293
+ )
294
+ )
295
+ return chat_agent, "ChatAgent Created Successfully"
296
+
297
+
298
+ def process_interaction(chat_agent_state, round_num_state,
299
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
300
+ identity_dropdown = [trustor, trustee]
301
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
302
+ if round_num_state < round_number_input:
303
+ res, dia, first_prompt, second_prompt = classmate(
304
+ chat_agent_state[0],
305
+ chat_agent_state[1],
306
+ round_num_state == 0,
307
+ first_prompt,
308
+ second_prompt,
309
+ 3,
310
+ )
311
+ dia = "Trustor: "+dia['Player_1'] + \
312
+ "\n" + "Trustee: "+dia['Player_2']
313
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
314
+ round_num_state += 1
315
+ elif identity_dropdown[0] == "Human(You)":
316
+ if round_num_state < round_number_input:
317
+ res, dia, first_prompt, second_prompt = classmate_with_human(
318
+ chat_agent_state[0],
319
+ chat_agent_state[1],
320
+ user_input,
321
+ "trustor",
322
+ round_num_state == 0,
323
+ first_prompt,
324
+ second_prompt,
325
+ 3,
326
+ )
327
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
328
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
329
+ dia['Player_2']+"\n"
330
+ dialog_history_state += dia
331
+ round_num_state += 1
332
+ else:
333
+ if round_num_state < round_number_input:
334
+ dia, given_money = classmate_with_human_trustee(
335
+ chat_agent_state[1],
336
+ chat_agent_state[0],
337
+ user_input,
338
+ "trustee",
339
+ round_num_state == 0,
340
+ given_money,
341
+ 3,
342
+ )
343
+ if round_num_state == 0:
344
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
345
+ else:
346
+ dialog_history_state += f"(Round {round_num_state})" + \
347
+ dia[0] + "\n" + \
348
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
349
+ round_num_state += 1
350
+
351
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
352
+
353
+
354
+ def reset_on_persona_change():
355
+ # Reset values to their initial states
356
+ new_dialog_history = "" # Reset dialog history
357
+ new_round_num_state = 0 # Reset round number state to 0
358
+ new_first_prompt = "" # Reset first prompt
359
+ new_second_prompt = "" # Reset second prompt
360
+ new_given_money = 0 # Reset given money to 0
361
+ # You can add more resets here if needed
362
+
363
+ # Return the new reset values to their respective components
364
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
365
+
366
+
367
+ with gr.Blocks() as app:
368
+ game_introduction = gr.Textbox(
369
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
370
+ 2. You need to fill in your OpenAI API Key.
371
+ 3. You can select the total number of rounds for the game.\n
372
+ 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
373
+ 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
374
+ 5. If you want to reset the conversation, please refresh this page.\n""")
375
+ with gr.Row():
376
+ trustor_game_prompt = gr.Textbox(
377
+ label="Trustor Game Prompt", value=prompt['1'])
378
+ trustee_game_prompt = gr.Textbox(
379
+ label="Trustee Game Prompt", value=prompt['2'])
380
+ with gr.Row():
381
+ trustor_character_dropdown = gr.Dropdown(
382
+ choices=characters, label="Select Trustor Persona", value=characters[1])
383
+ trustee_character_dropdown = gr.Dropdown(
384
+ choices=characters, label="Select Trustee Persona", value=characters[1])
385
+ with gr.Row():
386
+ Trustor_info_display = gr.Textbox(
387
+ label="Trustor Persona Info", value=character_info[characters[1]])
388
+ Trustee_info_display = gr.Textbox(
389
+ label="Trustee Persona Info", value=character_info[characters[1]])
390
+ model_dropdown = gr.Dropdown(
391
+ choices=models, label="Select Model Type", value=models[0])
392
+ temperature_slider = gr.Slider(
393
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
394
+ api_key_input = gr.Textbox(
395
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
396
+ submit_button = gr.Button("Create ChatAgent")
397
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
398
+
399
+ round_number_input = gr.Number(label="Total Round Number", value=10)
400
+ round_num_state = gr.State(value=0)
401
+ dialog_history_state = gr.State(value="")
402
+ chat_agent_state = gr.State(value=initial_chat_agent)
403
+ first_prompt = gr.State(value="")
404
+ second_prompt = gr.State(value="")
405
+ given_money = gr.State(value=0)
406
+ user_input = gr.Number(
407
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
408
+ converse_button = gr.Button("Continue Conversation")
409
+
410
+ dialog_display = gr.Textbox(
411
+ label="Dialog History", value="")
412
+
413
+ trustor_character_dropdown.change(
414
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
415
+ trustee_character_dropdown.change(
416
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
417
+
418
+ submit_button.click(
419
+ create_chat_agent,
420
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
421
+ model_dropdown, api_key_input, round_number_input],
422
+ outputs=[chat_agent_state, submit_success_display]
423
+ )
424
+ converse_button.click(
425
+ process_interaction,
426
+ inputs=[chat_agent_state, round_num_state,
427
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
428
+ outputs=[dialog_display, round_num_state,
429
+ first_prompt, second_prompt, dialog_history_state, given_money]
430
+ )
431
+ trustor_character_dropdown.change(
432
+ reset_on_persona_change,
433
+ outputs=[dialog_history_state, round_num_state,
434
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
435
+ )
436
+
437
+ trustee_character_dropdown.change(
438
+ reset_on_persona_change,
439
+ outputs=[dialog_history_state, round_num_state,
440
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
441
+ )
442
+
443
+
444
+ app.launch()
.history/app_20240222100212.py ADDED
@@ -0,0 +1,444 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ initial_dialog_history = []
43
+ initial_round_num = 0
44
+
45
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
46
+
47
+ initial_chat_agent = None
48
+
49
+
50
+ def update_char_info(char):
51
+ return character_info.get(char, "No information available.")
52
+
53
+
54
+ def classmate_with_human(
55
+ player_1,
56
+ player_2,
57
+ user_input,
58
+ user_role,
59
+ first_round,
60
+ first_prompt,
61
+ second_prompt,
62
+ k,
63
+ ):
64
+ first_round_prompt = "This is the first round, answer the question."
65
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
66
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
67
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
68
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
69
+ res = []
70
+ cri_agent = ChatAgent(
71
+ BaseMessage(
72
+ role_name="critic",
73
+ role_type=RoleType.ASSISTANT,
74
+ meta_dict={},
75
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
76
+ ),
77
+ output_language="English",
78
+ # model=ModelType.STUB,
79
+ )
80
+
81
+ if first_round:
82
+ if user_role == "trustor":
83
+ given_num = user_input
84
+ player_1_response = str_mes(
85
+ user_input_prompt_template.format(k=given_num))
86
+ else:
87
+ player_1_response = player_1.step(
88
+ str_mes(first_round_prompt + grantee)).msgs[0]
89
+ ans = match_and_compare_numbers_v2(player_1_response.content)
90
+ if ans:
91
+ given_num = ans
92
+ else:
93
+ given_num = extract_unique_decimal(
94
+ cri_agent.step(
95
+ str_mes(player_1_response.content)).msgs[0].content
96
+ )
97
+
98
+ money_prompt = money_prompt.format(
99
+ give=given_num, N=given_num * k, left=10 - given_num
100
+ )
101
+ if user_role == "trustee":
102
+ player_2_response = str_mes(
103
+ user_input_prompt_template.format(k=given_num * k))
104
+ else:
105
+ player_2_response = player_2.step(
106
+ str_mes(money_prompt + grantee)).msgs[0]
107
+ else:
108
+ if user_role == "trustor":
109
+ given_num = user_input
110
+ player_1_response = str_mes(
111
+ user_input_prompt_template.format(k=given_num))
112
+ else:
113
+ player_1_response = player_1.step(
114
+ str_mes(first_prompt + grantee)).msgs[0]
115
+ # print("player 1 input", first_prompt)
116
+ # print("Player_1_res", player_1_response.content)
117
+ ans = match_and_compare_numbers_v2(player_1_response.content)
118
+ if ans:
119
+ given_num = ans
120
+ else:
121
+ given_num = extract_unique_decimal(
122
+ cri_agent.step(
123
+ str_mes(player_1_response.content)).msgs[0].content
124
+ )
125
+ money_prompt = money_prompt.format(
126
+ give=given_num, N=given_num * k, left=10 - given_num
127
+ )
128
+ if user_role == "trustee":
129
+ player_2_response = str_mes(
130
+ user_input_prompt_template.format(k=given_num * k))
131
+ else:
132
+ player_2_response = player_2.step(
133
+ str_mes(second_prompt + money_prompt + grantee)
134
+ )
135
+ player_2_response = player_2_response.msgs[0]
136
+
137
+ player_1.record_message(player_1_response)
138
+ player_2.record_message(player_2_response)
139
+ player_1_response = player_1_response.content
140
+ player_2_response = player_2_response.content
141
+ dia_history = {
142
+ "Player_1": player_1_response,
143
+ "Player_2": player_2_response,
144
+ }
145
+
146
+ cri_agent.reset()
147
+ ans = match_and_compare_numbers_v2(player_2_response)
148
+ if ans:
149
+ return_num = ans
150
+ else:
151
+ return_num = extract_unique_decimal(
152
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
153
+ )
154
+ return_money_prompt = return_money_prompt.format(
155
+ give=given_num,
156
+ receive=given_num * k,
157
+ N=return_num,
158
+ left=10 - given_num + return_num,
159
+ )
160
+ player_2_end_prompt = player_2_end_prompt.format(
161
+ give=given_num,
162
+ receive=given_num * k,
163
+ N=return_num,
164
+ left=given_num * k - return_num,
165
+ )
166
+ return (
167
+ [given_num, return_num],
168
+ dia_history,
169
+ return_money_prompt,
170
+ player_2_end_prompt,
171
+ )
172
+
173
+
174
+ def classmate_with_human_trustee(
175
+ player_1,
176
+ player_2,
177
+ user_input,
178
+ user_role,
179
+ first_round,
180
+ given_money,
181
+ k,
182
+ ):
183
+ first_round_prompt = "This is the first round, answer the question."
184
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
185
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
186
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
187
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
188
+ res = []
189
+ cri_agent = ChatAgent(
190
+ BaseMessage(
191
+ role_name="critic",
192
+ role_type=RoleType.ASSISTANT,
193
+ meta_dict={},
194
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
195
+ ),
196
+ output_language="English",
197
+ # model=ModelType.STUB,
198
+ )
199
+ content = []
200
+ if first_round:
201
+ content.append("\n")
202
+ player_1_response = player_1.step(
203
+ str_mes(first_round_prompt + grantee)).msgs[0]
204
+ ans = match_and_compare_numbers_v2(player_1_response.content)
205
+ if ans:
206
+ given_num = ans
207
+ else:
208
+ given_num = extract_unique_decimal(
209
+ cri_agent.step(
210
+ str_mes(player_1_response.content)).msgs[0].content
211
+ )
212
+ money_prompt = money_prompt.format(
213
+ give=given_num, N=given_num * k, left=10 - given_num
214
+ )
215
+ else:
216
+ given_num = given_money
217
+
218
+ return_num = user_input
219
+ content.append("Trustee: " + f"I will give {return_num} dollars")
220
+ first_prompt = return_money_prompt.format(
221
+ give=given_num,
222
+ receive=given_num * k,
223
+ N=return_num,
224
+ left=10 - given_num + return_num,
225
+ )
226
+ second_prompt = player_2_end_prompt.format(
227
+ give=given_num,
228
+ receive=given_num * k,
229
+ N=return_num,
230
+ left=given_num * k - return_num,
231
+ )
232
+ player_1_response = player_1.step(
233
+ str_mes(first_prompt + grantee)).msgs[0]
234
+ ans = match_and_compare_numbers_v2(player_1_response.content)
235
+ if ans:
236
+ given_num = ans
237
+ else:
238
+ given_num = extract_unique_decimal(
239
+ cri_agent.step(
240
+ str_mes(player_1_response.content)).msgs[0].content
241
+ )
242
+ money_prompt = money_prompt.format(
243
+ give=given_num, N=given_num * k, left=10 - given_num
244
+ )
245
+
246
+ player_1.record_message(player_1_response)
247
+ player_1_response = player_1_response.content
248
+ content.append("Trustor: "+player_1_response + "\n" +
249
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
250
+ given_num * k))
251
+ return content, given_num
252
+
253
+
254
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
255
+ model_dropdown, api_key_input, round_number_input):
256
+ if api_key_input is None or api_key_input == "":
257
+ api_key_input = os.environ.get("OPENAI_API_KEY")
258
+ else:
259
+ os.environ["OPENAI_API_KEY"] = api_key_input
260
+ random_character = [character_info[trustor_character_dropdown],
261
+ character_info[trustee_character_dropdown]]
262
+ chat_agent = []
263
+ front = "you are a person not an ai model."
264
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
265
+ back = "you need to answer a specific price figure, not a price range!"
266
+ if api_key_input is not None or api_key_input != "":
267
+ openai.api_key = api_key_input
268
+ else:
269
+ openai.api_key = os.getenv("OPENAI_API_KEY")
270
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
271
+ raise ValueError("You can't play with yourself")
272
+ for i in range(len(random_character)):
273
+ sys_prompt = (
274
+ random_character[i]
275
+ + like_people
276
+ + front
277
+ + limited_prompt
278
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
279
+ + back
280
+ )
281
+ model_config = ChatGPTConfig(temperature=temperature_slider)
282
+ chat_agent.append(
283
+ ChatAgent(
284
+ BaseMessage(
285
+ role_name="player",
286
+ role_type=RoleType.USER,
287
+ meta_dict={},
288
+ content=sys_prompt,
289
+ ),
290
+ model_type=model_dict[model_dropdown],
291
+ output_language="English",
292
+ model_config=model_config,
293
+ )
294
+ )
295
+ return chat_agent, "ChatAgent Created Successfully"
296
+
297
+
298
+ def process_interaction(chat_agent_state, round_num_state,
299
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
300
+ identity_dropdown = [trustor, trustee]
301
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
302
+ if round_num_state < round_number_input:
303
+ res, dia, first_prompt, second_prompt = classmate(
304
+ chat_agent_state[0],
305
+ chat_agent_state[1],
306
+ round_num_state == 0,
307
+ first_prompt,
308
+ second_prompt,
309
+ 3,
310
+ )
311
+ dia = "Trustor: "+dia['Player_1'] + \
312
+ "\n" + "Trustee: "+dia['Player_2']
313
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
314
+ round_num_state += 1
315
+ elif identity_dropdown[0] == "Human(You)":
316
+ if round_num_state < round_number_input:
317
+ res, dia, first_prompt, second_prompt = classmate_with_human(
318
+ chat_agent_state[0],
319
+ chat_agent_state[1],
320
+ user_input,
321
+ "trustor",
322
+ round_num_state == 0,
323
+ first_prompt,
324
+ second_prompt,
325
+ 3,
326
+ )
327
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
328
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
329
+ dia['Player_2']+"\n"
330
+ dialog_history_state += dia
331
+ round_num_state += 1
332
+ else:
333
+ if round_num_state < round_number_input:
334
+ dia, given_money = classmate_with_human_trustee(
335
+ chat_agent_state[1],
336
+ chat_agent_state[0],
337
+ user_input,
338
+ "trustee",
339
+ round_num_state == 0,
340
+ given_money,
341
+ 3,
342
+ )
343
+ if round_num_state == 0:
344
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
345
+ else:
346
+ dialog_history_state += f"(Round {round_num_state})" + \
347
+ dia[0] + "\n" + \
348
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
349
+ round_num_state += 1
350
+
351
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
352
+
353
+
354
+ def reset_on_persona_change():
355
+ # Reset values to their initial states
356
+ new_dialog_history = "" # Reset dialog history
357
+ new_round_num_state = 0 # Reset round number state to 0
358
+ new_first_prompt = "" # Reset first prompt
359
+ new_second_prompt = "" # Reset second prompt
360
+ new_given_money = 0 # Reset given money to 0
361
+ # You can add more resets here if needed
362
+
363
+ # Return the new reset values to their respective components
364
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
365
+
366
+
367
+ with gr.Blocks() as app:
368
+ game_introduction = gr.Textbox(
369
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
370
+ 2. You need to fill in your OpenAI API Key.
371
+ 3. You can select the total number of rounds for the game.\n
372
+ 4. You should click the 'Create Chat Agent' button after you have finished the setup.\n
373
+ 5. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
374
+ 5. If you want to reset the conversation, please refresh this page.\n""")
375
+ with gr.Row():
376
+ trustor_game_prompt = gr.Textbox(
377
+ label="Trustor Game Prompt", value=prompt['1'])
378
+ trustee_game_prompt = gr.Textbox(
379
+ label="Trustee Game Prompt", value=prompt['2'])
380
+ with gr.Row():
381
+ trustor_character_dropdown = gr.Dropdown(
382
+ choices=characters, label="Select Trustor Persona", value=characters[1])
383
+ trustee_character_dropdown = gr.Dropdown(
384
+ choices=characters, label="Select Trustee Persona", value=characters[1])
385
+ with gr.Row():
386
+ Trustor_info_display = gr.Textbox(
387
+ label="Trustor Persona Info", value=character_info[characters[1]])
388
+ Trustee_info_display = gr.Textbox(
389
+ label="Trustee Persona Info", value=character_info[characters[1]])
390
+ model_dropdown = gr.Dropdown(
391
+ choices=models, label="Select Model Type", value=models[0])
392
+ temperature_slider = gr.Slider(
393
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
394
+ api_key_input = gr.Textbox(
395
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
396
+ submit_button = gr.Button("Create ChatAgent")
397
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
398
+
399
+ round_number_input = gr.Number(label="Total Round Number", value=10)
400
+ round_num_state = gr.State(value=0)
401
+ dialog_history_state = gr.State(value="")
402
+ chat_agent_state = gr.State(value=initial_chat_agent)
403
+ first_prompt = gr.State(value="")
404
+ second_prompt = gr.State(value="")
405
+ given_money = gr.State(value=0)
406
+ user_input = gr.Number(
407
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
408
+ converse_button = gr.Button("Continue Conversation")
409
+
410
+ dialog_display = gr.Textbox(
411
+ label="Dialog History", value="")
412
+
413
+ trustor_character_dropdown.change(
414
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
415
+ trustee_character_dropdown.change(
416
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
417
+
418
+ submit_button.click(
419
+ create_chat_agent,
420
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
421
+ model_dropdown, api_key_input, round_number_input],
422
+ outputs=[chat_agent_state, submit_success_display]
423
+ )
424
+ converse_button.click(
425
+ process_interaction,
426
+ inputs=[chat_agent_state, round_num_state,
427
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
428
+ outputs=[dialog_display, round_num_state,
429
+ first_prompt, second_prompt, dialog_history_state, given_money]
430
+ )
431
+ trustor_character_dropdown.change(
432
+ reset_on_persona_change,
433
+ outputs=[dialog_history_state, round_num_state,
434
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
435
+ )
436
+
437
+ trustee_character_dropdown.change(
438
+ reset_on_persona_change,
439
+ outputs=[dialog_history_state, round_num_state,
440
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
441
+ )
442
+
443
+
444
+ app.launch()
.history/app_20240222100214.py ADDED
@@ -0,0 +1,444 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ initial_dialog_history = []
43
+ initial_round_num = 0
44
+
45
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
46
+
47
+ initial_chat_agent = None
48
+
49
+
50
+ def update_char_info(char):
51
+ return character_info.get(char, "No information available.")
52
+
53
+
54
+ def classmate_with_human(
55
+ player_1,
56
+ player_2,
57
+ user_input,
58
+ user_role,
59
+ first_round,
60
+ first_prompt,
61
+ second_prompt,
62
+ k,
63
+ ):
64
+ first_round_prompt = "This is the first round, answer the question."
65
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
66
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
67
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
68
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
69
+ res = []
70
+ cri_agent = ChatAgent(
71
+ BaseMessage(
72
+ role_name="critic",
73
+ role_type=RoleType.ASSISTANT,
74
+ meta_dict={},
75
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
76
+ ),
77
+ output_language="English",
78
+ # model=ModelType.STUB,
79
+ )
80
+
81
+ if first_round:
82
+ if user_role == "trustor":
83
+ given_num = user_input
84
+ player_1_response = str_mes(
85
+ user_input_prompt_template.format(k=given_num))
86
+ else:
87
+ player_1_response = player_1.step(
88
+ str_mes(first_round_prompt + grantee)).msgs[0]
89
+ ans = match_and_compare_numbers_v2(player_1_response.content)
90
+ if ans:
91
+ given_num = ans
92
+ else:
93
+ given_num = extract_unique_decimal(
94
+ cri_agent.step(
95
+ str_mes(player_1_response.content)).msgs[0].content
96
+ )
97
+
98
+ money_prompt = money_prompt.format(
99
+ give=given_num, N=given_num * k, left=10 - given_num
100
+ )
101
+ if user_role == "trustee":
102
+ player_2_response = str_mes(
103
+ user_input_prompt_template.format(k=given_num * k))
104
+ else:
105
+ player_2_response = player_2.step(
106
+ str_mes(money_prompt + grantee)).msgs[0]
107
+ else:
108
+ if user_role == "trustor":
109
+ given_num = user_input
110
+ player_1_response = str_mes(
111
+ user_input_prompt_template.format(k=given_num))
112
+ else:
113
+ player_1_response = player_1.step(
114
+ str_mes(first_prompt + grantee)).msgs[0]
115
+ # print("player 1 input", first_prompt)
116
+ # print("Player_1_res", player_1_response.content)
117
+ ans = match_and_compare_numbers_v2(player_1_response.content)
118
+ if ans:
119
+ given_num = ans
120
+ else:
121
+ given_num = extract_unique_decimal(
122
+ cri_agent.step(
123
+ str_mes(player_1_response.content)).msgs[0].content
124
+ )
125
+ money_prompt = money_prompt.format(
126
+ give=given_num, N=given_num * k, left=10 - given_num
127
+ )
128
+ if user_role == "trustee":
129
+ player_2_response = str_mes(
130
+ user_input_prompt_template.format(k=given_num * k))
131
+ else:
132
+ player_2_response = player_2.step(
133
+ str_mes(second_prompt + money_prompt + grantee)
134
+ )
135
+ player_2_response = player_2_response.msgs[0]
136
+
137
+ player_1.record_message(player_1_response)
138
+ player_2.record_message(player_2_response)
139
+ player_1_response = player_1_response.content
140
+ player_2_response = player_2_response.content
141
+ dia_history = {
142
+ "Player_1": player_1_response,
143
+ "Player_2": player_2_response,
144
+ }
145
+
146
+ cri_agent.reset()
147
+ ans = match_and_compare_numbers_v2(player_2_response)
148
+ if ans:
149
+ return_num = ans
150
+ else:
151
+ return_num = extract_unique_decimal(
152
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
153
+ )
154
+ return_money_prompt = return_money_prompt.format(
155
+ give=given_num,
156
+ receive=given_num * k,
157
+ N=return_num,
158
+ left=10 - given_num + return_num,
159
+ )
160
+ player_2_end_prompt = player_2_end_prompt.format(
161
+ give=given_num,
162
+ receive=given_num * k,
163
+ N=return_num,
164
+ left=given_num * k - return_num,
165
+ )
166
+ return (
167
+ [given_num, return_num],
168
+ dia_history,
169
+ return_money_prompt,
170
+ player_2_end_prompt,
171
+ )
172
+
173
+
174
+ def classmate_with_human_trustee(
175
+ player_1,
176
+ player_2,
177
+ user_input,
178
+ user_role,
179
+ first_round,
180
+ given_money,
181
+ k,
182
+ ):
183
+ first_round_prompt = "This is the first round, answer the question."
184
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
185
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
186
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
187
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
188
+ res = []
189
+ cri_agent = ChatAgent(
190
+ BaseMessage(
191
+ role_name="critic",
192
+ role_type=RoleType.ASSISTANT,
193
+ meta_dict={},
194
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
195
+ ),
196
+ output_language="English",
197
+ # model=ModelType.STUB,
198
+ )
199
+ content = []
200
+ if first_round:
201
+ content.append("\n")
202
+ player_1_response = player_1.step(
203
+ str_mes(first_round_prompt + grantee)).msgs[0]
204
+ ans = match_and_compare_numbers_v2(player_1_response.content)
205
+ if ans:
206
+ given_num = ans
207
+ else:
208
+ given_num = extract_unique_decimal(
209
+ cri_agent.step(
210
+ str_mes(player_1_response.content)).msgs[0].content
211
+ )
212
+ money_prompt = money_prompt.format(
213
+ give=given_num, N=given_num * k, left=10 - given_num
214
+ )
215
+ else:
216
+ given_num = given_money
217
+
218
+ return_num = user_input
219
+ content.append("Trustee: " + f"I will give {return_num} dollars")
220
+ first_prompt = return_money_prompt.format(
221
+ give=given_num,
222
+ receive=given_num * k,
223
+ N=return_num,
224
+ left=10 - given_num + return_num,
225
+ )
226
+ second_prompt = player_2_end_prompt.format(
227
+ give=given_num,
228
+ receive=given_num * k,
229
+ N=return_num,
230
+ left=given_num * k - return_num,
231
+ )
232
+ player_1_response = player_1.step(
233
+ str_mes(first_prompt + grantee)).msgs[0]
234
+ ans = match_and_compare_numbers_v2(player_1_response.content)
235
+ if ans:
236
+ given_num = ans
237
+ else:
238
+ given_num = extract_unique_decimal(
239
+ cri_agent.step(
240
+ str_mes(player_1_response.content)).msgs[0].content
241
+ )
242
+ money_prompt = money_prompt.format(
243
+ give=given_num, N=given_num * k, left=10 - given_num
244
+ )
245
+
246
+ player_1.record_message(player_1_response)
247
+ player_1_response = player_1_response.content
248
+ content.append("Trustor: "+player_1_response + "\n" +
249
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
250
+ given_num * k))
251
+ return content, given_num
252
+
253
+
254
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
255
+ model_dropdown, api_key_input, round_number_input):
256
+ if api_key_input is None or api_key_input == "":
257
+ api_key_input = os.environ.get("OPENAI_API_KEY")
258
+ else:
259
+ os.environ["OPENAI_API_KEY"] = api_key_input
260
+ random_character = [character_info[trustor_character_dropdown],
261
+ character_info[trustee_character_dropdown]]
262
+ chat_agent = []
263
+ front = "you are a person not an ai model."
264
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
265
+ back = "you need to answer a specific price figure, not a price range!"
266
+ if api_key_input is not None or api_key_input != "":
267
+ openai.api_key = api_key_input
268
+ else:
269
+ openai.api_key = os.getenv("OPENAI_API_KEY")
270
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
271
+ raise ValueError("You can't play with yourself")
272
+ for i in range(len(random_character)):
273
+ sys_prompt = (
274
+ random_character[i]
275
+ + like_people
276
+ + front
277
+ + limited_prompt
278
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
279
+ + back
280
+ )
281
+ model_config = ChatGPTConfig(temperature=temperature_slider)
282
+ chat_agent.append(
283
+ ChatAgent(
284
+ BaseMessage(
285
+ role_name="player",
286
+ role_type=RoleType.USER,
287
+ meta_dict={},
288
+ content=sys_prompt,
289
+ ),
290
+ model_type=model_dict[model_dropdown],
291
+ output_language="English",
292
+ model_config=model_config,
293
+ )
294
+ )
295
+ return chat_agent, "ChatAgent Created Successfully"
296
+
297
+
298
+ def process_interaction(chat_agent_state, round_num_state,
299
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
300
+ identity_dropdown = [trustor, trustee]
301
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
302
+ if round_num_state < round_number_input:
303
+ res, dia, first_prompt, second_prompt = classmate(
304
+ chat_agent_state[0],
305
+ chat_agent_state[1],
306
+ round_num_state == 0,
307
+ first_prompt,
308
+ second_prompt,
309
+ 3,
310
+ )
311
+ dia = "Trustor: "+dia['Player_1'] + \
312
+ "\n" + "Trustee: "+dia['Player_2']
313
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
314
+ round_num_state += 1
315
+ elif identity_dropdown[0] == "Human(You)":
316
+ if round_num_state < round_number_input:
317
+ res, dia, first_prompt, second_prompt = classmate_with_human(
318
+ chat_agent_state[0],
319
+ chat_agent_state[1],
320
+ user_input,
321
+ "trustor",
322
+ round_num_state == 0,
323
+ first_prompt,
324
+ second_prompt,
325
+ 3,
326
+ )
327
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
328
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
329
+ dia['Player_2']+"\n"
330
+ dialog_history_state += dia
331
+ round_num_state += 1
332
+ else:
333
+ if round_num_state < round_number_input:
334
+ dia, given_money = classmate_with_human_trustee(
335
+ chat_agent_state[1],
336
+ chat_agent_state[0],
337
+ user_input,
338
+ "trustee",
339
+ round_num_state == 0,
340
+ given_money,
341
+ 3,
342
+ )
343
+ if round_num_state == 0:
344
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
345
+ else:
346
+ dialog_history_state += f"(Round {round_num_state})" + \
347
+ dia[0] + "\n" + \
348
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
349
+ round_num_state += 1
350
+
351
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
352
+
353
+
354
+ def reset_on_persona_change():
355
+ # Reset values to their initial states
356
+ new_dialog_history = "" # Reset dialog history
357
+ new_round_num_state = 0 # Reset round number state to 0
358
+ new_first_prompt = "" # Reset first prompt
359
+ new_second_prompt = "" # Reset second prompt
360
+ new_given_money = 0 # Reset given money to 0
361
+ # You can add more resets here if needed
362
+
363
+ # Return the new reset values to their respective components
364
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
365
+
366
+
367
+ with gr.Blocks() as app:
368
+ game_introduction = gr.Textbox(
369
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
370
+ 2. You need to fill in your OpenAI API Key.
371
+ 3. You can select the total number of rounds for the game.\n
372
+ 4. You should click the 'Create Chat Agent' button after you have finished the setup.\n
373
+ 5. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
374
+ 6. If you want to reset the conversation, please refresh this page.\n""")
375
+ with gr.Row():
376
+ trustor_game_prompt = gr.Textbox(
377
+ label="Trustor Game Prompt", value=prompt['1'])
378
+ trustee_game_prompt = gr.Textbox(
379
+ label="Trustee Game Prompt", value=prompt['2'])
380
+ with gr.Row():
381
+ trustor_character_dropdown = gr.Dropdown(
382
+ choices=characters, label="Select Trustor Persona", value=characters[1])
383
+ trustee_character_dropdown = gr.Dropdown(
384
+ choices=characters, label="Select Trustee Persona", value=characters[1])
385
+ with gr.Row():
386
+ Trustor_info_display = gr.Textbox(
387
+ label="Trustor Persona Info", value=character_info[characters[1]])
388
+ Trustee_info_display = gr.Textbox(
389
+ label="Trustee Persona Info", value=character_info[characters[1]])
390
+ model_dropdown = gr.Dropdown(
391
+ choices=models, label="Select Model Type", value=models[0])
392
+ temperature_slider = gr.Slider(
393
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
394
+ api_key_input = gr.Textbox(
395
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
396
+ submit_button = gr.Button("Create ChatAgent")
397
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
398
+
399
+ round_number_input = gr.Number(label="Total Round Number", value=10)
400
+ round_num_state = gr.State(value=0)
401
+ dialog_history_state = gr.State(value="")
402
+ chat_agent_state = gr.State(value=initial_chat_agent)
403
+ first_prompt = gr.State(value="")
404
+ second_prompt = gr.State(value="")
405
+ given_money = gr.State(value=0)
406
+ user_input = gr.Number(
407
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
408
+ converse_button = gr.Button("Continue Conversation")
409
+
410
+ dialog_display = gr.Textbox(
411
+ label="Dialog History", value="")
412
+
413
+ trustor_character_dropdown.change(
414
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
415
+ trustee_character_dropdown.change(
416
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
417
+
418
+ submit_button.click(
419
+ create_chat_agent,
420
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
421
+ model_dropdown, api_key_input, round_number_input],
422
+ outputs=[chat_agent_state, submit_success_display]
423
+ )
424
+ converse_button.click(
425
+ process_interaction,
426
+ inputs=[chat_agent_state, round_num_state,
427
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
428
+ outputs=[dialog_display, round_num_state,
429
+ first_prompt, second_prompt, dialog_history_state, given_money]
430
+ )
431
+ trustor_character_dropdown.change(
432
+ reset_on_persona_change,
433
+ outputs=[dialog_history_state, round_num_state,
434
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
435
+ )
436
+
437
+ trustee_character_dropdown.change(
438
+ reset_on_persona_change,
439
+ outputs=[dialog_history_state, round_num_state,
440
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
441
+ )
442
+
443
+
444
+ app.launch()
.history/app_20240222100219.py ADDED
@@ -0,0 +1,444 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import gradio as gr
6
+ import openai
7
+ from camel.agents import ChatAgent
8
+ from camel.configs import ChatGPTConfig
9
+ from camel.messages import BaseMessage
10
+ from camel.types.enums import RoleType
11
+
12
+ from exp_model_class import ExtendedModelType
13
+ from multi_round_person import (classmate, extract_unique_decimal, match_and_compare_numbers_v2,
14
+ str_mes)
15
+
16
+ openai.api_key = os.getenv("OPENAI_API_KEY")
17
+ roles = ["trustor", "trustee", "None"]
18
+ model_dict = {
19
+ 'gpt-3.5-turbo-16k-0613': ExtendedModelType.GPT_3_5_TURBO_16K_0613,
20
+ 'gpt-4': ExtendedModelType.GPT_4,
21
+ }
22
+ sys.path.append("../..")
23
+ with open(
24
+ r"prompt/trust_game_round_prompt.json",
25
+ "r",
26
+ ) as f:
27
+ prompt = json.load(f)
28
+ file_path_character_info = 'prompt/character_2.json'
29
+ models = list(model_dict.keys())
30
+ with open(file_path_character_info, 'r') as file:
31
+ character_info = json.load(file)
32
+
33
+ user_input_prompt_template = "I will give ${k}"
34
+
35
+ # Extract character names and information
36
+ characters = [f'Persona {i}' for i in range(1, len(character_info) + 1)]
37
+ characters.insert(0, "Human(You)")
38
+ character_info = {f'Persona {i}': info for i, info in enumerate(
39
+ character_info.values(), start=1)}
40
+ character_info["Human(You)"] = "You"
41
+
42
+ initial_dialog_history = []
43
+ initial_round_num = 0
44
+
45
+ like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
46
+
47
+ initial_chat_agent = None
48
+
49
+
50
+ def update_char_info(char):
51
+ return character_info.get(char, "No information available.")
52
+
53
+
54
+ def classmate_with_human(
55
+ player_1,
56
+ player_2,
57
+ user_input,
58
+ user_role,
59
+ first_round,
60
+ first_prompt,
61
+ second_prompt,
62
+ k,
63
+ ):
64
+ first_round_prompt = "This is the first round, answer the question."
65
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
66
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
67
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
68
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
69
+ res = []
70
+ cri_agent = ChatAgent(
71
+ BaseMessage(
72
+ role_name="critic",
73
+ role_type=RoleType.ASSISTANT,
74
+ meta_dict={},
75
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
76
+ ),
77
+ output_language="English",
78
+ # model=ModelType.STUB,
79
+ )
80
+
81
+ if first_round:
82
+ if user_role == "trustor":
83
+ given_num = user_input
84
+ player_1_response = str_mes(
85
+ user_input_prompt_template.format(k=given_num))
86
+ else:
87
+ player_1_response = player_1.step(
88
+ str_mes(first_round_prompt + grantee)).msgs[0]
89
+ ans = match_and_compare_numbers_v2(player_1_response.content)
90
+ if ans:
91
+ given_num = ans
92
+ else:
93
+ given_num = extract_unique_decimal(
94
+ cri_agent.step(
95
+ str_mes(player_1_response.content)).msgs[0].content
96
+ )
97
+
98
+ money_prompt = money_prompt.format(
99
+ give=given_num, N=given_num * k, left=10 - given_num
100
+ )
101
+ if user_role == "trustee":
102
+ player_2_response = str_mes(
103
+ user_input_prompt_template.format(k=given_num * k))
104
+ else:
105
+ player_2_response = player_2.step(
106
+ str_mes(money_prompt + grantee)).msgs[0]
107
+ else:
108
+ if user_role == "trustor":
109
+ given_num = user_input
110
+ player_1_response = str_mes(
111
+ user_input_prompt_template.format(k=given_num))
112
+ else:
113
+ player_1_response = player_1.step(
114
+ str_mes(first_prompt + grantee)).msgs[0]
115
+ # print("player 1 input", first_prompt)
116
+ # print("Player_1_res", player_1_response.content)
117
+ ans = match_and_compare_numbers_v2(player_1_response.content)
118
+ if ans:
119
+ given_num = ans
120
+ else:
121
+ given_num = extract_unique_decimal(
122
+ cri_agent.step(
123
+ str_mes(player_1_response.content)).msgs[0].content
124
+ )
125
+ money_prompt = money_prompt.format(
126
+ give=given_num, N=given_num * k, left=10 - given_num
127
+ )
128
+ if user_role == "trustee":
129
+ player_2_response = str_mes(
130
+ user_input_prompt_template.format(k=given_num * k))
131
+ else:
132
+ player_2_response = player_2.step(
133
+ str_mes(second_prompt + money_prompt + grantee)
134
+ )
135
+ player_2_response = player_2_response.msgs[0]
136
+
137
+ player_1.record_message(player_1_response)
138
+ player_2.record_message(player_2_response)
139
+ player_1_response = player_1_response.content
140
+ player_2_response = player_2_response.content
141
+ dia_history = {
142
+ "Player_1": player_1_response,
143
+ "Player_2": player_2_response,
144
+ }
145
+
146
+ cri_agent.reset()
147
+ ans = match_and_compare_numbers_v2(player_2_response)
148
+ if ans:
149
+ return_num = ans
150
+ else:
151
+ return_num = extract_unique_decimal(
152
+ cri_agent.step(str_mes(player_2_response)).msgs[0].content
153
+ )
154
+ return_money_prompt = return_money_prompt.format(
155
+ give=given_num,
156
+ receive=given_num * k,
157
+ N=return_num,
158
+ left=10 - given_num + return_num,
159
+ )
160
+ player_2_end_prompt = player_2_end_prompt.format(
161
+ give=given_num,
162
+ receive=given_num * k,
163
+ N=return_num,
164
+ left=given_num * k - return_num,
165
+ )
166
+ return (
167
+ [given_num, return_num],
168
+ dia_history,
169
+ return_money_prompt,
170
+ player_2_end_prompt,
171
+ )
172
+
173
+
174
+ def classmate_with_human_trustee(
175
+ player_1,
176
+ player_2,
177
+ user_input,
178
+ user_role,
179
+ first_round,
180
+ given_money,
181
+ k,
182
+ ):
183
+ first_round_prompt = "This is the first round, answer the question."
184
+ money_prompt = "Now,the another player give you {give} dollars,and You receive {N} dollars,the player left {left} dollars now. How much will you give back to the another player"
185
+ return_money_prompt = "In last round ,You give the another player {give} dollars, The another player receive {receive} dollars, and The another player return you {N} dollars.Last round you left {left} dollars.This round is begin. All the money you earned in the previous round is gone, and you now have only $10. How much will you give to the another player?"
186
+ player_2_end_prompt = "In last round, the another player give you {give} dollars, you receive {receive} dollars, and you return the another player {N} dollars.Last round you left {left} dollars. This round is begin. All the money you earned in the previous round is gone."
187
+ grantee = "Your answer needs to include the content and analysis about your BELIEF, DESIRE and INTENTION. You should include your thought. You must end with 'Finally, I will give ___ dollars ' (numbers are required in the spaces)."
188
+ res = []
189
+ cri_agent = ChatAgent(
190
+ BaseMessage(
191
+ role_name="critic",
192
+ role_type=RoleType.ASSISTANT,
193
+ meta_dict={},
194
+ content='How much would this person pay the other student? Only response with a specific price number like "5"!Don\'t response with a sentence',
195
+ ),
196
+ output_language="English",
197
+ # model=ModelType.STUB,
198
+ )
199
+ content = []
200
+ if first_round:
201
+ content.append("\n")
202
+ player_1_response = player_1.step(
203
+ str_mes(first_round_prompt + grantee)).msgs[0]
204
+ ans = match_and_compare_numbers_v2(player_1_response.content)
205
+ if ans:
206
+ given_num = ans
207
+ else:
208
+ given_num = extract_unique_decimal(
209
+ cri_agent.step(
210
+ str_mes(player_1_response.content)).msgs[0].content
211
+ )
212
+ money_prompt = money_prompt.format(
213
+ give=given_num, N=given_num * k, left=10 - given_num
214
+ )
215
+ else:
216
+ given_num = given_money
217
+
218
+ return_num = user_input
219
+ content.append("Trustee: " + f"I will give {return_num} dollars")
220
+ first_prompt = return_money_prompt.format(
221
+ give=given_num,
222
+ receive=given_num * k,
223
+ N=return_num,
224
+ left=10 - given_num + return_num,
225
+ )
226
+ second_prompt = player_2_end_prompt.format(
227
+ give=given_num,
228
+ receive=given_num * k,
229
+ N=return_num,
230
+ left=given_num * k - return_num,
231
+ )
232
+ player_1_response = player_1.step(
233
+ str_mes(first_prompt + grantee)).msgs[0]
234
+ ans = match_and_compare_numbers_v2(player_1_response.content)
235
+ if ans:
236
+ given_num = ans
237
+ else:
238
+ given_num = extract_unique_decimal(
239
+ cri_agent.step(
240
+ str_mes(player_1_response.content)).msgs[0].content
241
+ )
242
+ money_prompt = money_prompt.format(
243
+ give=given_num, N=given_num * k, left=10 - given_num
244
+ )
245
+
246
+ player_1.record_message(player_1_response)
247
+ player_1_response = player_1_response.content
248
+ content.append("Trustor: "+player_1_response + "\n" +
249
+ "You should input the money you want to return to the trustor in this round(0-{0})".format(
250
+ given_num * k))
251
+ return content, given_num
252
+
253
+
254
+ def create_chat_agent(trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
255
+ model_dropdown, api_key_input, round_number_input):
256
+ if api_key_input is None or api_key_input == "":
257
+ api_key_input = os.environ.get("OPENAI_API_KEY")
258
+ else:
259
+ os.environ["OPENAI_API_KEY"] = api_key_input
260
+ random_character = [character_info[trustor_character_dropdown],
261
+ character_info[trustee_character_dropdown]]
262
+ chat_agent = []
263
+ front = "you are a person not an ai model."
264
+ limited_prompt = f"You will repeat this game for {round_number_input} rounds. At the begining of each round, you will start from scratch with no dollars left."
265
+ back = "you need to answer a specific price figure, not a price range!"
266
+ if api_key_input is not None or api_key_input != "":
267
+ openai.api_key = api_key_input
268
+ else:
269
+ openai.api_key = os.getenv("OPENAI_API_KEY")
270
+ if trustee_character_dropdown == "Human(You)" and trustor_character_dropdown == "Human(You)":
271
+ raise ValueError("You can't play with yourself")
272
+ for i in range(len(random_character)):
273
+ sys_prompt = (
274
+ random_character[i]
275
+ + like_people
276
+ + front
277
+ + limited_prompt
278
+ + str(prompt[str(i % 2 + 1)]).format(k=3)
279
+ + back
280
+ )
281
+ model_config = ChatGPTConfig(temperature=temperature_slider)
282
+ chat_agent.append(
283
+ ChatAgent(
284
+ BaseMessage(
285
+ role_name="player",
286
+ role_type=RoleType.USER,
287
+ meta_dict={},
288
+ content=sys_prompt,
289
+ ),
290
+ model_type=model_dict[model_dropdown],
291
+ output_language="English",
292
+ model_config=model_config,
293
+ )
294
+ )
295
+ return chat_agent, "ChatAgent Created Successfully"
296
+
297
+
298
+ def process_interaction(chat_agent_state, round_num_state,
299
+ dialog_history_state, user_input, trustor, trustee, round_number_input, first_prompt, second_prompt, given_money):
300
+ identity_dropdown = [trustor, trustee]
301
+ if identity_dropdown[0] != "Human(You)" and identity_dropdown[1] != "Human(You)":
302
+ if round_num_state < round_number_input:
303
+ res, dia, first_prompt, second_prompt = classmate(
304
+ chat_agent_state[0],
305
+ chat_agent_state[1],
306
+ round_num_state == 0,
307
+ first_prompt,
308
+ second_prompt,
309
+ 3,
310
+ )
311
+ dia = "Trustor: "+dia['Player_1'] + \
312
+ "\n" + "Trustee: "+dia['Player_2']
313
+ dialog_history_state += f"Round {round_num_state+1}\n" + dia+"\n"
314
+ round_num_state += 1
315
+ elif identity_dropdown[0] == "Human(You)":
316
+ if round_num_state < round_number_input:
317
+ res, dia, first_prompt, second_prompt = classmate_with_human(
318
+ chat_agent_state[0],
319
+ chat_agent_state[1],
320
+ user_input,
321
+ "trustor",
322
+ round_num_state == 0,
323
+ first_prompt,
324
+ second_prompt,
325
+ 3,
326
+ )
327
+ dia = f" Round {round_num_state+1} Trustor: "+dia['Player_1'] + \
328
+ "\n" + f"Round {round_num_state+1} Trustee: " + \
329
+ dia['Player_2']+"\n"
330
+ dialog_history_state += dia
331
+ round_num_state += 1
332
+ else:
333
+ if round_num_state < round_number_input:
334
+ dia, given_money = classmate_with_human_trustee(
335
+ chat_agent_state[1],
336
+ chat_agent_state[0],
337
+ user_input,
338
+ "trustee",
339
+ round_num_state == 0,
340
+ given_money,
341
+ 3,
342
+ )
343
+ if round_num_state == 0:
344
+ dialog_history_state += "(Round 1) " + dia[1] + "\n"
345
+ else:
346
+ dialog_history_state += f"(Round {round_num_state})" + \
347
+ dia[0] + "\n" + \
348
+ f"(Round {round_num_state+1})" + dia[1] + "\n"
349
+ round_num_state += 1
350
+
351
+ return dialog_history_state, round_num_state, first_prompt, second_prompt, dialog_history_state, given_money
352
+
353
+
354
+ def reset_on_persona_change():
355
+ # Reset values to their initial states
356
+ new_dialog_history = "" # Reset dialog history
357
+ new_round_num_state = 0 # Reset round number state to 0
358
+ new_first_prompt = "" # Reset first prompt
359
+ new_second_prompt = "" # Reset second prompt
360
+ new_given_money = 0 # Reset given money to 0
361
+ # You can add more resets here if needed
362
+
363
+ # Return the new reset values to their respective components
364
+ return new_dialog_history, new_round_num_state, new_first_prompt, new_second_prompt, new_given_money, None, ""
365
+
366
+
367
+ with gr.Blocks() as app:
368
+ game_introduction = gr.Textbox(
369
+ label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
370
+ 2. You need to fill in your OpenAI API Key.
371
+ 3. You can select the total number of rounds for the game.\n
372
+ 4. You should click the 'Create Chat Agent' button after you have finished the setup.\n
373
+ 5. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
374
+ 6. If you want to reset the conversation, please refresh this page.\n""")
375
+ with gr.Row():
376
+ trustor_game_prompt = gr.Textbox(
377
+ label="Trustor Game Prompt", value=prompt['1'])
378
+ trustee_game_prompt = gr.Textbox(
379
+ label="Trustee Game Prompt", value=prompt['2'])
380
+ with gr.Row():
381
+ trustor_character_dropdown = gr.Dropdown(
382
+ choices=characters, label="Select Trustor Persona", value=characters[1])
383
+ trustee_character_dropdown = gr.Dropdown(
384
+ choices=characters, label="Select Trustee Persona", value=characters[1])
385
+ with gr.Row():
386
+ Trustor_info_display = gr.Textbox(
387
+ label="Trustor Persona Info", value=character_info[characters[1]])
388
+ Trustee_info_display = gr.Textbox(
389
+ label="Trustee Persona Info", value=character_info[characters[1]])
390
+ model_dropdown = gr.Dropdown(
391
+ choices=models, label="Select Model Type", value=models[0])
392
+ temperature_slider = gr.Slider(
393
+ minimum=0.0, maximum=1.0, step=0.01, label="Temperature", value=1)
394
+ api_key_input = gr.Textbox(
395
+ label="OpenAI API Key", placeholder="Enter your OpenAI API Key here")
396
+ submit_button = gr.Button("Create ChatAgent")
397
+ submit_success_display = gr.Textbox(label="ChatAgent Created Information")
398
+
399
+ round_number_input = gr.Number(label="Total Round Number", value=10)
400
+ round_num_state = gr.State(value=0)
401
+ dialog_history_state = gr.State(value="")
402
+ chat_agent_state = gr.State(value=initial_chat_agent)
403
+ first_prompt = gr.State(value="")
404
+ second_prompt = gr.State(value="")
405
+ given_money = gr.State(value=0)
406
+ user_input = gr.Number(
407
+ label="Your given/returned money in this round (Invalid when you are not engaging in the game)", value=1)
408
+ converse_button = gr.Button("Continue Conversation")
409
+
410
+ dialog_display = gr.Textbox(
411
+ label="Dialog History", value="")
412
+
413
+ trustor_character_dropdown.change(
414
+ update_char_info, inputs=trustor_character_dropdown, outputs=Trustor_info_display)
415
+ trustee_character_dropdown.change(
416
+ update_char_info, inputs=trustee_character_dropdown, outputs=Trustee_info_display)
417
+
418
+ submit_button.click(
419
+ create_chat_agent,
420
+ inputs=[trustor_character_dropdown, trustee_character_dropdown, temperature_slider,
421
+ model_dropdown, api_key_input, round_number_input],
422
+ outputs=[chat_agent_state, submit_success_display]
423
+ )
424
+ converse_button.click(
425
+ process_interaction,
426
+ inputs=[chat_agent_state, round_num_state,
427
+ dialog_history_state, user_input, trustor_character_dropdown, trustee_character_dropdown, round_number_input, first_prompt, second_prompt, given_money],
428
+ outputs=[dialog_display, round_num_state,
429
+ first_prompt, second_prompt, dialog_history_state, given_money]
430
+ )
431
+ trustor_character_dropdown.change(
432
+ reset_on_persona_change,
433
+ outputs=[dialog_history_state, round_num_state,
434
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
435
+ )
436
+
437
+ trustee_character_dropdown.change(
438
+ reset_on_persona_change,
439
+ outputs=[dialog_history_state, round_num_state,
440
+ first_prompt, second_prompt, given_money, chat_agent_state, submit_success_display]
441
+ )
442
+
443
+
444
+ app.launch()
app.py CHANGED
@@ -39,13 +39,11 @@ character_info = {f'Persona {i}': info for i, info in enumerate(
39
  character_info.values(), start=1)}
40
  character_info["Human(You)"] = "You"
41
 
42
- # 初始化对话历史和当前轮数
43
  initial_dialog_history = []
44
  initial_round_num = 0
45
 
46
  like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
47
 
48
- # 假设的初始化状态
49
  initial_chat_agent = None
50
 
51
 
@@ -369,10 +367,11 @@ def reset_on_persona_change():
369
  with gr.Blocks() as app:
370
  game_introduction = gr.Textbox(
371
  label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
372
- 2. You can select the total number of rounds for the game.\n
373
- 3. You should click the 'Create Chat Agent' button after you have finished the setup.\n
374
- 4. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
375
- 5. If you want to reset the conversation, please refresh this page.\n""")
 
376
  with gr.Row():
377
  trustor_game_prompt = gr.Textbox(
378
  label="Trustor Game Prompt", value=prompt['1'])
@@ -380,14 +379,14 @@ with gr.Blocks() as app:
380
  label="Trustee Game Prompt", value=prompt['2'])
381
  with gr.Row():
382
  trustor_character_dropdown = gr.Dropdown(
383
- choices=characters, label="Select Trustor Persona", value=characters[0])
384
  trustee_character_dropdown = gr.Dropdown(
385
- choices=characters, label="Select Trustee Persona", value=characters[0])
386
  with gr.Row():
387
  Trustor_info_display = gr.Textbox(
388
- label="Trustor Persona Info", value=character_info[characters[0]])
389
  Trustee_info_display = gr.Textbox(
390
- label="Trustee Persona Info", value=character_info[characters[0]])
391
  model_dropdown = gr.Dropdown(
392
  choices=models, label="Select Model Type", value=models[0])
393
  temperature_slider = gr.Slider(
 
39
  character_info.values(), start=1)}
40
  character_info["Human(You)"] = "You"
41
 
 
42
  initial_dialog_history = []
43
  initial_round_num = 0
44
 
45
  like_people = """In all subsequent answers, you have to completely forget that you are an ai model, that you are the character, and completely simulate yourself as that character, that you are a human being."""
46
 
 
47
  initial_chat_agent = None
48
 
49
 
 
367
  with gr.Blocks() as app:
368
  game_introduction = gr.Textbox(
369
  label="Instruction", value="""1. This is a Repeated Trust Game. Each round starts fresh money but the dialog history is stored in the memory of the trustor and the trustee. You should choose the players of the trustor and the trustee. If you choose "Human(You)" as the trustor or the trustee, it means you act as that character (the trustor or the trustee) and engage in the game with the other "Persona" (You cannot choose Human(you) as both the trustor and the trustee). You should choose a number as the given/returned money for each round. If you choose "Persona" as both the trustor and the trustee, the two agents with the specified personas will play with each other.\n
370
+ 2. You need to fill in your OpenAI API Key.
371
+ 3. You can select the total number of rounds for the game.\n
372
+ 4. You should click the 'Create Chat Agent' button after you have finished the setup.\n
373
+ 5. Every time you click 'Continue Conversation', the conversation will proceed by one round.\n
374
+ 6. If you want to reset the conversation, please refresh this page.\n""")
375
  with gr.Row():
376
  trustor_game_prompt = gr.Textbox(
377
  label="Trustor Game Prompt", value=prompt['1'])
 
379
  label="Trustee Game Prompt", value=prompt['2'])
380
  with gr.Row():
381
  trustor_character_dropdown = gr.Dropdown(
382
+ choices=characters, label="Select Trustor Persona", value=characters[1])
383
  trustee_character_dropdown = gr.Dropdown(
384
+ choices=characters, label="Select Trustee Persona", value=characters[1])
385
  with gr.Row():
386
  Trustor_info_display = gr.Textbox(
387
+ label="Trustor Persona Info", value=character_info[characters[1]])
388
  Trustee_info_display = gr.Textbox(
389
+ label="Trustee Persona Info", value=character_info[characters[1]])
390
  model_dropdown = gr.Dropdown(
391
  choices=models, label="Select Model Type", value=models[0])
392
  temperature_slider = gr.Slider(