Merge remote-tracking branch 'jiutian/jiutian' into feat/jiutian

This commit is contained in:
yaojin
2024-08-14 20:29:53 +08:00
6 changed files with 55 additions and 15 deletions
+31 -11
View File
@@ -3,6 +3,7 @@ import math
from typing import List, Optional
from uuid import UUID
from bisheng.api.errcode.base import UnAuthorizedError
from bisheng.api.services.assistant import AssistantService
from bisheng.api.services.audit_log import AuditLogService
from bisheng.api.services.base import BaseService
@@ -22,8 +23,10 @@ from bisheng.database.base import session_getter
from bisheng.database.models.assistant import AssistantDao, AssistantStatus
from bisheng.database.models.flow import Flow, FlowDao, FlowStatus
from bisheng.database.models.flow_version import FlowVersionDao
from bisheng.database.models.group_resource import GroupResourceDao, ResourceTypeEnum
from bisheng.database.models.message import ChatMessage, ChatMessageDao, ChatMessageRead, MessageDao
from bisheng.database.models.user import UserDao
from bisheng.database.models.user_group import UserGroupDao
from bisheng.graph.graph.base import Graph
from bisheng.utils.logger import logger
from bisheng.utils.util import get_cache_key
@@ -45,30 +48,46 @@ expire = 600 # reids 60s 过期
response_model=UnifiedResponseModel[PageList[AppChatList]],
status_code=200)
def get_app_chat_list(*,
flow_name: Optional[str] = None,
user_name: Optional[str] = None,
keyword: Optional[str] = None,
page_num: Optional[int] = 1,
page_size: Optional[int] = 20,
login_user: UserPayload = Depends(get_login_user)):
"""通过消息表进行聊天App统计,全量表查询,"""
"""性能问题后续优化"""
group_flow_ids = []
if not login_user.is_admin():
# 判断下是否是用户组管理员
user_groups = UserGroupDao.get_user_admin_group(login_user.user_id)
if not user_groups:
raise UnAuthorizedError.http_exception()
user_group_ids = [user_group.group_id for user_group in user_groups]
# 获取分组下的所有资源ID
resources = GroupResourceDao.get_groups_resource(user_group_ids,
resource_types=[ResourceTypeEnum.FLOW, ResourceTypeEnum.ASSISTANT])
group_flow_ids = [one.third_id for one in resources]
if not group_flow_ids:
return resp_200(PageList(list=[], total=0))
flow_ids, user_ids = [], []
if flow_name:
flows = FlowDao.get_flow_list_by_name(name=flow_name)
if flows:
flow_ids = [flow.id for flow in flows]
if user_name:
users = UserDao.search_user_by_name(user_name=user_name)
if users:
user_ids = [user.user_id for user in users]
if keyword:
flows = FlowDao.get_flow_list_by_name(name=keyword)
assistants = AssistantDao.get_all_assistants(name=keyword, page=0, limit=0)
users = UserDao.search_user_by_name(user_name=keyword)
if flows:
flow_ids = [flow.id for flow in flows]
if assistants:
flow_ids = flow_ids.extend([assistant.id for assistant in assistants])
if user_ids:
user_ids = [user.user_id for user in users]
# 检索内容为空
if not flow_ids and not user_ids:
return resp_200(PageList(list=[], total=0))
if group_flow_ids:
if flow_ids:
flow_ids = list(set(flow_ids) & set(group_flow_ids))
else:
flow_ids = group_flow_ids
res, count = MessageDao.app_list_group_by_chat_id(page_size=page_size,
page_num=page_num,
@@ -86,7 +105,8 @@ def get_app_chat_list(*,
flow_map.update(assistant_map)
res_obj = PageList(list=[
AppChatList(user_name=user_map[one['user_id']], flow_name=flow_map[one['flow_id']], **one)
AppChatList(user_name=user_map.get(one['user_id'], one['user_id']),
flow_name=flow_map.get(one['flow_id'], one['flow_id']), **one)
for one in res
],
total=count)
+1 -1
View File
@@ -146,7 +146,7 @@ async def upload_preset_file(*,
'output': json.loads(qa.answers)[0]
} for question in qa.questions])
with tempfile.NamedTemporaryFile(mode='w+', suffix='.json') as filepath:
json.dump(qa_list, filepath)
json.dump(qa_list, filepath, ensure_ascii=False, indent=2)
filepath.seek(0)
return FinetuneFileService.upload_preset_file(name, 1, filepath.name, current_user)
@@ -79,6 +79,23 @@ class GroupResourceDao(GroupResourceBase):
statement = statement.offset(page_size * (page_num - 1)).limit(page_size)
return session.exec(statement).all()
@classmethod
def get_groups_resource(cls,
group_ids: List[int],
resource_types: List[ResourceTypeEnum] = None,
name: str = None,
page_size: int = None,
page_num: int = None) -> list[GroupResource]:
with session_getter() as session:
statement = select(GroupResource).where(GroupResource.group_id.in_(group_ids))
if resource_types:
statement = statement.where(GroupResource.type.in_([r.value for r in resource_types]))
if name:
statement = statement.where(GroupResource.third_id.like(f'%{name}%'))
if page_num and page_size:
statement = statement.offset(page_size * (page_num - 1)).limit(page_size)
return session.exec(statement).all()
@classmethod
def get_resource_group(cls, resource_type: ResourceTypeEnum, third_id: str) -> list[GroupResource]:
"""
@@ -92,10 +92,10 @@ class MessageDao(MessageBase):
func.sum(case((ChatMessage.copied == 1, 1), else_=0)), )
if flow_ids:
count_stat.where(ChatMessage.flow_id.in_(flow_ids))
count_stat = count_stat.where(ChatMessage.flow_id.in_(flow_ids))
sql = sql.where(ChatMessage.flow_id.in_(flow_ids))
if user_ids:
count_stat.where(ChatMessage.user_id.in_(user_ids))
count_stat = count_stat.where(ChatMessage.user_id.in_(user_ids))
sql = sql.where(ChatMessage.user_id.in_(user_ids))
sql = sql.group_by(ChatMessage.chat_id, ChatMessage.user_id,
ChatMessage.flow_id).order_by(
+1 -1
View File
@@ -27,7 +27,7 @@ class DatabaseService(Service):
connect_args = {'check_same_thread': False}
else:
connect_args = {}
return create_engine(self.database_url, connect_args=connect_args, pool_size=100, max_overflow=20, pool_pre_ping=True)
return create_engine(self.database_url, connect_args=connect_args, pool_size=100, max_overflow=20, pool_pre_ping=True, echo=True)
def __enter__(self):
self._session = Session(self.engine)
@@ -398,6 +398,9 @@ class MilvusWithPermissionCheck(MilvusLangchain):
# Todo: normalize the es score on a scale [0, 1]
return 1 - distance
def _select_relevance_score_fn(self) -> Callable[[float], float]:
return self._relevance_score_fn
class ElasticsearchWithPermissionCheck(VectorStore, ABC):
"""