Introduction aux agents LangChain

Développer des applications LLM avec LangChain

Jonathan Bennion

AI Engineer & LangChain Contributor

Qu'est-ce qu'un agent ?

 

Agents : utilisent des LLM pour poser des actions

Outils : fonctions appelées par l'agent

 

  • Maintenant → agent ReAct

Un agent qui décide quel outil utiliser selon l'entrée de l'utilisateur ou de l'utilisatrice.

Développer des applications LLM avec LangChain

Agents ReAct

  • Reason + Act

 

Quel temps fait-il à Kingston, en Jamaïque ?

Thought: I should call Weather() to find the
weather in Kingston, Jamaica.


Act: Weather("Kingston, Jamaica")
Observe: The weather is mostly sunny with temperatures of 82°F.

Une boucle : réfléchir, agir, observer.

Développer des applications LLM avec LangChain

LangGraph

Logo de LangGraph.

 

  • Branche de LangChain axée sur la conception de systèmes d'agents
  • Syntaxe unifiée, indépendante des outils
  • pip install langgraph==0.2.74
Développer des applications LLM avec LangChain

Agent ReAct

from langgraph.prebuilt import create_react_agent
from langchain_community.agent_toolkits.load_tools import load_tools

llm = ChatOpenAI(model="gpt-4o-mini", api_key=openai_api_key) tools = load_tools(["llm-math"], llm=llm)
agent = create_react_agent(llm, tools)
messages = agent.invoke({"messages": [("human", "What is the square root of 101?")]})
print(messages)
Développer des applications LLM avec LangChain

Agent ReAct

{'messages': [
    HumanMessage(content='What is the square root of 101?', ...),
    AIMessage(content='', ..., tool_calls=[{'name': 'Calculator', 'args': {'__arg1': 'sqrt(101)'}, ...),
    ToolMessage(content='Answer: 10.04987562112089', ...),
    AIMessage(content='The square root of 101 is approximately 10.05.', ...)
]}
print(messages['messages'][-1].content)
The square root of 101 is approximately 10.05.
Développer des applications LLM avec LangChain

Passons à la pratique !

Développer des applications LLM avec LangChain

Preparing Video For Download...