Spaces:
Build error
Build error
File size: 5,034 Bytes
a5dc687 b153ce7 6789d19 6a8247a e338b03 0fa85db e338b03 0fa85db a5dc687 65d6bfa 84ad949 a5dc687 84ad949 65d6bfa 84ad949 0fa85db 84ad949 | 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | import gradio as gr
import os
from helper import load_world, save_world
from together import Together
from helper import get_together_api_key, load_env
client = Together(api_key=get_together_api_key())
world = load_world('./YourWorld_L1.json')
kingdom = world['kingdoms']['Varanasi Kingdom']
town = kingdom['towns']["Sarnath"]
character = town['npcs']['Rohan Vyas']
system_prompt = """You are an AI Game master. Your job is to create a
start to an adventure based on the world, kingdom, town and character
a player is playing as.
Instructions:
You must only use 2-4 sentences \
Write in second person. For example: "You are Jack" \
Write in present tense. For example "You stand at..." \
First describe the character and their backstory. \
Then describes where they start and what they see around them."""
world_info = f"""
World: {world}
Kingdom: {kingdom}
Town: {town}
Your Character: {character}
"""
model_output = client.chat.completions.create(
model="meta-llama/Llama-3-70b-chat-hf",
temperature=1.0,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": world_info + '\nYour Start:'}
],
)
start = model_output.choices[0].message.content
print(start)
world['start'] = start
#save_world(world, '../shared_data/Kyropeia.json') # preserve video version
save_world(world, '.YourWorld_L1.json')
game_state = {
"world": world['description'],
"kingdom": kingdom['description'],
"town": town['description'],
"character": character['description'],
"start": start,
}
def run_action12(message, history, game_state):
if(message == 'start game'):
return game_state['start']
system_prompt = """You are an AI Game master. Your job is to write what \
happens next in a player's adventure game.\
Instructions: \
You must on only write 1-3 sentences in response. \
Always write in second person present tense. \
Ex. (You look north and see...)"""
world_info = f"""
World: {game_state['world']}
Kingdom: {game_state['kingdom']}
Town: {game_state['town']}
Your Character: {game_state['character']}"""
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": world_info}
]
for action in history:
messages.append({"role": "assistant", "content": action[0]})
messages.append({"role": "user", "content": action[1]})
messages.append({"role": "user", "content": message})
model_output = client.chat.completions.create(
model="meta-llama/Llama-3-70b-chat-hf",
messages=messages
)
result = model_output.choices[0].message.content
return result
def run_action(message, history, game_state):
if message.lower() == 'start game':
return game_state['start']
system_prompt = """You are an AI Game master. Your job is to write what \
happens next in a player's adventure game.\
Instructions: \
- Write only 1-3 sentences. \
- Always write in second person, e.g., "You look north and see..." \
- Write in present tense."""
world_info = f"""
World: {game_state['world']}
Kingdom: {game_state['kingdom']}
Town: {game_state['town']}
Your Character: {game_state['character']}"""
# Build the conversation context
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": world_info}
]
for action in history:
# Ensure action is in the correct format before appending
if isinstance(action, tuple) and len(action) == 2:
messages.append({"role": "assistant", "content": action[0]})
messages.append({"role": "user", "content": action[1]})
else:
# If action is not in the correct format, log a warning or handle the case
print(f"Warning: Invalid action format: {action}")
# Add the user's current message
messages.append({"role": "user", "content": message})
# Get the model's response
model_output = client.chat.completions.create(
model="meta-llama/Llama-3-70b-chat-hf",
messages=messages
)
return model_output.choices[0].message.content
def main_loop(message, history):
return run_action(message, history, game_state)
demo = None # Added to allow restart
def start_game(main_loop, share=False):
global demo
# If demo is already running, close it first
if demo is not None:
demo.close()
demo = gr.ChatInterface(
main_loop,
chatbot=gr.Chatbot(
height=250,
placeholder="Type 'start game' to begin",
type='messages' # Updated parameter for Gradio's chatbot messages
),
textbox=gr.Textbox(
placeholder="What do you do next?",
container=False,
scale=7
),
title="AI RPG",
theme="soft",
examples=["Look around", "Continue the story"],
cache_examples=False,
)
demo.launch(share=share, server_name="0.0.0.0")
start_game(main_loop, True) |