feat:LLM 分析文件并生成结构化建议
This commit is contained in:
+27
-4
@@ -7,13 +7,16 @@ from uuid import uuid4
|
||||
from langgraph.types import Command
|
||||
from loguru import logger
|
||||
|
||||
from app.agent.data_tools import DataContext
|
||||
from app.config import settings
|
||||
from app.core.exceptions import BizException
|
||||
from app.datafiles.service import DataFileService
|
||||
from app.observability.tracing import astream_screen
|
||||
from app.processor.history import trim_history
|
||||
from app.processor.state import RunContext
|
||||
from app.schemas.agent import AgentResult, ResumeRequest
|
||||
from app.schemas.conversation import Turn
|
||||
from app.schemas.data_file import DataFileRecord, RequestDataFile
|
||||
from app.schemas.request import DesignRequest
|
||||
from app.services.conversation import ConversationStore
|
||||
|
||||
@@ -43,8 +46,9 @@ class AgentService:
|
||||
conversation_id, settings.conversation_history_turns
|
||||
)
|
||||
# 附件引用的文件不属于本会话时在流开始前返回 404
|
||||
data_files = await self.data_files.request_files(
|
||||
user_id, conversation_id, request.attachments
|
||||
attached = request.data_file_ids
|
||||
records = await self.data_files.request_files(
|
||||
user_id, conversation_id, attached
|
||||
)
|
||||
except BaseException:
|
||||
self.release(thread_id)
|
||||
@@ -64,7 +68,16 @@ class AgentService:
|
||||
settings.history_max_tokens,
|
||||
)
|
||||
# 同样只信任服务端读取的结果;会话内的文件每一轮都可用,不附带也能查询
|
||||
payload["data_files"] = [item.model_dump(mode="json") for item in data_files]
|
||||
# 请求会进检查点,只放摘要;工具要用的列概况和路径通过运行上下文传给图
|
||||
payload["data_files"] = [
|
||||
RequestDataFile(
|
||||
file_id=record.file_id,
|
||||
name=record.name,
|
||||
attached_this_turn=record.file_id in attached,
|
||||
tables=record.tables,
|
||||
).model_dump(mode="json")
|
||||
for record in records
|
||||
]
|
||||
|
||||
return (
|
||||
thread_id,
|
||||
@@ -76,6 +89,7 @@ class AgentService:
|
||||
},
|
||||
conversation_id,
|
||||
user_id,
|
||||
self._run_context(records),
|
||||
)
|
||||
|
||||
async def prepare_resume(self, request: ResumeRequest, user_id: int):
|
||||
@@ -117,6 +131,10 @@ class AgentService:
|
||||
)
|
||||
}
|
||||
)
|
||||
# 运行上下文不在检查点里,恢复时按会话重新读取;期间删除或过期的文件工具查不到
|
||||
records = await self.data_files.request_files(
|
||||
user_id, conversation_id, set()
|
||||
)
|
||||
|
||||
# 每次有效补充或取消生成新的输入 ID,任务和会话保持不变
|
||||
return (
|
||||
@@ -127,11 +145,15 @@ class AgentService:
|
||||
),
|
||||
conversation_id,
|
||||
user_id,
|
||||
self._run_context(records),
|
||||
)
|
||||
except BaseException:
|
||||
self.release(thread_id)
|
||||
raise
|
||||
|
||||
def _run_context(self, records: list[DataFileRecord]) -> RunContext:
|
||||
return RunContext(data=DataContext(tuple(records), self.data_files.root))
|
||||
|
||||
def release(self, thread_id):
|
||||
self._active.discard(thread_id)
|
||||
self._conversation_of.pop(thread_id, None)
|
||||
@@ -199,7 +221,7 @@ class AgentService:
|
||||
clarification=state.get("clarification") if waiting else None,
|
||||
)
|
||||
|
||||
async def stream(self, thread_id, payload, session_id, user_id=None):
|
||||
async def stream(self, thread_id, payload, session_id, user_id=None, context=None):
|
||||
"""消费图事件,完整状态只用于整理最终结果,不直接暴露给客户端"""
|
||||
state = None
|
||||
with logger.contextualize(thread_id=thread_id):
|
||||
@@ -210,6 +232,7 @@ class AgentService:
|
||||
thread_id=thread_id,
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
context=context,
|
||||
)
|
||||
) as events:
|
||||
async for mode, data in events:
|
||||
|
||||
Reference in New Issue
Block a user