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,26 @@
|
||||
"""Shared bootstrap for book-translation 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
|
||||
|
||||
try:
|
||||
import tiktoken # noqa: F401
|
||||
except ImportError:
|
||||
tiktoken_stub = ModuleType("tiktoken")
|
||||
encoder = type("Enc", (), {"encode": lambda self, text: list(text or "")})
|
||||
tiktoken_stub.encoding_for_model = lambda _model: encoder()
|
||||
tiktoken_stub.get_encoding = lambda _name: encoder()
|
||||
sys.modules["tiktoken"] = tiktoken_stub
|
||||
@@ -0,0 +1,61 @@
|
||||
"""回归测试:Glossary Agent 返回不合规 JSON 时,run_orchestration 不应崩溃。
|
||||
|
||||
覆盖两类模型失误(此前会让整轮管理者模式直接 KeyError/AttributeError):
|
||||
1) glossary 条目缺 en/zh 键、或值为显式 null / 空串 -> 条目被丢弃;
|
||||
2) 顶层 JSON 是数组而非对象 -> glossary_agent 返回空表。
|
||||
不依赖真实 API:llm_chat / get_client 被打桩。
|
||||
"""
|
||||
|
||||
import json
|
||||
|
||||
import agents
|
||||
|
||||
# 混合各种坏条目的 glossary:错键名 / null / 空串 都应被丢弃,只有合规条目保留。
|
||||
GLOSSARY_JSON = json.dumps({
|
||||
"glossary": [
|
||||
{"term": "token", "translation": "词元"}, # 错键名
|
||||
{"en": None, "zh": "提示词"}, # 显式 null
|
||||
{"en": "", "zh": "时延"}, # 空串
|
||||
{"en": "attention", "zh": "注意力", "pos": "名词"}, # 合规
|
||||
]
|
||||
}, ensure_ascii=False)
|
||||
|
||||
CHAPTERS = {"Chapter 1: Intro": "# Chapter 1\nSome text about attention."}
|
||||
|
||||
|
||||
def _install_fake_llm(glossary_payload=GLOSSARY_JSON):
|
||||
def fake_llm_chat(client, tracker, agent, messages, json_mode=False, note=""):
|
||||
tracker.record(agent, 10, 5, note)
|
||||
if agent == "Glossary":
|
||||
return glossary_payload
|
||||
return "译文"
|
||||
agents.get_client = lambda: object()
|
||||
agents.llm_chat = fake_llm_chat
|
||||
|
||||
|
||||
def test_orchestration_skips_malformed_glossary_entries(tmp_path):
|
||||
_install_fake_llm()
|
||||
result = agents.run_orchestration(
|
||||
CHAPTERS, str(tmp_path), enable_glossary=True, enable_proofreading=False)
|
||||
glossary = result["glossary"]
|
||||
# 所有存活条目必须是非空 en/zh 字符串(下游 g["en"]/g["zh"] 索引的前提)
|
||||
for g in glossary:
|
||||
assert isinstance(g["en"], str) and g["en"].strip()
|
||||
assert isinstance(g["zh"], str) and g["zh"].strip()
|
||||
ens = {g["en"] for g in glossary}
|
||||
assert "attention" in ens # 合规条目保留
|
||||
assert "term" not in ens # 错键名条目已丢弃
|
||||
for en in agents.EDITORIAL_MANDATE: # 编辑部指定术语仍会补齐
|
||||
assert en in ens
|
||||
assert (tmp_path / "glossary.json").exists() # 产物正常落盘
|
||||
assert (tmp_path / "chapter1_zh.md").read_text(encoding="utf-8") == "译文"
|
||||
|
||||
|
||||
def test_glossary_agent_tolerates_json_array():
|
||||
_install_fake_llm(glossary_payload='["not", "an", "object"]')
|
||||
assert agents.glossary_agent(None, agents.TokenTracker(), "book text") == []
|
||||
|
||||
|
||||
def test_glossary_agent_tolerates_missing_glossary_key():
|
||||
_install_fake_llm(glossary_payload='{"terms": []}')
|
||||
assert agents.glossary_agent(None, agents.TokenTracker(), "book text") == []
|
||||
@@ -0,0 +1,28 @@
|
||||
"""Non-dict proofread issue entries must not AttributeError on .get."""
|
||||
from agents import _report_issues
|
||||
|
||||
|
||||
def test_string_issue_items_dropped():
|
||||
report = {
|
||||
"issues": [
|
||||
"术语不一致:token",
|
||||
{"chapter": "Ch1", "type": "术语不一致", "detail": "用了标记"},
|
||||
],
|
||||
}
|
||||
issues = _report_issues(report)
|
||||
assert issues == [{"chapter": "Ch1", "type": "术语不一致", "detail": "用了标记"}]
|
||||
details = [
|
||||
i.get("detail", "") for i in issues if i.get("chapter") == "Ch1"
|
||||
]
|
||||
assert details == ["用了标记"]
|
||||
|
||||
|
||||
def test_null_and_dict_issues_still_work():
|
||||
assert _report_issues({"issues": None}) == []
|
||||
assert _report_issues({"issues": [{"chapter": "a", "detail": "x"}]}) == [
|
||||
{"chapter": "a", "detail": "x"}
|
||||
]
|
||||
|
||||
|
||||
def test_issues_scalar_like_empty():
|
||||
assert _report_issues({"issues": "not a list"}) == []
|
||||
@@ -0,0 +1,33 @@
|
||||
"""Null glossary from Glossary Agent must behave like empty list."""
|
||||
|
||||
import agents
|
||||
|
||||
|
||||
def test_glossary_agent_null_glossary_like_empty():
|
||||
def fake_llm_chat(client, tracker, agent, messages, json_mode=False, note=""):
|
||||
tracker.record(agent, 10, 5, note)
|
||||
return '{"glossary": null}'
|
||||
|
||||
agents.llm_chat = fake_llm_chat
|
||||
assert agents.glossary_agent(None, agents.TokenTracker(), "book") == []
|
||||
|
||||
|
||||
def test_orchestration_tolerates_null_glossary(tmp_path):
|
||||
def fake_llm_chat(client, tracker, agent, messages, json_mode=False, note=""):
|
||||
tracker.record(agent, 10, 5, note)
|
||||
if agent == "Glossary":
|
||||
return '{"glossary": null}'
|
||||
return "译文"
|
||||
|
||||
agents.get_client = lambda: object()
|
||||
agents.llm_chat = fake_llm_chat
|
||||
result = agents.run_orchestration(
|
||||
{"Chapter 1": "token embedding"},
|
||||
str(tmp_path),
|
||||
enable_glossary=True,
|
||||
enable_proofreading=False,
|
||||
)
|
||||
assert isinstance(result["glossary"], list)
|
||||
for g in result["glossary"]:
|
||||
assert isinstance(g["en"], str) and g["en"].strip()
|
||||
assert result["translations"]["Chapter 1"] == "译文"
|
||||
@@ -0,0 +1,15 @@
|
||||
"""Null proofread issues must not TypeError when building report summaries."""
|
||||
from agents import _report_issues
|
||||
|
||||
|
||||
def test_null_issues_like_empty():
|
||||
assert _report_issues({"issues": None}) == []
|
||||
summary_issues = _report_issues({"issues": None})[:5]
|
||||
assert summary_issues == []
|
||||
details = [i.get("detail", "") for i in _report_issues({"issues": None})]
|
||||
assert details == []
|
||||
|
||||
|
||||
def test_issues_preserved():
|
||||
issues = [{"chapter": "a", "detail": "fix me"}]
|
||||
assert _report_issues({"issues": issues}) == issues
|
||||
@@ -0,0 +1,54 @@
|
||||
"""Proofreading must return a dict when the model emits a JSON array or junk."""
|
||||
from agents import _loads_lenient, _report_issues, proofreading_agent
|
||||
|
||||
|
||||
def test_loads_lenient_empty_and_junk_return_none():
|
||||
assert _loads_lenient("") is None
|
||||
assert _loads_lenient("not json") is None
|
||||
assert _loads_lenient('{"a": 1}') == {"a": 1}
|
||||
|
||||
|
||||
def test_report_issues_non_dict():
|
||||
assert _report_issues([]) == []
|
||||
assert _report_issues(None) == []
|
||||
|
||||
|
||||
def test_proofreading_agent_json_array_returns_empty_dict(monkeypatch):
|
||||
calls = []
|
||||
|
||||
def fake_llm_chat(client, tracker, agent, messages, json_mode=False, note=""):
|
||||
calls.append(note)
|
||||
return "[]"
|
||||
|
||||
monkeypatch.setattr("agents.llm_chat", fake_llm_chat)
|
||||
report = proofreading_agent(
|
||||
client=object(),
|
||||
tracker=type("T", (), {"record": lambda *a, **k: None})(),
|
||||
translations={"ch1": "hello"},
|
||||
glossary=[],
|
||||
)
|
||||
assert report == {}
|
||||
assert _report_issues(report) == []
|
||||
assert report.get("chapters_need_revision", []) == []
|
||||
assert calls == ["一致性审校"]
|
||||
|
||||
|
||||
def test_proofreading_agent_valid_object(monkeypatch):
|
||||
payload = {
|
||||
"issues": [{"chapter": "ch1", "detail": "x"}],
|
||||
"chapters_need_revision": ["ch1"],
|
||||
"summary": "ok",
|
||||
}
|
||||
|
||||
def fake_llm_chat(client, tracker, agent, messages, json_mode=False, note=""):
|
||||
import json
|
||||
return json.dumps(payload)
|
||||
|
||||
monkeypatch.setattr("agents.llm_chat", fake_llm_chat)
|
||||
report = proofreading_agent(
|
||||
client=object(),
|
||||
tracker=type("T", (), {"record": lambda *a, **k: None})(),
|
||||
translations={"ch1": "hello"},
|
||||
glossary=[],
|
||||
)
|
||||
assert report == payload
|
||||
Reference in New Issue
Block a user