This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import traceback | ||
import uvicorn | ||
from fastapi import FastAPI, Body | ||
from EdgeGPT import Chatbot | ||
import os | ||
os.system(f'pip install --upgrade -r requirements.txt') | ||
|
||
|
||
app = FastAPI() | ||
|
||
# 调试开关 | ||
DEBUG = False | ||
|
||
# 手动配置时按需要修改监听地址和端口 | ||
HOST = "0.0.0.0" | ||
PORT = 8007 | ||
|
||
# 手动配置时按需要修改cookie文件路径,将./cookie.json修改为你自己的文件位置(一般不用修改) | ||
chatbot = Chatbot(cookiePath='./cookie.json') | ||
flag = False | ||
|
||
|
||
@app.get("/ping") | ||
def ping(): | ||
return {"message": "pong"} | ||
|
||
|
||
@app.post("/bing") | ||
async def chatGPT(body: dict = Body(...)): | ||
global flag | ||
prompt = body["prompt"] | ||
print("User: " + prompt) | ||
|
||
if flag: | ||
return {"message": "接口繁忙,请稍后再试。"} | ||
|
||
try: | ||
flag = True | ||
if prompt == "!reset": | ||
await chatbot.reset() | ||
print("OK") | ||
flag = False | ||
return {"message": "OK"} | ||
answer = (await chatbot.ask(prompt))["item"]["messages"][1]["adaptiveCards"][0]["body"][0]["text"] | ||
print("Bingbot: " + answer) | ||
except Exception as e: | ||
traceback.print_exc() | ||
await chatbot.reset() | ||
answer = "ERROR" | ||
if DEBUG: | ||
answer = "ERROR" + e | ||
pass | ||
flag = False | ||
return {"message": answer} | ||
|
||
if __name__ == "__main__": | ||
uvicorn.run(app, host=HOST, port=PORT) |