-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.py
85 lines (61 loc) · 2.55 KB
/
index.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
from dotenv import load_dotenv
# Load local .env file
load_dotenv()
import json
import os
from halo import Halo
from lib.prompts import prepare_define_story_prompt, prepare_generate_story_prompt, prepare_system_prompt
from lib.gpt import get_gpt_chat_response, get_gpt_response
from lib.print import narrator_print, player_print, game_end_print
from lib.game import get_narration_mechanism, get_side_characters, get_story_setting, get_story_theme, get_story_rounds, get_side_characters_w_occurrence, get_round_side_characters
GPT_MODEL = os.environ['GPT_MODEL']
def clear_terminal():
os.system('cls' if os.name == 'nt' else 'clear')
spinner = Halo(spinner='dots')
theme = get_story_theme()
clear_terminal()
setting = get_story_setting()
clear_terminal()
side_characters = get_side_characters()
clear_terminal()
story_rounds = get_story_rounds()
clear_terminal()
mechanism = get_narration_mechanism()
clear_terminal()
with_choices = False
if mechanism == 'choice_based':
with_choices = True
if (len(side_characters)):
side_characters = get_side_characters_w_occurrence(story_rounds, side_characters)
print('\n')
spinner.start('Creating your story...')
story_setting_gpt_response = get_gpt_response(prompt=prepare_define_story_prompt(theme, setting), model=GPT_MODEL, temperature=1.2)
story_setting = json.loads(story_setting_gpt_response)
messages = [
{"role": "system", "content": prepare_system_prompt()},
{"role": "user", "content": prepare_generate_story_prompt(story_setting, with_choices)}
]
spinner.stop()
while True:
clear_terminal()
# Decrease remaining rounds by 1 on every player prompt
story_rounds -= 1
narrator_print('Narrator: \n')
response = get_gpt_chat_response(messages, model=GPT_MODEL, print_stream=True, print_func=narrator_print, temperature=0.2)
messages.append({"role": "assistant", "content": response})
# End game condition check
if story_rounds < 1:
break
player_print('\nPlayer: \n')
player_input = input()
available_side_chars = get_round_side_characters(story_rounds, side_characters)
if (len(available_side_chars)):
for char in available_side_chars:
player_input+= f". ADD CHARACTER: '{char['character']}'"
if (story_rounds == 3):
player_input+= ". DRAW" # Draw the user close to the ending of the game
if (story_rounds == 1):
player_input+= ". END" # Finalize the game, resulting in ending it.
messages.append({"role": "user", "content": player_input})
game_end_print("\n\nTHE END")
game_end_print("\n\n\nThank you for playing!")