This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathmain.py
149 lines (136 loc) · 5.93 KB
/
main.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from typing import Union, Any, AsyncGenerator
from fastapi import FastAPI, WebSocket, Request, Response
from fastapi.responses import StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
from EdgeGPT import EdgeGPT as gpt
from EdgeGPT.ImageGen import ImageGenAsync
import uvicorn
import json, asyncio
from pydantic import BaseModel
with open('./config/server.json', 'r', encoding="utf-8") as f: #获取服务端配置
config = json.loads(f.read())
'''
服务端配置如下:
port:端口
host:0.0.0.0或127.0.0.1
useUserCookie:是否需要用户在网页客户端使用自己的cookie登录,如果为假,默认使用服务端设置的cookie.json
tips:每个键是一个人格的名称,键的值是对应的咒语,网页客户端可以使用指令切换人格
'''
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*'],
)
class DrawTip(BaseModel):
tip : str = "a cat"
cookie : str = None
@app.get('/') #返回web客户端
async def Index() -> Response:
return Response(open(f'./index.html', 'r', encoding="utf-8").read())
@app.get('/static/{filepath}') #返回静态文件
async def getFile(filepath : str) -> Response:
if filepath[-4::] == '.jpg' or filepath[-4::] == '.png':
return Response(open(f'./static/{filepath}', 'rb').read())
else:
return Response(open(f'./static/{filepath}', 'r', encoding="utf-8").read())
@app.websocket('/chat/ws') #返回bing的回答
async def wsStream(ws: WebSocket):
"""
客户端发送格式:
{
"tipName" : 人格名称,
"question" : 问题,
"style" : 聊天风格,
}
"""
await ws.accept()
if config.get('useUserCookie'):
msg = await ws.receive_text()
chatbot = await gpt.Chatbot.create(cookies = json.loads(msg))
else:
chatbot = await gpt.Chatbot.create(cookies = json.loads(open('./config/cookie.json', 'r', encoding='utf-8').read()))
while True:
try:
msg = await ws.receive_json() #接收信息
response = { #向客户端发送的信息格式
"answer" : "",
"error" : "",
"urls" : [],
"done" : False,
"suggested" : []
}
imageInfo = None
async for final, ans in chatbot.ask_stream(
prompt = msg.get('question'),
conversation_style = msg.get('style'),
webpage_context = config.get('tips').get(msg.get('tipName')) if msg.get('tipName') in config.get('tips') else msg.get('tipName')):
if not final:
response['answer'] = ans
imageInfo = ans
await ws.send_text(json.dumps(response))
else:
response['done'] = True
if ans.get('item').get('result').get('value') == 'Throttled':
response['error'] = '已到达一天的最大聊天次数,无法继续聊天'
await ws.send_text(json.dumps(response))
if ans.get('item').get('result').get('value') == 'Success':
response['answer'] = ans['item']['messages'][-1]['adaptiveCards'][0]['body'][0]['text']
response['suggested'] = [i['text'] for i in ans['item']['messages'][-1]['suggestedResponses']]
response['urls'] = [{'name' : i['providerDisplayName'], 'url' : i['seeMoreUrl']} for i in ans['item']['messages'][-1]['sourceAttributions']]
await ws.send_text(json.dumps(response))
else:
response['error'] = '未知错误,value:' + str(ans.get('item').get('result').get('value'))
await ws.send_text(json.dumps(response))
except Exception as e:
if e.args[0] == "adaptiveCards":
response['answer'] = imageInfo
response['done'] = True
await ws.send_text(json.dumps(response))
else:
response['done'] = True
response['error'] = '未知错误,服务端报错:' + str(e)
await ws.send_text(json.dumps(response))
print('Error:', e.args)
@app.post('/chat/image') #单独返回画图信息
async def aiDraw(drawTip : DrawTip) -> Response:
'''
返回格式:
{
"answer" : 回答,
"error" : 错误
}
'''
res = {
"answer" : None,
"error" : None
}
try:
if config.get('useUserCookie'):
cookie = drawTip.cookie
else:
cookie = json.loads(open('./config/cookie.json', 'r', encoding = 'utf-8').read())
async with ImageGenAsync(all_cookies = cookie) as creator:
images = await creator.get_images(drawTip.tip)
res['answer'] = images
return Response(json.dumps(res))
except Exception as e:
res['error'] = '未知错误,服务端报错:' + str(e)
return Response(json.dumps(res))
@app.get('/chat/getTipNames') #获取人格名称列表
async def tipnames() -> Response:
lst = [i for i in config.get('tips')]
lst = [lst, config.get('useUserCookie')]
return Response(json.dumps(lst))
@app.get('/config/{filepath}') #获取web客户端默认配置
async def clientjs(filepath) ->Response:
if filepath == 'client.js':
return Response(open('./config/client.js', 'r', encoding= 'utf-8').read())
else:
return Response('不允许查看该文件', status_code = 404)
if __name__ == '__main__':
print('Server Start')
print('Go to http://127.0.0.1:' + str(config.get('port')) + "/ and start to chat!")
uvicorn.run(app, host = config['host'], port = config['port']) #__,启动!