Outils personnalisés pour les agents

Développement d'applications LLM avec LangChain

Jonathan Bennion

AI Engineer & LangChain Contributor

Formats d'outils

from langchain_community.agent_toolkits.load_tools import load_tools

tools = load_tools(["llm-math"], llm=llm)

print(tools[0].name)
Calculator
print(tools[0].description)
Useful for when you need to answer questions about math.
  • Utilisé par le LLM/l’agent comme contexte pour déterminer quand l'appeler
Développement d'applications LLM avec LangChain

Formats d'outils

print(tools[0].return_direct)
False
Développement d'applications LLM avec LangChain

Définition d'une fonction personnalisée

def financial_report(company_name: str, revenue: int, expenses: int) -> str:
    """Generate a financial report for a company that calculates net income."""
    net_income = revenue - expenses

    report = f"Financial Report for {company_name}:
"
    report += f"Revenue: ${revenue}
"
    report += f"Expenses: ${expenses}
"
    report += f"Net Income: ${net_income}

"

    return report

Développement d'applications LLM avec LangChain

Appel de la fonction

print(financial_report(company_name="LemonadeStand", revenue=100, expenses=50))
Financial Report for LemonadeStand:
Revenue: $100
Expenses: $50
Net Income: $50
Développement d'applications LLM avec LangChain

Des fonctions aux outils

from langchain_core.tools import tool

@tool
def financial_report(company_name: str, revenue: int, expenses: int) -> str: """Generate a financial report for a company that calculates net income.""" net_income = revenue - expenses report = f"Financial Report for {company_name}: " report += f"Revenue: ${revenue} " report += f"Expenses: ${expenses} " report += f"Net Income: ${net_income} " return report
Développement d'applications LLM avec LangChain

Analyse de notre nouvel outil

print(financial_report.name)

print(financial_report.description)
print(financial_report.return_direct)
print(financial_report.args)
financial_report

Generate a financial report for a company that calculates net income.
False
{'company_name': {'title': 'Company Name', 'type': 'string'}, 'revenue': {'title': 'Revenue', 'type': 'integer'}, 'expenses': {'title': 'Expenses', 'type': 'integer'}}
Développement d'applications LLM avec LangChain

Intégration de l'outil personnalisé

from langgraph.prebuilt import create_react_agent

llm = ChatOpenAI(model="gpt-4o-mini", api_key=openai_api_key, temperature=0)
agent = create_react_agent(llm, [financial_report])

messages = agent.invoke({"messages": [("human", "TechStack generated made $10 million with $8 million of costs. Generate a financial report.")]}) print(messages)
Développement d'applications LLM avec LangChain

Intégration de l'outil personnalisé

{'messages': [
    HumanMessage(content='TechStack generated made $10 million dollars with $8 million of...', ...),
    AIMessage(content='', ..., tool_calls=[{'name': 'financial_report',
                                            'args': {'company_name': 'TechStack',
                                                     'revenue': 10000000, 'expenses': 8000000}, ...),
    ToolMessage(content='Financial Report for TechStack:
Revenue: $10000000
Expenses...', ...),
    AIMessage(content='Here is the financial report for TechStack...', ...)
]}
Développement d'applications LLM avec LangChain

Sorties de l'outil

print(messages['messages'][-1].content)
Here is the financial report for TechStack:
- Revenue: $10,000,000
- Expenses: $8,000,000
- Net Income: $2,000,000
Financial Report for TechStack:
Revenue: $10000000
Expenses: $8000000
Net Income: $2000000
Développement d'applications LLM avec LangChain

Passons à la pratique !

Développement d'applications LLM avec LangChain

Preparing Video For Download...