workflow_agents.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from langgraph.graph import StateGraph, START, END
  2. # ⚠️ Import corrigé (selon version)
  3. try:
  4. from langgraph.prebuilt import ToolNode
  5. except ImportError:
  6. # fallback si ToolNode n'existe pas
  7. from langchain_core.runnables import RunnableLambda
  8. def ToolNode(tools):
  9. def run_tools(state):
  10. return state # à adapter si besoin
  11. return RunnableLambda(run_tools)
  12. # Assure-toi d'importer également agent_llm_vision depuis ton fichier Agents
  13. from Agents import AgentState, agent_extracteur, agent_builder, agent_ocr
  14. # 💡 Note : Pense à ajouter la fonction agent_llm_vision dans ton fichier Agents.py !
  15. try:
  16. from Agents import agent_llm_vision
  17. except ImportError:
  18. # Fallback temporaire si tu ne l'as pas encore écrit dans Agents.py
  19. def agent_llm_vision(state: AgentState):
  20. print("🤖 Exécution de l'Agent LLM Vision...")
  21. return {"messages": ["Traitement vision effectué"]}
  22. workflow = StateGraph(AgentState)
  23. # 1. Définition des Nœuds
  24. workflow.add_node("agent_ocr", agent_ocr)
  25. workflow.add_node("agent_llm_vision", agent_llm_vision) # <-- Nouveau nœud rouge
  26. workflow.add_node("agent_extracteur", agent_extracteur)
  27. workflow.add_node("agent_builder", agent_builder)
  28. # 2. Fonction de routage conditionnel
  29. def route_after_ocr(state: AgentState):
  30. """
  31. Cette fonction regarde le flag 'use_vision' retourné par agent_ocr
  32. pour choisir le chemin dans le graphe.
  33. """
  34. if state.get("use_vision") is True:
  35. return "vision_path"
  36. else:
  37. return "extracteur_path"
  38. # 3. Définition des Arêtes (Edges)
  39. workflow.add_edge(START, "agent_ocr")
  40. # 🔀 Flèche conditionnelle (La logique "If true" de ton schéma)
  41. workflow.add_conditional_edges(
  42. "agent_ocr",
  43. route_after_ocr,
  44. {
  45. "vision_path": "agent_llm_vision", # Si use_vision == True -> Va vers LLM Vision
  46. "extracteur_path": "agent_extracteur" # Si use_vision == False -> Va vers agent_extracteur
  47. }
  48. )
  49. # Suite et fin des flux
  50. workflow.add_edge("agent_extracteur", "agent_builder")
  51. workflow.add_edge("agent_llm_vision", "agent_builder")
  52. workflow.add_edge("agent_builder", END)
  53. # 5. Compilation
  54. app = workflow.compile()
  55. # 6. Graph
  56. try:
  57. with open("graph_workflow.png", "wb") as f:
  58. f.write(app.get_graph().draw_mermaid_png())
  59. print(" Graphique du workflow généré sous : graph_workflow.png")
  60. except Exception as e:
  61. print(f" Erreur génération image : {e}")