| | |
| | | from app.config.config import settings |
| | | from app.config.const import * |
| | | from app.models import DialogModel, ApiTokenModel, UserTokenModel, ComplexChatSessionDao, ChatDataRequest, \ |
| | | ComplexChatDao, KnowledgeModel, UserModel |
| | | ComplexChatDao, KnowledgeModel, UserModel, KnowledgeUserModel |
| | | from app.models.v2.session_model import ChatSessionDao, ChatData |
| | | from app.service.v2.app_driver.chat_agent import ChatAgent |
| | | from app.service.v2.app_driver.chat_data import ChatBaseApply |
| | |
| | | |
| | | |
| | | async def add_session_log(db, session_id: str, question: str, chat_id: str, user_id, event_type: str, |
| | | conversation_id: str, agent_type): |
| | | conversation_id: str, agent_type, query: dict=None): |
| | | try: |
| | | session = await ChatSessionDao(db).update_or_insert_by_id( |
| | | session_id=session_id, |
| | |
| | | agent_id=chat_id, |
| | | agent_type=agent_type, |
| | | tenant_id=user_id, |
| | | message={"role": "user", "content": question}, |
| | | message={"role": "user", "content": question, "query": query}, |
| | | conversation_id=conversation_id, |
| | | event_type=event_type |
| | | ) |
| | |
| | | query = db.query(KnowledgeModel) |
| | | if user.permission != "admin": |
| | | klg_list = [j.id for i in user.groups for j in i.knowledges] |
| | | for i in db.query(KnowledgeUserModel).filter(KnowledgeUserModel.user_id == user_id, KnowledgeUserModel.status == 1).all(): |
| | | if i.kb_id not in klg_list: |
| | | klg_list.append(i.kb_id) |
| | | query = query.filter(or_(KnowledgeModel.id.in_(klg_list), KnowledgeModel.tenant_id == str(user_id))) |
| | | kb_list= query.all() |
| | | for kb in kb_list: |
| | |
| | | else: |
| | | query = "start new conversation" |
| | | session = await add_session_log(db, session_id, query if query else "start new conversation", chat_id, user_id, |
| | | mode, conversation_id, DF_TYPE) |
| | | mode, conversation_id, DF_TYPE, chat_data.to_dict()) |
| | | if session: |
| | | conversation_id = session.conversation_id |
| | | try: |