Introducere în agenții LangChain

Developing LLM Applications with LangChain

Jonathan Bennion

AI Engineer & LangChain Contributor

Ce sunt agenții?

 

Agenți: utilizează LLM-uri pentru a efectua acțiuni

Instrumente: funcții apelate de agent

 

  • Acum → Agent ReAct

Un agent care decide ce instrument să utilizeze în funcție de cererea utilizatorului.

Developing LLM Applications with LangChain

Agenți ReAct

  • Raționează + Acționează

 

Cum este vremea în Kingston, Jamaica?

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 ciclu de gândire, acțiune și observare.

Developing LLM Applications with LangChain

LangGraph

Logo LangGraph.

 

  • Ramură LangChain dedicată proiectării sistemelor de agenți
  • Sintaxă unificată, independentă de instrumente
  • pip install langgraph==0.2.74
Developing LLM Applications with 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)
Developing LLM Applications with 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.
Developing LLM Applications with LangChain

Să exersăm!

Developing LLM Applications with LangChain

Preparing Video For Download...