更新环境配置,添加gevent和gunicorn依赖;新增chat_dify_by_workorder.py文件以处理工单对话逻辑;优化PgSql类中的异常处理,确保连接失败时抛出异常;改进意图识别API,使用单例模式管理意图识别器实例,增强线程安全性;新增workflow_chat.py文件以支持新工作流对话功能。
This commit is contained in:
+46
-33
@@ -23,7 +23,7 @@ class PgSql:
|
||||
连接到 PostgreSQL 数据库。
|
||||
|
||||
使用预定义的凭据连接到 'dify' 数据库。
|
||||
如果连接失败,会打印错误信息。
|
||||
如果连接失败,会抛出异常。
|
||||
"""
|
||||
try:
|
||||
# 连接数据库
|
||||
@@ -36,17 +36,16 @@ class PgSql:
|
||||
)
|
||||
|
||||
except (Exception, psycopg2.Error) as error:
|
||||
print("Error while connecting to PostgreSQL", error)
|
||||
raise Exception(f"Error while connecting to PostgreSQL: {error}")
|
||||
|
||||
def close_connection(self):
|
||||
"""
|
||||
关闭当前的 PostgreSQL 数据库连接。
|
||||
|
||||
如果存在活动的连接,则关闭它并打印确认信息。
|
||||
如果存在活动的连接,则关闭它。
|
||||
"""
|
||||
if self.connection:
|
||||
self.connection.close()
|
||||
print("PostgreSQL connection is closed")
|
||||
|
||||
|
||||
def get_appinfo(self, appid:str)->dict | None:
|
||||
@@ -74,7 +73,7 @@ class PgSql:
|
||||
return dict(zip(colnames, result))
|
||||
return None
|
||||
except (Exception, psycopg2.Error) as error:
|
||||
print("Error while getting tenant_id by appid", error)
|
||||
raise Exception(f"Error while getting tenant_id by appid: {error}")
|
||||
|
||||
|
||||
def get_messages_info(self, appid:str, query:str)->dict | None:
|
||||
@@ -103,7 +102,7 @@ class PgSql:
|
||||
return dict(zip(colnames, result))
|
||||
return None
|
||||
except (Exception, psycopg2.Error) as error:
|
||||
print("Error while getting messages_info", error)
|
||||
raise Exception(f"Error while getting messages_info: {error}")
|
||||
|
||||
def get_messages_info_by_id(self, message_id:str)->dict | None:
|
||||
"""
|
||||
@@ -123,7 +122,7 @@ class PgSql:
|
||||
return dict(zip(colnames, result))
|
||||
return None
|
||||
except (Exception, psycopg2.Error) as error:
|
||||
print("Error while getting messages_info", error)
|
||||
raise Exception(f"Error while getting messages_info by id: {error}")
|
||||
|
||||
def get_workflow_node_executions_info(self, workflow_run_id:str)->list[dict] | None:
|
||||
"""
|
||||
@@ -150,7 +149,7 @@ class PgSql:
|
||||
return [dict(zip(colnames, row)) for row in result]
|
||||
return None
|
||||
except (Exception, psycopg2.Error) as error:
|
||||
print("Error while getting workflow_node_executions_info", error)
|
||||
raise Exception(f"Error while getting workflow_node_executions_info: {error}")
|
||||
|
||||
class DifyTool:
|
||||
"""
|
||||
@@ -165,16 +164,21 @@ class DifyTool:
|
||||
根据消息 ID 从 'messages' 表中获取消息信息。
|
||||
"""
|
||||
dify_pgsql = PgSql()
|
||||
messages_info = dify_pgsql.get_messages_info_by_id(message_id)
|
||||
if not messages_info:
|
||||
return None
|
||||
workflow_node_executions_info = dify_pgsql.get_workflow_node_executions_info(messages_info['workflow_run_id'])
|
||||
if not workflow_node_executions_info:
|
||||
return None
|
||||
return {
|
||||
"messages_info": messages_info,
|
||||
"workflow_node_executions_info": workflow_node_executions_info
|
||||
}
|
||||
try:
|
||||
messages_info = dify_pgsql.get_messages_info_by_id(message_id)
|
||||
if not messages_info:
|
||||
return None
|
||||
workflow_node_executions_info = dify_pgsql.get_workflow_node_executions_info(messages_info['workflow_run_id'])
|
||||
if not workflow_node_executions_info:
|
||||
return None
|
||||
return {
|
||||
"messages_info": messages_info,
|
||||
"workflow_node_executions_info": workflow_node_executions_info
|
||||
}
|
||||
except Exception as e:
|
||||
raise Exception(f"Error in get_message_debug_info_by_id: {e}")
|
||||
finally:
|
||||
dify_pgsql.close_connection()
|
||||
|
||||
|
||||
@staticmethod
|
||||
@@ -195,21 +199,30 @@ class DifyTool:
|
||||
查询到的应用数据、消息数据和节点执行数据。
|
||||
"""
|
||||
dify_pgsql = PgSql()
|
||||
appinfo = dify_pgsql.get_appinfo(appid)
|
||||
if not appinfo:
|
||||
return None
|
||||
messages_info = dify_pgsql.get_messages_info(appid, query)
|
||||
if not messages_info:
|
||||
return None
|
||||
workflow_node_executions_info = dify_pgsql.get_workflow_node_executions_info(messages_info['workflow_run_id'])
|
||||
if not workflow_node_executions_info:
|
||||
return None
|
||||
return {
|
||||
"appinfo": appinfo,
|
||||
"messages_info": messages_info,
|
||||
"workflow_node_executions_info": workflow_node_executions_info
|
||||
}
|
||||
try:
|
||||
appinfo = dify_pgsql.get_appinfo(appid)
|
||||
if not appinfo:
|
||||
return None
|
||||
messages_info = dify_pgsql.get_messages_info(appid, query)
|
||||
if not messages_info:
|
||||
return None
|
||||
workflow_node_executions_info = dify_pgsql.get_workflow_node_executions_info(messages_info['workflow_run_id'])
|
||||
if not workflow_node_executions_info:
|
||||
return None
|
||||
return {
|
||||
"appinfo": appinfo,
|
||||
"messages_info": messages_info,
|
||||
"workflow_node_executions_info": workflow_node_executions_info
|
||||
}
|
||||
except Exception as e:
|
||||
raise Exception(f"Error in get_message_debug_info_by_query: {e}")
|
||||
finally:
|
||||
dify_pgsql.close_connection()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(DifyTool.get_message_debug_info_by_query("ccf92b97-2789-4a3f-90e0-135a869a37c5", "电力建设计价通软件,导入结算后没有暂列金怎么办?要手动添加么?"))
|
||||
try:
|
||||
result = DifyTool.get_message_debug_info_by_query("ccf92b97-2789-4a3f-90e0-135a869a37c5", "电力建设计价通软件,导入结算后没有暂列金怎么办?要手动添加么?")
|
||||
print(result)
|
||||
except Exception as e:
|
||||
print(f"执行出错: {e}")
|
||||
|
||||
Reference in New Issue
Block a user