This commit is contained in:
2025-08-25 11:36:38 +08:00
parent fa858ddd90
commit 68d10d6471
2 changed files with 29 additions and 11 deletions

View File

@@ -9,8 +9,8 @@ from Config.Config import ZHIPU_API_KEY
# 设置日志
logger = logging.getLogger(__name__)
# 添加历史记录存储变量
history_records = []
# 添加历史记录存储变量 - 使用字典按会话ID存储
session_history = {}
MAX_HISTORY = 10
def create_llm_client() -> AsyncOpenAI:
@@ -123,8 +123,15 @@ async def getGgbCommand(QvqResult):
yield f"错误: {str(e)}"
async def translate_to_ggb(natural_language_cmd):
global history_records
async def translate_to_ggb(natural_language_cmd, session_id):
global session_history
# 确保当前会话ID存在于历史记录字典中
if session_id not in session_history:
session_history[session_id] = []
# 获取当前会话的历史记录
history_records = session_history[session_id]
# 构建包含历史记录的提示
history_prompt = """
@@ -175,6 +182,8 @@ async def translate_to_ggb(natural_language_cmd):
# 保持历史记录不超过最大数量
if len(history_records) > MAX_HISTORY:
history_records.pop(0)
# 更新会话历史
session_history[session_id] = history_records
except Exception as e:
print(f"翻译指令错误: {str(e)}")
@@ -259,7 +268,8 @@ async def process_geometry_image(image_url: str):
# 示例用法
if __name__ == '__main__':
async def test_translate():
async for content in translate_to_ggb("把A和B连接起来"):
# 测试时使用固定会话ID
async for content in translate_to_ggb("把A和B连接起来", "test_session_123"):
if content:
print(content, end="", flush=True)