|
| 1 | +import logging |
| 2 | + |
| 3 | +import typer |
| 4 | +from dotenv import load_dotenv |
| 5 | + |
| 6 | +from template_langgraph.agents.chat_with_tools_agent.agent import graph as chat_with_tools_agent_graph |
| 7 | +from template_langgraph.agents.issue_formatter_agent.agent import graph as issue_formatter_agent_graph |
| 8 | +from template_langgraph.agents.kabuto_helpdesk_agent import graph as kabuto_helpdesk_agent_graph |
| 9 | +from template_langgraph.agents.task_decomposer_agent.agent import graph as task_decomposer_agent_graph |
| 10 | +from template_langgraph.loggers import get_logger |
| 11 | + |
| 12 | +# Initialize the Typer application |
| 13 | +app = typer.Typer( |
| 14 | + add_completion=False, |
| 15 | + help="template-langgraph CLI", |
| 16 | +) |
| 17 | + |
| 18 | +# Set up logging |
| 19 | +logger = get_logger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +def get_agent_graph(name: str): |
| 23 | + if name == "chat_with_tools_agent": |
| 24 | + return chat_with_tools_agent_graph |
| 25 | + elif name == "issue_formatter_agent": |
| 26 | + return issue_formatter_agent_graph |
| 27 | + elif name == "task_decomposer_agent": |
| 28 | + return task_decomposer_agent_graph |
| 29 | + elif name == "kabuto_helpdesk_agent": |
| 30 | + return kabuto_helpdesk_agent_graph |
| 31 | + else: |
| 32 | + raise ValueError(f"Unknown agent name: {name}") |
| 33 | + |
| 34 | + |
| 35 | +@app.command() |
| 36 | +def png( |
| 37 | + name: str = typer.Option( |
| 38 | + "chat_with_tools_agent", |
| 39 | + "--name", |
| 40 | + "-n", |
| 41 | + help="Name of the agent to draw", |
| 42 | + ), |
| 43 | + output_file_path: str = typer.Option( |
| 44 | + "output.png", |
| 45 | + "--output", |
| 46 | + "-o", |
| 47 | + help="Path to the output PNG file", |
| 48 | + ), |
| 49 | + verbose: bool = typer.Option( |
| 50 | + False, |
| 51 | + "--verbose", |
| 52 | + "-v", |
| 53 | + help="Enable verbose output", |
| 54 | + ), |
| 55 | +): |
| 56 | + # Set up logging |
| 57 | + if verbose: |
| 58 | + logger.setLevel(logging.DEBUG) |
| 59 | + |
| 60 | + logger.debug(f"This is a debug message with name: {name}") |
| 61 | + typer.echo(f"Drawing agent: {name}") |
| 62 | + get_agent_graph(name).get_graph().draw_mermaid_png( |
| 63 | + output_file_path=output_file_path, |
| 64 | + ) |
| 65 | + typer.echo(f"Graph saved to {output_file_path}") |
| 66 | + |
| 67 | + |
| 68 | +@app.command() |
| 69 | +def run( |
| 70 | + name: str = typer.Option( |
| 71 | + "chat_with_tools_agent", |
| 72 | + "--name", |
| 73 | + "-n", |
| 74 | + help="Name of the agent to draw", |
| 75 | + ), |
| 76 | + question: str = typer.Option( |
| 77 | + "「鬼灯」を実行すると、KABUTOが急に停止します。原因と対策を教えてください。", |
| 78 | + "--question", |
| 79 | + "-q", |
| 80 | + help="Question to ask the agent", |
| 81 | + ), |
| 82 | + verbose: bool = typer.Option( |
| 83 | + False, |
| 84 | + "--verbose", |
| 85 | + "-v", |
| 86 | + help="Enable verbose output", |
| 87 | + ), |
| 88 | +): |
| 89 | + # Set up logging |
| 90 | + if verbose: |
| 91 | + logger.setLevel(logging.DEBUG) |
| 92 | + |
| 93 | + if name == "chat_with_tools_agent": |
| 94 | + from template_langgraph.agents.chat_with_tools_agent.agent import ( |
| 95 | + AgentState, |
| 96 | + ) |
| 97 | + |
| 98 | + for event in chat_with_tools_agent_graph.stream( |
| 99 | + input=AgentState( |
| 100 | + messages=[ |
| 101 | + { |
| 102 | + "role": "user", |
| 103 | + "content": question, |
| 104 | + } |
| 105 | + ], |
| 106 | + ) |
| 107 | + ): |
| 108 | + logger.info("-" * 20) |
| 109 | + logger.info(f"Event: {event}") |
| 110 | + |
| 111 | + if name == "issue_formatter_agent": |
| 112 | + from template_langgraph.agents.issue_formatter_agent.agent import ( |
| 113 | + AgentState, |
| 114 | + ) |
| 115 | + |
| 116 | + for event in issue_formatter_agent_graph.stream( |
| 117 | + input=AgentState( |
| 118 | + messages=[ |
| 119 | + { |
| 120 | + "role": "user", |
| 121 | + "content": question, |
| 122 | + } |
| 123 | + ], |
| 124 | + ) |
| 125 | + ): |
| 126 | + logger.info("-" * 20) |
| 127 | + logger.info(f"Event: {event}") |
| 128 | + |
| 129 | + if name == "kabuto_helpdesk_agent": |
| 130 | + from template_langgraph.agents.kabuto_helpdesk_agent import KabutoHelpdeskAgent |
| 131 | + |
| 132 | + agent = KabutoHelpdeskAgent( |
| 133 | + tools=None, # ツールはカスタムせず、デフォルトのツールを使用 |
| 134 | + ) |
| 135 | + response = agent.run( |
| 136 | + question=question, |
| 137 | + ) |
| 138 | + logger.info(f"Agent result: {response}") |
| 139 | + |
| 140 | + # エージェントの応答を表示 |
| 141 | + logger.info(f"Answer: {response['messages'][-1].content}") |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + load_dotenv( |
| 146 | + override=True, |
| 147 | + verbose=True, |
| 148 | + ) |
| 149 | + app() |
0 commit comments