From dd5ed7fc53fe0df101b2d1645446f06c8417a16d Mon Sep 17 00:00:00 2001 From: HuangHai <10402852@qq.com> Date: Sun, 7 Sep 2025 08:08:16 +0800 Subject: [PATCH] 'commit' --- .../VikingDBMemoryService.cpython-310.pyc | Bin 9744 -> 9794 bytes dsLightRag/Volcengine/chat.py | 56 ++++++++++-------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/dsLightRag/Volcengine/__pycache__/VikingDBMemoryService.cpython-310.pyc b/dsLightRag/Volcengine/__pycache__/VikingDBMemoryService.cpython-310.pyc index 2c87295cec91e7a21e471a55c03753d7fa0162c0..2ed7c6feac153f4cad4affd8acce0bb0bc9b0d18 100644 GIT binary patch delta 1981 zcmZ{kS!`QX5QcNS$6FlR+3Yx*lQvG$qzEY>Ld24WEI5fuKqY3mH9j|qsS~^BUekq& zfLcIeYtq9`XlVaZzNW`R@{SZzCxt8T>I;Ujv zxtyBOv-!o+Pt0p!b@C7E05eKGw$Chq2o)rvKqC;7zdJ@*kL-62vW}AO?6$vGh-a|31)5fZVbikV7K z@TS%QQi$7u761XnJph)Oa8ngmg3~N6{|!d%$o+zB4qfaXhtVdg;dq!}IOf$fSA|H! zU>Bn>90Ahuw@@crmErK&;aM0h0<{S{WQX98RRBgY2h0Nsu(d>Ub&rzna_N`w9hQ!V zV73xyqmXmA>tWJ;TuRpUF~?DuHTg_^`(!tXh7(7ZFY3R3OQXN-MSXVPt%*=MN(-q{ zRRy6I(n3{)dbFr&x_A=PY>(yn3E?zFEjza;6!~X;>(VWx*eW~a(e$)l)aV-0Zd~76 zx-RN*-EgRj&?j8x{utJDA!|4&Q+h!YONfJ&7)%(}qR1IGp=yQQ>&lSFRr+g91UGY) zWJ8d7o}z(x7t&PaR9A=fz7bO|?Tt3Y;@c?88SIt^?9=yP^aQc||9Pm7V zA`L6mCE}1=Bq@(;`9-n)GUL{M5)HBMOIM?NnerleCMwN5Do>1mw}zx$$eMi zSKx%4;w-Q`-Av5SelE?&npnI@_BHF))|;6&1D4;vTx%NWEH?lIGgoFN*md37$oiM` zHa~mYIr!mSi8lyqwsLu9v>VRG0Wl`8HBZ#PO=7}2Da%S}N@$&3i~nG#O4s;rR@Cn=jRp*Te^K9+cm-Z}b|q z6T=fz_su9nQ_1A;(EVdmlgi{>6T_kgJ{`c_KnpM=zv$@R-$SCU!ew(Yd|r!}ZGB;j U&FZ#TY8=eQ-2b?vfxfW&KfA4{0ssI2 delta 1845 zcmZ{kTToj?7{{}@oP=B;KmrLNg+M4AKU6xkRNs8lGj$vcC)RS$(j!gONByTrs*wh0HLc0oX)sHUI%qAe6WR!k z(GU#_DLQPU4Ycu;jW+5{uQ*aMv5wHFSV#4sST|GqL7UpltH}?go5XJ?VeWDElT>N1 zbIl%%ioC1|hyl&qgZ zcD@#?=e_n~``v34f4AWjh60+ux}qwUx^Twdjw4JZzuvDo0s@qY)|QW^%X+Kc{bSPdR*vV zE?o%TAY_Ceue&R;ODI@BAe%%0HVCu|s9rYAzpgu&KsT$z5~XHl(uSdF?}LNebL1DPPlE`$iJa!nG#`ZrIuoZhtzzd7J-fmLU@P#NvE zyW_(1UmO3mcOV8Ujc(2%9&j;Hs z6EOta`tDrVh*rM7ktq23S>D!8rXiwW55Cab9?y7MV4fVF<{X!=@me1C{_B zJHzR*%fR~pdSxH+{mFREG{g{4D{zvZNDjAj2uY1wV*1iTMvOZ7*6am?eGI?*`H#sP x4J-^f2@C+`;mLpR=;`YgYp2cQ^bmafc0bwtgzR32J**HX@&4nD1$TtJ{{j7FgbDxv diff --git a/dsLightRag/Volcengine/chat.py b/dsLightRag/Volcengine/chat.py index 0c5b1fed..a750856f 100644 --- a/dsLightRag/Volcengine/chat.py +++ b/dsLightRag/Volcengine/chat.py @@ -1,3 +1,4 @@ +import logging import os import time @@ -14,11 +15,16 @@ AI 就能用刚写入的记忆来回答。 import json import time from VikingDBMemoryService import initialize_services, ensure_collection_exists, search_relevant_memories - +# 控制日志输出 +logger = logging.getLogger('CollectionMemory') +logger.setLevel(logging.INFO) +handler = logging.StreamHandler() +handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')) +logger.addHandler(handler) def handle_conversation_turn(memory_service, llm_client, collection_name, user_id, user_message, conversation_history): """处理一轮对话,包括记忆搜索和LLM响应。""" - print("\n" + "=" * 60) - print(f"用户: {user_message}") + logger.info("\n" + "=" * 60) + logger.info(f"用户: {user_message}") relevant_memories = search_relevant_memories(memory_service, collection_name, user_id, user_message) @@ -28,7 +34,7 @@ def handle_conversation_turn(memory_service, llm_client, collection_name, user_i [f"- {json.dumps(mem['memory_info'], ensure_ascii=False)}" for mem in relevant_memories]) system_prompt += f"\n\n这是我们过去的一些对话记忆,请参考:\n{memory_context}\n\n请利用这些信息来更好地理解和回应用户。" - print("AI正在思考...") + logger.info("AI正在思考...") try: messages = [{"role": "system", "content": system_prompt}] + conversation_history + [ @@ -39,10 +45,10 @@ def handle_conversation_turn(memory_service, llm_client, collection_name, user_i ) assistant_reply = completion.choices[0].message.content except Exception as e: - print(f"LLM调用失败: {e}") + logger.info(f"LLM调用失败: {e}") assistant_reply = "抱歉,我现在有点混乱,无法回应。我们可以稍后再聊吗?" - print(f"伙伴: {assistant_reply}") + logger.info(f"伙伴: {assistant_reply}") conversation_history.extend([ {"role": "user", "content": user_message}, @@ -53,10 +59,10 @@ def handle_conversation_turn(memory_service, llm_client, collection_name, user_i def archive_conversation(memory_service, collection_name, user_id, assistant_id, conversation_history, topic_name): """将对话历史归档到记忆数据库。""" if not conversation_history: - print("没有对话可以归档。") + logger.info("没有对话可以归档。") return False - print(f"\n正在归档关于 '{topic_name}' 的对话...") + logger.info(f"\n正在归档关于 '{topic_name}' 的对话...") session_id = f"{topic_name}_{int(time.time())}" metadata = { "default_user_id": user_id, @@ -71,11 +77,11 @@ def archive_conversation(memory_service, collection_name, user_id, assistant_id, messages=conversation_history, metadata=metadata ) - print(f"对话已成功归档,会话ID: {session_id}") - print("正在等待记忆索引更新...") + logger.info(f"对话已成功归档,会话ID: {session_id}") + logger.info("正在等待记忆索引更新...") return True except Exception as e: - print(f"归档对话失败: {e}") + logger.info(f"归档对话失败: {e}") return False def setup_memory_collection(collection_name="emotional_support"): @@ -83,18 +89,18 @@ def setup_memory_collection(collection_name="emotional_support"): try: memory_service, _ = initialize_services() ensure_collection_exists(memory_service, collection_name) - print(f"记忆体 '{collection_name}' 创建/验证成功") + logger.info(f"记忆体 '{collection_name}' 创建/验证成功") # 添加集合就绪等待 - print("等待集合准备就绪...") + logger.info("等待集合准备就绪...") if wait_for_collection_ready(memory_service, collection_name): - print(f"集合 '{collection_name}' 已就绪") + logger.info(f"集合 '{collection_name}' 已就绪") return memory_service else: - print(f"集合 '{collection_name}' 未能就绪") + logger.info(f"集合 '{collection_name}' 未能就绪") return None except Exception as e: - print(f"记忆体创建失败: {e}") + logger.info(f"记忆体创建失败: {e}") return None def wait_for_collection_ready(memory_service, collection_name, timeout=300, interval=10): @@ -112,18 +118,18 @@ def wait_for_collection_ready(memory_service, collection_name, timeout=300, inte collection_info = memory_service.get_collection(collection_name) # 根据Volcengine API文档,状态可能在Status字段中,值可能为"READY"、"CREATING"等 status = collection_info.get("Status", "UNKNOWN") - print(f"集合 '{collection_name}' 当前状态: {status}") + logger.info(f"集合 '{collection_name}' 当前状态: {status}") if status == "READY": return True time.sleep(interval) except Exception as e: - print(f"检查集合状态失败: {e}") + logger.info(f"检查集合状态失败: {e}") time.sleep(interval) - print(f"集合 '{collection_name}' 在{timeout}秒内未就绪") + logger.info(f"集合 '{collection_name}' 在{timeout}秒内未就绪") return False def main(): - print("开始端到端记忆测试...") + logger.info("开始端到端记忆测试...") collection_name="emotional_support" try: @@ -138,10 +144,10 @@ def main(): user_id = "xiaoming" # 用户ID:小明 assistant_id = "assistant1" # 助手ID:助手1 except Exception as e: - print(f"初始化失败: {e}") + logger.info(f"初始化失败: {e}") return - print("\n--- 阶段 1: 初始对话 ---") + logger.info("\n--- 阶段 1: 初始对话 ---") initial_conversation_history = [] handle_conversation_turn( memory_service, llm_client, collection_name, user_id, @@ -154,13 +160,13 @@ def main(): initial_conversation_history ) - print("\n--- 阶段 2: 归档记忆 ---") + logger.info("\n--- 阶段 2: 归档记忆 ---") archive_conversation( memory_service, collection_name, user_id, assistant_id, initial_conversation_history, "study_stress_discussion" ) - print("\n--- 阶段 3: 验证记忆 ---") + logger.info("\n--- 阶段 3: 验证记忆 ---") verification_conversation_history = [] handle_conversation_turn( memory_service, llm_client, collection_name, user_id, @@ -168,7 +174,7 @@ def main(): verification_conversation_history ) - print("\n端到端记忆测试完成!") + logger.info("\n端到端记忆测试完成!") if __name__ == "__main__": setup_memory_collection()