feat(agent-core): 补齐提示词编排与结构化解析
This commit is contained in:
14
tests/conftest.py
Normal file
14
tests/conftest.py
Normal file
@@ -0,0 +1,14 @@
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def force_mock_llm_provider_for_tests(monkeypatch):
|
||||
"""
|
||||
测试环境固定使用 mock Provider。
|
||||
|
||||
当前项目会从根目录 `.env` 自动读取真实模型配置,这对本地运行很有帮助,
|
||||
但单元测试和页面回归测试不应该依赖外部网络或真实密钥状态。
|
||||
因此这里统一覆盖为 mock,保证测试稳定、可重复。
|
||||
"""
|
||||
monkeypatch.setenv("LLM_PROVIDER", "mock")
|
||||
monkeypatch.setenv("LLM_MODEL", "mock-model")
|
||||
@@ -1,24 +1,125 @@
|
||||
from agent_core.orchestrator import run_agent
|
||||
from agent_core.orchestrator import build_messages, run_agent
|
||||
from agent_core.rag.ingest import ingest_document
|
||||
from agent_core.rag.retriever import retrieve
|
||||
|
||||
|
||||
def test_run_agent_returns_structured_mock_result():
|
||||
def test_run_agent_returns_structured_result_from_llm_output():
|
||||
scenario = {
|
||||
"id": "knowledge_qa",
|
||||
"name": "知识库问答助手",
|
||||
"agent": {
|
||||
"role": "知识库助手",
|
||||
"goal": "基于资料回答问题",
|
||||
"instructions": ["仅根据证据回答"],
|
||||
},
|
||||
"rag": {"enabled": True, "collection": "knowledge_qa", "top_k": 3},
|
||||
"tools": ["generate_action_items"],
|
||||
"output": {"type": "general_answer"},
|
||||
}
|
||||
provider_response = """
|
||||
{
|
||||
"answer": "请先隔离异常现场,再通知负责人。",
|
||||
"confidence": "high",
|
||||
"references": [
|
||||
{"source": "sop.md", "excerpt": "异常处理 SOP:先隔离现场"}
|
||||
]
|
||||
}
|
||||
"""
|
||||
|
||||
result = run_agent(scenario, "如何处理异常?")
|
||||
class FakeProvider:
|
||||
def generate(self, messages, response_format=None):
|
||||
from agent_core.llm_provider import LLMResponse
|
||||
|
||||
return LLMResponse(
|
||||
content=provider_response,
|
||||
model_name="demo-model",
|
||||
success=True,
|
||||
)
|
||||
|
||||
result = run_agent(
|
||||
scenario,
|
||||
"如何处理异常?",
|
||||
options={"llm_provider": FakeProvider()},
|
||||
)
|
||||
|
||||
assert result.status == "success"
|
||||
assert result.answer
|
||||
assert result.answer == "请先隔离异常现场,再通知负责人。"
|
||||
assert result.structured_output["output_type"] == "general_answer"
|
||||
assert result.structured_output["confidence"] == "high"
|
||||
assert isinstance(result.references, list)
|
||||
assert result.tool_calls[0]["tool_name"] == "generate_action_items"
|
||||
assert result.model_name == "demo-model"
|
||||
|
||||
|
||||
def test_run_agent_falls_back_when_llm_returns_non_json():
|
||||
scenario = {
|
||||
"id": "document_review",
|
||||
"name": "文档审核助手",
|
||||
"agent": {
|
||||
"role": "审核助手",
|
||||
"goal": "总结审核意见",
|
||||
"instructions": ["输出重点问题"],
|
||||
},
|
||||
"rag": {"enabled": False},
|
||||
"tools": [],
|
||||
"output": {"type": "document_review_report"},
|
||||
}
|
||||
|
||||
class FakeProvider:
|
||||
def generate(self, messages, response_format=None):
|
||||
from agent_core.llm_provider import LLMResponse
|
||||
|
||||
return LLMResponse(
|
||||
content="这是非 JSON 的普通回答",
|
||||
model_name="demo-model",
|
||||
success=True,
|
||||
)
|
||||
|
||||
result = run_agent(
|
||||
scenario,
|
||||
"请检查合同风险",
|
||||
options={"llm_provider": FakeProvider()},
|
||||
)
|
||||
|
||||
assert result.status == "success"
|
||||
assert result.answer == "这是非 JSON 的普通回答"
|
||||
assert result.structured_output["output_type"] == "document_review_report"
|
||||
assert result.structured_output["summary"] == "这是非 JSON 的普通回答"
|
||||
assert result.structured_output["parse_mode"] == "fallback"
|
||||
|
||||
|
||||
def test_build_messages_contains_role_goal_references_and_tool_results():
|
||||
scenario = {
|
||||
"name": "质量异常分析助手",
|
||||
"agent": {
|
||||
"role": "质量管理专家",
|
||||
"goal": "生成结构化质量分析报告",
|
||||
"instructions": ["必须引用知识库", "缺失信息要说明"],
|
||||
},
|
||||
"output": {"type": "quality_report"},
|
||||
}
|
||||
|
||||
messages = build_messages(
|
||||
scenario_config=scenario,
|
||||
user_input="分析 A 线异常",
|
||||
references=[{"source": "sop.md", "content": "先隔离现场"}],
|
||||
tool_calls=[
|
||||
{
|
||||
"tool_name": "query_demo_records",
|
||||
"success": True,
|
||||
"result": {"records": [{"title": "A线缺陷"}]},
|
||||
"error": "",
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
assert messages[0]["role"] == "system"
|
||||
assert "质量管理专家" in messages[0]["content"]
|
||||
assert "生成结构化质量分析报告" in messages[0]["content"]
|
||||
assert "quality_report" in messages[0]["content"]
|
||||
assert "先隔离现场" in messages[1]["content"]
|
||||
assert "A线缺陷" in messages[1]["content"]
|
||||
assert "分析 A 线异常" in messages[2]["content"]
|
||||
|
||||
|
||||
def test_rag_ingest_and_retrieve_filters_by_scenario_and_query(tmp_path):
|
||||
|
||||
Reference in New Issue
Block a user