This commit is contained in:
朱潮 2026-01-08 23:10:09 +08:00
commit b93c40d5a5
2 changed files with 36 additions and 71 deletions

View File

@ -85,7 +85,7 @@ class AgentConfig:
robot_type = "deep_agent"
preamble_text, system_prompt = get_preamble_text(request.language, request.system_prompt)
enable_thinking = request.enable_thinking and "```guideline" in request.system_prompt
enable_thinking = request.enable_thinking and "<guidelines>" in request.system_prompt
config = cls(
bot_id=request.bot_id,
@ -126,7 +126,8 @@ class AgentConfig:
robot_type = bot_config.get("robot_type", "general_agent")
if robot_type == "catalog_agent":
robot_type = "deep_agent"
enable_thinking = request.enable_thinking and "```guideline" in bot_config.get("system_prompt")
enable_thinking = request.enable_thinking and "<guidelines>" in bot_config.get("system_prompt")
config = cls(
bot_id=request.bot_id,

View File

@ -493,15 +493,15 @@ def get_language_text(language: str):
return language_map.get(language.lower(), '')
def get_preamble_text(language: str, system_prompt: str):
# 首先检查system_prompt中是否有preamble代码块
# 首先检查system_prompt中是否有preamble标签
if system_prompt:
preamble_pattern = r'```preamble\s*\n(.*?)\n```'
preamble_pattern = r'<preamble>\s*(.*?)\s*</preamble>'
preamble_matches = re.findall(preamble_pattern, system_prompt, re.DOTALL)
if preamble_matches:
# 提取preamble内容
preamble_content = preamble_matches[0].strip()
if preamble_content:
# 从system_prompt中删除preamble代码块
# 从system_prompt中删除preamble标签
cleaned_system_prompt = re.sub(preamble_pattern, '', system_prompt, flags=re.DOTALL)
return preamble_content, cleaned_system_prompt
@ -697,27 +697,40 @@ def extract_block_from_system_prompt(system_prompt: str) -> tuple[str, str, str,
terms_list = []
# 首先分割所有的代码块
block_pattern = r'```(\w+)\s*\n(.*?)\n```'
# 使用XML标签格式解析块
blocks_to_remove = []
for match in re.finditer(block_pattern, system_prompt, re.DOTALL):
block_type, content = match.groups()
# 解析 <guidelines>
guidelines_pattern = r'<guidelines>\s*(.*?)\s*</guidelines>'
match = re.search(guidelines_pattern, system_prompt, re.DOTALL)
if match:
guidelines = match.group(1).strip()
blocks_to_remove.append(match.group(0))
if block_type == 'guideline' or block_type == 'guidelines':
guidelines = content.strip()
# 解析 <tools>
tools_pattern = r'<tools>\s*(.*?)\s*</tools>'
match = re.search(tools_pattern, system_prompt, re.DOTALL)
if match:
tools = match.group(1).strip()
blocks_to_remove.append(match.group(0))
# 解析 <scenarios>
scenarios_pattern = r'<scenarios>\s*(.*?)\s*</scenarios>'
match = re.search(scenarios_pattern, system_prompt, re.DOTALL)
if match:
scenarios = match.group(1).strip()
blocks_to_remove.append(match.group(0))
# 解析 <terms>
terms_pattern = r'<terms>\s*(.*?)\s*</terms>'
match = re.search(terms_pattern, system_prompt, re.DOTALL)
if match:
try:
terms = parse_terms_text(match.group(1).strip())
terms_list.extend(terms)
blocks_to_remove.append(match.group(0))
elif block_type == 'tools':
tools = content.strip()
elif block_type == 'scenarios':
scenarios = content.strip()
elif block_type == 'terms':
try:
terms = parse_terms_text(content.strip())
terms_list.extend(terms)
blocks_to_remove.append(match.group(0))
except Exception as e:
logger.error(f"Error parsing terms: {e}")
except Exception as e:
logger.error(f"Error parsing terms: {e}")
# 从system_prompt中移除这些已解析的块
cleaned_prompt = system_prompt
@ -729,55 +742,6 @@ def extract_block_from_system_prompt(system_prompt: str) -> tuple[str, str, str,
return cleaned_prompt, guidelines, tools, scenarios, terms_list
def parse_guidelines_text(text: str) -> List[Dict[str, Any]]:
"""
解析guidelines文本支持多种格式
Args:
text: guidelines文本内容
Returns:
List[Dict]: guidelines列表
"""
guidelines = []
# 尝试解析JSON格式
if text.strip().startswith('[') or text.strip().startswith('{'):
try:
data = json.loads(text)
if isinstance(data, list):
for item in data:
if isinstance(item, dict):
guidelines.append(item)
elif isinstance(data, dict):
guidelines.append(data)
return guidelines
except json.JSONDecodeError:
pass
# 解析行格式,支持多种分隔符
lines = [line.strip() for line in text.split('\n') if line.strip()]
for line in lines:
# 跳过注释行
if line.startswith('#') or line.startswith('//'):
continue
# 尝试解析 "id) Condition: ... Action: ..." 格式
id_condition_action_pattern = r'(\d+)\)\s*Condition:\s*(.*?)\s*Action:\s*(.*?)(?:\s*Priority:\s*(\d+))?$'
match = re.match(id_condition_action_pattern, line, re.IGNORECASE)
if match:
guidelines.append({
'guideline_id': int(match.group(1)),
'condition': match.group(2).strip(),
'action': match.group(3).strip(),
'priority': int(match.group(4)) if match.group(4) else 1
})
continue
return guidelines
def parse_terms_text(text: str) -> List[Dict[str, Any]]:
"""
解析terms文本支持多种格式