- Introduce WorkflowLLMRouter for pre-response LLM routing, allowing agents to determine the appropriate function to call based on user input. - Implement UserTurnRoutingProcessor to manage user turns before reaching the LLM, ensuring proper routing and handling of user messages. - Refactor WorkflowBrain to integrate new routing logic and enhance agent stage configuration, including entry modes and resource management. - Update service factory to support dynamic LLM resource configuration based on workflow settings. - Add tests for new routing functionality and ensure proper handling of user messages in various scenarios.
76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
from __future__ import annotations
|
|
|
|
import unittest
|
|
from types import SimpleNamespace
|
|
from unittest.mock import patch
|
|
|
|
from models import AssistantConfig
|
|
from services.workflow_router import WorkflowLLMRouter
|
|
|
|
|
|
class WorkflowLLMRouterTest(unittest.IsolatedAsyncioTestCase):
|
|
async def test_uses_required_tool_choice_without_developer_messages(self):
|
|
requests = []
|
|
|
|
class FakeCompletions:
|
|
async def create(self, **kwargs):
|
|
requests.append(kwargs)
|
|
return SimpleNamespace(
|
|
choices=[
|
|
SimpleNamespace(
|
|
message=SimpleNamespace(
|
|
tool_calls=[
|
|
SimpleNamespace(
|
|
function=SimpleNamespace(name="goto_age", arguments="{}")
|
|
)
|
|
]
|
|
)
|
|
)
|
|
]
|
|
)
|
|
|
|
class FakeClient:
|
|
def __init__(self, **_kwargs):
|
|
self.chat = SimpleNamespace(completions=FakeCompletions())
|
|
self.closed = False
|
|
|
|
async def close(self):
|
|
self.closed = True
|
|
|
|
cfg = AssistantConfig(
|
|
type="workflow",
|
|
model="deepseek-chat",
|
|
llm_api_key="secret",
|
|
llm_base_url="https://llm.test/v1",
|
|
)
|
|
router = WorkflowLLMRouter(cfg)
|
|
edges = [
|
|
{
|
|
"id": "age",
|
|
"data": {"condition": "用户已经回答姓名", "priority": 10},
|
|
}
|
|
]
|
|
|
|
with patch("services.workflow_router.AsyncOpenAI", FakeClient):
|
|
selected = await router.select_edge(
|
|
node_name="询问姓名",
|
|
node_prompt="询问用户姓名",
|
|
edges=edges,
|
|
history=[{"role": "user", "message": "我叫李白"}],
|
|
variables={"customer_type": "new"},
|
|
edge_name=lambda _edge: "goto_age",
|
|
edge_description=lambda _edge: "用户已经回答姓名",
|
|
)
|
|
|
|
self.assertEqual(selected, "goto_age")
|
|
self.assertEqual(requests[0]["tool_choice"], "required")
|
|
self.assertEqual(
|
|
[message["role"] for message in requests[0]["messages"]],
|
|
["system", "user"],
|
|
)
|
|
self.assertNotIn("developer", str(requests[0]["messages"]))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|