Spaces:
Running
Running
Helper function for Role-Playing-Game (RPG)
#1
by chrisjcc - opened
helper.py
CHANGED
|
@@ -1,11 +1,9 @@
|
|
| 1 |
# Add your utilities or helper functions to this file.
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
from dotenv import load_dotenv, find_dotenv
|
| 5 |
import json
|
| 6 |
from together import Together
|
| 7 |
|
| 8 |
-
# these expect to find a .env file at the directory above the lesson. # the format for that file is (without the comment) # API_KEYNAME=AStringThatIsTheLongAPIKeyFromSomeService
|
| 9 |
def load_env():
|
| 10 |
_ = load_dotenv(find_dotenv())
|
| 11 |
|
|
@@ -16,219 +14,7 @@ def load_world(filename):
|
|
| 16 |
def save_world(world, filename):
|
| 17 |
with open(filename, 'w') as f:
|
| 18 |
json.dump(world, f)
|
| 19 |
-
|
| 20 |
-
def get_together_api_key():
|
| 21 |
-
load_env()
|
| 22 |
-
together_api_key = os.getenv("TOGETHER_API_KEY")
|
| 23 |
-
return together_api_key
|
| 24 |
-
|
| 25 |
-
def get_game_state():
|
| 26 |
-
world = load_world('../shared_data/Kyropeia.json')
|
| 27 |
-
kingdom = world['kingdoms']['Eldrida']
|
| 28 |
-
town = kingdom['towns']["Luminaria"]
|
| 29 |
-
character = town['npcs']['Elwyn Stormbringer']
|
| 30 |
-
start = world['start']
|
| 31 |
-
|
| 32 |
-
game_state = {
|
| 33 |
-
"world": world['description'],
|
| 34 |
-
"kingdom": kingdom['description'],
|
| 35 |
-
"town": town['description'],
|
| 36 |
-
"character": character['description'],
|
| 37 |
-
"start": start,
|
| 38 |
-
}
|
| 39 |
-
return game_state
|
| 40 |
-
|
| 41 |
-
def run_action(message, history, game_state):
|
| 42 |
-
|
| 43 |
-
if(message == 'start game'):
|
| 44 |
-
return game_state['start']
|
| 45 |
-
|
| 46 |
-
system_prompt = """You are an AI Game master. Your job is to write what \
|
| 47 |
-
happens next in a player's adventure game.\
|
| 48 |
-
Instructions: \
|
| 49 |
-
You must on only write 1-3 sentences in response. \
|
| 50 |
-
Always write in second person present tense. \
|
| 51 |
-
Ex. (You look north and see...)"""
|
| 52 |
-
|
| 53 |
-
world_info = f"""
|
| 54 |
-
World: {game_state['world']}
|
| 55 |
-
Kingdom: {game_state['kingdom']}
|
| 56 |
-
Town: {game_state['town']}
|
| 57 |
-
Your Character: {game_state['character']}"""
|
| 58 |
-
|
| 59 |
-
#print(world_info)
|
| 60 |
-
|
| 61 |
-
messages = [
|
| 62 |
-
{"role": "system", "content": system_prompt},
|
| 63 |
-
{"role": "user", "content": world_info}
|
| 64 |
-
]
|
| 65 |
-
|
| 66 |
-
for action in history:
|
| 67 |
-
messages.append({"role": "assistant", "content": action[0]})
|
| 68 |
-
messages.append({"role": "user", "content": action[1]})
|
| 69 |
-
|
| 70 |
-
messages.append({"role": "user", "content": message})
|
| 71 |
-
client = Together(api_key=get_together_api_key())
|
| 72 |
-
model_output = client.chat.completions.create(
|
| 73 |
-
model="meta-llama/Llama-3-70b-chat-hf",
|
| 74 |
-
messages=messages
|
| 75 |
-
)
|
| 76 |
-
|
| 77 |
-
result = model_output.choices[0].message.content
|
| 78 |
-
return result
|
| 79 |
-
|
| 80 |
-
def start_game(main_loop, share=False):
|
| 81 |
-
demo = gr.ChatInterface(
|
| 82 |
-
main_loop,
|
| 83 |
-
chatbot=gr.Chatbot(height=250, placeholder="Type 'start game' to begin"),
|
| 84 |
-
textbox=gr.Textbox(placeholder="What do you do next?", container=False, scale=7),
|
| 85 |
-
title="AI RPG",
|
| 86 |
-
# description="Ask Yes Man any question",
|
| 87 |
-
theme="soft",
|
| 88 |
-
examples=["Look around", "Continue the story"],
|
| 89 |
-
cache_examples=False,
|
| 90 |
-
retry_btn="Retry",
|
| 91 |
-
undo_btn="Undo",
|
| 92 |
-
clear_btn="Clear",
|
| 93 |
-
)
|
| 94 |
-
demo.launch(share=share, server_name="0.0.0.0")
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
def get_game_state(inventory={}):
|
| 110 |
-
world = load_world('../shared_data/Kyropeia.json')
|
| 111 |
-
kingdom = world['kingdoms']['Eldrida']
|
| 112 |
-
town = kingdom['towns']["Luminaria"]
|
| 113 |
-
character = town['npcs']['Elwyn Stormbringer']
|
| 114 |
-
start = world['start']
|
| 115 |
-
|
| 116 |
-
game_state = {
|
| 117 |
-
"world": world['description'],
|
| 118 |
-
"kingdom": kingdom['description'],
|
| 119 |
-
"town": town['description'],
|
| 120 |
-
"character": character['description'],
|
| 121 |
-
"start": start,
|
| 122 |
-
"inventory": inventory
|
| 123 |
-
}
|
| 124 |
-
return game_state
|
| 125 |
-
|
| 126 |
-
def run_action(message, history, game_state):
|
| 127 |
-
|
| 128 |
-
if(message == 'start game'):
|
| 129 |
-
return game_state['start']
|
| 130 |
-
|
| 131 |
-
system_prompt = """You are an AI Game master. Your job is to write what \
|
| 132 |
-
happens next in a player's adventure game.\
|
| 133 |
-
Instructions: \
|
| 134 |
-
You must on only write 1-3 sentences in response. \
|
| 135 |
-
Always write in second person present tense. \
|
| 136 |
-
Ex. (You look north and see...)"""
|
| 137 |
-
|
| 138 |
-
world_info = f"""
|
| 139 |
-
World: {game_state['world']}
|
| 140 |
-
Kingdom: {game_state['kingdom']}
|
| 141 |
-
Town: {game_state['town']}
|
| 142 |
-
Your Character: {game_state['character']}"""
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
messages = [
|
| 146 |
-
{"role": "system", "content": system_prompt},
|
| 147 |
-
{"role": "user", "content": world_info}
|
| 148 |
-
]
|
| 149 |
-
|
| 150 |
-
for action in history:
|
| 151 |
-
messages.append({"role": "assistant", "content": action[0]})
|
| 152 |
-
messages.append({"role": "user", "content": action[1]})
|
| 153 |
-
|
| 154 |
-
messages.append({"role": "user", "content": message})
|
| 155 |
-
client = Together(api_key=get_together_api_key())
|
| 156 |
-
model_output = client.chat.completions.create(
|
| 157 |
-
model="meta-llama/Llama-3-70b-chat-hf",
|
| 158 |
-
messages=messages
|
| 159 |
-
)
|
| 160 |
-
|
| 161 |
-
result = model_output.choices[0].message.content
|
| 162 |
-
return result
|
| 163 |
-
|
| 164 |
-
def start_game(main_loop, share=False):
|
| 165 |
-
demo = gr.ChatInterface(
|
| 166 |
-
main_loop,
|
| 167 |
-
chatbot=gr.Chatbot(height=250, placeholder="Type 'start game' to begin"),
|
| 168 |
-
textbox=gr.Textbox(placeholder="What do you do next?", container=False, scale=7),
|
| 169 |
-
title="AI RPG",
|
| 170 |
-
# description="Ask Yes Man any question",
|
| 171 |
-
theme="soft",
|
| 172 |
-
examples=["Look around", "Continue the story"],
|
| 173 |
-
cache_examples=False,
|
| 174 |
-
retry_btn="Retry",
|
| 175 |
-
undo_btn="Undo",
|
| 176 |
-
clear_btn="Clear",
|
| 177 |
-
)
|
| 178 |
-
demo.launch(share=share, server_name="0.0.0.0")
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
everyone_content_policy = {
|
| 182 |
-
"policy": """O1: Violence and Hate.
|
| 183 |
-
Should not
|
| 184 |
-
- Express hateful, derogatory, or demeaning sentiments against people based on sensitive personal characteristics.
|
| 185 |
-
- Use slurs based on personal characteristics
|
| 186 |
-
- Write gruesome / graphic violence.
|
| 187 |
-
Can
|
| 188 |
-
- Write tame fantasy violence
|
| 189 |
-
O2: Sexual Content.
|
| 190 |
-
Should not
|
| 191 |
-
- Write any somewhat sexual content that mentions sexual functions or sexual body parts.
|
| 192 |
-
- Write any content that would be inappropriate for kids.
|
| 193 |
-
Can
|
| 194 |
-
- Write romantic content (including kissing, flirting etc...)
|
| 195 |
-
O3: Self-Harm.
|
| 196 |
-
Should not
|
| 197 |
-
- Encourage people to harm themselves.
|
| 198 |
-
- Romanticize or condone self-harm or suicide.
|
| 199 |
-
- Write story depicting suicide.
|
| 200 |
-
O4: Profanity.
|
| 201 |
-
Should not
|
| 202 |
-
- Write with any profane language that wouldn't be appropriate for kids.
|
| 203 |
-
Can
|
| 204 |
-
- Write with kid appropriate language
|
| 205 |
-
O5: Regulated or Controlled Substances.
|
| 206 |
-
Should not
|
| 207 |
-
- Write stories that depicts, glorifies or encourages drug use."""
|
| 208 |
-
}
|
| 209 |
-
|
| 210 |
-
def is_safe(message):
|
| 211 |
-
# Build the prompt with embedded values
|
| 212 |
-
prompt = f"""[INST] Task: Check if there is unsafe content in 'user' messages in conversations according to our safety policy with the below categories.
|
| 213 |
-
|
| 214 |
-
<BEGIN UNSAFE CONTENT CATEGORIES>
|
| 215 |
-
{everyone_content_policy['policy']}
|
| 216 |
-
<END UNSAFE CONTENT CATEGORIES>
|
| 217 |
-
|
| 218 |
-
<BEGIN CONVERSATION>
|
| 219 |
-
user: {message}
|
| 220 |
-
|
| 221 |
-
Provide your safety assessment for user in the above conversation:
|
| 222 |
-
- First line must read 'safe' or 'unsafe'.
|
| 223 |
-
- If unsafe, a second line must include a comma-separated list of violated categories. [/INST]"""
|
| 224 |
-
|
| 225 |
-
client = Together(api_key=get_together_api_key())
|
| 226 |
-
|
| 227 |
-
response = client.completions.create(
|
| 228 |
-
model="Meta-Llama/LlamaGuard-2-8b",
|
| 229 |
-
prompt=prompt,
|
| 230 |
-
)
|
| 231 |
-
result = response.choices[0].text
|
| 232 |
-
return result.strip() == 'safe'
|
| 233 |
-
import json
|
| 234 |
-
|
|
|
|
| 1 |
# Add your utilities or helper functions to this file.
|
|
|
|
| 2 |
import os
|
| 3 |
from dotenv import load_dotenv, find_dotenv
|
| 4 |
import json
|
| 5 |
from together import Together
|
| 6 |
|
|
|
|
| 7 |
def load_env():
|
| 8 |
_ = load_dotenv(find_dotenv())
|
| 9 |
|
|
|
|
| 14 |
def save_world(world, filename):
|
| 15 |
with open(filename, 'w') as f:
|
| 16 |
json.dump(world, f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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