| | |
| | | # 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 |
| | | |
| | | |
| | |
| | | |
| | | @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") |
| | |
| | | |
| | | 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 |
| | |
| | | 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): |
| | |
| | | class ChatDialogData(BaseModel): |
| | | sessionId: Optional[str] = "" |
| | | question: str |
| | | chatId: str |
| | |
| | | } |
| | | |
| | | |
| | | 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 |
| | |
| | | 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()) |