Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,6 +24,7 @@ TOKEN = str(os.getenv("TOKEN", "")).strip()
|
|
| 24 |
DEBUG_MODE = str(os.getenv("DEBUG", "false")).lower() == "true"
|
| 25 |
THINK_TAGS_MODE = str(os.getenv("THINK_TAGS_MODE", "reasoning"))
|
| 26 |
ANONYMOUS_MODE = str(os.getenv("ANONYMOUS_MODE", "true")).lower() == "true"
|
|
|
|
| 27 |
|
| 28 |
# 性能优化配置
|
| 29 |
MAX_WORKERS = int(os.getenv("MAX_WORKERS", "10"))
|
|
@@ -121,6 +122,10 @@ SUMMARY_MATCH_RE = re.compile(r"(?s)<summary>.*?</summary>")
|
|
| 121 |
DURATION_MATCH_RE = re.compile(r'duration="(\d+)"')
|
| 122 |
NEWLINE_ARROW_RE = re.compile(r'\n>\s?')
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
# HTML模板
|
| 125 |
HTML_TEMPLATE = """
|
| 126 |
<!DOCTYPE html>
|
|
@@ -523,6 +528,10 @@ HTML_TEMPLATE = """
|
|
| 523 |
<span class="info-label">调试模式</span>
|
| 524 |
<span class="info-value" id="debug-mode">{{DEBUG_MODE}}</span>
|
| 525 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 526 |
</div>
|
| 527 |
|
| 528 |
<div class="info-card">
|
|
@@ -960,6 +969,29 @@ class utils:
|
|
| 960 |
|
| 961 |
@staticmethod
|
| 962 |
class response:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 963 |
@staticmethod
|
| 964 |
def parse(stream):
|
| 965 |
for line in stream.iter_lines():
|
|
@@ -991,6 +1023,17 @@ class utils:
|
|
| 991 |
if "finish_reason" in data_obj and data_obj["finish_reason"] == "tool_calls":
|
| 992 |
return None
|
| 993 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 994 |
global phaseBak
|
| 995 |
before = ""
|
| 996 |
|
|
@@ -1139,7 +1182,8 @@ def index():
|
|
| 1139 |
MAX_WORKERS=MAX_WORKERS,
|
| 1140 |
DEBUG_MODE=DEBUG_MODE,
|
| 1141 |
ANONYMOUS_MODE=ANONYMOUS_MODE,
|
| 1142 |
-
THINK_TAGS_MODE=THINK_TAGS_MODE
|
|
|
|
| 1143 |
)
|
| 1144 |
|
| 1145 |
@app.route("/api/test-connection")
|
|
@@ -1592,6 +1636,7 @@ if __name__ == "__main__":
|
|
| 1592 |
log.info("备选模型:%s", MODEL)
|
| 1593 |
log.info("思考处理:%s", THINK_TAGS_MODE)
|
| 1594 |
log.info("访客模式:%s", ANONYMOUS_MODE)
|
|
|
|
| 1595 |
log.info("显示调试:%s", DEBUG_MODE)
|
| 1596 |
log.info("最大工作线程:%s", MAX_WORKERS)
|
| 1597 |
log.info("请求超时:%s秒", REQUEST_TIMEOUT)
|
|
|
|
| 24 |
DEBUG_MODE = str(os.getenv("DEBUG", "false")).lower() == "true"
|
| 25 |
THINK_TAGS_MODE = str(os.getenv("THINK_TAGS_MODE", "reasoning"))
|
| 26 |
ANONYMOUS_MODE = str(os.getenv("ANONYMOUS_MODE", "true")).lower() == "true"
|
| 27 |
+
CLEAN_SEARCH_REFS = str(os.getenv("CLEAN_SEARCH_REFS", "true")).lower() == "true" # 添加清理搜索引用的开关
|
| 28 |
|
| 29 |
# 性能优化配置
|
| 30 |
MAX_WORKERS = int(os.getenv("MAX_WORKERS", "10"))
|
|
|
|
| 122 |
DURATION_MATCH_RE = re.compile(r'duration="(\d+)"')
|
| 123 |
NEWLINE_ARROW_RE = re.compile(r'\n>\s?')
|
| 124 |
|
| 125 |
+
# 添加搜索引用清理的正则表达式
|
| 126 |
+
SEARCH_REF_RE = re.compile(r'\[ref_id=[^\]]+\]\n?') # 匹配 [ref_id=...] 格式的引用
|
| 127 |
+
SEARCH_URL_RE = re.compile(r'†https?://[^\s\n]+') # 清理带†符号的URL
|
| 128 |
+
|
| 129 |
# HTML模板
|
| 130 |
HTML_TEMPLATE = """
|
| 131 |
<!DOCTYPE html>
|
|
|
|
| 528 |
<span class="info-label">调试模式</span>
|
| 529 |
<span class="info-value" id="debug-mode">{{DEBUG_MODE}}</span>
|
| 530 |
</div>
|
| 531 |
+
<div class="info-item">
|
| 532 |
+
<span class="info-label">清理搜索引用</span>
|
| 533 |
+
<span class="info-value" id="clean-refs">{{CLEAN_SEARCH_REFS}}</span>
|
| 534 |
+
</div>
|
| 535 |
</div>
|
| 536 |
|
| 537 |
<div class="info-card">
|
|
|
|
| 969 |
|
| 970 |
@staticmethod
|
| 971 |
class response:
|
| 972 |
+
@staticmethod
|
| 973 |
+
def clean_search_references(content):
|
| 974 |
+
"""
|
| 975 |
+
清理搜索结果中的引用标记
|
| 976 |
+
去除 [ref_id=...] 格式的引用,只保留实际内容
|
| 977 |
+
"""
|
| 978 |
+
if not content:
|
| 979 |
+
return content
|
| 980 |
+
|
| 981 |
+
# 去除所有 [ref_id=...] 格式的引用
|
| 982 |
+
cleaned = SEARCH_REF_RE.sub('', content)
|
| 983 |
+
|
| 984 |
+
# 清理带†符号的URL
|
| 985 |
+
cleaned = SEARCH_URL_RE.sub('', cleaned)
|
| 986 |
+
|
| 987 |
+
# 清理多余的空行
|
| 988 |
+
cleaned = re.sub(r'\n{3,}', '\n\n', cleaned)
|
| 989 |
+
|
| 990 |
+
# 去除开头和结尾的空白
|
| 991 |
+
cleaned = cleaned.strip()
|
| 992 |
+
|
| 993 |
+
return cleaned
|
| 994 |
+
|
| 995 |
@staticmethod
|
| 996 |
def parse(stream):
|
| 997 |
for line in stream.iter_lines():
|
|
|
|
| 1023 |
if "finish_reason" in data_obj and data_obj["finish_reason"] == "tool_calls":
|
| 1024 |
return None
|
| 1025 |
|
| 1026 |
+
# 清理搜索引用(如果存在且启用了清理功能)
|
| 1027 |
+
if CLEAN_SEARCH_REFS and '[ref_id=' in content:
|
| 1028 |
+
debug("检测到搜索引用,正在清理...")
|
| 1029 |
+
original_length = len(content)
|
| 1030 |
+
content = utils.response.clean_search_references(content)
|
| 1031 |
+
debug("清理搜索引用完成,原长度: %d, 新长度: %d", original_length, len(content))
|
| 1032 |
+
|
| 1033 |
+
# 如果清理后内容为空,返回 None
|
| 1034 |
+
if not content:
|
| 1035 |
+
return None
|
| 1036 |
+
|
| 1037 |
global phaseBak
|
| 1038 |
before = ""
|
| 1039 |
|
|
|
|
| 1182 |
MAX_WORKERS=MAX_WORKERS,
|
| 1183 |
DEBUG_MODE=DEBUG_MODE,
|
| 1184 |
ANONYMOUS_MODE=ANONYMOUS_MODE,
|
| 1185 |
+
THINK_TAGS_MODE=THINK_TAGS_MODE,
|
| 1186 |
+
CLEAN_SEARCH_REFS=CLEAN_SEARCH_REFS
|
| 1187 |
)
|
| 1188 |
|
| 1189 |
@app.route("/api/test-connection")
|
|
|
|
| 1636 |
log.info("备选模型:%s", MODEL)
|
| 1637 |
log.info("思考处理:%s", THINK_TAGS_MODE)
|
| 1638 |
log.info("访客模式:%s", ANONYMOUS_MODE)
|
| 1639 |
+
log.info("清理搜索引用:%s", CLEAN_SEARCH_REFS)
|
| 1640 |
log.info("显示调试:%s", DEBUG_MODE)
|
| 1641 |
log.info("最大工作线程:%s", MAX_WORKERS)
|
| 1642 |
log.info("请求超时:%s秒", REQUEST_TIMEOUT)
|