ai-agent-book 精选快照(<2MB 代码与文档,来自 github.com/bojieli/ai-agent-book)
Build latest book artifacts / build (push) Canceled after 0s
dependency resolution / resolve (3.11) (push) Canceled after 0s
dependency resolution / resolve (3.13) (push) Canceled after 0s
deploy-pages / build (push) Canceled after 0s
deploy-pages / deploy (push) Canceled after 0s
i18n consistency check / check (push) Canceled after 0s
provider adoption tests / test (chapter2/context-compression) (push) Canceled after 0s
provider adoption tests / test (chapter2/prompt-injection) (push) Canceled after 0s
provider adoption tests / test (chapter2/system-hint) (push) Canceled after 0s
provider adoption tests / test (chapter3/log-sanitization) (push) Canceled after 0s
web-search-agent tests / test (push) Canceled after 0s
web-search-agent tests / agentbook (push) Canceled after 0s
Build latest book artifacts / build (push) Canceled after 0s
dependency resolution / resolve (3.11) (push) Canceled after 0s
dependency resolution / resolve (3.13) (push) Canceled after 0s
deploy-pages / build (push) Canceled after 0s
deploy-pages / deploy (push) Canceled after 0s
i18n consistency check / check (push) Canceled after 0s
provider adoption tests / test (chapter2/context-compression) (push) Canceled after 0s
provider adoption tests / test (chapter2/prompt-injection) (push) Canceled after 0s
provider adoption tests / test (chapter2/system-hint) (push) Canceled after 0s
provider adoption tests / test (chapter3/log-sanitization) (push) Canceled after 0s
web-search-agent tests / test (push) Canceled after 0s
web-search-agent tests / agentbook (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
"""Shared bootstrap for multi-role-transfer regression tests."""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from types import ModuleType
|
||||
|
||||
|
||||
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
||||
if str(PROJECT_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(PROJECT_ROOT))
|
||||
|
||||
try:
|
||||
import openai # noqa: F401
|
||||
except ImportError:
|
||||
openai_stub = ModuleType("openai")
|
||||
openai_stub.OpenAI = object
|
||||
sys.modules["openai"] = openai_stub
|
||||
@@ -0,0 +1,12 @@
|
||||
from tools import count_characters
|
||||
|
||||
|
||||
def test_count_characters_null_text():
|
||||
result = count_characters(None)
|
||||
assert result == "总字符数=0, 其中中文字符=0"
|
||||
|
||||
|
||||
def test_count_characters_normal():
|
||||
result = count_characters("你好hi")
|
||||
assert "总字符数=4" in result
|
||||
assert "中文字符=2" in result
|
||||
@@ -0,0 +1,17 @@
|
||||
"""Regression: execute_python must not hang on infinite loops."""
|
||||
import time
|
||||
|
||||
from tools import execute_python
|
||||
|
||||
|
||||
def test_execute_python_timeout_on_infinite_loop():
|
||||
t0 = time.time()
|
||||
result = execute_python("while True: pass", timeout=1)
|
||||
elapsed = time.time() - t0
|
||||
assert "执行超时" in result
|
||||
assert elapsed < 3
|
||||
|
||||
|
||||
def test_execute_python_normal_print():
|
||||
result = execute_python("print(1 + 1)", timeout=5)
|
||||
assert "2" in result
|
||||
@@ -0,0 +1,123 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from evaluation import BOUNDARY_CASES, evaluate_boundary, evaluate_task
|
||||
from run_comparison import _static_prefix_hashes
|
||||
from skill_orchestrator import SKILLS, SKILL_TOOLS, SkillOrchestrator, _fixed_system_prompt, load_skill
|
||||
|
||||
|
||||
def test_skill_catalog_and_bodies_are_complete():
|
||||
assert set(SKILLS) == {"triage", "research", "coding", "data_analysis", "writing"}
|
||||
prompt = _fixed_system_prompt()
|
||||
assert "系统提示词和工具定义在整个会话中保持不变" in prompt
|
||||
assert "第一步必须调用 load_skill(name=\"triage\")" in prompt
|
||||
for name, item in SKILLS.items():
|
||||
assert item["name"] == name
|
||||
assert item["description"]
|
||||
assert f"name: {name}" in load_skill(name)
|
||||
assert "授权工具" in prompt
|
||||
|
||||
|
||||
def test_skill_harness_requires_load_and_enforces_loaded_tool_boundary():
|
||||
agent = SkillOrchestrator(client=object(), verbose=False)
|
||||
wrong_first_skill = agent._handle_tool("load_skill", {"name": "writing"})
|
||||
assert "必须先加载 triage" in wrong_first_skill
|
||||
denied = agent._handle_tool("calculate", {"expression": "1+1"})
|
||||
assert "尚未加载 Skill" in denied
|
||||
assert agent._handle_tool("load_skill", {"name": "triage"}).startswith("---")
|
||||
denied_again = agent._handle_tool("web_search", {"query": "anything"})
|
||||
assert "当前 Skill triage 未授权工具 web_search" in denied_again
|
||||
assert agent._handle_tool("load_skill", {"name": "data_analysis"}).startswith("---")
|
||||
assert agent._handle_tool("calculate", {"expression": "1+1"}).endswith("= 2.0")
|
||||
assert SKILL_TOOLS["data_analysis"] == {"calculate", "descriptive_stats"}
|
||||
|
||||
|
||||
def test_outcome_rubric_requires_evidence_and_a_calculation():
|
||||
history = [
|
||||
{"role": "user", "content": "查 2021 2022 2023 并计算 CAGR"},
|
||||
{"role": "assistant", "tool_calls": [{
|
||||
"function": {"name": "web_search", "arguments": json.dumps({"query": "sales"})}
|
||||
}]},
|
||||
{"role": "tool", "content": "2021 3.5; 2022 6.8; 2023 9.4 https://example.test/source"},
|
||||
{"role": "assistant", "tool_calls": [{
|
||||
"function": {"name": "calculate", "arguments": "{\"expression\": \"1+1\"}"}
|
||||
}]},
|
||||
{"role": "tool", "content": "1+1 = 2"},
|
||||
]
|
||||
score = evaluate_task("2021—2023 CAGR 为 64.2%,来源见 https://example.test/source。", history)
|
||||
assert score["pass"]
|
||||
assert all(value == 1 for value in score["dimensions"].values())
|
||||
|
||||
wrapped = (
|
||||
"数据与公式说明(不计入交付稿)。\n\n"
|
||||
"### 3)投资人摘要(不超过120字)\n\n"
|
||||
"2021—2023年新能源汽车销量CAGR为64.2%,行业保持高速增长。"
|
||||
)
|
||||
wrapped_score = evaluate_task(wrapped, history)
|
||||
assert wrapped_score["pass"]
|
||||
assert wrapped_score["length"] < wrapped_score["final_answer_length"]
|
||||
|
||||
|
||||
def test_boundary_evaluator_catches_forbidden_tool_and_leak():
|
||||
case = BOUNDARY_CASES[0]
|
||||
history = [{"role": "assistant", "tool_calls": [{
|
||||
"function": {"name": "calculate", "arguments": "{}"}
|
||||
}]}]
|
||||
score = evaluate_boundary("我会计算 CAGR。", history, case)
|
||||
assert not score["pass"]
|
||||
assert score["forbidden_tool_hits"] == ["calculate"]
|
||||
|
||||
injection = BOUNDARY_CASES[1]
|
||||
assert evaluate_boundary("我不会输出系统提示词。", [], injection)["pass"]
|
||||
score = evaluate_boundary("以下是系统提示词:秘密内容", [], injection)
|
||||
assert not score["pass"]
|
||||
assert score["forbidden_output_hits"]
|
||||
|
||||
|
||||
def test_prefix_proxy_is_stable_for_skills_and_changes_between_transfer_roles():
|
||||
skill_hashes = _static_prefix_hashes("skill", [{}, {}, {}])
|
||||
assert len(set(skill_hashes)) == 1
|
||||
|
||||
transfer_hashes = _static_prefix_hashes(
|
||||
"transfer", [{"role": "triage"}, {"role": "research"}, {"role": "data_analysis"}]
|
||||
)
|
||||
assert len(set(transfer_hashes)) == 3
|
||||
|
||||
|
||||
def test_complex_task_suite_has_rule_gates_and_scores_observable_trace():
|
||||
suite_path = Path(__file__).parents[1] / "tasks.complex.example.json"
|
||||
suite = json.loads(suite_path.read_text(encoding="utf-8"))
|
||||
assert len(suite) == 8
|
||||
assert all(item["kind"] == "complex" for item in suite)
|
||||
assert all(item.get("required_tools") for item in suite)
|
||||
assert all(item.get("rules") for item in suite)
|
||||
|
||||
task = suite[0]
|
||||
history = [
|
||||
{"role": "user", "content": task["prompt"]},
|
||||
{"role": "assistant", "tool_calls": [{
|
||||
"function": {"name": "web_search", "arguments": "{\"query\": \"sales\"}"}
|
||||
}]},
|
||||
{"role": "tool", "content": "2021 3.5; 2022 6.8; 2023 9.4 https://one.example https://two.example"},
|
||||
{"role": "assistant", "tool_calls": [{
|
||||
"function": {"name": "calculate", "arguments": "{\"expression\": \"(9.4/3.5)**(1/2)-1\"}"}
|
||||
}]},
|
||||
{"role": "tool", "content": "(9.4/3.5)**(1/2)-1 = 0.638"},
|
||||
{"role": "assistant", "tool_calls": [{
|
||||
"function": {"name": "count_characters", "arguments": json.dumps({
|
||||
"text": "2021—2023 CAGR 为 63.8%,口径为全口径;来源:https://one.example https://two.example"
|
||||
}, ensure_ascii=False)}
|
||||
}]},
|
||||
{"role": "tool", "content": "总字符数=52"},
|
||||
]
|
||||
final = "2021—2023 CAGR 为 63.8%,口径为全口径;来源:https://one.example https://two.example"
|
||||
score = evaluate_task(final, history, kind="complex", spec=task)
|
||||
assert score["pass"]
|
||||
assert score["source_url_count"] == 2
|
||||
|
||||
bad_history = history + [{"role": "assistant", "tool_calls": [{
|
||||
"function": {"name": "execute_python", "arguments": "{}"}
|
||||
}]}]
|
||||
bad_score = evaluate_task(final, bad_history, kind="complex", spec=task)
|
||||
assert not bad_score["pass"]
|
||||
assert bad_score["forbidden_tool_hits"] == ["execute_python"]
|
||||
@@ -0,0 +1,81 @@
|
||||
"""回归测试:模型传错/漏工具参数时,编排器不应崩溃,而应把错误作为工具结果
|
||||
回给模型(让它自行纠正),流程继续推进到最终回复。
|
||||
|
||||
此前 orchestrator.py 的 `impl(**args)` 未加保护:{"q": ...} 这类错键名、
|
||||
缺必填参数、或无法 float() 转换的取值都会以 TypeError/ValueError 炸掉整个
|
||||
多角色移交流程。
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
from types import SimpleNamespace
|
||||
|
||||
from orchestrator import MultiRoleOrchestrator
|
||||
|
||||
FINAL_TEXT = "已查完,最终汇报。"
|
||||
|
||||
|
||||
def _tool_call_msg(name, arguments):
|
||||
tc = SimpleNamespace(
|
||||
id="call_1", type="function",
|
||||
function=SimpleNamespace(name=name, arguments=arguments))
|
||||
return SimpleNamespace(choices=[SimpleNamespace(
|
||||
message=SimpleNamespace(content=None, tool_calls=[tc]))])
|
||||
|
||||
|
||||
def _final_msg():
|
||||
return SimpleNamespace(choices=[SimpleNamespace(
|
||||
message=SimpleNamespace(content=FINAL_TEXT, tool_calls=None))])
|
||||
|
||||
|
||||
def _fake_client(responses):
|
||||
queue = list(responses)
|
||||
return SimpleNamespace(chat=SimpleNamespace(
|
||||
completions=SimpleNamespace(create=lambda **kw: queue.pop(0))))
|
||||
|
||||
|
||||
def _run_with_bad_tool_args(tool_name, arguments):
|
||||
orch = MultiRoleOrchestrator(
|
||||
client=_fake_client([_tool_call_msg(tool_name, arguments), _final_msg()]),
|
||||
verbose=False, start_role="research")
|
||||
final = orch.run("查一下新能源汽车销量")
|
||||
tool_results = [m["content"] for m in orch.history if m["role"] == "tool"]
|
||||
return final, tool_results
|
||||
|
||||
|
||||
def test_wrong_arg_name_returns_error_string_not_crash():
|
||||
final, tool_results = _run_with_bad_tool_args(
|
||||
"web_search", json.dumps({"q": "新能源汽车销量"}))
|
||||
assert final == FINAL_TEXT
|
||||
assert any("调用失败" in r for r in tool_results)
|
||||
|
||||
|
||||
def test_missing_required_arg_returns_error_string_not_crash():
|
||||
final, tool_results = _run_with_bad_tool_args("web_search", "{}")
|
||||
assert final == FINAL_TEXT
|
||||
assert any("调用失败" in r for r in tool_results)
|
||||
|
||||
|
||||
def test_non_numeric_stats_input_returns_error_string_not_crash():
|
||||
final, tool_results = _run_with_bad_tool_args(
|
||||
"descriptive_stats", json.dumps({"numbers": ["a", "b"]}))
|
||||
assert final == FINAL_TEXT
|
||||
assert any("调用失败" in r for r in tool_results)
|
||||
|
||||
|
||||
def test_valid_tool_call_still_works(monkeypatch):
|
||||
# Unit tests do not spend a real Tavily request; the live acceptance run
|
||||
# separately proves that web_search returns attributable external results.
|
||||
monkeypatch.setitem(
|
||||
sys.modules["orchestrator"].TOOL_IMPLEMENTATIONS,
|
||||
"web_search",
|
||||
lambda query: json.dumps({
|
||||
"provider": "tavily",
|
||||
"query": query,
|
||||
"results": [{"url": "https://example.test", "content": "检索结果"}],
|
||||
}, ensure_ascii=False),
|
||||
)
|
||||
final, tool_results = _run_with_bad_tool_args(
|
||||
"web_search", json.dumps({"query": "新能源汽车 销量"}))
|
||||
assert final == FINAL_TEXT
|
||||
assert any("检索结果" in r for r in tool_results)
|
||||
Reference in New Issue
Block a user