forked from djylb/koishi-bingchat-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbing.py
44 lines (34 loc) · 960 Bytes
/
bing.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
41
42
43
44
from EdgeGPT import Chatbot
from fastapi import FastAPI, Body
import uvicorn
app = FastAPI()
HOST = "0.0.0.0"
PORT = 8007
chatbot = Chatbot(cookiePath='./cookie.json')
flag = False
@app.get("/ping")
def ping():
return {"message": "pong"}
@app.post("/chat")
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")
return {"message": "OK"}
answer = (await chatbot.ask(prompt))["item"]["messages"][1]["adaptiveCards"][0]["body"][0]["text"]
print("Bingbot: " + answer)
except:
await chatbot.reset()
answer = "ERROR"
pass
flag = False
return {"message": answer}
if __name__ == "__main__":
uvicorn.run(app, host=HOST, port=PORT)