workflow_Agent.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from langgraph.graph import StateGraph , START , END
  2. from langgraph.prebuilt import ToolNode, tools_condition
  3. from Agents import AgentState , agent_analyseur , agent_executor , tools , agent_reporter
  4. # 1. Initialisation du graphe avec l'état personnalisé
  5. workflow = StateGraph(AgentState)
  6. # 2. Ajout des nœuds
  7. workflow.add_node("analyseur", agent_analyseur)
  8. workflow.add_node("executor", agent_executor)
  9. workflow.add_node("reporter", agent_reporter)
  10. workflow.add_node("tools", ToolNode(tools))
  11. # 3. Définition des arêtes stables
  12. workflow.add_edge(START, "analyseur")
  13. workflow.add_edge("analyseur", "executor")
  14. workflow.add_edge("tools", "executor")
  15. workflow.add_edge("reporter", END)
  16. # 4. Logique de routage personnalisée
  17. def router(state: AgentState):
  18. last_message = state["messages"][-1]
  19. # Si l'exécuteur demande un outil, on y va
  20. if last_message.tool_calls:
  21. return "tools"
  22. # Sinon, cela signifie qu'il a fini son analyse technique -> on passe au rapport
  23. return "reporter"
  24. # On applique la condition sur l'executor
  25. workflow.add_conditional_edges(
  26. "executor",
  27. router,
  28. {"tools": "tools", "reporter": "reporter"}
  29. )
  30. # 5. Compilation
  31. app = workflow.compile()
  32. # Pour sauvegarder l'image du workflow
  33. with open("graph_workflow.png", "wb") as f:
  34. f.write(app.get_graph().draw_mermaid_png())
  35. print("Graphique du workflow généré sous : graph_workflow.png")