zhaoqingang 1 年間 前
コミット
7666bdbabf

+ 1 - 0
alembic.ini

@@ -61,6 +61,7 @@ version_path_separator = os  # Use os.pathsep. Default configuration used for ne
 # are written from script.py.mako
 # output_encoding = utf-8
 
+; sqlalchemy.url = mysql+pymysql://root:infini_rag_flow@192.168.20.119:5455/rag_basic
 sqlalchemy.url = mysql+pymysql://root:infini_rag_flow@192.168.20.119:5455/rag_basic
 
 

+ 1 - 1
app/api/v2/chat.py

@@ -12,4 +12,4 @@ chat1_router = APIRouter()
 
 @chat1_router.get("/chat_dialog")
 async def api_chat_dialog(dialog: ChatDialogData, db: Session = Depends(get_db), current_user: UserModel = Depends(get_current_user)):
-    return StreamingResponse(await service_chat_dialog(dialog.question, dialog.sessionId), media_type="text/event-stream")
+    return StreamingResponse(await service_chat_dialog(dialog.chatId ,dialog.question, dialog.sessionId), media_type="text/event-stream")

+ 14 - 5
app/models/v2/session_model.py

@@ -5,7 +5,7 @@ from typing import Optional
 
 import pytz
 from pydantic import BaseModel
-from sqlalchemy import Column, String, Enum as SQLAlchemyEnum, Integer, DateTime, JSON, TEXT
+from sqlalchemy import Column, String, Integer, DateTime, JSON, TEXT, Index
 
 from app.models.agent_model import AgentType
 # from app.models import current_time
@@ -16,16 +16,24 @@ def current_time():
     return datetime.now(tz)
 
 class SessionModel(Base):
-    __tablename__ = "sessions"
-    id = Column(String(255), primary_key=True)
+    __tablename__ = "chat_sessions"
+
+    __table_args__ = (
+        Index('idx_username', 'username'),
+    )
+
+    id = Column(Integer, primary_key=True)
     name = Column(String(255))
     agent_id = Column(String(255))
-    agent_type = Column(SQLAlchemyEnum(AgentType), nullable=False)  # 目前只存basic的,ragflow和bisheng的调接口获取
+    agent_type = Column(Integer)  # 目前只存basic的,ragflow和bisheng的调接口获取
     create_date = Column(DateTime, default=current_time)  # 创建时间,默认值为当前时区时间
-    update_date = Column(DateTime, default=current_time, onupdate=current_time)  # 更新时间,默认值为当前时区时间,更新时自动更新
+    update_date = Column(DateTime, default=current_time, onupdate=current_time, index=True)  # 更新时间,默认值为当前时区时间,更新时自动更新
     tenant_id = Column(Integer)  # 创建人
     message = Column(TEXT)  # 说明
+    reference = Column(TEXT)  # 说明
     conversation_id = Column(String(64))
+    session_id = Column(String(36), index=True)
+    chat_mode = Column(Integer)
 
     # to_dict 方法
     def to_dict(self):
@@ -63,3 +71,4 @@ class SessionModel(Base):
 class ChatDialogData(BaseModel):
     sessionId: Optional[str] = ""
     question: str
+    chatId: str

+ 3 - 2
app/service/v2/app_driver/chat_dialog.py

@@ -15,7 +15,8 @@ class ChatDialog(ChatBase):
         }
 
 
-    async def chat_completions(self):
-        async for rag_response in self.http_stream(token, chat_id, chat_history):
+    async def chat_completions(self, url, data, headers):
+
+        async for rag_response in self.http_stream(url, data, headers):
 
             yield rag_response

+ 16 - 7
app/service/v2/chat.py

@@ -1,14 +1,23 @@
+import json
 
-
-async def service_chat_dialog(question: str, session_id: str):
-
-    if session_id:
-        ...
-
+from app.service.v2.app_driver.chat_dialog import ChatDialog
 
 
+async def service_chat_dialog(chat_id:str, question: str, session_id: str):
+    token = "ragflow-YzMzE1NDRjYzMyZjExZWY5ZjkxMDI0Mm"
+    url = f"/api/v1/chats/{chat_id}/completions"
+    chat = ChatDialog(token)
+    data = {
+        "question": question,
+        "stream": True,
+        "session_id": session_id
+    }
+    headers = {
+        'Content-Type': 'application/json',
+        'Authorization': f"Bearer {token}"
+    }
     try:
-        for ans in chat(dia, msg, True, **req):
+        for ans in chat.chat_completions(url, data, headers):
 
             yield "data:" + json.dumps({"code": 0, "message": "", "data": ans}, ensure_ascii=False) + "\n\n"
         ConversationService.update_by_id(conv.id, conv.to_dict())