更新python后端

This commit is contained in:
BBIT-Kai
2025-09-18 17:18:18 +08:00
parent 2fc209e6e6
commit de6a350da8
45 changed files with 2524 additions and 89 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ from langchain.prompts import PromptTemplate
chatPrompt = PromptTemplate(
input_variables=["aiRole", "history", "userInput"],
template = """
是一个人,用户画像为:{aiRole}
用户画像为:{aiRole}
你需要基于你的角色性格,使用中文回答用户。
聊天历史:
+82
View File
@@ -0,0 +1,82 @@
from langchain.prompts import PromptTemplate
from config.llm import llm,llmThink
import db.milvus as milvus
import db.postgres as pg
import json
memPathPrompt = PromptTemplate(
input_variables=["ai_role", "CHAT_RECORD"],
template = """
你是一个记忆筛选器,负责判断最近对话的信息中,用户的回复内容是否对业务具有长期价值或潜在价值,或者可以帮助形成用户画像。
首先,请仔细阅读以下关于你业务的描述:
<ai_role>
{ai_role}
</ai_role>
现在,请仔细阅读以下你与用户的聊天记录:
<聊天记录>
{CHAT_RECORD}
</聊天记录>
请仔细考虑以下标准:
1. 长期价值:用户最新回复信息是否能为你的业务提供知识积累、经验总结、数据支持。
2. 相关性:用户最新回复是否与业务核心需求、目标、流程或潜在业务场景相关。
3. 潜在可用性:用户最新回复是否可能在未来的业务场景中被重复使用、参考或触发进一步操作。
你需要根据以上标准给出判断并得出"yes""no"
yes:用户最新回复具有直接或潜在长期价值,值得保留。
no:用户最新回复价值有限或几乎不会在未来业务中使用。
回复不要带任何标点符号以及空格、换行符。
请给出你的判断结果:
"""
)
memPathChain = memPathPrompt | llmThink
memPrompt = PromptTemplate(
input_variables=["CHAT_RECORD"],
template = """
你的任务是对给定的聊天记录进行关键信息的记忆总结。请仔细阅读以下聊天记录,并按照要求进行总结:
<聊天记录>
{CHAT_RECORD}
</聊天记录>
在总结时,请遵循以下指南:
1. 提取聊天记录中的用户所说的关键信息,包括主要话题、重要观点、达成的共识或决定等。
2. 用简洁明了的语言进行总结有价值的信息,避免冗长和复杂的表述。
3. 确保总结内容准确反映聊天记录中用户的核心内容,并尽可能简短。
4. 总结内容应包含时间,并确保时间是准确的。
5. 你需要针对你的业务场景{ai_role},展开对用户最后回复的总结。
请生成你的总结,以用户、时间开头:
"""
)
memChain = memPrompt | llmThink
def take_memory(ai_id:str,sessionId: str,user_id:str, max_retry=3):
"""根据用户输入选择数据来源"""
history = pg.get_history_with_time(sessionId,10)
print("获取的历史记录:",history)
ai_service = pg.get_description(ai_id)
if ai_service == "":
# AI描述没有描述,则取业务字段
json = pg.get_ai_personality(ai_id)
if json.get("业务", "") == "":
# AI没有任何描述,无法对记忆价值进行判断
print("AI没有任何描述,无法对记忆价值进行判断")
return
else:
ai_service = json["业务"]
print("获取的描述是:", ai_service)
choice = memPathChain.invoke({
"ai_role": ai_service,
"CHAT_RECORD": history,
}).content.strip().lower()
print("记忆判断器判断的结果是:", choice)
if choice == "yes":
# 对对话进行总结
memory = memChain.invoke({
"CHAT_RECORD": history,
"ai_role": ai_service,
}).content.strip().lower()
print("记忆生成结果是:", memory)
milvus.add_memory(mem = memory,user_id = user_id, is_active = True, ai_id = ai_id)
return
+69
View File
@@ -0,0 +1,69 @@
from config.llm import llm
from langchain.prompts import PromptTemplate
from config.ssDb import ssDBLC
from langchain_community.agent_toolkits import create_sql_agent
#______________________________________________________________SQL描述_____________________________________________________________________
sqlDescriptionPrompt = PromptTemplate(
input_variables=["sql"],
template = """
你是一个SQL专家,精通SQLServer数据库。请把一下SQL查询语句用通俗易懂的中文进行总结。
SQL语句:{sql}
有以下要求:
1. 不要任何解释
2. 不能有标点符号
3. 不能有markdown语法
4. 要用业务语言描述,不能有专业语句例如SQL表名等
请生成你认为合适的标题,:
"""
)
sqlDescriptionChain = sqlDescriptionPrompt | llm
def get_sql_description_response( sql: str) -> str:
return sqlDescriptionChain.invoke({
"sql": sql
})
#______________________________________________________________第一次生成SQL_____________________________________________________________________
sqlPrompt = PromptTemplate(
input_variables=["userInput"],
template = """
你是一个SQL专家,精通SQLServer数据库。
请根据用户的需求,生成相应的SQL查询语句。
只需要返回SQL语句,不要任何解释。
用户需求:{userInput}
请生成SQL语句:
"""
)
sqlChain = sqlPrompt | llm
agent = create_sql_agent(
llm=llm,
db=ssDBLC,
agent_type="tool-calling",
verbose=True
)
# def get_chat_sql_response2( userInput: str) -> str:
# return sqlChain.invoke({
# "userInput": userInput
# })
def get_chat_sql_response( userInput: str) -> str:
return agent.invoke({"input": userInput})["output"]
#______________________________________________________________改进SQL_____________________________________________________________________
sqlImprovePrompt = PromptTemplate(
input_variables=["userInput", "sql"],
template = """
你是一个SQL专家,精通SQLServer数据库。
请根据用户的需求,改进已有的SQL查询语句。
只需要返回改进后的SQL语句,不要任何解释。
已有SQL{sql}
用户需求:{userInput}
"""
)
sqlImproveChain = sqlImprovePrompt | llm
def get_chat_sql_improve_response( userInput: str) -> str:
return sqlImproveChain.invoke({
"userInput": userInput
})
+1
View File
@@ -10,6 +10,7 @@ titlePrompt = PromptTemplate(
2. 直接概括本次对话的核心内容。
3. 避免使用笼统或无意义的词语,如“讨论”、“聊天”等。
4. 保持自然、易懂、专业或有趣(可根据场景调整风格)。
5. 不能出现标点符号。
用户原话:"{userStr}"
"""
)