-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.py
50 lines (40 loc) · 1.78 KB
/
cli.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
from virtual_game_master import VirtualGameMasterConfig, VirtualGameMaster
from chat_api_selector import VirtualGameMasterChatAPISelector
def display_recent_messages(app: VirtualGameMaster, num_messages: int = 4):
recent_messages = app.history.messages[-num_messages:]
for message in recent_messages:
role = "Game Master" if message.role == "assistant" else "You"
print(f"{role}: {message.content}\n")
def run_cli(app: VirtualGameMaster):
app.load()
print("Welcome to the Virtual Game Master App! Type in your next message to the Game Master or use '" + app.config.COMMAND_PREFIX + "help' to show all available commands.")
print("Use '+' at the end of a line to continue input on the next line.")
display_recent_messages(app)
while True:
user_input = ""
while True:
line = input("You: " if not user_input else "... ")
if line.endswith('+'):
user_input += line[:-1] + "\n"
else:
user_input += line
break
response_generator, should_exit = app.process_input(user_input.strip(), True)
if should_exit:
break
print(f"\n", flush=True)
if isinstance(response_generator, str):
print(f"Game Master: {response_generator}\n")
else:
print(f"Game Master:", end="", flush=True)
for tok in response_generator:
print(tok, end="", flush=True)
print("\n")
# Usage
if __name__ == "__main__":
config = VirtualGameMasterConfig.from_env(".env")
config.GAME_SAVE_FOLDER = "chat_history/new_nov_new_24"
api_selector = VirtualGameMasterChatAPISelector(config)
api = api_selector.get_api()
vgm_app = VirtualGameMaster(config, api, True)
run_cli(vgm_app)