MCPcopy Create free account
hub / github.com/microsoft/TypeChat / TextRequestRouter

Class TextRequestRouter

python/examples/multiSchema/router.py:16–50  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14
15
16class TextRequestRouter:
17 _current_agents: dict[str, AgentInfo]
18 _validator: TypeChatValidator[TaskClassification]
19 _translator: TypeChatJsonTranslator[TaskClassification]
20
21 def __init__(self, model: TypeChatLanguageModel):
22 super().__init__()
23 self._validator = TypeChatValidator(TaskClassification)
24 self._translator = TypeChatJsonTranslator(model, self._validator, TaskClassification)
25 self._current_agents = {}
26
27 def register_agent(self, name: str, description: str, handler: Callable[[str], Awaitable[Any]]):
28 agent = AgentInfo(name=name, description=description, handler=handler)
29 self._current_agents[name] = agent
30
31 async def route_request(self, line: str):
32 classes_str = json.dumps(self._current_agents, indent=2, default=lambda o: None, allow_nan=False)
33
34 prompt_fragment = F"""
35Classify ""{line}"" using the following classification table:
36'''
37{classes_str}
38'''
39"""
40
41 result = await self._translator.translate(prompt_fragment)
42 if isinstance(result, Failure):
43 print("Translation Failed ❌")
44 print(f"Context: {result.message}")
45 else:
46 result = result.value
47 print("Translation Succeeded! ✅\n")
48 print(f"The target class is {result['task_kind']}")
49 target = self._current_agents[result["task_kind"]]
50 await target.get("handler")(line)

Callers 1

mainFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…