新增标题提取

This commit is contained in:
zcr
2026-03-12 15:19:26 +08:00
parent 510a5117ee
commit 25abdfa38a
3 changed files with 86 additions and 78 deletions

View File

@@ -1,27 +1,44 @@
from langchain_core.messages import HumanMessage, AIMessage
from langchain_core.prompts import PromptTemplate
from src.server.deep_agent.init_llm import title_llm
def conversation_title(full_conversation):
title_prompt = PromptTemplate(
input_variables=["full_conversation"],
template="""
请严格按照以下要求生成对话标题:
1. 标题长度8-15个字纯中文无标点、无特殊符号、无换行
2. 标题内容:基于完整对话,精准概括核心主题(兼顾用户需求和助手回复)
3. 标题风格:自然口语化,符合中文表达习惯,不冗余
完整对话内容:
{full_conversation}
仅输出标题,不要输出任何额外解释、说明或标点符号。
"""
)
title_chain = title_prompt | title_llm
response = title_chain.invoke({"full_conversation": full_conversation})
return response
async def conversation_title(agent, config):
state = agent.get_state(config)
messages = state.values.get("messages", [])
if len(messages) < 2:
return None
user_msg = None
ai_msg = None
if __name__ == '__main__':
print(conversation_title("你好"))
for m in messages:
if isinstance(m, HumanMessage) and user_msg is None:
user_msg = m.content
if isinstance(m, AIMessage) and ai_msg is None:
ai_msg = m.content
if user_msg and ai_msg:
break
if not user_msg or not ai_msg:
return None
prompt = f"""
请根据以下首次对话内容生成一个简洁、精准的标题2-15个字
用户:{user_msg}
助手:{ai_msg}
要求:
1. 标题需概括对话核心内容
2. 语言简洁、符合中文表达习惯
3. 仅返回标题,不要额外解释
"""
response = await title_llm.ainvoke(prompt)
title = response.content.strip()
# 去掉可能的符号
title = title.replace("", "").replace("", "")
return title