Wprowadzenie do agentów LangChain

Tworzenie aplikacji LLM z LangChain

Jonathan Bennion

AI Engineer & LangChain Contributor

Czym są agenty?

 

Agenty: używają LLM do podejmowania działań

Narzędzia: funkcje wywoływane przez agenta

 

  • Teraz → agent ReAct

Agent podejmuje decyzję o wyborze narzędzia na podstawie danych wejściowych użytkownika.

Tworzenie aplikacji LLM z LangChain

Agenty ReAct

  • Rozumowanie + Działanie

 

Jaka jest pogoda w Kingston na Jamajce?

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.

Pętla myślenia, działania i obserwacji.

Tworzenie aplikacji LLM z LangChain

LangGraph

Logo LangGraph.

 

  • Gałąź LangChain skupiona na projektowaniu systemów agentowych
  • Ujednolicona składnia niezależna od narzędzi
  • pip install langgraph==0.2.74
Tworzenie aplikacji LLM z 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)
Tworzenie aplikacji LLM z 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.
Tworzenie aplikacji LLM z LangChain

Czas na ćwiczenia!

Tworzenie aplikacji LLM z LangChain

Preparing Video For Download...