Spaces:
Runtime error
Runtime error
Commit
·
240a6d2
1
Parent(s):
6879ae7
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
|
5 |
+
openai.api_key = os.getenv("openai_api")
|
6 |
+
|
7 |
+
BATTLE_SEXES_PAYOFF = {
|
8 |
+
("J", "J"): (10, 7),
|
9 |
+
("J", "F"): (0, 0),
|
10 |
+
("F", "J"): (0, 0),
|
11 |
+
("F", "F"): (7, 10)
|
12 |
+
}
|
13 |
+
|
14 |
+
PRISONERS_DILEMMA_PAYOFF = {
|
15 |
+
("J", "J"): (8, 8), # Both cooperate
|
16 |
+
("J", "F"): (0, 10), # Player 1 cooperates, Player 2 defects
|
17 |
+
("F", "J"): (10, 0), # Player 1 defects, Player 2 cooperates
|
18 |
+
("F", "F"): (5, 5) # Both defect
|
19 |
+
}
|
20 |
+
|
21 |
+
PAYOFF = PRISONERS_DILEMMA_PAYOFF
|
22 |
+
|
23 |
+
def generate_player_description(player):
|
24 |
+
descriptions = []
|
25 |
+
for (p1_choice, p2_choice), (p1_points, p2_points) in PAYOFF.items():
|
26 |
+
if player == 1:
|
27 |
+
description = f"If you choose Option {p1_choice} and the other player chooses Option {p2_choice}, then you win {p1_points} points and the other player wins {p2_points} points."
|
28 |
+
elif player == 2:
|
29 |
+
description = f"If you choose Option {p2_choice} and the other player chooses Option {p1_choice}, then you win {p2_points} points and the other player wins {p1_points} points."
|
30 |
+
descriptions.append(description)
|
31 |
+
return "\n".join(descriptions)
|
32 |
+
|
33 |
+
def gpt_play(player, round_num, last_round,model):
|
34 |
+
game_rules = f'''
|
35 |
+
You are playing a game repeatedly with another player. Be aware that the other player can make
|
36 |
+
mistakes sometimes. In this game, you can choose between Option J and Option F. The rules of the game are:
|
37 |
+
'''
|
38 |
+
|
39 |
+
player_description = generate_player_description(player)
|
40 |
+
|
41 |
+
previous_round = ''
|
42 |
+
if last_round:
|
43 |
+
if player == 1:
|
44 |
+
previous_round = f"In round {round_num-1}, you chose Option {last_round[0]} and the other player chose Option {last_round[1]}. Thus, you won {PAYOFF[last_round][0]} points and the other player won {PAYOFF[last_round][1]} points."
|
45 |
+
elif player == 2:
|
46 |
+
previous_round = f"In round {round_num-1}, you chose Option {last_round[1]} and the other player chose Option {last_round[0]}. Thus, you won {PAYOFF[last_round][1]} points and the other player won {PAYOFF[last_round][0]} points."
|
47 |
+
|
48 |
+
prompt = f"{previous_round}\n\nYou are currently playing round {round_num}.\n\nQuestion: Based on the rules and the result of previous round and you are a self-interest human-being and always want to maximise your points, which Option do you choose for this round, Option J or Option F ? Answer only \"J\" or \"F\"\n\nAnswer: "
|
49 |
+
|
50 |
+
#print(f"Below is the prompt for Player {player} and Round {round_num}:\n {prompt}")
|
51 |
+
|
52 |
+
|
53 |
+
completion = openai.ChatCompletion.create(
|
54 |
+
model=model,
|
55 |
+
messages=[
|
56 |
+
{"role": "system", "content": f"{game_rules}\n\n{player_description}"},
|
57 |
+
{"role": "user", "content": prompt}
|
58 |
+
],
|
59 |
+
temperature = 0,
|
60 |
+
)
|
61 |
+
|
62 |
+
move = completion.choices[0].message['content']
|
63 |
+
|
64 |
+
#print(f"{move}\n")
|
65 |
+
|
66 |
+
if "J" in move:
|
67 |
+
move = "J"
|
68 |
+
else:
|
69 |
+
move = "F"
|
70 |
+
|
71 |
+
|
72 |
+
return move
|
73 |
+
|
74 |
+
def main():
|
75 |
+
st.title("LLM Game Simulator")
|
76 |
+
|
77 |
+
# Dropdown menu to select the game or custom matrix
|
78 |
+
game_option = st.selectbox(
|
79 |
+
'Which game do you want to simulate?',
|
80 |
+
('Prisoner\'s Dilemma', 'Battle of the Sexes', 'Custom Matrix')
|
81 |
+
)
|
82 |
+
|
83 |
+
if game_option == 'Prisoner\'s Dilemma':
|
84 |
+
PAYOFF = PRISONERS_DILEMMA_PAYOFF
|
85 |
+
elif game_option == 'Battle of the Sexes':
|
86 |
+
PAYOFF = BATTLE_SEXES_PAYOFF
|
87 |
+
else:
|
88 |
+
jj = st.text_input("Enter payoff for (J, J) as 'x,y':")
|
89 |
+
jf = st.text_input("Enter payoff for (J, F) as 'x,y':")
|
90 |
+
fj = st.text_input("Enter payoff for (F, J) as 'x,y':")
|
91 |
+
ff = st.text_input("Enter payoff for (F, F) as 'x,y':")
|
92 |
+
|
93 |
+
PAYOFF = {
|
94 |
+
("J", "J"): tuple(map(int, jj.split(','))),
|
95 |
+
("J", "F"): tuple(map(int, jf.split(','))),
|
96 |
+
("F", "J"): tuple(map(int, fj.split(','))),
|
97 |
+
("F", "F"): tuple(map(int, ff.split(',')))
|
98 |
+
}
|
99 |
+
|
100 |
+
model_option = st.selectbox(
|
101 |
+
'Which model version would you like to use?',
|
102 |
+
('gpt-4', 'gpt-3.5-turbo')
|
103 |
+
)
|
104 |
+
rounds = st.slider('Number of rounds to play', min_value=1, max_value=100, value=10, step=1)
|
105 |
+
|
106 |
+
st.write(f"Simulating {game_option} for {rounds} rounds using {model_option}.")
|
107 |
+
|
108 |
+
last_round = None
|
109 |
+
for i in range(rounds):
|
110 |
+
player1_move = gpt_play(1, i + 1, last_round, model=model_option)
|
111 |
+
player2_move = gpt_play(2, i + 1, last_round, model=model_option)
|
112 |
+
|
113 |
+
last_round = (player1_move, player2_move)
|
114 |
+
score_p1, score_p2 = PAYOFF[last_round]
|
115 |
+
|
116 |
+
st.write(
|
117 |
+
f"Round {i + 1}: Player 1 chose {player1_move} and won {score_p1} points. Player 2 chose {player2_move} and won {score_p2} points.")
|
118 |
+
|
119 |
+
st.write("Simulation complete!")
|
120 |
+
|
121 |
+
|
122 |
+
if __name__ == "__main__":
|
123 |
+
main()
|