Repository: microsoft/TypeChat
File: python/examples/multiSchema/agents.py (Python)

Code before:
            case "neg":
                return -1 * args[0]
            case "id":
                return args[0]

    async def handle_request(self, line: str):
        result = await self._translator.translate(line)
        if isinstance(result, Failure):
            print("Translation Failed ❌")
            print(f"Context: {result.message}")
        else:
            result = result.value
            print("Translation Succeeded! ✅\n")
            print("JSON View")
            print(json.dumps(result, indent=2))

            math_result = await evaluate_json_program(result, self._handle_jsonProgram_call)
            print(f"Math Result: {math_result}")


class MusicAgent:
    _validator: TypeChatValidator[music_schema.PlayerActions]
    _translator: TypeChatTranslator[music_schema.PlayerActions]
    _client_context: ClientContext

    def __init__(self, model: TypeChatModel, vals: dict[str,str | None]):
        self._validator = TypeChatValidator(music_schema.PlayerActions)
        self._translator = TypeChatTranslator(model, self._validator, music_schema.PlayerActions)
        self._client_context = get_client_context(vals)

    async def handle_request(self, line: str):
        result = await self._translator.translate(line)
        if isinstance(result, Failure):
            print("Translation Failed ❌")
            print(f"Context: {result.message}")
        else:
            result = result.value
            print("Translation Succeeded! ✅\n")
            print("JSON View")
            print(json.dumps(result, indent=2))

            try:
                for action in result["actions"]:
                    await handle_call(action, self._client_context)
            except Exception as error:
                print("An exception occurred: ", error)


Review comment (suggestion):
```suggestion
    def __init__(self, model: TypeChatModel, vals: dict[str, str | None]):
```

Code after:
                return args[0] + args[1]
            case "sub":
                return args[0] - args[1]
            case "mul":
                return args[0] * args[1]
            case "div":
                return args[0] / args[1]
            case "neg":
                return -1 * args[0]
            case "id":
                return args[0]
            case _:
                raise ValueError(f'Unexpected function name {func}')

    async def handle_request(self, line: str):
        result = await self._translator.translate(line)
        if isinstance(result, Failure):
            print("Translation Failed ❌")
            print(f"Context: {result.message}")
        else:
            result = result.value
            print("Translation Succeeded! ✅\n")
            print("JSON View")
            print(json.dumps(result, indent=2))

            math_result = await evaluate_json_program(result, self._handle_json_program_call)
            print(f"Math Result: {math_result}")


class MusicAgent:
    _validator: TypeChatValidator[music_schema.PlayerActions]
    _translator: TypeChatTranslator[music_schema.PlayerActions]
    _client_context: ClientContext
    _authentication_vals: dict[str, str | None]

    def __init__(self, model: TypeChatModel, authentication_vals: dict[str, str | None]):
        super().__init__()
        self._validator = TypeChatValidator(music_schema.PlayerActions)
        self._translator = TypeChatTranslator(model, self._validator, music_schema.PlayerActions)
        self._authentication_vals = authentication_vals

    async def authenticate(self):
        self._client_context = await get_client_context(self._authentication_vals)

    async def handle_request(self, line: str):
        if not self._client_context:
            await self.authenticate()

        result = await self._translator.translate(line)
        if isinstance(result, Failure):
            print("Translation Failed ❌")