Spaces:
Sleeping
Sleeping
from typing import List | |
import re | |
from dataclasses import dataclass | |
from chat_gpt import get_chatgpt_answer, get_chatgpt_serious_answer | |
N_CHOICES = 3 | |
class ConversationSituation: | |
character: str | |
character_emoji: str | |
situation: str | |
goal: str | |
goal_message: str | |
first_message: str | |
starting_choices: List[str] | |
class ConversationResponse: | |
message: str | |
has_won: bool | |
choices: List[str] | |
sample_conversation_talent = ConversationSituation( | |
character='Celebrity Judge', | |
character_emoji='😎', | |
situation="""You're auditioning for a talent show, and you are talking to the celebrity judge, who is known for being a tough critic. You will have to decide which of the acts you practiced you will perform. The judge is a little grumpy, but can be impressed if you appeal to his heart.""", | |
goal="impress the judge in the talent show", | |
goal_message="Showcase your talent and shine on the stage!", | |
first_message="Show me what you've got! I have high standards, you know. 🎤", | |
starting_choices=[ | |
"Prepare to be amazed! ✨", | |
"Advice to improve, please? 🎶", | |
"Doubting my standout potential... 😔", | |
"Prove you wrong on stage! 😎", | |
] | |
) | |
sample_conversation_ghost = ConversationSituation( | |
character='Ghost', | |
character_emoji='👻', | |
situation="""You and your friends have decided to explore a haunted house on Halloween night. The ghost haunting the house is known for playing pranks on visitors.""", | |
goal="survive the night without getting scared", | |
goal_message="Stay calm and make it out alive!", | |
first_message="Welcome to my haunted house! Prepare to be scared!", | |
starting_choices=[ | |
"I'm not afraid of ghosts! 👻", | |
"Please, Mr. Ghost, go easy on us! 🙏", | |
"I admit, I'm a little scared... 😨", | |
"You're not a real ghost, just a trickster! 😒", | |
] | |
) | |
sample_conversation = ConversationSituation( | |
character='Papá Noel', | |
character_emoji='🎅', | |
situation="""Aunque has sido una buena niña durante casi todo el año, has cometido algunas travesuras. Papá Noel no quiere enviarte regalos esta Navidad, pero intentarás convencerlo de cambiar de opinión. Papá Noel habla español y es argentino.""", | |
goal="""get a present""", | |
goal_message="""Conseguí un regalo!""", | |
first_message="""Te estás portando mal, serás castigada! No voy a darte ningún regalo este año.""", | |
starting_choices=[ | |
"No me importan los regalos 😏", | |
# "Nooo, Papá Noel, por favor! 🙏", | |
"Lo entiendo, sí, me porté mal 😞", | |
"Mentira! Me porté bien 😤" | |
] | |
) | |
sample_conversation_christmas = ConversationSituation( | |
character='Tiny, el gato', | |
character_emoji='🐱', | |
situation="""The cat is known for its independent nature and prefers to keep its distance from humans. It sees petting as an intrusion into its personal space. The player must find a way to appeal to the cat's curiosity or offer a desirable incentive, such as treats or toys, to convince the cat to allow them to pet it. Patience and understanding are key in this goal. The cat speaks Spanish and is from Argentina.""", | |
goal="""pet the cat""", | |
goal_message="""Hazte amigo del gato!""", | |
first_message="""¡Miau! ¿Quién se atreve a interrumpir mi siesta?""", | |
starting_choices=[ | |
"Perdón, rey de la siesta 👑", | |
"Hola gatito, ¿puedo dormir con vos? 😴", | |
# "Perdón, prometo no hacer ruido la próxima 🙇", | |
"Uh, disculpá, ¿me voy? 🚶♂️", | |
] | |
) | |
sample_conversation_alex = ConversationSituation( | |
character='Alex', | |
character_emoji='👱🏻', | |
situation="""Your best friend, Alex, has been acting distant lately and canceling plans at the last minute. You're worried about their well-being and want to find out what's going on.""", | |
goal="""Find out about Alex parents divorce""", | |
goal_message="""Get Alex to open up about what's been bothering them.""", | |
first_message="""Hey, sorry I had to cancel again.""", | |
starting_choices=[ | |
"I'm here to listen. Take your time. 😊", | |
"If you don't want to talk, that's okay too. 🙏", | |
"Just know that I care about you deeply. ❤️", | |
"Is there anything I can do to support you? 🤗" | |
] | |
) | |
def generate_chat_response_old(user_msg: str) -> ConversationResponse: | |
message = 'Did you just said ' + user_msg + '?' | |
has_won = False | |
new_choices = [ | |
"a", "b", "c", "d", "e" | |
] | |
return ConversationResponse(message=message, has_won=has_won, choices=new_choices) | |
def _parse_conversation_response(string: str) -> ConversationResponse: | |
lines = string.split("\n") | |
message = lines[0] | |
has_won = "Outcome: Won" in string | |
pattern = r'\d+\.\s+"([^"]+)"' | |
choices = re.findall(pattern, string) | |
return ConversationResponse(message, has_won, choices) | |
def generate_chat_response(situation: ConversationSituation, dialogue: str, n_choices=N_CHOICES): | |
prompt_template = """RULES | |
Welcome to "TextQuest", an interactive text-based game where you engage in realistic conversations with various characters through your phone. Each conversation reflects different relationships in your life, and your choices will shape the outcome of the story. Get ready to navigate through challenging scenarios, make crucial decisions, and see where your text-based adventures take you! | |
Note: Remember, the character's relationship to you influences the dynamics of the conversation. The goal must be achievable through your response options, and the game ends either when you successfully meet the goal or when you clearly fail to do so. Choose your options wisely, and let the text-based journey begin! | |
INSTRUCTIONS | |
Reply to the conversation as {opponent} with the format below. | |
You must include {n} options if the game continues, ignore otherwise. At least one of them must really increase the chance of winning. One other of the options must mislead the Player to lower the chances of winning. If the Player hasn't achieved the goal after the 5th reply, {opponent} must decide whether they will help the player achieve the goal and drive the conversation towards the outcome. | |
CONTEXT | |
Opponent: {opponent} | |
Situation: {situation} | |
Win Condition: {goal} | |
FORMAT | |
<{opponent} 7-word response to the last Player message in the conversation> | |
Outcome: <Won, Lost, or Continue, depending on whether the player achieved the goal> | |
New Options: | |
<Index>. "<A possible new response from the Player. Must have a length of 5 words max. and end with an emoji>" | |
END OF FORMAT | |
CONVERSATION | |
{conversation} | |
{opponent}:""" | |
prompt = prompt_template.format( | |
opponent=situation.character, | |
situation=situation.situation, | |
goal=situation.goal, | |
conversation=dialogue, | |
n=n_choices | |
) | |
raw_response = get_chatgpt_answer(prompt) | |
conversation_response = _parse_conversation_response(raw_response) | |
print(raw_response) | |
return conversation_response | |
def generate_last_response(situation: ConversationSituation, dialogue: str): | |
prompt_template = """RULES | |
Welcome to "TextQuest", an interactive text-based game where you engage in realistic conversations with various characters through your phone. Each conversation reflects different relationships in your life, and your choices will shape the outcome of the story. Get ready to navigate through challenging scenarios, make crucial decisions, and see where your text-based adventures take you! | |
Note: Remember, the character's relationship to you influences the dynamics of the conversation. The goal must be achievable through your response options, and the game ends either when you successfully meet the goal or when you clearly fail to do so. Choose your options wisely, and let the text-based journey begin! | |
INSTRUCTIONS | |
Based on the whole conversation, decide whether you want the Player to {goal} or not. | |
As the {opponent}, you must send one final message and the outcome must be one of Won or Lost. Reply to the conversation as {opponent} with the format below. | |
CONTEXT | |
Opponent: {opponent} | |
Situation: {situation} | |
Goal: {goal} | |
FORMAT | |
<{opponent} 7-word response to the last Player message in the conversation> | |
Outcome: <Won or Lost depending on whether the player achieved the goal> | |
END OF FORMAT | |
CONVERSATION | |
{conversation} | |
{opponent}:""" | |
prompt = prompt_template.format( | |
opponent=situation.character, | |
situation=situation.situation, | |
goal=situation.goal, | |
conversation=dialogue | |
) | |
raw_response = get_chatgpt_serious_answer(prompt) | |
conversation_response = _parse_conversation_response(raw_response) | |
print(raw_response) | |
return conversation_response |