Eric Botti
commited on
Commit
·
0c31321
1
Parent(s):
a92f249
added basic logging
Browse files- src/agents.py +1 -2
- src/game.py +15 -4
- src/game_utils.py +8 -0
- src/parser.py +2 -2
src/agents.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
from kani import Kani
|
2 |
|
3 |
-
|
4 |
class LogMessagesKani(Kani):
|
5 |
def __init__(self, engine, log_filepath: str = None, *args, **kwargs):
|
6 |
super().__init__(engine, *args, **kwargs)
|
@@ -11,6 +10,6 @@ class LogMessagesKani(Kani):
|
|
11 |
|
12 |
# Logs Message to File
|
13 |
if self.log_filepath:
|
14 |
-
with open(self.log_filepath, "a") as log_file:
|
15 |
log_file.write(message.model_dump_json())
|
16 |
log_file.write("\n")
|
|
|
1 |
from kani import Kani
|
2 |
|
|
|
3 |
class LogMessagesKani(Kani):
|
4 |
def __init__(self, engine, log_filepath: str = None, *args, **kwargs):
|
5 |
super().__init__(engine, *args, **kwargs)
|
|
|
10 |
|
11 |
# Logs Message to File
|
12 |
if self.log_filepath:
|
13 |
+
with open(self.log_filepath, "a+") as log_file:
|
14 |
log_file.write(message.model_dump_json())
|
15 |
log_file.write("\n")
|
src/game.py
CHANGED
@@ -1,20 +1,27 @@
|
|
1 |
-
import
|
2 |
|
3 |
from game_utils import *
|
4 |
from models import *
|
5 |
from player import Player
|
6 |
-
|
7 |
from parser import ParserKani
|
8 |
|
9 |
# Default Values
|
10 |
NUMBER_OF_PLAYERS = 5
|
11 |
|
12 |
class Game:
|
|
|
|
|
|
|
|
|
|
|
13 |
def __init__(self,
|
14 |
human_name: str = None,
|
15 |
number_of_players: int = NUMBER_OF_PLAYERS
|
16 |
):
|
17 |
|
|
|
|
|
|
|
18 |
# Gather Player Names
|
19 |
if human_name:
|
20 |
ai_names = random_names(number_of_players - 1)
|
@@ -40,13 +47,16 @@ class Game:
|
|
40 |
else:
|
41 |
role = "herd"
|
42 |
|
43 |
-
self.
|
|
|
|
|
44 |
|
45 |
# Game State
|
46 |
self.player_responses = []
|
47 |
|
48 |
# Parser
|
49 |
-
self.
|
|
|
50 |
|
51 |
def format_responses(self) -> str:
|
52 |
"""Formats the responses of the players into a single string."""
|
@@ -126,5 +136,6 @@ class Game:
|
|
126 |
# Herd Wins by Failed Chameleon Guess - 1 Point (each)
|
127 |
# Herd Wins by Correctly Guessing Chameleon - 2 points (each)
|
128 |
|
|
|
129 |
|
130 |
|
|
|
1 |
+
import os
|
2 |
|
3 |
from game_utils import *
|
4 |
from models import *
|
5 |
from player import Player
|
|
|
6 |
from parser import ParserKani
|
7 |
|
8 |
# Default Values
|
9 |
NUMBER_OF_PLAYERS = 5
|
10 |
|
11 |
class Game:
|
12 |
+
|
13 |
+
log_dir = os.path.join(os.pardir, "experiments")
|
14 |
+
player_log_file = "{game_id}-{role}-{player_num}.jsonl"
|
15 |
+
parser_log_file = "{game_id}-parser.jsonl"
|
16 |
+
|
17 |
def __init__(self,
|
18 |
human_name: str = None,
|
19 |
number_of_players: int = NUMBER_OF_PLAYERS
|
20 |
):
|
21 |
|
22 |
+
# Game ID
|
23 |
+
self.game_id = game_id()
|
24 |
+
|
25 |
# Gather Player Names
|
26 |
if human_name:
|
27 |
ai_names = random_names(number_of_players - 1)
|
|
|
47 |
else:
|
48 |
role = "herd"
|
49 |
|
50 |
+
log_path = os.path.join(self.log_dir, self.player_log_file.format(game_id=self.game_id, role=role, player_num=i))
|
51 |
+
|
52 |
+
self.players.append(Player(name, controller, role, log_filepath=log_path))
|
53 |
|
54 |
# Game State
|
55 |
self.player_responses = []
|
56 |
|
57 |
# Parser
|
58 |
+
parser_log_path = os.path.join(self.log_dir, self.parser_log_file.format(game_id=self.game_id))
|
59 |
+
self.parser = ParserKani.default(parser_log_path)
|
60 |
|
61 |
def format_responses(self) -> str:
|
62 |
"""Formats the responses of the players into a single string."""
|
|
|
136 |
# Herd Wins by Failed Chameleon Guess - 1 Point (each)
|
137 |
# Herd Wins by Correctly Guessing Chameleon - 2 points (each)
|
138 |
|
139 |
+
# Log Game Info
|
140 |
|
141 |
|
src/game_utils.py
CHANGED
@@ -2,6 +2,14 @@
|
|
2 |
Utilities for the game including random selections and prompts.
|
3 |
"""
|
4 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
|
7 |
def random_animal():
|
|
|
2 |
Utilities for the game including random selections and prompts.
|
3 |
"""
|
4 |
import random
|
5 |
+
import string
|
6 |
+
|
7 |
+
ALPHABET = string.ascii_lowercase + string.digits
|
8 |
+
ID_LENGTH = 8
|
9 |
+
|
10 |
+
|
11 |
+
def game_id():
|
12 |
+
return ''.join(random.choices(ALPHABET, k=ID_LENGTH)) # Using this instead of uuid for shorter game ids
|
13 |
|
14 |
|
15 |
def random_animal():
|
src/parser.py
CHANGED
@@ -66,10 +66,10 @@ class ParserKani(LogMessagesKani):
|
|
66 |
return FORMAT_INSTRUCTIONS.format(schema=schema_str)
|
67 |
|
68 |
@classmethod
|
69 |
-
def default(cls):
|
70 |
"""Default ParserKani with OpenAIEngine."""
|
71 |
engine = OpenAIEngine(model="gpt-3.5-turbo")
|
72 |
-
return cls(engine)
|
73 |
|
74 |
|
75 |
|
|
|
66 |
return FORMAT_INSTRUCTIONS.format(schema=schema_str)
|
67 |
|
68 |
@classmethod
|
69 |
+
def default(cls, log_filepath: str = None):
|
70 |
"""Default ParserKani with OpenAIEngine."""
|
71 |
engine = OpenAIEngine(model="gpt-3.5-turbo")
|
72 |
+
return cls(engine, log_filepath=log_filepath)
|
73 |
|
74 |
|
75 |
|