pratikshahp commited on
Commit
65d6bfa
·
verified ·
1 Parent(s): 0fa85db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -1
app.py CHANGED
@@ -51,7 +51,7 @@ game_state = {
51
  "start": start,
52
  }
53
 
54
- def run_action(message, history, game_state):
55
 
56
  if(message == 'start game'):
57
  return game_state['start']
@@ -86,6 +86,50 @@ Your Character: {game_state['character']}"""
86
  result = model_output.choices[0].message.content
87
  return result
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  def main_loop(message, history):
90
  return run_action(message, history, game_state)
91
 
 
51
  "start": start,
52
  }
53
 
54
+ def run_action12(message, history, game_state):
55
 
56
  if(message == 'start game'):
57
  return game_state['start']
 
86
  result = model_output.choices[0].message.content
87
  return result
88
 
89
+ def run_action(message, history, game_state):
90
+ if message.lower() == 'start game':
91
+ return game_state['start']
92
+
93
+ system_prompt = """You are an AI Game master. Your job is to write what \
94
+ happens next in a player's adventure game.\
95
+ Instructions: \
96
+ - Write only 1-3 sentences. \
97
+ - Always write in second person, e.g., "You look north and see..." \
98
+ - Write in present tense."""
99
+
100
+ world_info = f"""
101
+ World: {game_state['world']}
102
+ Kingdom: {game_state['kingdom']}
103
+ Town: {game_state['town']}
104
+ Your Character: {game_state['character']}"""
105
+
106
+ # Build the conversation context
107
+ messages = [
108
+ {"role": "system", "content": system_prompt},
109
+ {"role": "user", "content": world_info}
110
+ ]
111
+
112
+ for action in history:
113
+ # Ensure action is in the correct format before appending
114
+ if isinstance(action, tuple) and len(action) == 2:
115
+ messages.append({"role": "assistant", "content": action[0]})
116
+ messages.append({"role": "user", "content": action[1]})
117
+ else:
118
+ # If action is not in the correct format, log a warning or handle the case
119
+ print(f"Warning: Invalid action format: {action}")
120
+
121
+ # Add the user's current message
122
+ messages.append({"role": "user", "content": message})
123
+
124
+ # Get the model's response
125
+ model_output = client.chat.completions.create(
126
+ model="meta-llama/Llama-3-70b-chat-hf",
127
+ messages=messages
128
+ )
129
+
130
+ return model_output.choices[0].message.content
131
+
132
+
133
  def main_loop(message, history):
134
  return run_action(message, history, game_state)
135