-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathutils.py
More file actions
56 lines (46 loc) · 1.62 KB
/
Copy pathutils.py
File metadata and controls
56 lines (46 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
from dataclasses import asdict, dataclass
from dotenv import load_dotenv
from jinja2 import Template
load_dotenv()
def resolve_api_key(model: str) -> str:
if "claude" in model:
return os.getenv("ANTHROPIC_API_KEY")
if "gpt" in model:
return os.getenv("OPENAI_API_KEY")
@dataclass
class GameContext:
"""
A class that gives agent access to a partial view of the game state.
NOTE: Instead of passing `game` directly as a reference to the agent,
we create this interface instead to make the communication of game state
more explicit and controlled. We go with this loose coupling to avoid
making the agent too dependent on the entire game object.
"""
id: str
name: str
player_id: str
prompts: dict
round: int
rounds: int
working_dir: str
def render_and_set_prompts(self):
"""Render and set prompts using the current game context."""
context = asdict(self)
del context["prompts"]
for key, template_str in self.prompts.items():
rendered = Template(template_str).render(**context)
setattr(self, key, rendered)
def to_dict(self):
"""Convert the GameContext to a dictionary, including dynamically added attributes."""
result = asdict(self)
declared = set(self.__dataclass_fields__)
for attr in dir(self):
if (
not attr.startswith("_")
and attr not in declared
and not callable(getattr(self, attr))
):
result[attr] = getattr(self, attr)
del result["prompts"]
return result