Building Intelligent Workflows: A Deep Dive into Langchain’s Agent Framework

Arunachalam J
4 min readApr 12, 2024

--

Agent Framework in Langchain

Agent

Langchain, an open-source framework, has emerged as a powerful tool for developing applications powered by Large Language Models (LLMs). While LLMs excel at tasks like text generation and translation, Langchain’s agent framework takes it a step further. It allows you to create intelligent agents that can not only understand your instructions but also take concrete actions to fulfill them.

This article delves into the core concepts of Langchain’s agent framework, equipping you to build robust and interactive AI workflows.

At its heart, a Langchain agent follows a well-defined architecture:

  1. Input Reception: The agent receives user input in natural language. This could be a question, a command, or a request.
  2. Processing with LLM: The agent leverages the power of an LLM to understand the intent behind the input. The LLM analyzes the text and determines the most appropriate course of action.
  3. Plan Execution: Based on the LLM’s analysis, the agent formulates a plan to achieve the desired outcome. This plan may involve interacting with external tools or services.
  4. Output Delivery: Finally, the agent delivers the results of its execution back to the user. This could be a response generated by the LLM, data retrieved from an external source, or a combination of both.

Key Components of an Agent:

Langchain’s agent framework provides several key components to facilitate the creation of intelligent agents:

  • LLMs: These are the engines that power the agent’s understanding and decision-making. Langchain integrates with various LLMs, allowing you to choose the one best suited for your specific task.
  • Prompts: These are carefully crafted instructions that guide the LLM towards the desired outcome. The agent framework provides tools to construct effective prompts based on the user input.
  • Tools: Langchain offers a rich ecosystem of pre-built tools that agents can leverage. These tools encompass functionalities like web scraping, data manipulation, and interaction with APIs. This allows agents to extend their capabilities beyond simple LLM interactions.
  • AgentExecutor: This crucial component takes the formulated plan from the agent and executes it. It handles the interaction with external tools and ensures the seamless execution of the workflow.

Unleashing Agent Power with Design Patterns:

Langchain’s design patterns empower you to build even more sophisticated agents. Here are two notable ones:

  • Reflexive Pattern: This pattern enables agents to continuously learn and improve their outputs. Agents can analyze their past performance, incorporate user feedback, and refine their responses over time.
  • Tool Use Pattern: This pattern allows agents to utilize external tools to complete tasks. By integrating with various tools, agents can access and process information from diverse sources, significantly expanding their capabilities.

Building Your First Langchain Agent:

It is relatively easy to create and use the langchain frameworks to create a robust agent leveraging state of an art LLM models and tools.

We will start with installing the dependencies:

Run below pip command to install langchain and tavilly

%pip install --upgrade --quiet  langchain-openai tavily-python

Once the modules are installed we can import the module to our project, below code does exactly that

from langchain import hub
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_openai import ChatOpenAI

To create an agent we need to set up its dependencies namely appropriate prompts, llm model set up and agent creation with tools.

# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/openai-tools-agent")

# Choose the LLM that will drive the agent
# Only certain models support this
llm = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0)

# Construct the OpenAI Tools agent
agent = create_openai_tools_agent(llm, tools, prompt)

Now we have successfully created a OpenAI tools agent we should set up agent executor to efficiently run it.

# Create an agent executor by passing in the agent and tools
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

agent_executor.invoke({"input": "what is LangChain?"})

Below is the result from above invocation



> Entering new AgentExecutor chain...

Invoking: `tavily_search_results_json` with `{'query': 'LangChain'}`


[{'url': 'https://www.ibm.com/topics/langchain', 'content': 'LangChain is essentially a library of abstractions for Python and Javascript, representing common steps and concepts LangChain is an open source orchestration framework for the development of applications using large language models other LangChain features, like the eponymous chains. LangChain provides integrations for over 25 different embedding methods, as well as for over 50 different vector storesLangChain is a tool for building applications using large language models (LLMs) like chatbots and virtual agents. It simplifies the process of programming and integration with external data sources and software workflows. It supports Python and Javascript languages and supports various LLM providers, including OpenAI, Google, and IBM.'}]LangChain is an open source orchestration framework for the development of applications using large language models. It is essentially a library of abstractions for Python and Javascript, representing common steps and concepts. LangChain simplifies the process of programming and integration with external data sources and software workflows. It supports various large language model providers, including OpenAI, Google, and IBM. You can find more information about LangChain on the IBM website: [LangChain - IBM](https://www.ibm.com/topics/langchain)

> Finished chain.

We can clearly see how our agent is able to use the tool to surf through internet to fetch relevant information and generate accurately.

Conclusion:

By leveraging Langchain’s agent framework and design patterns, you can create powerful AI workflows that understand your needs, take action in the real world, and continuously learn from their interactions. This paves the way for a future where AI seamlessly integrates into our lives, assisting us in complex tasks and constantly evolving alongside us.

Reference:

Langchain: https://python.langchain.com/docs/modules/agents

Deeplearning ai: https://www.deeplearning.ai/the-batch/how-agents-can-improve-llm-performance/

--

--