sync update from LAN repo (source ji @ 85078a31)
This commit is contained in:
1 parent
5d6627af50
commit
e602d88695
864 files changed
+99150
-6346
No files matched your search
@@ -0,0 +1,33 @@
|
||||
from deerflow.integrations.weknora.local_index.ranking import (
|
||||
title_boosted_rrf_score,
|
||||
title_match_strength,
|
||||
)
|
||||
|
||||
|
||||
def test_exact_chinese_title_is_strongest_match() -> None:
|
||||
exact = title_match_strength("海湖庄园", "海湖庄园")
|
||||
broad = title_match_strength("海湖庄园", "特朗普")
|
||||
assert exact == 1.0
|
||||
assert broad == 0.0
|
||||
|
||||
|
||||
def test_question_wrappers_do_not_hide_title_phrase() -> None:
|
||||
assert title_match_strength("请介绍一下海湖庄园的历史", "海湖庄园") == 1.0
|
||||
|
||||
|
||||
def test_title_inside_longer_natural_language_question_is_retained() -> None:
|
||||
assert title_match_strength("请查找海湖庄园最新消息", "海湖庄园") > 0.7
|
||||
|
||||
|
||||
def test_possessive_character_inside_real_title_is_not_deleted() -> None:
|
||||
assert title_match_strength("党的二十大", "党的二十大") == 1.0
|
||||
|
||||
|
||||
def test_alias_match_is_discounted_but_still_useful() -> None:
|
||||
assert 0 < title_match_strength("Mar-a-Lago", "美国总统官邸", ["Mar-a-Lago"]) < 1
|
||||
|
||||
|
||||
def test_rrf_title_boost_is_weighted_and_can_be_disabled() -> None:
|
||||
base = 1 / 62
|
||||
assert title_boosted_rrf_score(base, 1.0, weight=0.25) == base * 1.25
|
||||
assert title_boosted_rrf_score(base, 1.0, enabled=False, weight=1.0) == base
|
||||
@@ -0,0 +1,49 @@
|
||||
from types import SimpleNamespace
|
||||
|
||||
from deerflow.config import memory_config as memory_config_module
|
||||
from deerflow.config.memory_config import (
|
||||
MemoryConfig,
|
||||
delete_recall_budget_cache,
|
||||
get_effective_memory_config,
|
||||
get_memory_config,
|
||||
set_memory_config,
|
||||
set_recall_budget_cache,
|
||||
)
|
||||
|
||||
|
||||
def test_runtime_disable_cannot_be_reactivated_by_config_or_user_override(monkeypatch):
|
||||
user_id = "memory-disabled-test-user"
|
||||
original = get_memory_config()
|
||||
try:
|
||||
monkeypatch.setattr(
|
||||
memory_config_module,
|
||||
"get_memory_runtime_settings",
|
||||
lambda: SimpleNamespace(enabled=False),
|
||||
)
|
||||
set_memory_config(MemoryConfig(enabled=True))
|
||||
set_recall_budget_cache(user_id, "high")
|
||||
|
||||
effective = get_effective_memory_config(user_id)
|
||||
|
||||
assert effective.enabled is False
|
||||
finally:
|
||||
delete_recall_budget_cache(user_id)
|
||||
set_memory_config(original)
|
||||
|
||||
|
||||
def test_admin_runtime_enable_activates_memory_even_when_yaml_default_is_off(monkeypatch):
|
||||
original = get_memory_config()
|
||||
try:
|
||||
monkeypatch.setattr(
|
||||
memory_config_module,
|
||||
"get_memory_runtime_settings",
|
||||
lambda: SimpleNamespace(enabled=True),
|
||||
)
|
||||
set_memory_config(MemoryConfig(enabled=False, version="v1"))
|
||||
|
||||
effective = get_effective_memory_config("memory-enabled-test-user")
|
||||
|
||||
assert effective.enabled is True
|
||||
assert effective.version == "v1"
|
||||
finally:
|
||||
set_memory_config(original)
|
||||
@@ -2,7 +2,7 @@ import pytest
|
||||
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
|
||||
|
||||
from deerflow.agents.middlewares.writing_setup_round_cap_middleware import WritingSetupRoundCapMiddleware
|
||||
from deerflow.models.message_ordering import normalize_chat_payload_messages, normalize_langchain_messages
|
||||
from deerflow.models.message_ordering import normalize_chat_payload_messages, normalize_langchain_messages, normalize_model_request_messages
|
||||
from deerflow.models.patched_openai import PatchedChatOpenAI
|
||||
|
||||
|
||||
@@ -49,6 +49,37 @@ def test_normalize_langchain_messages_moves_late_systems_before_history():
|
||||
assert normalized[1].content == "user 0"
|
||||
|
||||
|
||||
def test_normalize_chat_payload_messages_merges_adjacent_leading_systems():
|
||||
payload = {
|
||||
"messages": [
|
||||
{"role": "system", "content": "agent prompt"},
|
||||
{"role": "system", "content": "compaction summary"},
|
||||
{"role": "user", "content": "continue"},
|
||||
]
|
||||
}
|
||||
|
||||
normalize_chat_payload_messages(payload)
|
||||
|
||||
assert [message["role"] for message in payload["messages"]] == ["system", "user"]
|
||||
assert payload["messages"][0]["content"] == "agent prompt\n\n---\n\ncompaction summary"
|
||||
|
||||
|
||||
def test_normalize_model_request_messages_folds_compaction_summary_into_system_slot():
|
||||
system_message, messages = normalize_model_request_messages(
|
||||
SystemMessage(content="agent prompt"),
|
||||
[
|
||||
SystemMessage(content="compaction summary"),
|
||||
HumanMessage(content="continue"),
|
||||
],
|
||||
)
|
||||
|
||||
assert system_message is not None
|
||||
assert system_message.content == "agent prompt\n\n---\n\ncompaction summary"
|
||||
assert len(messages) == 1
|
||||
assert isinstance(messages[0], HumanMessage)
|
||||
assert all(not isinstance(message, SystemMessage) for message in messages)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("turns", [12, 25, 50])
|
||||
def test_patched_openai_payload_has_only_leading_system_after_many_turns(turns):
|
||||
model = PatchedChatOpenAI(
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import logging
|
||||
from uuid import uuid4
|
||||
|
||||
from deerflow.models.timing_callback import ModelTimingCallback
|
||||
|
||||
|
||||
def test_model_timing_callback_logs_connection_and_completion(caplog):
|
||||
callback = ModelTimingCallback(model_name="qwen", base_url="http://model/v1")
|
||||
run_id = uuid4()
|
||||
|
||||
with caplog.at_level(logging.INFO, logger="deerflow.models.timing_callback"):
|
||||
callback.on_chat_model_start({}, [[object()]], run_id=run_id)
|
||||
callback.on_llm_new_token("", run_id=run_id, chunk=object())
|
||||
callback.on_llm_end(object(), run_id=run_id)
|
||||
|
||||
messages = [record.getMessage() for record in caplog.records]
|
||||
assert any("LLM_CONNECT_START" in message for message in messages)
|
||||
assert any("LLM_CONNECT_OK" in message and "first_chunk_ms=" in message for message in messages)
|
||||
assert any("LLM_REQUEST_DONE" in message and "total_ms=" in message for message in messages)
|
||||
|
||||
|
||||
def test_model_timing_callback_logs_failure(caplog):
|
||||
callback = ModelTimingCallback(model_name="qwen", base_url="http://model/v1")
|
||||
run_id = uuid4()
|
||||
|
||||
with caplog.at_level(logging.INFO, logger="deerflow.models.timing_callback"):
|
||||
callback.on_chat_model_start({}, [[object()]], run_id=run_id)
|
||||
callback.on_llm_error(TimeoutError("upstream timeout"), run_id=run_id)
|
||||
|
||||
message = caplog.records[-1].getMessage()
|
||||
assert "LLM_REQUEST_FAILED" in message
|
||||
assert "TimeoutError" in message
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
from langchain_core.messages import AIMessage, AIMessageChunk
|
||||
|
||||
from deerflow.models.patched_openai import (
|
||||
_append_reasoning_content,
|
||||
_extract_reasoning_content,
|
||||
_restore_reasoning_content,
|
||||
)
|
||||
|
||||
|
||||
def test_extracts_legacy_reasoning_content():
|
||||
assert _extract_reasoning_content({"reasoning_content": "legacy"}) == "legacy"
|
||||
|
||||
|
||||
def test_extracts_vllm_reasoning():
|
||||
assert _extract_reasoning_content({"reasoning": "new spelling"}) == "new spelling"
|
||||
|
||||
|
||||
def test_normalizes_vllm_reasoning_for_existing_clients():
|
||||
message = AIMessageChunk(content="")
|
||||
|
||||
updated = _append_reasoning_content(message, "thinking", reasoning="thinking")
|
||||
|
||||
assert updated.additional_kwargs["reasoning_content"] == "thinking"
|
||||
assert updated.additional_kwargs["reasoning"] == "thinking"
|
||||
|
||||
|
||||
def test_restores_original_vllm_reasoning_spelling_for_follow_up_turn():
|
||||
original = AIMessage(
|
||||
content="answer",
|
||||
additional_kwargs={
|
||||
"reasoning": "thinking",
|
||||
"reasoning_content": "thinking",
|
||||
},
|
||||
)
|
||||
payload = {"role": "assistant", "content": "answer"}
|
||||
|
||||
_restore_reasoning_content(payload, original)
|
||||
|
||||
assert payload["reasoning"] == "thinking"
|
||||
assert "reasoning_content" not in payload
|
||||
Reference in new issue
Block a user