| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | from typing import AsyncGenerator, List |
| 8 | |
| 9 | from llama_models.llama3.api.datatypes import * # noqa: F403 |
| 10 | from llama_toolchain.agentic_system.api import * # noqa: F403 |
| 11 | from llama_toolchain.memory.api import * # noqa: F403 |
| 12 | from llama_toolchain.safety.api import * # noqa: F403 |
| 13 | |
| 14 | from llama_toolchain.agentic_system.api import ( |
| 15 | AgenticSystemTurnResponseEventType as EventType, |
| 16 | ) |
| 17 | from llama_toolchain.tools.custom.datatypes import CustomTool |
| 18 | |
| 19 | |
| 20 | class AgentWithCustomToolExecutor: |
| 21 | def __init__( |
| 22 | self, |
| 23 | api: AgenticSystem, |
| 24 | agent_id: str, |
| 25 | session_id: str, |
| 26 | agent_config: AgentConfig, |
| 27 | custom_tools: List[CustomTool], |
| 28 | ): |
| 29 | self.api = api |
| 30 | self.agent_id = agent_id |
| 31 | self.session_id = session_id |
| 32 | self.agent_config = agent_config |
| 33 | self.custom_tools = custom_tools |
| 34 | |
| 35 | async def execute_turn( |
| 36 | self, |
| 37 | messages: List[Message], |
| 38 | attachments: Optional[List[Attachment]] = None, |
| 39 | max_iters: int = 5, |
| 40 | stream: bool = True, |
| 41 | ) -> AsyncGenerator: |
| 42 | tools_dict = {t.get_name(): t for t in self.custom_tools} |
| 43 | |
| 44 | current_messages = messages.copy() |
| 45 | n_iter = 0 |
| 46 | while n_iter < max_iters: |
| 47 | n_iter += 1 |
| 48 | |
| 49 | request = AgenticSystemTurnCreateRequest( |
| 50 | agent_id=self.agent_id, |
| 51 | session_id=self.session_id, |
| 52 | messages=current_messages, |
| 53 | attachments=attachments, |
| 54 | stream=stream, |
| 55 | ) |
| 56 | |
| 57 | turn = None |
| 58 | async for chunk in self.api.create_agentic_system_turn(request): |
| 59 | if chunk.event.payload.event_type != EventType.turn_complete.value: |
| 60 | yield chunk |
| 61 | else: |
| 62 | turn = chunk.event.payload.turn |
| 63 | |
| 64 | message = turn.output_message |
| 65 | if len(message.tool_calls) == 0: |
| 66 | yield chunk |
| 67 | return |
| 68 | |
| 69 | if message.stop_reason == StopReason.out_of_tokens: |
| 70 | yield chunk |
| 71 | return |
| 72 | |
| 73 | tool_call = message.tool_calls[0] |
| 74 | if tool_call.tool_name not in tools_dict: |
| 75 | m = ToolResponseMessage( |
| 76 | call_id=tool_call.call_id, |
| 77 | tool_name=tool_call.tool_name, |
| 78 | content=f"Unknown tool `{tool_call.tool_name}` was called. Try again with something else", |
| 79 | ) |
| 80 | next_message = m |
| 81 | else: |
| 82 | tool = tools_dict[tool_call.tool_name] |
| 83 | result_messages = await execute_custom_tool(tool, message) |
| 84 | next_message = result_messages[0] |
| 85 | |
| 86 | yield next_message |
| 87 | current_messages = [next_message] |
| 88 | |
| 89 | |
| 90 | async def execute_custom_tool(tool: CustomTool, message: Message) -> List[Message]: |
| 91 | result_messages = await tool.run([message]) |
| 92 | assert ( |
| 93 | len(result_messages) == 1 |
| 94 | ), f"Expected single message, got {len(result_messages)}" |
| 95 | |
| 96 | return result_messages |