API d'outils LangChain
Décorateur @tool
| Paramètre | Type | Valeur par défaut | Description |
|---|---|---|---|
| args_schema | BaseModel ou None | Aucune | Modèle de validation des paramètres. S'il n'est pas fourni, il est généré automatiquement à partir de la signature de la fonction. |
| return_direct | bool | False | Indique s'il faut retourner directement (ignorer la réflexion ultérieure du modèle) |
| name | str ou None | Nom de la fonction | Nom de l'outil |
| description | str ou None | Docstring de la fonction | Description de l’outil |
Attributs et méthodes clés de BaseTool
| Attribut/Méthode | Description |
|---|---|
| name | Nom de l'outil (string) |
| description | Description de l'outil (string) |
| args_schema | Modèle Pydantic de paramètre |
| return_direct | Indique s'il faut retourner directement (bool) |
| invoke(input) | Appeler l'outil ; input est le dictionnaire de paramètres |
| ainvoke(input) | Appeler l'outil de manière asynchrone |
Marqueurs d'injection de dépendances
| Marker | Objectif | Utilisation |
|---|---|---|
| InjectedState | Injecter l'état de l'Agent | Annotated[dict, InjectedState] |
| InjectedStore | Injecter le store inter-session | Annotated[BaseStore, InjectedStore()] |
| InjectedToolCallId | Injecter l'ID d'appel d'outil | Annotated[str, InjectedToolCallId] |
| InjectedToolArg | Marqueur d'injection générique | Annotated[T, InjectedToolArg] |
Exemples d'utilisation courants
Exemples
from langchain.tools import tool, InjectedState, InjectedStore, ToolException
from typing import Annotated
from langgraph.store.base import BaseStore
# Outil de base
@tool
def my_tool(param: str) -> str:
"""Description de l'outil"""
return f"Résultat : {param}"
# Avec validation des paramètres
from pydantic import BaseModel, Field
class MyInput(BaseModel):
param: str = Field(description="Description du paramètre", min_length=1)
@tool(args_schema=MyInput)
def validated_tool(param: str) -> str:
return param
# Retour direct
@tool(return_direct=True)
def query_tool(query: str) -> str:
return f"Résultat : {query}"
# Injecter l'état
@tool
def stateful_tool(
param: str,
state: Annotated[dict, InjectedState],
) -> str:
return f"Nombre de messages: {len(state.get('messages', []))}"
# Injecter le Store
@tool
def store_tool(
key: str,
store: Annotated[BaseStore, InjectedStore()],
) -> str:
item = store.get(("ns",), key)
return str(item.value if item else "None")
# Gestion des exceptions
@tool
def safe_tool(param: int) -> str:
if param < 0:
raise ToolException(f"Le paramètre doit être un nombre positif: {param}")
return f"OK: {param}"
from typing import Annotated
from langgraph.store.base import BaseStore
# Outil de base
@tool
def my_tool(param: str) -> str:
"""Description de l'outil"""
return f"Résultat : {param}"
# Avec validation des paramètres
from pydantic import BaseModel, Field
class MyInput(BaseModel):
param: str = Field(description="Description du paramètre", min_length=1)
@tool(args_schema=MyInput)
def validated_tool(param: str) -> str:
return param
# Retour direct
@tool(return_direct=True)
def query_tool(query: str) -> str:
return f"Résultat : {query}"
# Injecter l'état
@tool
def stateful_tool(
param: str,
state: Annotated[dict, InjectedState],
) -> str:
return f"Nombre de messages: {len(state.get('messages', []))}"
# Injecter le Store
@tool
def store_tool(
key: str,
store: Annotated[BaseStore, InjectedStore()],
) -> str:
item = store.get(("ns",), key)
return str(item.value if item else "None")
# Gestion des exceptions
@tool
def safe_tool(param: int) -> str:
if param < 0:
raise ToolException(f"Le paramètre doit être un nombre positif: {param}")
return f"OK: {param}"