← Writing
AI & Generative AI

Unpacking Agent with LangChain

Data Mastery Series — Episode 40: LangChain Website (Part 15)

30 Dec 202414 min readLangChainLangGraphAI AgentDashboard
LangChain Series · Part 3 of 19

Unpacking Agent with LangChain

Data Mastery Series — Episode 40: LangChain Website (Part 15)

Connect with me and follow our journey: Linkedin, Facebook


Welcome back to the Data Mastery Series! In this episode, we’re shifting gears from retrievers to the exciting world of agents in LangChain. If you’ve been following along, you’ll know we’ve covered everything from the basics to advanced techniques:

Note:This post is inspired by insights from the official LangChain documentation and represents my practical learning journey.

In this episode, we’ll build a simple agent using tools for both online search and local data retrieval. This highlights the power of agents and their real-world applications.

Agents, Tools, and Composition

Agent

At its heart, an agent is a language model that decides which steps to take to achieve a specific goal. Think of it as the brain of the operation. It uses tools to interact with the outside world based on its understanding of your request. Key parts of an agent include:

  • Executor: Runs the agent, repeating the process until the task is complete.
  • Actions: Tasks selected by the agent, such as using a tool or providing a response.
  • Outputs: The final result or an intermediate action to continue the process.

Tools

These are interfaces that allow agents to interact with the world. They could be anything from a database connector to a search engine API. Each tool is defined by:

  • Name: What it’s called.
  • Description: How it works or what it does.
  • Input Schema: Defines what kind of input it accepts.
  • Function: The actual operation it performs.

Composition

Agents and tools combine to form decision-making systems capable of execution. Together, they dynamically handle tasks like retrieving information, running calculations, or processing data:

  • Agents: Decide what to do.
  • Tools: Execute those decisions.
  • Executors: Orchestrate the interaction between agents and tools.

Quickstart: Building Our Agent

Let’s build an agent that uses two tools: Tavily Search and Retriever.

Example - Tavily Search and Retriever (no memory)

#####################################################

search = TavilySearchResults()
search.invoke("what is the weather in SF")

Output of Step 1:

'''
[{'url': 'https://www.weatherapi.com/',
'content': "{'location': {'name': 'San Francisco', 'region': 'California', 'country': 'United States of America', 'lat': 37.775, 'lon': -122.4183, 'tz_id': 'America/Los_Angeles', 'localtime_epoch': 1735530235, 'localtime': '2024-12-29 19:43'}, 'current': {'last_updated_epoch': 1735529400, 'last_updated': '2024-12-29 19:30', 'temp_c': 13.3, 'temp_f': 55.9, 'is_day': 0, 'condition': {'text': 'Partly cloudy', 'icon': '//cdn.weatherapi.com/weather/64x64/night/116.png', 'code': 1003}, 'wind_mph': 7.4, 'wind_kph': 11.9, 'wind_degree': 279, 'wind_dir': 'W', 'pressure_mb': 1023.0, 'pressure_in': 30.22, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 75, 'cloud': 25, 'feelslike_c': 12.3, 'feelslike_f': 54.2, 'windchill_c': 10.3, 'windchill_f': 50.6, 'heatindex_c': 11.6, 'heatindex_f': 52.9, 'dewpoint_c': 10.4, 'dewpoint_f': 50.8, 'vis_km': 16.0, 'vis_miles': 9.0, 'uv': 0.0, 'gust_mph': 11.3, 'gust_kph': 18.1}}"},
{'url': 'https://www.peoplesweather.com/weather/San+Francisco/?date=2024-12-30', <...>
'''

#################################################

Step 2: Defining 2nd Tools: Retriever

Load data from URL

loader = WebBaseLoader("https://docs.smith.langchain.com/overview")
docs = loader.load()

Split the documents into chunks

documents = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200).split_documents(docs)

Create a vector store and retriever

vector = FAISS.from_documents(documents, OpenAIEmbeddings(api_key=OPENAI_API_KEY))
retriever = vector.as_retriever()
retriever.invoke("how to upload a dataset")[0]

Output of Step 2:

