From 6f2c6422788c62c24a00bc7ec3c64b8176de866c Mon Sep 17 00:00:00 2001 From: zxstty Date: Mon, 18 Aug 2025 20:58:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E5=AF=B9=E8=AF=9D=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E5=80=99=E5=8F=AF=E4=BB=A5=E6=8C=87=E5=AE=9Atitle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/routers/conversation.py | 9 +++++++-- apps/schemas/collection.py | 2 +- apps/services/conversation.py | 2 ++ apps/services/rag.py | 10 +++++++++- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/apps/routers/conversation.py b/apps/routers/conversation.py index dfb0ce426..41d7574fc 100644 --- a/apps/routers/conversation.py +++ b/apps/routers/conversation.py @@ -44,6 +44,7 @@ logger = logging.getLogger(__name__) async def create_new_conversation( + title: str, user_sub: str, app_id: str = "", llm_id: str = "empty", @@ -57,7 +58,8 @@ async def create_new_conversation( err = "Invalid app_id." raise RuntimeError(err) new_conv = await ConversationManager.add_conversation_by_user_sub( - user_sub, + title=title, + user_sub=user_sub, app_id=app_id, llm_id=llm_id, kb_ids=kb_ids or [], @@ -68,6 +70,7 @@ async def create_new_conversation( raise RuntimeError(err) return new_conv + @router.get( "", response_model=ConversationListRsp, @@ -126,6 +129,7 @@ async def get_conversation_list(user_sub: Annotated[str, Depends(get_user)]) -> @router.post("", response_model=AddConversationRsp) async def add_conversation( user_sub: Annotated[str, Depends(get_user)], + title: Annotated[str, Query(...)] = "New Chat", app_id: Annotated[str, Query(..., alias="appId")] = "", llm_id: Annotated[str, Body(..., alias="llmId")] = "empty", kb_ids: Annotated[list[str] | None, Body(..., alias="kbIds")] = None, @@ -138,7 +142,8 @@ async def add_conversation( app_id = app_id if app_id else "" debug = debug if debug is not None else False new_conv = await create_new_conversation( - user_sub, + title=title, + user_sub=user_sub, app_id=app_id, llm_id=llm_id, kb_ids=kb_ids or [], diff --git a/apps/schemas/collection.py b/apps/schemas/collection.py index a2991f851..22563e14e 100644 --- a/apps/schemas/collection.py +++ b/apps/schemas/collection.py @@ -106,7 +106,7 @@ class Conversation(BaseModel): id: str = Field(default_factory=lambda: str(uuid.uuid4()), alias="_id") user_sub: str - title: str = NEW_CHAT + title: str = Field(default=NEW_CHAT) created_at: float = Field(default_factory=lambda: round(datetime.now(tz=UTC).timestamp(), 3)) app_id: str | None = Field(default="") tasks: list[str] = [] diff --git a/apps/services/conversation.py b/apps/services/conversation.py index 02fa2b52e..3ced25872 100644 --- a/apps/services/conversation.py +++ b/apps/services/conversation.py @@ -40,6 +40,7 @@ class ConversationManager: @staticmethod async def add_conversation_by_user_sub( + title: str, user_sub: str, app_id: str, llm_id: str, kb_ids: list[str], *, debug: bool) -> Conversation | None: """通过用户ID新建对话""" @@ -75,6 +76,7 @@ class ConversationManager: conversation_id = str(uuid.uuid4()) conv = Conversation( _id=conversation_id, + title=title, user_sub=user_sub, app_id=app_id, llm=llm_item, diff --git a/apps/services/rag.py b/apps/services/rag.py index 34b658433..af3b3e536 100644 --- a/apps/services/rag.py +++ b/apps/services/rag.py @@ -5,7 +5,7 @@ from datetime import UTC, datetime import json import logging from collections.abc import AsyncGenerator - +import re import httpx from typing import Any from fastapi import status @@ -335,6 +335,14 @@ class RAG: chunk = chunk[:index + 1] else: buffer = "" + # 匹配脚注 + footnotes = re.findall(r"\[\[\d+\]\]", chunk) + # 去除编号大于doc_cnt的脚注 + footnotes = [fn for fn in footnotes if int(fn[2:-2]) > doc_cnt] + footnotes = list(set(footnotes)) # 去重 + if footnotes: + for fn in footnotes: + chunk = chunk.replace(fn, "") output_tokens += TokenCalculator().calculate_token_length( messages=[ {"role": "assistant", "content": chunk}, -- Gitee