import os import openai import tweepy from fastapi import FastAPI openai.api_key = os.environ.get("openai_api_key") app = FastAPI() _tweet = "I'm a pretty average middle aged guy, got a solid job and had a good family but things got messy and took " \ "a turn for the worst. Everyone says you need to hit rock bottom to go back up so here I am!" def prompt(tweet, roast=True): roast_or_toast = "snarky joke" if roast else "encouragement" return f"A user tweeted this tweet:\n\n{tweet}\\n\nThe following {roast_or_toast} was given as an answer:" @app.get("/") def read_root(): response = openai.Completion.create( engine="text-davinci-002", prompt=prompt(_tweet, roast=True), temperature=0.7, max_tokens=60, top_p=1, frequency_penalty=0, presence_penalty=0 ) return response.choices[0].text.strip()