commit 5c5dbc0bf4ede488a6f2bf8595e8700697609e15 Author: Kare-Udon Date: Sun Mar 5 00:07:12 2023 +0800 Update diff --git a/README.md b/README.md new file mode 100644 index 0000000..e23bec3 --- /dev/null +++ b/README.md @@ -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 +``` \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..f0f0b24 --- /dev/null +++ b/main.py @@ -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 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6fd3341 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +fastapi~=0.92.0 +openai~=0.27.0 \ No newline at end of file