0
0
LangChainframework~30 mins

Custom agent logic in LangChain - Mini Project: Build & Apply

Choose your learning style9 modes available
Custom Agent Logic with LangChain
📖 Scenario: You are building a simple AI assistant using LangChain. This assistant will decide how to respond based on the user's input by using custom agent logic.
🎯 Goal: Create a LangChain agent with custom logic that chooses between two tools based on the user's question.
📋 What You'll Learn
Create a dictionary called tools with two tools: 'calculator' and 'search'
Create a variable called agent_config with a key use_search set to true
Write a function called custom_agent_logic that takes input_text and returns 'search' if agent_config['use_search'] is true and the input contains the word 'weather', otherwise returns 'calculator'
Create an agent instance called agent that uses custom_agent_logic to select the tool
💡 Why This Matters
🌍 Real World
Custom agents let you build AI assistants that decide how to respond based on the question, like choosing between searching the web or doing calculations.
💼 Career
Understanding custom agent logic is useful for AI developers building smart assistants, chatbots, or automation tools that need flexible decision-making.
Progress0 / 4 steps
1
Create the tools dictionary
Create a dictionary called tools with two entries: 'calculator' mapped to CalculatorTool() and 'search' mapped to SearchTool().
LangChain
Need a hint?

Use a Python dictionary with keys 'calculator' and 'search' and assign the respective tool instances.

2
Add agent configuration
Create a dictionary called agent_config with a key 'use_search' set to true.
LangChain
Need a hint?

Define a dictionary named agent_config with the key 'use_search' set to true.

3
Write the custom agent logic function
Write a function called custom_agent_logic that takes a parameter input_text. The function should return 'search' if agent_config['use_search'] is true and the string 'weather' is in input_text (case insensitive). Otherwise, return 'calculator'.
LangChain
Need a hint?

Use input_text.lower() to check for 'weather' ignoring case. Use an if-else statement to return the correct tool name.

4
Create the agent with custom logic
Create a variable called agent and assign it an instance of CustomAgent initialized with tools and custom_agent_logic as the logic function.
LangChain
Need a hint?

Instantiate CustomAgent with the tools dictionary and the custom_agent_logic function.