-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.py
85 lines (76 loc) · 3.27 KB
/
config.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import json
from typing import Any, Dict
from dotenv import load_dotenv
class VirtualGameMasterConfig:
def __init__(self):
self.GAME_SAVE_FOLDER: str = ""
self.INITIAL_GAME_STATE: str = ""
self.MAX_MESSAGES: int = 0
self.KEPT_MESSAGES: int = 0
self.SYSTEM_MESSAGE_FILE: str = ""
self.SAVE_SYSTEM_MESSAGE_FILE: str = ""
self.MAX_TOKENS: int = 0
self.API_TYPE: str = "openai"
self.API_KEY: str | None = None
self.API_URL: str = ""
self.MODEL: str = ""
self.TEMPERATURE: float = 0.7
self.TOP_P: float = 1.0
self.TOP_K: int = 0
self.MIN_P: float = 0.0
self.TFS_Z: float = 1.0
self.COMMAND_PREFIX: str = "@"
self.STOP_SEQUENCES: str = "[]"
@classmethod
def from_env(cls, env_file: str = ".env") -> "VirtualGameMasterConfig":
load_dotenv(env_file)
config = cls()
config.GAME_SAVE_FOLDER = os.getenv("GAME_SAVE_FOLDER")
config.INITIAL_GAME_STATE = os.getenv("INITIAL_GAME_STATE")
config.MAX_MESSAGES = int(os.getenv("MAX_MESSAGES"))
config.KEPT_MESSAGES = int(os.getenv("KEPT_MESSAGES"))
config.SYSTEM_MESSAGE_FILE = os.getenv("SYSTEM_MESSAGE_FILE")
config.SAVE_SYSTEM_MESSAGE_FILE = os.getenv("SAVE_SYSTEM_MESSAGE_FILE")
config.MAX_TOKENS = int(os.getenv("MAX_TOKENS_PER_RESPONSE"))
config.API_TYPE = os.getenv("API_TYPE", "openai").lower()
config.API_KEY = os.getenv("API_KEY", None)
config.API_URL = os.getenv("API_URL")
config.MODEL = os.getenv("MODEL")
config.TEMPERATURE = float(os.getenv("TEMPERATURE", 0.7))
config.TOP_P = float(os.getenv("TOP_P", 1.0))
config.TOP_K = int(os.getenv("TOP_K", 0))
config.MIN_P = float(os.getenv("MIN_P", 0.0))
config.TFS_Z = float(os.getenv("TFS_Z", 1.0))
config.COMMAND_PREFIX = os.getenv("COMMAND_PREFIX", "@")
config.STOP_SEQUENCES = os.getenv("STOP_SEQUENCES", "[]")
return config
@classmethod
def from_json(cls, json_file: str) -> "VirtualGameMasterConfig":
with open(json_file, "r") as f:
data = json.load(f)
config = cls()
for key, value in data.items():
if hasattr(config, key):
setattr(config, key, config._parse_value(key, value))
return config
def to_env(self, env_file: str = ".env") -> None:
with open(env_file, "w") as f:
for key, value in self.__dict__.items():
f.write(f"{key}={value}\n")
def to_json(self, json_file: str) -> None:
with open(json_file, "w") as f:
json.dump(self.__dict__, f, indent=2)
def _parse_value(self, key: str, value: Any) -> Any:
current_value = getattr(self, key)
if isinstance(current_value, bool):
return str(value).lower() in ("true", "1", "yes")
if current_value is None:
return value
return type(current_value)(value)
def update(self, updates: Dict[str, Any]) -> None:
for key, value in updates.items():
if hasattr(self, key):
setattr(self, key, self._parse_value(key, value))
def to_dict(self) -> Dict[str, Any]:
return {key: value for key, value in self.__dict__.items()}