🧪 Lesson: Testing the Agent
Module: Building a Basic AI Agent
🎯 Lesson Objective
By the end of this lesson, you’ll be able to:
-
Interact with your AI agent through code or a console
-
Provide user input and receive responses from the model
-
Evaluate how the agent handles various question types
-
Debug and improve agent responses through iteration
🧠 Why Testing Matters
Once your agent is initialized (using an LLM and optionally a prompt template or tools), it’s essential to:
-
✅ Ensure the agent responds correctly
-
🔍 Diagnose whether issues are with prompts, logic, or tools
-
🔁 Iteratively refine how the agent behaves before UI or deployment
✅ Step-by-Step: Test Your Agent via Terminal
🧩 Prerequisites
Make sure you have already:
-
Initialized your LLM (OpenAI or Ollama)
-
Created a
PromptTemplate(optional but recommended) -
Built a basic
LLMChainorAgent
✅ 1. Start with a Simple Agent
Here’s a basic setup with no tools, using LLMChain:
from langchain.llms import Ollama
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
# Set up the LLM
llm = Ollama(model="mistral", temperature=0.5)
# Create a prompt
template = "Answer the following question clearly and concisely:nnQuestion: {question}nnAnswer:"
prompt = PromptTemplate(input_variables=["question"], template=template)
# Create the chain
qa_chain = LLMChain(llm=llm, prompt=prompt)
✅ 2. Create a Test Loop in Python
Use a simple terminal interface to test the agent:
print("Welcome to your AI agent! Type 'exit' to quit.n")
while True:
question = input("You: ")
if question.lower() in ["exit", "quit"]:
print("Goodbye!")
break
response = qa_chain.run(question)
print(f"🤖 Agent: {response}n")
Now you can interact with your agent like a chatbot!
✅ 3. Sample Test Cases
Try the following prompts to assess capabilities:
| Type | Example Input | Expected Output |
|---|---|---|
| ✅ Factual | “What is the capital of Kenya?” | “Nairobi” |
| 📚 Explanation | “Explain how photosynthesis works.” | Clear and simple explanation |
| 🧠 Reasoning | “If I have 3 apples and eat 1, how many are left?” | “2 apples” |
| ❓ Unknown | “What is the meaning of life?” | Creative/philosophical answer |
| 🚫 Boundary | “Tell me a joke” | Should still respond if no tools are involved |
🛠️ Debugging Tips
| Problem | Solution |
|---|---|
| Agent gives vague answers | Adjust temperature, improve prompt |
| Repeats or rambles | Lower temperature (e.g., 0.3) |
| Doesn’t follow format | Use clearer prompt template |
| Incorrect facts | Try another model or improve instruction clarity |
🧪 Optional: Add Verbose Logging
If you’re using initialize_agent():
agent = initialize_agent(..., verbose=True)
This will print the agent’s inner reasoning, decisions, and tool calls — very helpful for debugging.
🧠 Optional: Use Streamlit to Test Visually
You can also test your agent in a simple Streamlit app:
import streamlit as st
st.title("🧠 Test Your AI Agent")
question = st.text_input("Ask a question:")
if question:
response = qa_chain.run(question)
st.markdown("**🤖 Agent:** " + response)
Run with:
streamlit run app.py
✅ Summary
| Step | What You Did |
|---|---|
| 🧠 Created a chain or agent | Using a prompt and model |
| 🔁 Ran a test loop | Input → agent → output |
| 🧪 Used test cases | Factual, reasoning, creative |
| 🛠️ Debugged responses | By tweaking prompt or model settings |
| 📺 Optional | Streamlit UI for visual testing |
30
