-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgptapi.py
40 lines (30 loc) · 1000 Bytes
/
gptapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from fastapi import FastAPI, HTTPException
import openai
from pydantic import BaseModel
import logging
logging.basicConfig(level=logging.INFO)
API_KEY = "OPENAI_API_KEY"
openai.api_key = API_KEY
app = FastAPI()
class prompt(BaseModel):
question: str
@app.get("/")
async def welcome():
return {"message": "gpt demo"}
@app.post("/prompt")
async def answer(item: prompt):
prompt_txt = item.question
logging.info(f"Prompt sent: {prompt_txt}")
try:
response = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt_txt}],
max_tokens=100,
temperature=0.7,
)
logging.info(f"OpenAI API response: {response}")
answer = response["choices"][0]["message"]["content"]
return {"content": answer}
except Exception as e:
logging.error(f"Error occurred: {str(e)}")
raise HTTPException(status_code=500, detail=f"Error occurred: {str(e)}")