sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)
This commit is contained in:
1 parent
4f7f07504a
commit
8bc8b0e082
1651 files changed
+278599
-11416
No files matched your search
@@ -7,7 +7,7 @@ from types import SimpleNamespace
|
||||
import pytest
|
||||
|
||||
from app.report_collaboration.agentscope_runtime.event_adapter import ConversationReplica, MemberStreamContext, SeqAllocator, make_emitter
|
||||
from app.report_collaboration.agentscope_runtime.model_adapter import DeerFlowChatModelAdapter, ModelAdapterError, record_attempt_usage, redact_secrets
|
||||
from app.report_collaboration.agentscope_runtime.model_adapter import DeerFlowChatModelAdapter, ModelAdapterError, estimate_tokens_for_text, record_attempt_usage, redact_secrets
|
||||
from app.report_collaboration.agentscope_runtime.model_resolver import freeze_plan_models, resolve_role_model
|
||||
from deerflow.config.app_config import AppConfig
|
||||
from deerflow.config.model_config import ModelConfig
|
||||
@@ -114,7 +114,7 @@ async def test_adapter_streams_text_tool_and_usage() -> None:
|
||||
adapter = DeerFlowChatModelAdapter(snapshot, app_config=_app(), llm_factory=lambda: llm, node_run_id="research-0-attempt1")
|
||||
pieces = [item async for item in adapter.astream_chat([{"role": "user", "content": "写报告"}], tools=[{"name": "web_search"}])]
|
||||
assert llm.bound_tools == [{"name": "web_search"}]
|
||||
assert any(not item.is_last and item.content and item.content[0].get("text") == "市" for item in pieces)
|
||||
assert any(not item.is_last and item.content and getattr(item.content[0], "text", "") == "市" for item in pieces)
|
||||
last = pieces[-1]
|
||||
assert last.is_last is True
|
||||
assert last.usage is not None
|
||||
@@ -128,6 +128,72 @@ async def test_adapter_streams_text_tool_and_usage() -> None:
|
||||
assert frozen.value.code == "MODEL_FROZEN"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_adapter_streams_reasoning_blocks_and_inline_think_tags() -> None:
|
||||
snapshot = resolve_role_model("writer", app_config=_app())
|
||||
llm = _FakeLLM(
|
||||
chunks=[
|
||||
SimpleNamespace(
|
||||
content="",
|
||||
additional_kwargs={"reasoning_content": "先整理证据。"},
|
||||
tool_call_chunks=[],
|
||||
usage_metadata=None,
|
||||
response_metadata={},
|
||||
),
|
||||
SimpleNamespace(
|
||||
content="<thi",
|
||||
additional_kwargs={},
|
||||
tool_call_chunks=[],
|
||||
usage_metadata=None,
|
||||
response_metadata={},
|
||||
),
|
||||
SimpleNamespace(
|
||||
content="nk>再组织章节。</think>报告正文",
|
||||
additional_kwargs={},
|
||||
tool_call_chunks=[],
|
||||
usage_metadata=None,
|
||||
response_metadata={},
|
||||
),
|
||||
]
|
||||
)
|
||||
adapter = DeerFlowChatModelAdapter(snapshot, app_config=_app(), llm_factory=lambda: llm)
|
||||
pieces = [item async for item in adapter.astream_chat([{"role": "user", "content": "开始"}])]
|
||||
blocks = [block for item in pieces[:-1] for block in item.content]
|
||||
assert "先整理证据。" in "".join(str(getattr(block, "thinking", "") or "") for block in blocks)
|
||||
assert "再组织章节。" in "".join(str(getattr(block, "thinking", "") or "") for block in blocks)
|
||||
assert "".join(str(getattr(block, "text", "") or "") for block in blocks) == "报告正文"
|
||||
assert len(pieces[-1].content) == 1
|
||||
assert getattr(pieces[-1].content[0], "text", "") == "报告正文"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_adapter_count_tokens_and_context_size_contract() -> None:
|
||||
"""AgentScope compress_context 依赖 model.context_size 与 count_tokens(messages, tools)。"""
|
||||
|
||||
snapshot = resolve_role_model("researcher", app_config=_app())
|
||||
adapter = DeerFlowChatModelAdapter(snapshot, app_config=_app())
|
||||
assert adapter.model == snapshot.provider_model
|
||||
assert isinstance(adapter.context_size, int) and adapter.context_size > 0
|
||||
|
||||
class _Msg:
|
||||
def __init__(self, text: str) -> None:
|
||||
self.content = text
|
||||
|
||||
def get_content_blocks(self):
|
||||
return [{"type": "text", "text": self.content}]
|
||||
|
||||
agentscope_style = [_Msg("市场分析" * 20)]
|
||||
tokens_msg = await adapter.count_tokens(agentscope_style, None)
|
||||
dict_style = [{"role": "user", "content": "hello world"}]
|
||||
tokens_dict = await adapter.count_tokens(dict_style, None)
|
||||
tools = [{"name": "web_search", "parameters": {"type": "object"}}]
|
||||
tokens_tools = await adapter.count_tokens(dict_style, tools)
|
||||
assert tokens_msg > 0
|
||||
assert tokens_dict == estimate_tokens_for_text("hello world")
|
||||
assert tokens_tools > tokens_dict
|
||||
assert await adapter.count_tokens([], None) == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_adapter_redacts_provider_errors_and_records_metrics() -> None:
|
||||
snapshot = resolve_role_model("writer", app_config=_app())
|
||||
@@ -156,7 +222,7 @@ async def test_adapter_redacts_provider_errors_and_records_metrics() -> None:
|
||||
@pytest.mark.asyncio
|
||||
async def test_protocol_events_feed_conversation_replica() -> None:
|
||||
snapshot = resolve_role_model("researcher", app_config=_app())
|
||||
llm = _FakeLLM(chunks=[SimpleNamespace(content="份额上升", tool_call_chunks=[], usage_metadata={"input_tokens": 1, "output_tokens": 2}, response_metadata={})])
|
||||
llm = _FakeLLM(chunks=[SimpleNamespace(content="份额上升", additional_kwargs={"reasoning_content": "先核对统计口径"}, tool_call_chunks=[], usage_metadata={"input_tokens": 1, "output_tokens": 2}, response_metadata={})])
|
||||
adapter = DeerFlowChatModelAdapter(snapshot, app_config=_app(), llm_factory=lambda: llm)
|
||||
replica = ConversationReplica(
|
||||
MemberStreamContext(
|
||||
@@ -166,6 +232,7 @@ async def test_protocol_events_feed_conversation_replica() -> None:
|
||||
node_run_id="research-0-attempt1",
|
||||
phase_id="p1",
|
||||
display_name="研究员",
|
||||
emit_reasoning=True,
|
||||
),
|
||||
make_emitter("s1", "r1", SeqAllocator()),
|
||||
)
|
||||
@@ -178,6 +245,117 @@ async def test_protocol_events_feed_conversation_replica() -> None:
|
||||
assert types[-1] == "message.completed"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_adapter_runs_inside_real_agentscope_agent() -> None:
|
||||
"""Production adapter satisfies the real AgentScope model/block contract."""
|
||||
|
||||
from agentscope.agent import Agent, InjectionConfig
|
||||
from agentscope.message import Msg, UserMsg
|
||||
|
||||
snapshot = resolve_role_model("researcher", app_config=_app())
|
||||
llm = _FakeLLM(
|
||||
chunks=[
|
||||
SimpleNamespace(
|
||||
content="正在分析",
|
||||
additional_kwargs={},
|
||||
tool_call_chunks=[],
|
||||
usage_metadata={"input_tokens": 3, "output_tokens": 2, "total_tokens": 5},
|
||||
response_metadata={},
|
||||
)
|
||||
]
|
||||
)
|
||||
adapter = DeerFlowChatModelAdapter(snapshot, app_config=_app(), llm_factory=lambda: llm)
|
||||
agent = Agent(
|
||||
name="研究员",
|
||||
system_prompt="完成当前研究任务。",
|
||||
model=adapter,
|
||||
injection_config=InjectionConfig(emit_hint_event=False),
|
||||
)
|
||||
|
||||
event_types: list[str] = []
|
||||
final_msg = None
|
||||
async for event in agent.reply_stream(UserMsg(name="user", content="分析市场"), yield_final_msg=True):
|
||||
if isinstance(event, Msg):
|
||||
final_msg = event
|
||||
else:
|
||||
event_types.append(str(getattr(getattr(event, "type", ""), "value", getattr(event, "type", ""))))
|
||||
|
||||
assert "MODEL_CALL_START" in event_types
|
||||
assert "TEXT_BLOCK_DELTA" in event_types
|
||||
assert final_msg is not None
|
||||
assert final_msg.get_text_content() == "正在分析"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_adapter_emits_real_tool_blocks_for_agentscope_structured_output() -> None:
|
||||
"""Tool calls from LangChain chunks reach AgentScope's structured-output tool."""
|
||||
|
||||
from agentscope.agent import Agent, InjectionConfig
|
||||
from agentscope.message import Msg, UserMsg
|
||||
from pydantic import BaseModel
|
||||
|
||||
class Result(BaseModel):
|
||||
answer: str
|
||||
|
||||
class StructuredLLM(_FakeLLM):
|
||||
async def astream(self, messages):
|
||||
_ = messages
|
||||
tool_name = next(
|
||||
item["function"]["name"]
|
||||
for item in (self.bound_tools or [])
|
||||
if item["function"]["name"].lower() == "generatestructuredoutput"
|
||||
)
|
||||
yield SimpleNamespace(
|
||||
content="",
|
||||
additional_kwargs={},
|
||||
tool_call_chunks=[
|
||||
SimpleNamespace(id="structured-1", name=tool_name, args='{"answer":', index=0)
|
||||
],
|
||||
usage_metadata=None,
|
||||
response_metadata={},
|
||||
)
|
||||
yield SimpleNamespace(
|
||||
content="",
|
||||
additional_kwargs={},
|
||||
tool_call_chunks=[SimpleNamespace(id=None, name=None, args='"ok"', index=0)],
|
||||
usage_metadata=None,
|
||||
response_metadata={},
|
||||
)
|
||||
yield SimpleNamespace(
|
||||
content="",
|
||||
additional_kwargs={},
|
||||
tool_call_chunks=[SimpleNamespace(id=None, name=None, args="}", index=0)],
|
||||
usage_metadata={"input_tokens": 3, "output_tokens": 3, "total_tokens": 6},
|
||||
response_metadata={},
|
||||
)
|
||||
|
||||
snapshot = resolve_role_model("researcher", app_config=_app())
|
||||
llm = StructuredLLM()
|
||||
adapter = DeerFlowChatModelAdapter(snapshot, app_config=_app(), llm_factory=lambda: llm)
|
||||
agent = Agent(
|
||||
name="研究员",
|
||||
system_prompt="返回结构化结果。",
|
||||
model=adapter,
|
||||
injection_config=InjectionConfig(emit_hint_event=False),
|
||||
)
|
||||
|
||||
final_msg = None
|
||||
tool_start_names: list[str] = []
|
||||
async for event in agent.reply_stream(
|
||||
UserMsg(name="user", content="开始"),
|
||||
structured_schema=Result,
|
||||
yield_final_msg=True,
|
||||
):
|
||||
if isinstance(event, Msg):
|
||||
final_msg = event
|
||||
elif str(getattr(getattr(event, "type", ""), "value", getattr(event, "type", ""))) == "TOOL_CALL_START":
|
||||
tool_start_names.append(str(getattr(event, "tool_call_name", "")))
|
||||
|
||||
assert final_msg is not None
|
||||
assert final_msg.structured_output["answer"] == "ok"
|
||||
assert tool_start_names == ["GenerateStructuredOutput"]
|
||||
|
||||
|
||||
def test_redact_helper() -> None:
|
||||
assert "sk-abc" not in redact_secrets("using sk-abc and Bearer tok")
|
||||
assert "[REDACTED]" in redact_secrets("api_key=super-secret")
|
||||
Reference in new issue
Block a user