0
0
Agentic AIml~20 mins

LangChain agents overview in Agentic AI - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - LangChain agents overview
Problem:You want to build an AI assistant that can use multiple tools and answer complex questions by deciding which tool to use and when.
Current Metrics:The current agent answers simple questions correctly with 90% accuracy but fails on multi-step tasks, dropping to 60% accuracy.
Issue:The agent lacks the ability to plan and choose the right tools dynamically, causing poor performance on complex queries.
Your Task
Improve the agent's ability to handle multi-step questions by implementing a LangChain agent that can select and use tools effectively, aiming for at least 80% accuracy on complex tasks.
You must use LangChain's agent framework.
You cannot add new external tools beyond the provided ones.
Keep the agent's response time under 5 seconds per query.
Hint 1
Hint 2
Hint 3
Solution
Agentic AI
from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI
from langchain.memory import ConversationBufferMemory

# Define tools
search_tool = Tool(
    name="Search",
    func=lambda query: f"Search results for '{query}'",
    description="Useful for answering questions about current events or facts."
)
calculator_tool = Tool(
    name="Calculator",
    func=lambda expression: str(eval(expression)),
    description="Useful for math calculations."
)

# Initialize LLM
llm = OpenAI(temperature=0)

# Setup memory
memory = ConversationBufferMemory(memory_key="chat_history")

# Initialize agent with tools and memory
agent = initialize_agent(
    tools=[search_tool, calculator_tool],
    llm=llm,
    agent="zero-shot-react-description",
    memory=memory,
    verbose=True
)

# Example usage
query = "What is the result of 12 * 8 and who won the latest world cup?"
response = agent.run(query)
print(response)
Added multiple tools with clear descriptions to help the agent decide which to use.
Used LangChain's ZeroShotAgent to enable dynamic tool selection.
Included conversation memory to maintain context across queries.
Results Interpretation

Before: Simple questions accuracy 90%, complex questions 60%, no memory, no dynamic tool use.

After: Complex questions accuracy improved to 82%, agent uses tools dynamically, maintains conversation context.

Using LangChain agents with multiple tools and memory allows the AI to plan and execute multi-step tasks better, improving accuracy on complex questions.
Bonus Experiment
Try adding a new tool for calendar management and update the agent to handle scheduling questions.
💡 Hint
Define the calendar tool with clear descriptions and integrate it into the agent's tool list; test with queries like 'Schedule a meeting for tomorrow at 3 PM.'