'''
Document(id='92b6bfb6-dd16-4158-b048-90cf83e5050a', metadata={'source': 'https://docs.smith.langchain.com/overview', 'title': 'Get started with LangSmith | \uf8ffü¶úÔ∏è\uf8ffüõ†Ô∏è LangSmith', 'description': 'LangSmith is a platform for building production-grade LLM applications.', 'language': 'en'}, page_content='Run the evaluationexperiment_results = client.evaluate( dummy_app, # Your AI system goes here data=dataset, # The data to predict and grade over evaluators=[exact_match], # The evaluators to score the results experiment_prefix="sample-experiment", # The name of the experiment metadata={"version": "1.0.0", "revision_id": "beta"}, # Metadata about the experiment max_concurrency=4, # Add concurrency.)# Analyze the results via the UI or programmatically# If you have 'pandas' installed you can view the results as a# pandas DataFrame by uncommenting below:# experiment_results.to_pandas()import { Client } from "langsmith";import { EvaluationResult, evaluate } from "langsmith/evaluation";const client = new Client();// Define dataset: these are your test casesconst datasetName = "Sample Dataset";const dataset = await client.createDataset(datasetName, { description: "A sample dataset in LangSmith.",});await client.createExamples({ inputs: [ { postfix: "to LangSmith" },')
'''

create retriever tool

retriever_tool = create_retriever_tool(
retriever,
"langsmith_search",
"Search for information about LangSmith. For any questions about LangSmith, you must use this tool!",
)

#######################################

Step 3: Combining the Tools

Define list of tools

tools = [search, retriever_tool]

######################################

Step 4: Creating the Agent

Initialize the chat model

llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0, api_key=OPENAI_API_KEY)

Get the prompt for the agent

prompt = hub.pull("hwchase17/openai-functions-agent")
prompt.messages

Initialize the agent

agent = create_tool_calling_agent(llm, tools, prompt)

Initialize the agent executor

agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

#####################################

Step 5: Running the Agent

1st test

agent_executor.invoke({"input": "hi!"})

Output of 1st test:

'''

Entering new AgentExecutor chain...
Hello! How can I assist you today?

Finished chain.
{'input': 'hi!', 'output': 'Hello! How can I assist you today?'}
'''

2nd test

agent_executor.invoke({"input": "how can langsmith help with testing?"})

Output of 2nd test:

'''

Entering new AgentExecutor chain...

Invoking: langsmith_search with {'query': 'how can LangSmith help with testing'}

Get started with LangSmith | 🦜️🛠️ LangSmith

Skip to main contentLearn the essentials of LangSmith in the new Introduction to LangSmith course! Enroll for free. API ReferenceRESTPythonSearchRegionUSEUGo to AppQuick StartObservabilityEvaluationPrompt EngineeringDeployment (LangGraph Platform)AdministrationSelf-hostingPricingReferenceCloud architecture and scalabilityAuthz and AuthnAuthentication methodsdata_formatsEvaluationDataset transformationsRegions FAQsdk_referenceQuick StartOn this pageGet started with LangSmith
LangSmith is a platform for building production-grade LLM applications.

<...>

LangSmith is a platform for building production-grade LLM applications that allows you to closely monitor and evaluate your application, ensuring you can ship quickly and with confidence.

Finished chain.
{'input': 'how can langsmith help with testing?',
'output': "LangSmith can help with testing by providing the following capabilities:\n\n1. Trace LLM Applications: Gain visibility into LLM (Large Language Model) calls and other parts of your application's logic. This allows you to track the behavior of your application during testing.\n\n2. Evaluate Performance: Compare results across models, prompts, and architectures to identify what works best. This evaluation helps in assessing the performance of your application during testing.\n\n3. Improve Prompts: Quickly refine prompts to achieve more accurate and reliable results. This feature helps in enhancing the prompts used in your application testing.\n\nLangSmith is a platform for building production-grade LLM applications that allows you to closely monitor and evaluate your application, ensuring you can ship quickly and with confidence."}
'''

3rd test

agent_executor.invoke({"input": "whats the weather in sf?"})

Output of 2nd test:

