Introduzione agli agenti LangChain

Sviluppare applicazioni LLM con LangChain

Jonathan Bennion

AI Engineer & LangChain Contributor

Cosa sono gli agenti?

 

Agenti: usano LLM per eseguire azioni

Strumenti: funzioni chiamate dall’agente

 

  • Ora → agente ReAct

Un agente che decide quale strumento usare in base all’input dell’utente.

Sviluppare applicazioni LLM con LangChain

Agenti ReAct

  • Reason + Act

 

Com’è il tempo a Kingston, Giamaica?

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.

Un ciclo di pensare, agire e osservare.

Sviluppare applicazioni LLM con LangChain

LangGraph

Logo di LangGraph.

 

  • Ramo di LangChain dedicato alla progettazione di sistemi di agenti
  • Sintassi unificata e indipendente dagli strumenti
  • pip install langgraph==0.2.74
Sviluppare applicazioni LLM con LangChain

Agente 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)
Sviluppare applicazioni LLM con LangChain

Agente 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.
Sviluppare applicazioni LLM con LangChain

Ayo berlatih!

Sviluppare applicazioni LLM con LangChain

Preparing Video For Download...