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

revise grammar error in introduciton

Browse files
Files changed (2) hide show
  1. .history/app_20240221102330.py +445 -0
  2. app.py +1 -1
.history/app_20240221102330.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[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()
app.py CHANGED
@@ -372,7 +372,7 @@ with gr.Blocks() as app:
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'])
 
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'])