最新哦,分词策略放到cli了

This commit is contained in:
minorQAQ committed 2026-06-01 22:31:40 +08:00
1 parent f9f9645fa1
commit 31fba5a9e9
1 file changed
+20 -7
+20 -7
View File
@@ -27,13 +27,21 @@ if str(_SRC) not in sys.path:
from agent import chatbot_agent as ca # noqa: E402
from web import ui_session as ui # noqa: E402
from agent.utils.image_utils import IMAGE_FILE_EXTENSIONS # noqa: E402
from agent.utils.agent_utils import DOC_EXT_MAP # noqa: E402
_WEB_DIR = Path(__file__).resolve().parent
_IMAGE_DIR = _WEB_DIR / "image"
_AUDIO_EXT = frozenset({".wav", ".mp3", ".m4a", ".flac", ".ogg", ".webm"})
_FILE_EXT = frozenset(DOC_EXT_MAP.keys())
_IMAGE_EXT = frozenset(IMAGE_FILE_EXTENSIONS)
def _infer_upload_kind(fp: str) -> str | None:
ext = os.path.splitext(fp)[1].lower()
if ext in _IMAGE_EXT:
return "image"
if ext in _AUDIO_EXT:
return "audio"
return "file"
def _data_url_for_file(fp: str) -> str | None:
@@ -84,6 +92,7 @@ def _serialize_content(content: Any, *, meta: dict[str, Any] | None = None, role
parts.append({"type": "text", "text": piece.get("text") or ""})
continue
fp = piece.get("path") or (piece.get("file") or {}).get("path")
display_name = piece.get("name")
if piece.get("type") == "image_url":
fp = fp or (piece.get("image_url") or {}).get("url")
if piece.get("type") == "audio":
@@ -95,19 +104,19 @@ def _serialize_content(content: Any, *, meta: dict[str, Any] | None = None, role
continue
ext = os.path.splitext(fp)[1].lower()
if ext in IMAGE_FILE_EXTENSIONS:
parts.append({"type": "image", "url": data_url, "path": fp, "name": os.path.basename(fp)})
parts.append({"type": "image", "url": data_url, "path": fp, "name": display_name or os.path.basename(fp)})
elif ext in _AUDIO_EXT:
audio_meta = meta.get("audio") if isinstance(meta.get("audio"), dict) else {}
parts.append({
"type": "audio",
"url": data_url,
"path": fp,
"name": os.path.basename(fp),
"name": display_name or os.path.basename(fp),
"autoplay": bool(meta.get("autoplay") or audio_meta.get("autoplay")) if role == "assistant" else False,
"duration_seconds": audio_meta.get("duration_seconds"),
})
else:
parts.append({"type": "file", "url": data_url, "path": fp, "name": os.path.basename(fp)})
parts.append({"type": "file", "url": data_url, "path": fp, "name": display_name or os.path.basename(fp)})
return parts
@@ -244,6 +253,7 @@ def _materialize_uploads_to_temp(
"""将上传文件落盘到临时目录,返回 ``(query_dict, tmp_root)``;调用方须在 finally 中删除 ``tmp_root``。"""
tmp_root = tempfile.mkdtemp(prefix="web_mm_")
file_paths: list[str] = []
file_names: list[str] = []
for uf in images or []:
if uf is None or not uf.filename:
continue
@@ -252,6 +262,7 @@ def _materialize_uploads_to_temp(
with open(dest, "wb") as f:
shutil.copyfileobj(uf.file, f)
file_paths.append(dest)
file_names.append(uf.filename)
for uf in files or []:
if uf is None or not uf.filename:
@@ -261,6 +272,7 @@ def _materialize_uploads_to_temp(
with open(dest, "wb") as f:
shutil.copyfileobj(uf.file, f)
file_paths.append(dest)
file_names.append(uf.filename)
if audio is not None and audio.filename:
ext = Path(audio.filename).suffix or ".bin"
@@ -268,8 +280,9 @@ def _materialize_uploads_to_temp(
with open(dest, "wb") as f:
shutil.copyfileobj(audio.file, f)
file_paths.append(dest)
file_names.append(audio.filename)
query = {"text": (text or "").strip(), "files": file_paths}
query = {"text": (text or "").strip(), "files": file_paths, "file_names": file_names}
return query, tmp_root
@@ -417,4 +430,4 @@ def serve_css() -> FileResponse:
@app.get("/app.js")
def serve_js() -> FileResponse:
return FileResponse(_WEB_DIR / "app.js")
return FileResponse(_WEB_DIR / "app.js")