Spaces:
Running
Running
Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pixeltable as pxt
|
| 2 |
+
import re
|
| 3 |
+
|
| 4 |
+
@pxt.udf
|
| 5 |
+
def generate_messages(genre: str, player_name: str, initial_scenario: str, player_input: str, turn_number: int) -> list[dict]:
|
| 6 |
+
return [
|
| 7 |
+
{
|
| 8 |
+
'role': 'system',
|
| 9 |
+
'content': f"""You are the game master for a {genre} RPG. The player's name is {player_name}.
|
| 10 |
+
Provide an engaging response to the player's action and present exactly 3 numbered options for their next move:
|
| 11 |
+
1. A dialogue option (saying something)
|
| 12 |
+
2. A random action they could take
|
| 13 |
+
3. A unique or unexpected choice
|
| 14 |
+
|
| 15 |
+
Format each option with a number (1., 2., 3.) followed by the action description."""
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
'role': 'user',
|
| 19 |
+
'content': f"Current scenario: {initial_scenario}\n"
|
| 20 |
+
f"Player's action: {player_input}\n"
|
| 21 |
+
f"Turn number: {turn_number}\n\n"
|
| 22 |
+
"Provide a response and 3 options:"
|
| 23 |
+
}
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
@pxt.udf
|
| 27 |
+
def extract_options(story_text: str) -> list[str]:
|
| 28 |
+
"""Extract the three options from the story text"""
|
| 29 |
+
# Look for numbered options (1., 2., 3.) and grab the text after them
|
| 30 |
+
options = re.findall(r'\d\.\s*(.*?)(?=\d\.|$)', story_text, re.DOTALL)
|
| 31 |
+
# Clean up the options and ensure we have exactly 3
|
| 32 |
+
options = [opt.strip() for opt in options[:3]]
|
| 33 |
+
while len(options) < 3:
|
| 34 |
+
options.append("Take another action...")
|
| 35 |
+
return options
|