zucchetti-llm-course

Lab 06: An Autonomous Agent (Agentic RAG)

Lecture: 7, Agentic RAG and Autonomous Systems Duration: 45 minutes Difficulty: Advanced

Objectives

Prerequisites

Requirement How to Check
Python 3.10+ python --version
Dependencies pip install openai rich python-dotenv
LLM access Ollama or DeepSeek API key

Quick Start

cd lab_06_agent
python lab_06_agent.py

Files

File Description
lab_06_agent.py Main script with ReAct loop and TODO section
setup_db.py Creates the hospital SQLite database
solutions.py Tool execution solution
data/hospital.db Auto-generated database (patients, labs, prescriptions)

Tasks

Task 1: Understand the ReAct Loop

Read the SYSTEM_PROMPT in lab_06_agent.py. Notice how we force the LLM to output specific prefixes (Thought:, Action:, Action Input:, Observation:). This structured output is what allows our Python code to parse the LLM’s intent and run the correct tool.

Task 2: Implement Tool Execution

Find the TODO: YOUR CODE HERE section in the run_agent() function. You need to map the parsed action string to the actual Python function (get_database_schema or execute_sql_query), run it with the action_input, and store the result in the observation variable.

Task 3: Watch the Agent Think

Run the script and watch the terminal. You will see the agent’s internal monologue:

  1. It realizes it doesn’t know the schema, so it calls get_database_schema.
  2. It reads the observation (the schema).
  3. It writes a SQL query and calls execute_sql_query.
  4. If it gets an error, it reads the error and writes a new SQL query.
  5. Once it has the data, it formulates the Final Answer.

Task 4: Interactive Challenge

Ask the agent complex questions that require JOINs:

Expected Output

User Question: What is the most common gender among our patients?

--- Step 1 ---
Thought: I need to know the database schema to find the table that stores patient genders.
Action: get_database_schema
Action Input: None
Observation: CREATE TABLE patients (subject_id INTEGER PRIMARY KEY, gender TEXT NOT NULL...)

--- Step 2 ---
Thought: The 'patients' table has a 'gender' column. I will query the count of each gender.
Action: execute_sql_query
Action Input: SELECT gender, COUNT(*) FROM patients GROUP BY gender ORDER BY COUNT(*) DESC LIMIT 1
Observation: Columns: gender, COUNT(*)
('M', 6)

--- Step 3 ---
Thought: The query returned 'M' (Male) with a count of 6.
Final Answer: The most common gender among patients is Male.

🆘 Troubleshooting

Issue Solution
Tool execution not implemented! You must complete the TODO in Task 2.
Agent gets stuck in a loop Sometimes local models struggle with ReAct parsing. Try using the DeepSeek API mode.
Infinite loops The max_steps parameter prevents the agent from running forever.