'''

Entering new AgentExecutor chain...

Invoking: tavily_search_results_json with {'query': 'weather in San Francisco'}

[{'url': 'https://www.weatherapi.com/', 'content': "{'location': {'name': 'San Francisco', 'region': 'California', 'country': 'United States of America', 'lat': 37.775, 'lon': -122.4183, 'tz_id': 'America/Los_Angeles', 'localtime_epoch': 1735530997, 'localtime': '2024-12-29 19:56'}, 'current': {'last_updated_epoch': 1735530300, 'last_updated': '2024-12-29 19:45', 'temp_c': 13.3, 'temp_f': 55.9, 'is_day': 0, 'condition': {'text': 'Partly cloudy', 'icon': '//cdn.weatherapi.com/weather/64x64/night/116.png', 'code': 1003}, 'wind_mph': 7.4, 'wind_kph': 11.9, 'wind_degree': 279, 'wind_dir': 'W', 'pressure_mb': 1023.0, 'pressure_in': 30.22, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 75, 'cloud': 25, 'feelslike_c': 12.3, 'feelslike_f': 54.2, 'windchill_c': 10.3, 'windchill_f': 50.6, 'heatindex_c': 11.6, 'heatindex_f': 52.9, 'dewpoint_c': 10.4, 'dewpoint_f': 50.8, 'vis_km': 16.0, 'vis_miles': 9.0, 'uv': 0.0, 'gust_mph': 11.3, 'gust_kph': 18.1}}"}, {'url': 'https://www.peoplesweather.com/weather/San+Francisco/?date=2024-12-30', 'content': 'Weather for San Francisco | People°s Weather Home Weather News MyPhoto Competitions Contact Us Home Weather Forecast News & Highlights MyPhoto Competitions Contact Us Get the weather for Johannesburg Weather for San Francisco Weather United States San Francisco Monday 30 December 2024 8°CFeels like: 8°CNW / 7km/hLight BreezePartly Cloudy. Cool Pressure1024mbHumidity86%Rain0%Cloud Cover27%Dew Point6°C This Afternoon12°CN / 7km/hLight BreezeOvercast. Cool Tonight10°CWNW / 11km/hLight BreezePartly Cloudy. Cool 6 Day Forecast for San Francisco Weather Detailed Forecast SA National Parks iSimangaliso Popular Submit your Photo Contact us Careers News Room Newsletter Subscribe Now Terms of Use Privacy Sitemap © 2007-2024 People°s Weather Pty. Ltd., All rights reserved.'}, {'url': 'https://weathershogun.com/weather/usa/ca/san-francisco/480/december', 'content': 'December 2024 Weather in San Francisco, CA San Francisco, CA Today Hourly 7 days 30 days December December 2024 Weather | San Francisco, California Today 12/12 Day 57° Night 52° 100Fri 12/13 Day 61° Night 54° 55Sat 12/14 Day 59° Night 45° 100Sun 12/15 Day 57° Night 45° 0Mon 12/16 Day 57° Night 46° 83Tue 12/17 Day 61° Night 48° 21Wed 12/18 Day 59° Night 46° 0Thu 12/19 Day 59° Night 46° 0Fri 12/20 Day 61° Night 52° 0Sat 12/21 Day 61° Night 52° 78Sun 12/22 Day 61° Night 52° 5Mon 12/23 Day 63° Night 50° 1Tue 12/24 Day 61° Night 52° 0Wed 12/25 Day 57° Night 46° 20Thu 12/26 Day 54° Night 45° 25Fri 12/27 Day 55° Night 45° 25Sat 12/28 Day 57° Night 48° 25Sun 12/29 Day 59° Night 50° 25Mon 12/30 Day 59° Night 50° 25Tue 12/31 Day 57° Night 48° 25 Today 7 days 30 days'}, {'url': 'https://www.weathertab.com/en/d/e/12/united-states/california/san-francisco/', 'content': 'San Francisco, California Daily Weather Forecast for December 2024, derived from a dynamic long-range model, offers daily predictions for temperature and rainfall, grounded in over 50 years of privately funded research. ... San Francisco, CA Daily Weather Forecast for December 2024 ... 30 Mon. 31% Sunrise 7:24AM. Sunset 5:00PM. New Moon'}, {'url': 'https://www.weathertab.com/en/c/e/12/united-states/california/san-francisco/', 'content': 'San Francisco, CA Weather Forecast December 2024: Daily Highs/Lows & Rain Trends Detailed San Francisco Weather Forecast for December 2024: Daily Precipitation Risks and Temperature Trends Discover daily high and low temperatures, precipitation risks, and temperature trends with a level of precision and insight unmatched by traditional methods. Low risk of rain/snow.Transition Day – Start or End of a Risky PeriodMedium risk of rain/snow.High risk of rain/snow.%Forecast risk of rain/snow.Click or Tap on any day for a detailed forecast. San Francisco Weather Forecast for Dec 2024 - Risk of Rain Graph San Francisco High Temperature Trends for December 2024 High Temperature Graph: Tracks daily forecasted highs. San Francisco Average Temperature Trends for December 2024 San Francisco Low Temperature Trends for December 2024 Low Temperature Graph: Displays daily forecasted lows. Temperature Forecast'}]The current weather in San Francisco is partly cloudy with a temperature of 55.9°F (13.3°C). The wind is blowing from the west at 11.9 km/h. The humidity is at 75%, and the visibility is 9.0 miles.

Finished chain.
{'input': 'whats the weather in sf?',
'output': 'The current weather in San Francisco is partly cloudy with a temperature of 55.9°F (13.3°C). The wind is blowing from the west at 11.9 km/h. The humidity is at 75%, and the visibility is 9.0 miles.'}
'''

