Update
This commit is contained in:
commit
5c5dbc0bf4
3 changed files with 33 additions and 0 deletions
9
README.md
Normal file
9
README.md
Normal 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
22
main.py
Normal 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
2
requirements.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fastapi~=0.92.0
|
||||
openai~=0.27.0
|
||||
Loading…
Add table
Add a link
Reference in a new issue