guildlines filter
This commit is contained in:
parent
2af0c5cedc
commit
51b6d085fd
@ -152,8 +152,8 @@ Please specify the applicability of each guideline:
|
|||||||
"checks": [
|
"checks": [
|
||||||
{
|
{
|
||||||
"guideline_id": "1",
|
"guideline_id": "1",
|
||||||
"condition": "具体的条件描述",
|
"condition": "Specific condition description",
|
||||||
"rationale": "<解释为什么条件满足或不满足>",
|
"rationale": "<Explain why the conditions are met or not met, and what action should be taken if the conditions are met>",
|
||||||
"applies": true/false
|
"applies": true/false
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@ -186,17 +186,23 @@ async def create_agent_and_generate_response(
|
|||||||
agent = all_results[-1] # agent创建的结果
|
agent = all_results[-1] # agent创建的结果
|
||||||
batch_results = all_results[:-1] # guideline批次的结果
|
batch_results = all_results[:-1] # guideline批次的结果
|
||||||
|
|
||||||
# 合并guideline分析结果
|
# 合并guideline分析结果,使用JSON格式的checks数组
|
||||||
valid_results = []
|
all_checks = []
|
||||||
for i, result in enumerate(batch_results):
|
for i, result in enumerate(batch_results):
|
||||||
if isinstance(result, Exception):
|
if isinstance(result, Exception):
|
||||||
print(f"Guideline batch {i} failed: {result}")
|
print(f"Guideline batch {i} failed: {result}")
|
||||||
continue
|
continue
|
||||||
if result and result.strip():
|
if result and isinstance(result, dict) and 'checks' in result:
|
||||||
valid_results.append(result.strip())
|
# 如果是JSON对象且包含checks数组,只保留applies为true的checks
|
||||||
|
applicable_checks = [check for check in result['checks'] if check.get('applies') is True]
|
||||||
|
all_checks.extend(applicable_checks)
|
||||||
|
elif result and isinstance(result, str) and result.strip():
|
||||||
|
# 如果是普通文本,保留原有逻辑
|
||||||
|
print(f"Non-JSON result from batch {i}: {result}")
|
||||||
|
|
||||||
if valid_results:
|
if all_checks:
|
||||||
guideline_analysis = "\n\n".join(valid_results)
|
# 将checks数组格式化为JSON字符串
|
||||||
|
guideline_analysis = json.dumps({"checks": all_checks}, ensure_ascii=False)
|
||||||
print(f"Merged guideline analysis result: {guideline_analysis}")
|
print(f"Merged guideline analysis result: {guideline_analysis}")
|
||||||
|
|
||||||
# 将分析结果添加到最后一个消息的内容中
|
# 将分析结果添加到最后一个消息的内容中
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import json
|
||||||
import asyncio
|
import asyncio
|
||||||
from typing import List, Dict, Optional, Union, Any
|
from typing import List, Dict, Optional, Union, Any
|
||||||
import aiohttp
|
import aiohttp
|
||||||
@ -285,13 +286,9 @@ def extract_guidelines_from_system_prompt(system_prompt: Optional[str]) -> tuple
|
|||||||
|
|
||||||
guidelines_text = "\n".join(matches).strip()
|
guidelines_text = "\n".join(matches).strip()
|
||||||
|
|
||||||
# # 从原始system_prompt中删除 ```guideline``` 内容块
|
# 从原始system_prompt中删除 ```guideline``` 内容块
|
||||||
# cleaned_prompt = re.sub(pattern, '', system_prompt, flags=re.DOTALL)
|
cleaned_prompt = re.sub(pattern, '', system_prompt, flags=re.DOTALL)
|
||||||
|
return cleaned_prompt, guidelines_text
|
||||||
# # 清理多余的空行
|
|
||||||
# cleaned_prompt = re.sub(r'\n\s*\n\s*\n', '\n\n', cleaned_prompt).strip()
|
|
||||||
|
|
||||||
return system_prompt, guidelines_text
|
|
||||||
|
|
||||||
|
|
||||||
def format_messages_to_chat_history(messages: List[Dict[str, str]]) -> str:
|
def format_messages_to_chat_history(messages: List[Dict[str, str]]) -> str:
|
||||||
@ -482,7 +479,20 @@ async def process_guideline_batch(
|
|||||||
batch_guidelines_text = "\n".join(guidelines_batch)
|
batch_guidelines_text = "\n".join(guidelines_batch)
|
||||||
batch_analysis = await call_guideline_llm(chat_history, batch_guidelines_text, model_name, api_key, model_server)
|
batch_analysis = await call_guideline_llm(chat_history, batch_guidelines_text, model_name, api_key, model_server)
|
||||||
|
|
||||||
return batch_analysis
|
# 从响应中提取 ```json 和 ``` 包裹的内容
|
||||||
|
json_pattern = r'```json\s*\n(.*?)\n```'
|
||||||
|
json_matches = re.findall(json_pattern, batch_analysis, re.DOTALL)
|
||||||
|
|
||||||
|
if json_matches:
|
||||||
|
try:
|
||||||
|
# 解析第一个找到的JSON对象
|
||||||
|
json_data = json.loads(json_matches[0])
|
||||||
|
return json_data # 返回解析后的JSON对象
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
print(f"Error parsing JSON from guideline analysis: {e}")
|
||||||
|
return batch_analysis # 如果JSON解析失败,返回原始文本
|
||||||
|
else:
|
||||||
|
return batch_analysis # 如果没有找到JSON格式,返回原始文本
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error processing guideline batch: {e}")
|
print(f"Error processing guideline batch: {e}")
|
||||||
return ""
|
return ""
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user