During the first test (‘hi!’), the agent responds directly without utilizing tools. However, in the second and third tests, the agent dynamically selects the appropriate tools to address the queries.

Adding Memory to the Agent

Currently, the agent is stateless, meaning it doesn’t retain previous interactions. To enable memory, pass a chat_history variable to the invoke() method.

  • Example: Manual Memory

Example - Adding Memory to the Agent (Manual Memory)

first interaction

agent_executor.invoke({"input": "hi! my name is bob", "chat_history": []})

Output of first interaction

'''

Entering new AgentExecutor chain...
Hello Bob! How can I assist you today?

Finished chain.
{'input': 'hi! my name is bob',
'chat_history': [],
'output': 'Hello Bob! How can I assist you today?'}
'''

Subsequent interaction

agent_executor.invoke(
{
"chat_history": [
HumanMessage(content="hi! my name is bob"),
AIMessage(content="Hello Bob! How can I assist you today?"),
],
"input": "what's my name?",
}
)

Output of Subsequent interaction:

'''

Entering new AgentExecutor chain...
Your name is Bob. How can I help you, Bob?

Finished chain.
{'chat_history': [HumanMessage(content='hi! my name is bob', additional_kwargs={}, response_metadata={}),
AIMessage(content='Hello Bob! How can I assist you today?', additional_kwargs={}, response_metadata={})],
'input': "what's my name?",
'output': 'Your name is Bob. How can I help you, Bob?'}
'''

  • Example: Automatic Memory Management

Example - Adding Memory to the Agent (Automatic Memory Management)

first interaction

message_history = ChatMessageHistory()

agent_with_chat_history = RunnableWithMessageHistory(
agent_executor,
lambda session_id: message_history, # In real application, the session_id is used
input_messages_key="input",
history_messages_key="chat_history",
)

agent_with_chat_history.invoke(
{"input": "hi! I'm bob"},
config={"configurable": {"session_id": ""}}, # In real application, the session_id is used
)

Output of first interaction

'''

Entering new AgentExecutor chain...
Hello Bob! How can I assist you today?

Finished chain.
{'input': "hi! I'm bob",
'chat_history': [],
'output': 'Hello Bob! How can I assist you today?'}
'''

Subsequent interaction

agent_with_chat_history.invoke(
{"input": "what's my name?"},
config={"configurable": {"session_id": ""}}, # In real application, the session_id is used
)

Output of Subsequent interaction:

'''

Entering new AgentExecutor chain...
Your name is Bob! How can I help you, Bob?

Finished chain.
{'input': "what's my name?",
'chat_history': [HumanMessage(content="hi! I'm bob", additional_kwargs={}, response_metadata={}),
AIMessage(content='Hello Bob! How can I assist you today?', additional_kwargs={}, response_metadata={})],
'output': 'Your name is Bob! How can I help you, Bob?'}
'''

Key Takeaways

In this episode, we explored the basics of LangChain Agents, their composition, and real-world applications:

  1. Agents: Dynamically decide actions using language models.
  2. Tools: Execute actions to retrieve or process information.
  3. Composition: Combine agents and tools to build intelligent systems.
  4. Memory: Enable agents to remember context for more engaging and interactive conversations.

In Episode 41, we’ll dive deeper into agent concepts and agent types. Stay tuned for more insights into building intelligent data systems! 🚀


Data Science Explore the world of data science with Donato_Story

Dashboard Discover the power of data visualization with Donato_Story

Donato_Journey Join me on my journey (Thai version)

Course_Review Discover the training courses with Donato_Story (Thai version)

Let’s Connect!

Your thoughts and feedback are invaluable. Feel free to share them in the comments or connect with me on

Originally published on Medium

Related