Thuta Learning
ProjectsAIadvanced

Mini Project: Document Chatbot – Part 3 (Adding an Agent Tool & Polish)

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Apply Mini Project: Document Chatbot – Part 3 (Adding an Agent Tool & Polish) in a hands-on project
  • Write and run the code yourself
  • Build out an entire project step by step

Let's think this through for a moment

Here we'll apply the Agents and Tools concepts from the Advanced chapter. We'll wrap Part 2's conversational RAG chain as a Tool called "document_search", add one more tool for questions the document can't answer (like a simple calculation), and build an Agent around both. The Agent can decide for itself which tool to use and when, based on the user's question. Finally, drawing on the production-observability-evaluation chapter's lesson, we'll polish the project into a usable app with try/except error handling and a simple CLI loop. By the end of this stage, you'll have a chatbot that can search document data, remember follow-up context via memory, and choose between tools through an agent.

Let's build it

Wrap Part 2's conv_chain as a function, then create a tool with Tool(name="document_search", func=..., description="Use this to answer questions about the user's document data"). Add one more simple tool, like a word counter/calculator. Add both tools to an agent with initialize_agent (or create_react_agent), setting verbose=True and a max_iterations value. Add try/except error handling, and finish it off as a simple CLI with a while True loop that keeps reading user input and stops when the user types "exit".

Code Example

python
from langchain.agents import Tool, initialize_agent, AgentType

def document_qa_func(question: str) -> str:
    result = conv_chain.invoke({"question": question})
    return result["answer"]

def word_count_func(text: str) -> str:
    return f"Word count: {len(text.split())}"

tools = [
    Tool(
        name="document_search",
        func=document_qa_func,
        description="User ရဲ့ ကိုယ်ပိုင် document ထဲက data နှင့် ပတ်သက်တဲ့ မေးခွန်းများကို ဖြေရန် အသုံးပြုပါ",
    ),
    Tool(
        name="word_counter",
        func=word_count_func,
        description="ပေးထားသော text ရဲ့ word count ကို ရေတွက်ရန် အသုံးပြုပါ",
    ),
]

agent = initialize_agent(
    tools=tools,
    llm=llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
    max_iterations=4,
)

def main():
    print("Document Chatbot (ပြီးဆုံးရန် 'exit' ရိုက်ပါ)")
    while True:
        user_input = input("You: ")
        if user_input.strip().lower() == "exit":
            break
        try:
            response = agent.invoke({"input": user_input})
            print("Bot:", response["output"])
        except Exception as e:
            print("Error တစ်ခု ဖြစ်ပွားခဲ့သည်:", e)

if __name__ == "__main__":
    main()
You should see
You'll end up with a CLI chatbot where the agent itself decides whether to use the document_search tool for questions about the document's data, or the word_counter tool for things like word counts, and answers accordingly.

5-Minute Try-It

In 5 minutes, write a third tool (for example, a current_date tool that returns today's date) and add it to the agent's tools list.

A Quick Warning

In a production app, never trust an agent's output directly with the user — always factor in error handling, max_iterations, and trace logging with an observability tool like LangSmith.

Easy traps

  • Writing an unclear tool description, causing the agent to pick the wrong tool and give incorrect answers
  • Forgetting to set max_iterations, letting the agent loop for too long and driving up API cost

Now Try It Yourself

In 5 minutes, write a third tool (for example, a current_date tool that returns today's date) and add it to the agent's tools list.

You'll know it worked when: You'll end up with a CLI chatbot where the agent itself decides whether to use the document_search tool for questions about the document's data, or the word_counter tool for things like word counts, and answers accordingly.

Mini Project: Document Chatbot – Part 3 (Adding an Agent Tool & Polish) | Thuta Learning