async def stream_chat(websocket: WebSocket, knowledge_base_id: str): await websocket.accept() turn = 1 while True: input_json = await websocket.receive_json() question, history, knowledge_base_id = input_json[""], input_json["history"], input_json["knowledge_base_id"] vs_path = os.path.join(VS_ROOT_PATH, knowledge_base_id) if not os.path.exists(vs_path): await websocket.send_json({"error": f"Knowledge base {knowledge_base_id} not found"}) await websocket.close() return await websocket.send_json({"question": question, "turn": turn, "flag": "start"}) last_print_len = 0 for resp, history in local_doc_qa.get_knowledge_based_answer( query=question, vs_path=vs_path, chat_history=history, streaming=True ): await websocket.send_text(resp["result"][last_print_len:]) last_print_len = len(resp["result"]) source_documents = [ f"""出处 [{inum + 1}] {os.path.split(doc.metadata['source'])[-1]}:\n\n{doc.page_content}\n\n""" f"""相关度:{doc.metadata['score']}\n\n""" for inum, doc in enumerate(resp["source_documents"]) ] await websocket.send_text( json.dumps( { "question": question, "turn": turn, "flag": "end", "sources_documents": source_documents, }, ensure_ascii=False, ) ) turn += 1
时间: 2024-04-27 18:23:37 浏览: 119
ws_stream_wasm-在WASM中使用Websocket的便捷库-Rust开发
这段代码看起来是一个基于 WebSocket 实现的 AI 对话系统,它会接收客户端发送过来的消息,然后调用本地文档问答系统来提供回答。其中,vs_path 是知识库的路径,local_doc_qa 是本地文档问答系统的实例,chat_history 则是历史对话记录,用于上下文理解。在获取到回答后,它会将回答和相关的文档信息返回给客户端。
阅读全文