Get startedGet started for free

Agents in LangChain

1. Agents in LangChain

Hello! I'm Dilini Sumanapala,

2. Meet your instructor

an AI Engineer with a background in cognitive neuroscience, natural language applications, and Founder of Genverv Ltd. Welcome to the course!

3. An overview of agents and tools

We'll be working with agents and tools. Agents, prominent across many industries, are intelligent, autonomous systems that make decisions and take actions, much like humans. Tools are functions that agents use to perform tasks, such as data query, research reports, or data analysis.

4. Basic concepts

Before starting, you should be familiar with Large Language Models, such as ChatGPT, prompts, tools, and using APIs for data retrieval and communication. LangChain is a framework for building powerful AI agents using these components.

5. Course overview

Our agents will use tools to solve math, use APIs to search Wikipedia, and determine when to switch between LLMs or tools based on a given task.

6. Reasoning and action

Agents can reason and act. For instance, an agent could reason that it needs a currency tool to

7. Reasoning and action

search for a specific currency and return the response. This agentic framework

8. Reasoning and action (ReAct)

is called "ReAct" - short for "Reasoning" and "Action".

9. Improving response accuracy

Combining tools with agents can also improve accuracy in domains like coding and math. Here, we see an earlier version of ChatGPT having trouble with the order of operations.

10. Breaking up problems

LangChain uses specific tools to break problems into smaller steps, reducing errors. For example, we can use a tool to handle the Order of Operations in math.

11. Expanding agents with LangGraph

LangGraph can enhance tool use even further by structuring tasks in workflows called graphs.

12. Graph structures

In these graphs, tasks called "nodes" are connected by rules called "edges". For example, a database query node can link to a document retrieval node, with an edge pointing to which document is retrieved. Before building such complex workflows,

13. Create a ReAct agent

we'll create our ReAct agent let's create a basic agent that does math. The tool module imported from LangChain's core tools lets us use custom functions. ChatOpenAI imported from langchain_openai enables communication with OpenAI's language models. The create_react_agent module, imported from LangGraph's pre-built functions, helps us create a ReAct agent that can reason and use tools. Finally, the math module lets us perform standard math. Then, once our model has been defined,

14. Create a ReAct agent

by passing the model and a pre-defined tool to the create_react_agent() function. Since our tool is a basic calculator, we'll define a math query as a string. We'll pass this query, labeled "human", to the agent using the .invoke() method, storing the output as a response. We'll then use the .content attribute to print the last item in "messages" of "response" to get the agent's answer. Perfect!

15. Let's practice!

Let's give it a go!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.