升级新库
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
from langchain_core.prompts import PromptTemplate
|
||||
|
||||
from config.llm import llm
|
||||
from langchain.prompts import PromptTemplate
|
||||
|
||||
chatPrompt = PromptTemplate(
|
||||
input_variables=["aiRole", "history", "userInput"],
|
||||
template = """
|
||||
template="""
|
||||
你的用户画像为:{aiRole}。
|
||||
你需要基于你的角色性格,使用中文回答用户。
|
||||
|
||||
@@ -15,13 +15,12 @@ chatPrompt = PromptTemplate(
|
||||
{userInput}
|
||||
|
||||
最后,请注意,不要编造数据,不知道就说不知道,现在,请生成你的回复:
|
||||
"""
|
||||
""",
|
||||
)
|
||||
chatChain = chatPrompt | llm
|
||||
|
||||
def get_chat_response(aiRole: str,history: str, userInput: str) -> str:
|
||||
return chatChain.invoke({
|
||||
"aiRole": aiRole,
|
||||
"history": history,
|
||||
"userInput": userInput
|
||||
})
|
||||
|
||||
def get_chat_response(aiRole: str, history: str, userInput: str) -> str:
|
||||
return chatChain.invoke(
|
||||
{"aiRole": aiRole, "history": history, "userInput": userInput}
|
||||
)
|
||||
|
||||
+34
-20
@@ -1,12 +1,12 @@
|
||||
from langchain.prompts import PromptTemplate
|
||||
from config.llm import llm,llmThink
|
||||
from langchain_core.prompts import PromptTemplate
|
||||
|
||||
import db.milvus as milvus
|
||||
import db.postgres as pg
|
||||
import json
|
||||
from config.llm import llmThink
|
||||
|
||||
memPathPrompt = PromptTemplate(
|
||||
input_variables=["ai_role", "CHAT_RECORD"],
|
||||
template = """
|
||||
template="""
|
||||
你是一个记忆筛选器,负责判断最近对话的信息中,用户的回复内容是否对业务具有长期价值或潜在价值,或者可以帮助形成用户画像。
|
||||
首先,请仔细阅读以下关于你业务的描述:
|
||||
<ai_role>
|
||||
@@ -31,12 +31,12 @@ no:用户最新回复价值有限或几乎不会在未来业务中使用。
|
||||
|
||||
回复不要带任何标点符号以及空格、换行符。
|
||||
请给出你的判断结果:
|
||||
"""
|
||||
""",
|
||||
)
|
||||
memPathChain = memPathPrompt | llmThink
|
||||
memPrompt = PromptTemplate(
|
||||
input_variables=["CHAT_RECORD"],
|
||||
template = """
|
||||
template="""
|
||||
你的任务是对给定的聊天记录进行关键信息的记忆总结。请仔细阅读以下聊天记录,并按照要求进行总结:
|
||||
<聊天记录>
|
||||
{CHAT_RECORD}
|
||||
@@ -48,13 +48,15 @@ memPrompt = PromptTemplate(
|
||||
4. 总结内容应包含时间,并确保时间是准确的。
|
||||
5. 你需要针对你的业务场景{ai_role},展开对用户最后回复的总结。
|
||||
请生成你的总结,以用户、时间开头:
|
||||
"""
|
||||
""",
|
||||
)
|
||||
memChain = memPrompt | llmThink
|
||||
def take_memory(ai_id:str,sessionId: str,user_id:str, max_retry=3):
|
||||
|
||||
|
||||
def take_memory(ai_id: str, sessionId: str, user_id: str, max_retry=3):
|
||||
"""根据用户输入选择数据来源"""
|
||||
history = pg.get_history_with_time(sessionId,10)
|
||||
print("获取的历史记录:",history)
|
||||
history = pg.get_history_with_time(sessionId, 10)
|
||||
print("获取的历史记录:", history)
|
||||
ai_service = pg.get_description(ai_id)
|
||||
if ai_service == "":
|
||||
# AI描述没有描述,则取业务字段
|
||||
@@ -66,17 +68,29 @@ def take_memory(ai_id:str,sessionId: str,user_id:str, max_retry=3):
|
||||
else:
|
||||
ai_service = json["业务"]
|
||||
print("获取的描述是:", ai_service)
|
||||
choice = memPathChain.invoke({
|
||||
"ai_role": ai_service,
|
||||
"CHAT_RECORD": history,
|
||||
}).content.strip().lower()
|
||||
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()
|
||||
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
|
||||
milvus.add_memory(mem=memory, user_id=user_id, is_active=True, ai_id=ai_id)
|
||||
return
|
||||
|
||||
+24
-27
@@ -1,13 +1,13 @@
|
||||
from langchain_community.agent_toolkits import create_sql_agent
|
||||
from langchain_core.prompts import PromptTemplate
|
||||
|
||||
from config.llm import llm
|
||||
from langchain.prompts import PromptTemplate
|
||||
from config.ssDb import ssDBLC
|
||||
from langchain_community.agent_toolkits import create_sql_agent
|
||||
from config.ssDb import ssDBLC
|
||||
|
||||
#______________________________________________________________SQL描述_____________________________________________________________________
|
||||
# ______________________________________________________________SQL描述_____________________________________________________________________
|
||||
sqlDescriptionPrompt = PromptTemplate(
|
||||
input_variables=["sql"],
|
||||
template = """
|
||||
template="""
|
||||
你是一个SQL专家,精通SQLServer数据库。请把一下SQL查询语句用通俗易懂的中文进行总结。
|
||||
SQL语句:{sql}
|
||||
有以下要求:
|
||||
@@ -16,54 +16,51 @@ sqlDescriptionPrompt = PromptTemplate(
|
||||
3. 不能有markdown语法
|
||||
4. 要用业务语言描述,不能有专业语句例如SQL表名等
|
||||
请生成你认为合适的标题,:
|
||||
"""
|
||||
""",
|
||||
)
|
||||
sqlDescriptionChain = sqlDescriptionPrompt | llm
|
||||
|
||||
def get_sql_description_response( sql: str) -> str:
|
||||
return sqlDescriptionChain.invoke({
|
||||
"sql": sql
|
||||
})
|
||||
|
||||
#______________________________________________________________第一次生成SQL_____________________________________________________________________
|
||||
def get_sql_description_response(sql: str) -> str:
|
||||
return sqlDescriptionChain.invoke({"sql": sql})
|
||||
|
||||
|
||||
# ______________________________________________________________第一次生成SQL_____________________________________________________________________
|
||||
sqlPrompt = PromptTemplate(
|
||||
input_variables=["userInput"],
|
||||
template = """
|
||||
template="""
|
||||
你是一个SQL专家,精通SQLServer数据库。
|
||||
请根据用户的需求,生成相应的SQL查询语句。
|
||||
只需要返回SQL语句,不要任何解释。
|
||||
用户需求:{userInput}
|
||||
请生成SQL语句:
|
||||
"""
|
||||
""",
|
||||
)
|
||||
sqlChain = sqlPrompt | llm
|
||||
agent = create_sql_agent(
|
||||
llm=llm,
|
||||
db=ssDBLC,
|
||||
agent_type="tool-calling",
|
||||
verbose=True
|
||||
)
|
||||
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:
|
||||
def get_chat_sql_response(userInput: str) -> str:
|
||||
return agent.invoke({"input": userInput})["output"]
|
||||
|
||||
#______________________________________________________________改进SQL_____________________________________________________________________
|
||||
|
||||
# ______________________________________________________________改进SQL_____________________________________________________________________
|
||||
sqlImprovePrompt = PromptTemplate(
|
||||
input_variables=["userInput", "sql"],
|
||||
template = """
|
||||
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
|
||||
})
|
||||
|
||||
def get_chat_sql_improve_response(userInput: str) -> str:
|
||||
return sqlImproveChain.invoke({"userInput": userInput})
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
from langchain_core.prompts import PromptTemplate
|
||||
|
||||
from langchain.prompts import PromptTemplate
|
||||
from config.llm import llm
|
||||
|
||||
summarizePrompt = PromptTemplate(
|
||||
input_variables=["aiRole", "history", "userStr", "infomation"],
|
||||
template = """
|
||||
template="""
|
||||
你是一个主干信息研发的 AI 助手,用户画像为:{aiRole}。
|
||||
请基于你的角色性格,保持中文简洁回答的,根据下方提示回答用户。
|
||||
|
||||
@@ -21,14 +21,17 @@ summarizePrompt = PromptTemplate(
|
||||
{infomation}
|
||||
···
|
||||
如果参考内容明显有问题,你要请用户重新描述问题,现在请生成你的回复:
|
||||
"""
|
||||
""",
|
||||
)
|
||||
summarizeChain = summarizePrompt | llm
|
||||
|
||||
|
||||
def getSummary(aiRole: str, history: str, userInput: str, infomation: str) -> str:
|
||||
return summarizeChain.invoke({
|
||||
"aiRole":aiRole,
|
||||
"history": history,
|
||||
"userStr": userInput,
|
||||
"infomation": infomation
|
||||
}).content
|
||||
return summarizeChain.invoke(
|
||||
{
|
||||
"aiRole": aiRole,
|
||||
"history": history,
|
||||
"userStr": userInput,
|
||||
"infomation": infomation,
|
||||
}
|
||||
).content
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import json
|
||||
import re
|
||||
|
||||
from langchain.schema import HumanMessage
|
||||
from langchain_core.messages import HumanMessage
|
||||
|
||||
from config.llm import *
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import json
|
||||
import re
|
||||
|
||||
from langchain.schema import HumanMessage
|
||||
from langchain_core.messages import HumanMessage
|
||||
|
||||
from config.llm import *
|
||||
from llm.ticketLLM import decode_barcode
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
from langchain_core.prompts import PromptTemplate
|
||||
|
||||
from langchain.prompts import PromptTemplate
|
||||
from config.llm import llm
|
||||
|
||||
titlePrompt = PromptTemplate(
|
||||
input_variables=["userStr"],
|
||||
template = """
|
||||
template="""
|
||||
请将用户的这句话总结成一个简短、精准的对话标题,要求:
|
||||
1. 不超过10个字(可根据需要调整长度)。
|
||||
2. 直接概括本次对话的核心内容。
|
||||
@@ -12,9 +12,10 @@ titlePrompt = PromptTemplate(
|
||||
4. 保持自然、易懂、专业或有趣(可根据场景调整风格)。
|
||||
5. 不能出现标点符号。
|
||||
用户原话:"{userStr}"
|
||||
"""
|
||||
""",
|
||||
)
|
||||
titleChain = titlePrompt | llm
|
||||
|
||||
|
||||
def get_title(userInput: str):
|
||||
return titleChain.invoke({"userStr": userInput}).content
|
||||
return titleChain.invoke({"userStr": userInput}).content
|
||||
|
||||
Reference in New Issue
Block a user