This commit is contained in:
Kare-Udon 2023-03-05 00:07:12 +08:00
commit 5c5dbc0bf4
3 changed files with 33 additions and 0 deletions

9
README.md Normal file
View file

@ -0,0 +1,9 @@
# ChatGPT Web API backend with Python and FastAPI
## Run
```bash
pip install -r requirements.txt
OPEN_AI_KEY=sk-xxxxxx uvicorn main:app --reload
```

22
main.py Normal file
View file

@ -0,0 +1,22 @@
import os
import openai
from fastapi import FastAPI
app = FastAPI()
openai.api_key = os.getenv("OPEN_AI_KEY")
@app.get("/getAns")
async def root(msg: str):
return {"ans": get_ans(msg)}
def get_ans(msg):
return openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": msg},
]
).choices[0].message.content

2
requirements.txt Normal file
View file

@ -0,0 +1,2 @@
fastapi~=0.92.0
openai~=0.27.0