import uuid import os import random import prompts import json from adaptors.llm import answer from adaptors.voice import say_new magic_w = os.environ["MAGICWORD"] ''' check if valid JSON ''' def is_valid_json(input_string): try: json_object = json.loads(input_string) except json.JSONDecodeError: return False return True def gen_unique_id(): return str(uuid.uuid4()) def check_magic_word(w): return magic_w in w def get_fixed_msg(msg_type): fixed_sayings = { "welcome": ["Welcome, welcome my friend! I'd be happy to tell you a story. But one thing first... Do you know the magic word?"], "wrong_magic_word": ["Oh, my dear, I'm very sorry. But it looks like you don't know the magic word. And I'm afraid I can't tell you the story without the magic word. Please ask around for the magic word and try again then. I'll wait here."], "ask_world": ["Wonderful! That is right... Get ready for a great story that you and I can create together. But before we start, let me ask you a few questions first. What kind of world would you like our story to unfold in? Maybe Middle Earth with elves and orcs? Or a distant future world full of space travel? It can be anything. Just tell me."], "ask_hero": ["That sounds great. And who should our hero be?"], "ask_plot": ["Splendid! Now let's think about the plot of our story. Can you describe in just a few words what our story should be all about. Maybe the hero finds the love of their life? Or maybe they travel far and wide to discover their powers and find friends... it can be anything you want. Just say it and we will make it happen."], "ask_ending": ["That is great. And what kind of ending would you like our story to have? A happy one? A tragic one? Something else?"], "ask_style": ["I see. Now lastly, what kind of storytelling style do you like most? Would you like our story to be funny? Or Epic? Or Poetic? You can decide!"], "its_the_end": ["And this is the end of our story. Thank you, my dear, for making it with me."], "no_more_story": ["I'm very sorry, my dear, but this story has ended. But you can come back again later and we will make another great story together."] } if msg_type in fixed_sayings: saying = random.choice(fixed_sayings[msg_type]) system_message = prompts.get('paraphraser') paraphrased = answer(system_message, [{ "role" : "user", "content" : saying }]) audio = say_new(paraphrased) return audio else: raise Exception(f"fixed saying with msg_type {msg_type} not found")