Spaces:
Running
Running
Upload 3 files
Browse files
app.py
CHANGED
@@ -25,6 +25,7 @@ CHAT_URL = "https://apps.abacus.ai/api/_chatLLMSendMessageSSE"
|
|
25 |
USER_INFO_URL = "https://abacus.ai/api/v0/_getUserInfo"
|
26 |
COMPUTE_POINTS_URL = "https://apps.abacus.ai/api/_getOrganizationComputePoints"
|
27 |
COMPUTE_POINTS_LOG_URL = "https://abacus.ai/api/v0/_getOrganizationComputePointLog"
|
|
|
28 |
|
29 |
|
30 |
USER_AGENTS = [
|
@@ -115,6 +116,26 @@ def save_model_usage_records():
|
|
115 |
print(f"保存模型调用记录失败: {e}")
|
116 |
|
117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
def resolve_config():
|
119 |
# 从环境变量读取多组配置
|
120 |
config_list = []
|
@@ -340,7 +361,7 @@ def init_session():
|
|
340 |
try:
|
341 |
model_map, models_set = get_model_map(session, cookies, session_token)
|
342 |
all_models.update(models_set)
|
343 |
-
USER_DATA.append((session, cookies, session_token, conversation_id, model_map))
|
344 |
except Exception as e:
|
345 |
print(f"配置用户 {i+1} 失败: {e}")
|
346 |
continue
|
@@ -425,7 +446,7 @@ def get_user_data():
|
|
425 |
print(f"使用配置 {CURRENT_USER+1}")
|
426 |
|
427 |
# Akiru uzantajn datumojn
|
428 |
-
session, cookies, session_token, conversation_id, model_map = USER_DATA[CURRENT_USER]
|
429 |
|
430 |
# Kontrolu ĉu la tokeno eksvalidiĝis, se jes, refreŝigu ĝin
|
431 |
if is_token_expired(session_token):
|
@@ -433,13 +454,153 @@ def get_user_data():
|
|
433 |
new_token = refresh_token(session, cookies)
|
434 |
if new_token:
|
435 |
# Ĝisdatigu la globale konservitan tokenon
|
436 |
-
USER_DATA[CURRENT_USER] = (session, cookies, new_token, conversation_id, model_map)
|
437 |
session_token = new_token
|
438 |
print(f"成功更新token: {session_token[:15]}...{session_token[-15:]}")
|
439 |
else:
|
440 |
print(f"警告:无法刷新Cookie {CURRENT_USER+1}的token,继续使用当前token")
|
441 |
|
442 |
-
return (session, cookies, session_token, conversation_id, model_map)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
443 |
|
444 |
|
445 |
def generate_trace_id():
|
@@ -451,7 +612,11 @@ def generate_trace_id():
|
|
451 |
|
452 |
def send_message(message, model, think=False):
|
453 |
"""Flua traktado kaj plusendo de mesaĝoj"""
|
454 |
-
(session, cookies, session_token, conversation_id, model_map) = get_user_data()
|
|
|
|
|
|
|
|
|
455 |
trace_id, sentry_trace = generate_trace_id()
|
456 |
|
457 |
# 计算输入token
|
@@ -578,7 +743,11 @@ def send_message(message, model, think=False):
|
|
578 |
|
579 |
def send_message_non_stream(message, model, think=False):
|
580 |
"""Ne-flua traktado de mesaĝoj"""
|
581 |
-
(session, cookies, session_token, conversation_id, model_map) = get_user_data()
|
|
|
|
|
|
|
|
|
582 |
trace_id, sentry_trace = generate_trace_id()
|
583 |
|
584 |
# 计算输入token
|
@@ -901,7 +1070,7 @@ def get_compute_points():
|
|
901 |
# 获取每个用户的计算点信息
|
902 |
for i, user_data in enumerate(USER_DATA):
|
903 |
try:
|
904 |
-
session, cookies, session_token, _, _ = user_data
|
905 |
|
906 |
# 检查token是否有效
|
907 |
if is_token_expired(session_token):
|
@@ -909,7 +1078,7 @@ def get_compute_points():
|
|
909 |
if not session_token:
|
910 |
print(f"用户{i+1}刷新token失败,无法获取计算点信息")
|
911 |
continue
|
912 |
-
USER_DATA[i] = (session, cookies, session_token, user_data[3], user_data[4])
|
913 |
|
914 |
headers = {
|
915 |
"accept": "application/json, text/plain, */*",
|
|
|
25 |
USER_INFO_URL = "https://abacus.ai/api/v0/_getUserInfo"
|
26 |
COMPUTE_POINTS_URL = "https://apps.abacus.ai/api/_getOrganizationComputePoints"
|
27 |
COMPUTE_POINTS_LOG_URL = "https://abacus.ai/api/v0/_getOrganizationComputePointLog"
|
28 |
+
CREATE_CONVERSATION_URL = "https://apps.abacus.ai/api/createDeploymentConversation"
|
29 |
|
30 |
|
31 |
USER_AGENTS = [
|
|
|
116 |
print(f"保存模型调用记录失败: {e}")
|
117 |
|
118 |
|
119 |
+
def update_conversation_id(user_index, conversation_id):
|
120 |
+
"""更新用户的conversation_id并保存到配置文件"""
|
121 |
+
try:
|
122 |
+
with open("config.json", "r") as f:
|
123 |
+
config = json.load(f)
|
124 |
+
|
125 |
+
if "config" in config and user_index < len(config["config"]):
|
126 |
+
config["config"][user_index]["conversation_id"] = conversation_id
|
127 |
+
|
128 |
+
# 保存到配置文件
|
129 |
+
with open("config.json", "w") as f:
|
130 |
+
json.dump(config, f, indent=4)
|
131 |
+
|
132 |
+
print(f"已将用户 {user_index+1} 的conversation_id更新为: {conversation_id}")
|
133 |
+
else:
|
134 |
+
print(f"更新conversation_id失败: 配置文件格式错误或用户索引越界")
|
135 |
+
except Exception as e:
|
136 |
+
print(f"更新conversation_id失败: {e}")
|
137 |
+
|
138 |
+
|
139 |
def resolve_config():
|
140 |
# 从环境变量读取多组配置
|
141 |
config_list = []
|
|
|
361 |
try:
|
362 |
model_map, models_set = get_model_map(session, cookies, session_token)
|
363 |
all_models.update(models_set)
|
364 |
+
USER_DATA.append((session, cookies, session_token, conversation_id, model_map, i))
|
365 |
except Exception as e:
|
366 |
print(f"配置用户 {i+1} 失败: {e}")
|
367 |
continue
|
|
|
446 |
print(f"使用配置 {CURRENT_USER+1}")
|
447 |
|
448 |
# Akiru uzantajn datumojn
|
449 |
+
session, cookies, session_token, conversation_id, model_map, user_index = USER_DATA[CURRENT_USER]
|
450 |
|
451 |
# Kontrolu ĉu la tokeno eksvalidiĝis, se jes, refreŝigu ĝin
|
452 |
if is_token_expired(session_token):
|
|
|
454 |
new_token = refresh_token(session, cookies)
|
455 |
if new_token:
|
456 |
# Ĝisdatigu la globale konservitan tokenon
|
457 |
+
USER_DATA[CURRENT_USER] = (session, cookies, new_token, conversation_id, model_map, user_index)
|
458 |
session_token = new_token
|
459 |
print(f"成功更新token: {session_token[:15]}...{session_token[-15:]}")
|
460 |
else:
|
461 |
print(f"警告:无法刷新Cookie {CURRENT_USER+1}的token,继续使用当前token")
|
462 |
|
463 |
+
return (session, cookies, session_token, conversation_id, model_map, user_index)
|
464 |
+
|
465 |
+
|
466 |
+
def create_conversation(session, cookies, session_token, external_application_id=None, deployment_id=None):
|
467 |
+
"""创建新的会话"""
|
468 |
+
if not (external_application_id and deployment_id):
|
469 |
+
print("无法创建新会话: 缺少必要参数")
|
470 |
+
return None
|
471 |
+
|
472 |
+
headers = {
|
473 |
+
"accept": "application/json, text/plain, */*",
|
474 |
+
"accept-language": "zh-CN,zh;q=0.9",
|
475 |
+
"content-type": "application/json",
|
476 |
+
"cookie": cookies,
|
477 |
+
"user-agent": random.choice(USER_AGENTS),
|
478 |
+
"x-abacus-org-host": "apps"
|
479 |
+
}
|
480 |
+
|
481 |
+
if session_token:
|
482 |
+
headers["session-token"] = session_token
|
483 |
+
|
484 |
+
create_payload = {
|
485 |
+
"deploymentId": deployment_id,
|
486 |
+
"name": "New Chat",
|
487 |
+
"externalApplicationId": external_application_id
|
488 |
+
}
|
489 |
+
|
490 |
+
try:
|
491 |
+
response = session.post(
|
492 |
+
CREATE_CONVERSATION_URL,
|
493 |
+
headers=headers,
|
494 |
+
json=create_payload
|
495 |
+
)
|
496 |
+
|
497 |
+
if response.status_code == 200:
|
498 |
+
data = response.json()
|
499 |
+
if data.get("success", False):
|
500 |
+
new_conversation_id = data.get("result", {}).get("deploymentConversationId")
|
501 |
+
if new_conversation_id:
|
502 |
+
print(f"成功创建新的conversation: {new_conversation_id}")
|
503 |
+
return new_conversation_id
|
504 |
+
|
505 |
+
print(f"创建会话失败: {response.status_code} - {response.text[:100]}")
|
506 |
+
return None
|
507 |
+
except Exception as e:
|
508 |
+
print(f"创建会话时出错: {e}")
|
509 |
+
return None
|
510 |
+
|
511 |
+
|
512 |
+
def is_conversation_valid(session, cookies, session_token, conversation_id, model_map, model):
|
513 |
+
"""检查会话ID是否有效"""
|
514 |
+
if not conversation_id:
|
515 |
+
return False
|
516 |
+
|
517 |
+
# 如果没有这些信息,无法验证
|
518 |
+
if not (model in model_map and len(model_map[model]) >= 2):
|
519 |
+
return False
|
520 |
+
|
521 |
+
external_app_id = model_map[model][0]
|
522 |
+
|
523 |
+
# 尝试发送一个空消息来测试会话ID是否有效
|
524 |
+
headers = {
|
525 |
+
"accept": "text/event-stream",
|
526 |
+
"content-type": "text/plain;charset=UTF-8",
|
527 |
+
"cookie": cookies,
|
528 |
+
"user-agent": random.choice(USER_AGENTS)
|
529 |
+
}
|
530 |
+
|
531 |
+
if session_token:
|
532 |
+
headers["session-token"] = session_token
|
533 |
+
|
534 |
+
payload = {
|
535 |
+
"requestId": str(uuid.uuid4()),
|
536 |
+
"deploymentConversationId": conversation_id,
|
537 |
+
"message": "", # 空消息
|
538 |
+
"isDesktop": False,
|
539 |
+
"externalApplicationId": external_app_id
|
540 |
+
}
|
541 |
+
|
542 |
+
try:
|
543 |
+
response = session.post(
|
544 |
+
CHAT_URL,
|
545 |
+
headers=headers,
|
546 |
+
data=json.dumps(payload),
|
547 |
+
stream=False
|
548 |
+
)
|
549 |
+
|
550 |
+
# 即使返回错误,只要不是缺少ID的错误,也说明ID是有效的
|
551 |
+
if response.status_code == 200:
|
552 |
+
return True
|
553 |
+
|
554 |
+
error_text = response.text
|
555 |
+
if "Missing required parameter" in error_text:
|
556 |
+
return False
|
557 |
+
|
558 |
+
# 其他类型的错误,可能ID是有效的但有其他问题
|
559 |
+
return True
|
560 |
+
except:
|
561 |
+
# 如果请求出错,无法确定,返回False让系统创建新ID
|
562 |
+
return False
|
563 |
+
|
564 |
+
|
565 |
+
def get_or_create_conversation(session, cookies, session_token, conversation_id, model_map, model, user_index):
|
566 |
+
"""获取有效的会话ID,如果无效则创建新会话"""
|
567 |
+
# 如果conversation_id为None或为空字符串,直接创建新会话
|
568 |
+
if not conversation_id:
|
569 |
+
print("会话ID为空,将创建新会话")
|
570 |
+
need_create = True
|
571 |
+
else:
|
572 |
+
# 检查现有会话ID是否有效
|
573 |
+
need_create = not is_conversation_valid(session, cookies, session_token, conversation_id, model_map, model)
|
574 |
+
if need_create:
|
575 |
+
print(f"会话ID {conversation_id} 无效,将创建新会话")
|
576 |
+
|
577 |
+
# 如果需要创建新会话
|
578 |
+
if need_create:
|
579 |
+
if model in model_map and len(model_map[model]) >= 2:
|
580 |
+
external_app_id = model_map[model][0]
|
581 |
+
# 创建会话时需要deployment_id,我们先使用一个固定值
|
582 |
+
# 在实际应用中应从API响应中获取
|
583 |
+
deployment_id = "14b2a314cc" # 这是从您提供的请求中获取的
|
584 |
+
|
585 |
+
new_conversation_id = create_conversation(
|
586 |
+
session, cookies, session_token,
|
587 |
+
external_application_id=external_app_id,
|
588 |
+
deployment_id=deployment_id
|
589 |
+
)
|
590 |
+
|
591 |
+
if new_conversation_id:
|
592 |
+
# 更新全局存储的会话ID
|
593 |
+
global USER_DATA, CURRENT_USER
|
594 |
+
session, cookies, session_token, _, model_map, _ = USER_DATA[CURRENT_USER]
|
595 |
+
USER_DATA[CURRENT_USER] = (session, cookies, session_token, new_conversation_id, model_map, user_index)
|
596 |
+
|
597 |
+
# 保存到配置文件
|
598 |
+
update_conversation_id(user_index, new_conversation_id)
|
599 |
+
|
600 |
+
return new_conversation_id
|
601 |
+
|
602 |
+
# 如果无法创建,返回原始ID
|
603 |
+
return conversation_id
|
604 |
|
605 |
|
606 |
def generate_trace_id():
|
|
|
612 |
|
613 |
def send_message(message, model, think=False):
|
614 |
"""Flua traktado kaj plusendo de mesaĝoj"""
|
615 |
+
(session, cookies, session_token, conversation_id, model_map, user_index) = get_user_data()
|
616 |
+
|
617 |
+
# 确保有有效的会话ID
|
618 |
+
conversation_id = get_or_create_conversation(session, cookies, session_token, conversation_id, model_map, model, user_index)
|
619 |
+
|
620 |
trace_id, sentry_trace = generate_trace_id()
|
621 |
|
622 |
# 计算输入token
|
|
|
743 |
|
744 |
def send_message_non_stream(message, model, think=False):
|
745 |
"""Ne-flua traktado de mesaĝoj"""
|
746 |
+
(session, cookies, session_token, conversation_id, model_map, user_index) = get_user_data()
|
747 |
+
|
748 |
+
# 确保有有效的会话ID
|
749 |
+
conversation_id = get_or_create_conversation(session, cookies, session_token, conversation_id, model_map, model, user_index)
|
750 |
+
|
751 |
trace_id, sentry_trace = generate_trace_id()
|
752 |
|
753 |
# 计算输入token
|
|
|
1070 |
# 获取每个用户的计算点信息
|
1071 |
for i, user_data in enumerate(USER_DATA):
|
1072 |
try:
|
1073 |
+
session, cookies, session_token, _, _, _ = user_data
|
1074 |
|
1075 |
# 检查token是否有效
|
1076 |
if is_token_expired(session_token):
|
|
|
1078 |
if not session_token:
|
1079 |
print(f"用户{i+1}刷新token失败,无法获取计算点信息")
|
1080 |
continue
|
1081 |
+
USER_DATA[i] = (session, cookies, session_token, user_data[3], user_data[4], i)
|
1082 |
|
1083 |
headers = {
|
1084 |
"accept": "application/json, text/plain, */*",
|