新增AnswerType.py文件,创建FastAPI应用以收集用户提问数据类型,添加健康检查和异步检索API,优化日志记录和错误处理。同时,新增DifyExporter类用于导出Dify系统中的对话和消息数据,支持从查询日志加载数据并保存为Excel文件。
This commit is contained in:
@@ -4,6 +4,9 @@ import psycopg2
|
||||
import os
|
||||
import json
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
|
||||
import sys
|
||||
sys.path.append(os.getcwd())
|
||||
from rag2_0.dify.dify_client import ChatClient
|
||||
from pydantic import BaseModel, Field
|
||||
from langchain.output_parsers import PydanticOutputParser
|
||||
@@ -167,6 +170,76 @@ class PgSql:
|
||||
return None
|
||||
except (Exception, psycopg2.Error) as error:
|
||||
raise Exception(f"Error while getting workflow_node_executions_info: {error}")
|
||||
|
||||
def get_app_conversations(self, appid:str)->list[str] | None:
|
||||
"""
|
||||
根据应用 ID 从 'conversations' 表中获取应用会话信息。
|
||||
"""
|
||||
with self.pg_sql_lock:
|
||||
try:
|
||||
with self.connection.cursor() as cursor:
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT DISTINCT conversation_id
|
||||
FROM messages
|
||||
WHERE app_id = %s AND invoke_from != 'debugger';
|
||||
""",
|
||||
(appid,)
|
||||
)
|
||||
result = cursor.fetchall()
|
||||
if result:
|
||||
colnames = [desc[0] for desc in cursor.description]
|
||||
return [dict(zip(colnames, row)) for row in result]
|
||||
return None
|
||||
except (Exception, psycopg2.Error) as error:
|
||||
raise Exception(f"Error while getting app_conversations: {error}")
|
||||
|
||||
def get_conversation_messages(self, conversation_id:str)->list[dict] | None:
|
||||
"""
|
||||
根据会话 ID 从 'messages' 表中获取会话消息信息。
|
||||
"""
|
||||
with self.pg_sql_lock:
|
||||
try:
|
||||
with self.connection.cursor() as cursor:
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT * FROM messages WHERE conversation_id = %s AND status = 'normal'
|
||||
""",
|
||||
(conversation_id,)
|
||||
)
|
||||
result = cursor.fetchall()
|
||||
if result:
|
||||
colnames = [desc[0] for desc in cursor.description]
|
||||
return [dict(zip(colnames, row)) for row in result]
|
||||
return None
|
||||
except (Exception, psycopg2.Error) as error:
|
||||
raise Exception(f"Error while getting conversation_messages: {error}")
|
||||
|
||||
def get_message_rating(self, msg_id):
|
||||
"""
|
||||
通过msg_id从message_feedbacks中找到对应的rating。
|
||||
:param msg_id: 消息ID (UUID格式)
|
||||
:return: rating 字符串
|
||||
"""
|
||||
with self.pg_sql_lock:
|
||||
rating = None
|
||||
try:
|
||||
with self.connection.cursor() as cursor:
|
||||
# 构建查询语句
|
||||
cursor.execute("""
|
||||
SELECT rating
|
||||
FROM message_feedbacks
|
||||
WHERE message_id = %s
|
||||
""",
|
||||
(msg_id,))
|
||||
# 执行查询
|
||||
row = cursor.fetchone()
|
||||
|
||||
if row:
|
||||
rating = row[0]
|
||||
except (Exception, psycopg2.Error) as error:
|
||||
raise Exception(f"Error while getting conversation_messages: {error}")
|
||||
return rating
|
||||
|
||||
class DifyTool:
|
||||
"""
|
||||
@@ -388,7 +461,6 @@ content: "{content}"
|
||||
avg_score = total_score / valid_scores if valid_scores > 0 else 0
|
||||
return retrieve_title, max_score, min_score, avg_score
|
||||
|
||||
|
||||
class NewWorkflowChat(BaseWorkflowChat):
|
||||
"""
|
||||
新工作流对话类,用于调用新工作流发送对话并解析获取相关数据
|
||||
|
||||
Reference in New Issue
Block a user