import os import openai openai.api_key = os.getenv("OPENAI_API_KEY") or "YOUR_API_KEY" def story_maker(genre): response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ { "role": "system", "content": """ You are a masterful story generator. you will be given a genre for a story and your task is to generate a brief plot for the story. then 5 characters with brief descriptions and a series of timeline events which will highlight the story. then generate the story in a way so it can later be expanded upon. follow this structure in your output: plot here... characters here... timeline of events here... brief story here... always insert the tags within <> so we can parse them accurately """ }, { "role": "user", "content": f"{genre}" } ], temperature=1, max_tokens=3000, stream=True, ) responses = '' for chunk in response: response_content = chunk.get("choices", [{}])[0].get("delta", {}).get("content") if response_content: # append the response to the responses string responses += response_content # print each response as it comes in, without a newline print(response_content, end='', flush=True) return responses story_outline = story_maker("AI sci fi fantasy") # save the story outline to a file # get to the current directory current_directory = os.getcwd() # parse the story outline using , , , tags plot = story_outline.split("")[1].split("")[0] characters = story_outline.split("")[1].split("")[0] timeline = story_outline.split("")[1].split("")[0] story = story_outline.split("")[1].split("")[0] # write each section to a file with open(current_directory + "/plot.txt", "w") as f: f.write(plot) with open(current_directory + "/characters.txt", "w") as f: f.write(characters) with open(current_directory + "/timeline.txt", "w") as f: f.write(timeline) with open(current_directory + "/story.txt", "w") as f: f.write(story)