Integration LangChain¶
Cette page presente l'integration complete de PIIGhost dans un agent LangGraph, basee sur l'exemple disponible dans examples/graph/.
Installation¶
Pour utiliser le middleware LangChain, installez les dependances supplementaires :
Dependance optionnelle
PIIAnonymizationMiddleware importe langchain au moment de son instanciation. Si langchain n'est pas installe, une ImportError explicite est levee avec le message "You must install piighost[langchain] for use middleware".
Structure de l'integration¶
flowchart TB
classDef model fill:#A5D6A7,stroke:#2E7D32,color:#000
classDef comp fill:#90CAF9,stroke:#1565C0,color:#000
classDef mw fill:#BBDEFB,stroke:#1565C0,color:#000
classDef agent fill:#FFF9C4,stroke:#F9A825,color:#000
MODEL["`**Modèle GLiNER2**
_fastino/gliner2-multi-v1_`"]:::model
DET["`**Gliner2Detector**
_encapsule le modèle NER_`"]:::comp
PIPE["`**ThreadAnonymizationPipeline**
_étend AnonymizationPipeline_
_contient ConversationMemory_`"]:::comp
MW["`**PIIAnonymizationMiddleware**
_hooks LangChain_`"]:::mw
AGENT["`**create_agent(middleware=[...])**
_point d'entrée LangGraph_`"]:::agent
MODEL -->|encapsulé par| DET
DET -->|injecté dans| PIPE
PIPE -->|passé à| MW
MW -->|enregistré auprès de| AGENT
Exemple complet¶
Comment fonctionne le middleware¶
Le PIIAnonymizationMiddleware intercepte chaque tour de l'agent en trois points :
abefore_model avant le LLM¶
flowchart LR
classDef user fill:#A5D6A7,stroke:#2E7D32,color:#000
classDef mw fill:#BBDEFB,stroke:#1565C0,color:#000
classDef llm fill:#FFF9C4,stroke:#F9A825,color:#000
U["`**Utilisateur**
_'Envoie un email à Patrick à Paris'_`"]:::user
M["`**Middleware**
_détection NER via_
_pipeline.anonymize()_`"]:::mw
L["`**Le LLM voit**
_'Envoie un email à <<PERSON_1>>_
_à <<LOCATION_1>>'_`"]:::llm
U --> M --> L
awrap_tool_call autour des outils¶
flowchart TB
classDef tool fill:#A5D6A7,stroke:#2E7D32,color:#000
classDef mw fill:#BBDEFB,stroke:#1565C0,color:#000
classDef llm fill:#FFF9C4,stroke:#F9A825,color:#000
L1["`**Le LLM appelle**
_send_email(to='<<PERSON_1>>', ...)_`"]:::llm
M1["`**Middleware**
_désanonymise les arguments_`"]:::mw
T1["`**L'outil reçoit**
_to='Patrick' (vraie valeur)_`"]:::tool
T2["`**L'outil retourne**
_'Email envoyé à Patrick.'_`"]:::tool
M2["`**Middleware**
_réanonymise la réponse_`"]:::mw
L2["`**Le LLM voit**
_'Email envoyé à <<PERSON_1>>.'_`"]:::llm
L1 --> M1 --> T1 --> T2 --> M2 --> L2
aafter_model après le LLM¶
flowchart LR
classDef user fill:#A5D6A7,stroke:#2E7D32,color:#000
classDef mw fill:#BBDEFB,stroke:#1565C0,color:#000
classDef llm fill:#FFF9C4,stroke:#F9A825,color:#000
L["`**Le LLM répond**
_'C'est fait ! Email envoyé à <<PERSON_1>>.'_`"]:::llm
M["`**Middleware**
_désanonymise tous les messages_`"]:::mw
U["`**L'utilisateur voit**
_'C'est fait ! Email envoyé à Patrick.'_`"]:::user
L --> M --> U
Utiliser l'agent¶
main.py
import asyncio
async def main():
response = await graph.ainvoke({
"messages": [{"role": "user", "content": "Envoie un email a Patrick a Paris"}]
})
print(response["messages"][-1].content)
# C'est fait ! Email envoye a Patrick.
asyncio.run(main())