From 28e0fe7c27a3f9a281f8e7c66c96ccada7291df5 Mon Sep 17 00:00:00 2001 From: "Yuyao Huang (Sam)" Date: Sun, 29 Mar 2026 18:29:18 +0800 Subject: [PATCH] =?UTF-8?q?refactor(agent):=20=E7=A7=BB=E9=99=A4=E6=9C=AA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=E9=80=9A=E7=94=A8=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 清理不再使用的通用问题检测函数及相关正则表达式模式,简化代码结构 --- orchestrator/agent.py | 47 ------------------------------------------- 1 file changed, 47 deletions(-) diff --git a/orchestrator/agent.py b/orchestrator/agent.py index 06f293d..b865d1a 100644 --- a/orchestrator/agent.py +++ b/orchestrator/agent.py @@ -8,7 +8,6 @@ from __future__ import annotations import asyncio import json import logging -import re from collections import defaultdict from typing import Dict, List, Optional @@ -86,35 +85,6 @@ Guidelines: MAX_ITERATIONS = 10 _TOOL_MAP = {t.name: t for t in TOOLS} -QUESTION_PATTERNS = [ - r'\?$', # ends with ? - r'?$', # ends with Chinese ? - r'\b(what|how|why|when|where|who|which|explain|describe|tell me|can you|could you|is there|are there|do you know)\b', - r'(什么|怎么|为什么|何时|哪里|谁|哪个|解释|描述|告诉我|能否|可以|有没有|是不是)', -] - - -def _is_general_question(text: str) -> bool: - """Check if text looks like a general knowledge question (not a project task).""" - text_lower = text.lower().strip() - - project_indicators = [ - 'create', 'make', 'build', 'fix', 'update', 'delete', 'remove', 'add', - 'implement', 'refactor', 'test', 'run', 'execute', 'start', 'stop', - 'project', 'folder', 'directory', 'file', 'code', 'session', - '创建', '制作', '构建', '修复', '更新', '删除', '添加', '实现', '重构', '测试', '运行', '项目', '文件夹', '文件', '代码', - ] - - for indicator in project_indicators: - if indicator in text_lower: - return False - - for pattern in QUESTION_PATTERNS: - if re.search(pattern, text_lower, re.IGNORECASE): - return True - - return False - class OrchestrationAgent: """Per-user agent with conversation history and active session tracking.""" @@ -186,23 +156,6 @@ class OrchestrationAgent: logger.exception("Passthrough error for user=%s", user_id) return f"[Error] {exc}" - # Direct Q&A: if no active session and message looks like a general question, answer directly - if not active_conv and _is_general_question(text): - logger.debug(" → direct Q&A (no tools)") - llm_no_tools = ChatOpenAI( - base_url=OPENAI_BASE_URL, - api_key=OPENAI_API_KEY, - model=OPENAI_MODEL, - temperature=0.7, - ) - qa_prompt = ( - "You are a helpful assistant. Answer the user's question concisely and accurately. " - "Reply in the same language the user uses.\n\n" - f"Question: {text}" - ) - response = await llm_no_tools.ainvoke([HumanMessage(content=qa_prompt)]) - return response.content or "" - messages: list[BaseMessage] = ( [SystemMessage(content=self._build_system_prompt(user_id))] + self._history[user_id]