Understanding LangChain Agents with Example

Saurabh Harak
3 min readMar 12, 2024

--

Imagine a situation: you urgently need to gather some specific information from either the whole boundless internet or a curated dataset. Here, intelligent agents can certainly be helpful. And not only that, but also these are things that simply look to do away with boring tasks, but they layer a smarts layer that can understand, make decisions, and act on information as it has the most meaning.

The implications behind such technology are vast, from improving customer support with instant, accurate retrieval of information to real-time power research tools that can sift through academic papers for relevant data.

Building an Agent with LangChain: A Deep Dive

Quickstart guide — LangChain: It provides a very detailed, step-to-step explanation on the setting up of an intelligent agent. It gives analysis full of examples and steps that one can act upon.

Setup: LangSmith

The first step is the setting up of LangSmith, an in-built tool within LangChain that guarantees observability and debuggability of the agents that you build. By setting specific environment variables, developers will be able to trace all the steps in LangSmith automatically, making the debugging process a lesser burden.

export LANGCHAIN_TRACING_V2="true"
export LANGCHAIN_API_KEY="<your-api-key>"

Defining Tools: Tavily and Retriever

The working core of our agent utilizes a couple of tools: Tavily for the web and a custom retriever to question any dataset. Setting up Tavily requires fetching an API key and exporting it as an environment variable.

The retriever, on the other side, is built on top of the dataset loaded into an index and uses built-in LangChain functionalities for the loading of documents and storage of vector values.

from langchain_community.tools.tavily_search import TavilySearchResults
search = TavilySearchResults()
search.invoke("what is the weather in SF")

For the retriever:

from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter

loader = WebBaseLoader("https://docs.smith.langchain.com/overview")
docs = loader.load()
documents = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200).split_documents(docs)
vector = FAISS.from_documents(documents, OpenAIEmbeddings())
retriever = vector.as_retriever()

Creating the Agent

Having defined the tools, the next task would be building the agent itself. LangChain allows the use of OpenAI Functions agents, among others. It will include the selection of the LLM, definition of the prompt, and integration of the tools.

from langchain import hub from langchain.agents import create_openai_functions_agent

llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0) prompt = hub.pull("hwchase17/openai-functions-agent")

agent = create_openai_functions_agent(llm, [search, retriever_tool], prompt)

Run the Agent

Finally, the agent is executed with an AgentExecutor that invokes the agent for running the tools depending on the input. This step shows an instance in which an agent can actually issue queries, be it for greeting the user or getting certain information.

from langchain.agents import AgentExecutor

agent_executor = AgentExecutor(agent=agent, tools=[search, retriever_tool], verbose=True)
agent_executor.invoke({"input": "hi!"})

LangChain provides an entirely new vision for the development and deployment of intelligent agents when they are used in conjunction with LangSmith for debugging and observability. Introducing the possibility of seamlessly integrating such additional tools as Tavily or Custom Retriever into the workflow, the agent uncovers absolutely fresh opportunities for developers.

Conclusion

The journey from setting up LangSmith to running an intelligent agent with LangChain conveys the flexibility and power of this platform. Breaking down the technical documentation into actionable steps and at every stage providing real-world examples showing how developers can use LangChain to build sophisticated agents capable of transforming how we interact with data online. The application areas can range from business intelligence to customer support to academic research, but will be as vast as the imagination of those who are using this technology.

--

--

Saurabh Harak

Hi, I'm a software developer/ML Engineer passionate about solving problems and delivering solutions through code. I love to explore new technologies.