|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from fast_agent import FastAgent |
| 4 | +from fast_agent.agents.agent_types import AgentConfig |
| 5 | +from fast_agent.agents.tool_agent import ToolAgent |
| 6 | +from fast_agent.agents.tool_runner import ToolRunnerHooks |
| 7 | +from fast_agent.context import Context |
| 8 | +from fast_agent.interfaces import ToolRunnerHookCapable |
| 9 | +from fast_agent.types import PromptMessageExtended |
| 10 | + |
| 11 | + |
| 12 | +def get_video_call_transcript(video_id: str) -> str: |
| 13 | + return "Assistant: Hi, how can I assist you today?\n\nCustomer: Hi, I wanted to ask you about last invoice I received..." |
| 14 | + |
| 15 | + |
| 16 | +class HookedToolAgent(ToolAgent, ToolRunnerHookCapable): |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + config: AgentConfig, |
| 20 | + context: Context | None = None, |
| 21 | + ): |
| 22 | + tools = [get_video_call_transcript] |
| 23 | + super().__init__(config, tools, context) |
| 24 | + self._hooks = ToolRunnerHooks( |
| 25 | + before_llm_call=self._add_style_hint, |
| 26 | + after_tool_call=self._log_tool_result, |
| 27 | + ) |
| 28 | + |
| 29 | + @property |
| 30 | + def tool_runner_hooks(self) -> ToolRunnerHooks | None: |
| 31 | + return self._hooks |
| 32 | + |
| 33 | + async def _add_style_hint(self, runner, messages: list[PromptMessageExtended]) -> None: |
| 34 | + if runner.iteration == 0: |
| 35 | + runner.append_messages("Keep the answer to one short sentence.") |
| 36 | + |
| 37 | + async def _log_tool_result(self, runner, message: PromptMessageExtended) -> None: |
| 38 | + if message.tool_results: |
| 39 | + tool_names = ", ".join(message.tool_results.keys()) |
| 40 | + print(f"[hook] tool results received: {tool_names}") |
| 41 | + |
| 42 | + |
| 43 | +fast = FastAgent("Example Tool Use Application (Hooks)") |
| 44 | + |
| 45 | + |
| 46 | +@fast.custom(HookedToolAgent) |
| 47 | +async def main() -> None: |
| 48 | + async with fast.run() as agent: |
| 49 | + await agent.default.generate( |
| 50 | + "What is the topic of the video call no.1234?", |
| 51 | + ) |
| 52 | + await agent.interactive() |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + asyncio.run(main()) |
0 commit comments