yitianlian commited on
Commit
a605c00
·
1 Parent(s): 7f4d817

fix a bug about human trustee

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