| 14 | |
| 15 | |
| 16 | class 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""" |
| 35 | Classify ""{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) |
no outgoing calls
no test coverage detected
searching dependent graphs…