tmp
zhaoqingang
2025-01-07 7666bdbabfb929f98e94310dbe99eabb435898a2
tmp
5个文件已修改
52 ■■■■■ 已修改文件
alembic.ini 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/api/v2/chat.py 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/models/v2/session_model.py 19 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/service/v2/app_driver/chat_dialog.py 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/service/v2/chat.py 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
alembic.ini
@@ -61,6 +61,7 @@
# 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
app/api/v2/chat.py
@@ -12,4 +12,4 @@
@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")
app/models/v2/session_model.py
@@ -5,7 +5,7 @@
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 @@
    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 ChatDialogData(BaseModel):
    sessionId: Optional[str] = ""
    question: str
    chatId: str
app/service/v2/app_driver/chat_dialog.py
@@ -15,7 +15,8 @@
        }
    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
app/service/v2/chat.py
@@ -1,14 +1,23 @@
import json
from app.service.v2.app_driver.chat_dialog import ChatDialog
async def service_chat_dialog(question: str, session_id: str):
    if session_id:
        ...
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())