|
| 1 | +from langchain_core.messages import AIMessage |
| 2 | +from langgraph.graph import END, START, StateGraph |
| 3 | + |
| 4 | +from template_langgraph.agents.basic_workflow_agent.models import AgentInput, AgentOutput, AgentState |
| 5 | +from template_langgraph.llms.azure_openais import AzureOpenAiWrapper |
| 6 | +from template_langgraph.loggers import get_logger |
| 7 | + |
| 8 | +logger = get_logger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +class BasicWorkflowAgent: |
| 12 | + def __init__(self): |
| 13 | + self.llm = AzureOpenAiWrapper().chat_model |
| 14 | + |
| 15 | + def create_graph(self): |
| 16 | + """Create the main graph for the agent.""" |
| 17 | + # Create the workflow state graph |
| 18 | + workflow = StateGraph(AgentState) |
| 19 | + |
| 20 | + # Create nodes |
| 21 | + workflow.add_node("initialize", self.initialize) |
| 22 | + workflow.add_node("do_something", self.do_something) |
| 23 | + workflow.add_node("finalize", self.finalize) |
| 24 | + |
| 25 | + # Create edges |
| 26 | + workflow.add_edge(START, "initialize") |
| 27 | + workflow.add_edge("initialize", "do_something") |
| 28 | + workflow.add_edge("do_something", "finalize") |
| 29 | + workflow.add_edge("finalize", END) |
| 30 | + |
| 31 | + # Compile the graph |
| 32 | + return workflow.compile() |
| 33 | + |
| 34 | + def initialize(self, state: AgentState) -> AgentState: |
| 35 | + """Initialize the agent with the given state.""" |
| 36 | + logger.info(f"Initializing BasicWorkflowAgent with state: {state}") |
| 37 | + # Here you can add any initialization logic if needed |
| 38 | + return state |
| 39 | + |
| 40 | + def do_something(self, state: AgentState) -> AgentState: |
| 41 | + """Perform some action with the given state.""" |
| 42 | + logger.info(f"Doing something with state: {state}") |
| 43 | + |
| 44 | + # Here you can add the logic for the action |
| 45 | + response: AIMessage = self.llm.invoke( |
| 46 | + input=state["messages"], |
| 47 | + ) |
| 48 | + logger.info(f"Response after doing something: {response}") |
| 49 | + state["messages"].append( |
| 50 | + { |
| 51 | + "role": "assistant", |
| 52 | + "content": response.content, |
| 53 | + } |
| 54 | + ) |
| 55 | + |
| 56 | + return state |
| 57 | + |
| 58 | + def finalize(self, state: AgentState) -> AgentState: |
| 59 | + """Finalize the agent's work and prepare the output.""" |
| 60 | + logger.info(f"Finalizing BasicWorkflowAgent with state: {state}") |
| 61 | + # Here you can add any finalization logic if needed |
| 62 | + return state |
| 63 | + |
| 64 | + def run_agent(self, input: AgentInput) -> AgentOutput: |
| 65 | + """Run the agent with the given input.""" |
| 66 | + logger.info(f"Running BasicWorkflowAgent with question: {input.model_dump_json(indent=2)}") |
| 67 | + app = self.create_graph() |
| 68 | + initial_state: AgentState = { |
| 69 | + "messages": [ |
| 70 | + { |
| 71 | + "role": "user", |
| 72 | + "content": input.request, |
| 73 | + } |
| 74 | + ], |
| 75 | + } |
| 76 | + final_state = app.invoke(initial_state) |
| 77 | + logger.info(f"Final state after running agent: {final_state}") |
| 78 | + return AgentOutput(response=final_state["messages"][-1].content) |
| 79 | + |
| 80 | + def draw_mermaid_png(self) -> bytes: |
| 81 | + """Draw the graph in Mermaid format.""" |
| 82 | + return self.create_graph().get_graph().draw_mermaid_png() |
0 commit comments