File size: 767 Bytes
2637615 6b0f734 2637615 6b0f734 2637615 6b0f734 2637615 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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))
|