-
Notifications
You must be signed in to change notification settings - Fork 5
/
LarkGPT_webhook.py
354 lines (270 loc) · 10.9 KB
/
LarkGPT_webhook.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from aiohttp import web
import asyncio
import openai
from threading import Thread, current_thread
# import web
import json
import requests
import datetime
import time
#Every instance of this class represents an available api loaded in the list
class Seat:
configPath = ''
def __init__(self, api):
self.api = api
self.maxToken = 2048
self.engie = "gpt-3.5-turbo"
self.lock = 0
self.user : User = None
def requestGpt(self, promote)->(str,int):
if self.lock == 1:
return ("请等待...",-1)
self.lock = 1
openai.api_key = self.api
try:
if 0 != self.user.constructMsg(promote):
raise Exception("[!]Unable to construst Message, promote:",promote)
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=self.user.msg
)
try:
response = completion['choices'][0]['message']['content']
tokenConsumed = completion['usage']["total_tokens"]
except Exception as e:
print(str(response))
raise Exception("[!]Error extracting the completion.")
except Exception as e:
print(e.args)
self.lock = 0
return ("[!]Sorry, Problems with OpenAI service, Please try again.\n"+e.args,-1)
self.lock = 0
return (response,tokenConsumed)
def sendBackUser(self, res):
if (self.user.openId is not None):
send(self.user.openId, res)
@classmethod
def addApi(self,token:str,user_id:str):
api = {
"api_token": token,
"owner": user_id,
"available": True
}
try:
with open(Seat.configPath,'r') as jsonFile:
config = json.load(jsonFile)
config["Api"].append(api)
with open(Seat.configPath,'w') as jsonFile:
json.dump(config, jsonFile, ensure_ascii=False)
print("[*]Token added: ",token[0:10],"Ower:",user_id)
return 0
except Exception as e:
print(e.args)
return -1
#Every instance of User class represents a user, carries the user's dialog
class User:
questionLengthLimit = 300
previousDialogLimit = 5
previousDialogLengthLimit = 800
userExpireTime = 1800 #in sec
def __init__(self,openId):
self.openId = openId
self.totalTokenCost = 0
self.systemMsg = None#"You are a helpful assistant. Today is {}".format(datetime.date.today())
self.question = []
self.response = []
self.msg = []
self.lastResponseTimeStamp = time.time()
def constructMsg(self,newQuestion):
# add the new question to the list, generate the msg for query api
if len(newQuestion) > User.questionLengthLimit:
return -1
else:
previousDialogNum = User.previousDialogLimit if len(self.question)>User.previousDialogLimit else len(self.question)
lengthCount = 0
for i in range(-1,-1*previousDialogNum-1,-1):
lengthCount += len(self.question[i]);
lengthCount += len(self.response[i]);
if lengthCount > User.previousDialogLengthLimit:
previousDialogNum = i
break
else :
previousDialogNum = -1*previousDialogNum-1
self.msg = []
if self.systemMsg != None:
self.msg.append({"role": "system", "content": self.systemMsg})
for i in range(-1, previousDialogNum, -1):
#organize the msg struct
self.msg.append({"role": "user", "content": self.question[i]})
self.msg.append({"role": "assistant", "content": self.response[i]})
self.msg.append({"role": "user", "content": newQuestion})
self.question.append(newQuestion)
return 0
def updateResponse(self, response:str, tokenConsumed):
if response != None :
self.response.append(response)
self.totalTokenCost += tokenConsumed
self.lastResponseTimeStamp = time.time()
return 0
else:
return -1
def cleanData(self):
self.question = []
self.response = []
self.msg = []
def handle_request(seatList:list[Seat], userList:list[User], message):
#将分配seat的功能放到新线程这里
try:
open_id = message["event"]["sender"]["sender_id"]["open_id"]
content:str = json.loads(message["event"]["message"]["content"])["text"]
except Exception as e:
print("[!]Error parsing incoming message,"+e.args)
print("[!]Message:\n",str(message))
return -1
#识别token添加
if(content.startswith("sk-") and len(content)<60 and len(content)>40):
tempUser = User(open_id)
tempSeat = Seat(content)
tempSeat.user = tempUser
#测试tempSeat可用性
if tempSeat.requestGpt("hello")[0].startswith("[!]Sorry,") is not True:
#如果可用,获取用户user_id,加入队列,更新config.json
user_id = message["event"]["sender"]["sender_id"]["user_id"]
#print("user_id:",user_id,"token:",content)
if Seat.addApi(content,user_id) == 0:
tempSeat.sendBackUser("[*]您的token:{0}已经加入服务,感谢您的支持!".format(content))
seatList.append(tempSeat)
del tempUser
return 0
else:
tempSeat.sendBackUser("[!]很抱歉,您的token:{0}暂时无法加入服务,感谢您的支持".format(content))
del tempUser
del tempSeat
return -1
else:
#如果不可用
tempSeat.sendBackUser("[!]很抱歉,您的token:{0}由于网络原因暂时无法加入服务,感谢您的支持".format(content))
del tempUser
del tempSeat
return -1
user = None
seat = None
#老用户
for userIt in userList:
if userIt.openId == open_id:#此用户有先前遗留的对话
user = userIt
for i in range(len(seatList)-1,-1,-1):
if seatList[i].lock == 0 :
seat = seatList[i]
seat.user = user
if seat == None : return -1
#新用户
if user is None:
user = User(open_id)#create new user instance
userList.append(user)
for i in range(len(seatList)-1,-1,-1):
if seatList[i].lock == 0 :
seat = seatList[i]
seat.user = user
if seat == None : return -1
#向新用户发送宣传信息
AD_STR = '''欢迎使用LarkGPT - 基于ChatGPT-Turbo
本项目开源:https://github.com/HuXioAn/GPT-Lark 欢迎🌟
如果想将你的API token加入到本机器人,可以直接发送token,感谢支持!
目前已支持连续对话,如果想清除历史,请输入[/exit]'''
seat.sendBackUser(AD_STR)
#过期清楚先前对话
if (time.time()-user.lastResponseTimeStamp)>User.userExpireTime: user.cleanData()
if content == "/exit":
user.cleanData()
seat.sendBackUser("[*]Conversation cleaned.")
return 0
else:
(response,tokenConsumed) = seat.requestGpt(content)
if tokenConsumed > 0:
seat.user.updateResponse(response, tokenConsumed)
seat.sendBackUser(response)
#调整顺序
seats.insert(0,seats.pop(seats.index(seat)))
async def listen_for_webhook(request):
# print("coming!!!!!")
if request.content_type == "application/json":
message = await request.json() # 提取消息内容
#print(message)
try:
if (
"header" in message
and message["header"].get("event_type", None) == "im.message.receive_v1"
):
Thread(target=handle_request, args=(seats, users, message)).start()
return web.Response(status=200)
else:
type = message["type"] # 确定消息类型
if type == "url_verification":
# print("verification!!!!")
token = message["token"]
if token == LARK_API_TOKEN:
challenge = {
"challenge": message["challenge"]} # 提取消息内容
res = json.dumps(challenge)
return web.Response(text=res, content_type="application/json")
except Exception as e:
return web.Response(status=200)
def get_tenant(data):
url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
res = requests.post(url=url, data=json.dumps(data))
tenant = json.loads(res.content.decode())
return tenant["tenant_access_token"]
def send(open_id, msg):
url = "https://open.feishu.cn/open-apis/im/v1/messages"
params = {"receive_id_type": "open_id"}
msgContent = {
"text": msg.lstrip(),
}
req = {
"receive_id": open_id, # chat id
"msg_type": "text",
"content": json.dumps(msgContent),
}
payload = json.dumps(req)
headers = {
"Authorization": "Bearer " + get_tenant(AppProfile), # your access token
"Content-Type": "application/json",
}
response = requests.request(
"POST", url, params=params, headers=headers, data=payload
)
if __name__ == "__main__":
#读取配置文件
configPath = "./api_config.json"
Seat.configPath = configPath
try:
with open(configPath) as jsonFile:
config = json.load(jsonFile)
port = config["WebHook"]["port"]
route = config["WebHook"]["route"]
LARK_API_TOKEN = config["Bot"]["bot_api_token"]
AppProfile = config["Bot"]["profile"]
openaiKeyList=[]
for apiDict in config["Api"]:
if apiDict.get("api_token"," ").isspace() is not True and apiDict["available"] == True:
openaiKeyList.append(apiDict["api_token"])
print("[*]Token added: ",apiDict["api_token"][0:10],"Ower:",apiDict["owner"])
except:
print("[!]No config file: ",configPath,"found.")
port = 6666
route = "/"
LARK_API_TOKEN = ""
openaiKeyList = []
AppProfile = {
"app_id": "",
"app_secret": "",
} # 变更机器人时更改
users = []
seats = []
for key in openaiKeyList:
seats.append(Seat(key))
print("[*] ",len(seats)," seats loaded.")
app = web.Application()
app.add_routes([web.post(route, listen_for_webhook)])
web.run_app(app, port=port)