from fastapi import FastAPI, HTTPException import openai # Initialize the FastAPI app app = FastAPI() # Set your OpenAI API key directly here openai.api_key = "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" @app.get("/") def greet_json(): return {"Hello": "World!"} @app.post("/chat") async def chat_with_gpt(prompt: str): try: response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}] ) return {"response": response.choices[0].message['content']} except Exception as e: raise HTTPException(status_code=500, detail=str(e))