File size: 18,195 Bytes
78aa11d
2577d6f
821c406
 
 
78aa11d
2577d6f
78aa11d
 
53bd6dc
 
78aa11d
 
fa94723
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94965d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa94723
 
 
 
 
94965d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa94723
 
 
 
94965d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa94723
 
 
 
 
 
 
 
 
94965d6
 
 
fa94723
 
94965d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa94723
 
 
94965d6
fa94723
 
 
 
 
94965d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa94723
 
 
 
 
 
 
 
 
 
 
 
94965d6
 
 
 
 
 
 
 
fa94723
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94965d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa94723
0d77f39
41ac444
fa94723
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fb579f
 
 
 
 
 
 
630f609
9fb579f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
630f609
9fb579f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2577d6f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
---
title: Agentbee | GAIA Project | HuggingFace Course
emoji: πŸ•΅πŸ»β€β™‚οΈ
colorFrom: indigo
colorTo: indigo
sdk: gradio
sdk_version: 6.3.0
app_file: app.py
pinned: false
hf_oauth: true
hf_oauth_expiration_minutes: 480
---

Check out the configuration reference at <https://huggingface.co/docs/hub/spaces-config-reference>

## Project Overview

**Project Name:** Final_Assignment_Template

**Purpose:** Course assignment template for building an AI agent that passes the GAIA benchmark (General AI Assistants). This project serves as a learning-focused workspace to support iterative agent development and experimentation.

**Target Users:** Students learning agent development through hands-on implementation

**Key Objectives:**

- Build production-ready code that passes GAIA test requirements
- Learn agent development through discovery-based implementation
- Develop systematic approach to complex AI task solving
- Document learning process and key decisions

## Project Architecture

**Technology Stack:**

- **Platform:** Hugging Face Spaces with OAuth integration
- **UI Framework:** Gradio 5.x with OAuth support
- **Agent Framework:** LangGraph (state graph orchestration)
- **LLM Providers (4-tier fallback):**
  - Google Gemini 2.0 Flash (free tier)
  - HuggingFace Inference API (free tier)
  - Groq (Llama 3.1 70B / Qwen 3 32B, free tier)
  - Anthropic Claude Sonnet 4.5 (paid tier)
- **Tools:**
  - Web Search: Tavily API / Exa API
  - File Parser: PyPDF2, openpyxl, python-docx, pillow
  - Calculator: Safe expression evaluator
  - Vision: Multimodal LLM (Gemini/Claude)
- **Language:** Python 3.12+
- **Package Manager:** uv

**Project Structure:**

```
Final_Assignment_Template/
β”œβ”€β”€ archive/             # Reference materials, previous solutions, static resources
β”œβ”€β”€ input/               # Input files, configuration, raw data
β”œβ”€β”€ output/              # Generated files, results, processed data
β”œβ”€β”€ test/                # Testing files, test scripts (99 tests)
β”œβ”€β”€ dev/                 # Development records (permanent knowledge packages)
β”œβ”€β”€ src/                 # Source code
β”‚   β”œβ”€β”€ agent/           # Agent orchestration
β”‚   β”‚   β”œβ”€β”€ graph.py     # LangGraph state machine
β”‚   β”‚   └── llm_client.py # Multi-provider LLM integration with retry logic
β”‚   └── tools/           # Agent tools
β”‚       β”œβ”€β”€ __init__.py  # Tool registry
β”‚       β”œβ”€β”€ web_search.py    # Tavily/Exa web search
β”‚       β”œβ”€β”€ file_parser.py   # Multi-format file reader
β”‚       β”œβ”€β”€ calculator.py    # Safe math evaluator
β”‚       └── vision.py        # Multimodal image/video analysis
β”œβ”€β”€ app.py               # Gradio UI with OAuth, LLM provider selection
β”œβ”€β”€ pyproject.toml       # uv package management
β”œβ”€β”€ requirements.txt     # Python dependencies (generated from pyproject.toml)
β”œβ”€β”€ .env                 # Local environment variables (API keys, config)
β”œβ”€β”€ README.md            # Project overview, architecture, workflow, specification
β”œβ”€β”€ CLAUDE.md            # Project-specific AI instructions
β”œβ”€β”€ PLAN.md              # Active implementation plan (temporary workspace)
β”œβ”€β”€ TODO.md              # Active task tracking (temporary workspace)
└── CHANGELOG.md         # Session changelog (temporary workspace)
```

**Core Components:**

- **GAIAAgent class** (src/agent/graph.py): LangGraph-based agent with state machine orchestration
  - Planning node: Analyze question and generate execution plan
  - Tool selection node: LLM function calling for dynamic tool selection
  - Tool execution node: Execute selected tools with timeout and error handling
  - Answer synthesis node: Generate factoid answer from evidence
- **LLM Client** (src/agent/llm_client.py): Multi-provider LLM integration
  - 4-tier fallback chain: Gemini β†’ HuggingFace β†’ Groq β†’ Claude
  - Exponential backoff retry logic (3 attempts per provider)
  - Runtime config for UI-based provider selection
  - Few-shot prompting for improved tool selection
- **Tool System** (src/tools/):
  - Web Search: Tavily/Exa API with query optimization
  - File Parser: Multi-format support (PDF, Excel, Word, CSV, images)
  - Calculator: Safe expression evaluator with graceful error handling
  - Vision: Multimodal analysis for images/videos
- **Gradio UI** (app.py):
  - Test & Debug tab: Single question testing with LLM provider dropdown
  - Full Evaluation tab: Run all GAIA questions with provider selection
  - Results export: JSON file download for analysis
  - OAuth integration for submission
- **Evaluation Infrastructure**: Pre-built orchestration (question fetching, submission, scoring)

**System Architecture Diagram:**

```mermaid
---
config:
  layout: elk
---
graph TB
    subgraph "UI Layer"
        GradioUI[Gradio UI<br/>LLM Provider Selection<br/>Test & Full Evaluation]
        OAuth[HF OAuth<br/>User authentication]
    end

    subgraph "Agent Orchestration (LangGraph)"
        GAIAAgent[GAIAAgent<br/>State Machine]
        PlanNode[Planning Node<br/>Analyze question]
        ToolSelectNode[Tool Selection Node<br/>LLM function calling]
        ToolExecNode[Tool Execution Node<br/>Run selected tools]
        SynthesizeNode[Answer Synthesis Node<br/>Generate factoid]
    end

    subgraph "LLM Layer (4-Tier Fallback)"
        LLMClient[LLM Client<br/>Retry + Fallback]
        Gemini[Gemini 2.0 Flash<br/>Free Tier 1]
        HF[HuggingFace API<br/>Free Tier 2]
        Groq[Groq Llama/Qwen<br/>Free Tier 3]
        Claude[Claude Sonnet 4.5<br/>Paid Tier 4]
    end

    subgraph "Tool Layer"
        WebSearch[Web Search<br/>Tavily/Exa]
        FileParser[File Parser<br/>PDF/Excel/Word]
        Calculator[Calculator<br/>Safe eval]
        Vision[Vision<br/>Multimodal LLM]
    end

    subgraph "External Services"
        API[GAIA Scoring API]
        QEndpoint["/questions endpoint"]
        SEndpoint["/submit endpoint"]
    end

    GradioUI --> OAuth
    OAuth -->|Authenticated| GAIAAgent
    GAIAAgent --> PlanNode
    PlanNode --> ToolSelectNode
    ToolSelectNode --> ToolExecNode
    ToolExecNode --> SynthesizeNode

    PlanNode --> LLMClient
    ToolSelectNode --> LLMClient
    SynthesizeNode --> LLMClient

    LLMClient -->|Try 1| Gemini
    LLMClient -->|Fallback 2| HF
    LLMClient -->|Fallback 3| Groq
    LLMClient -->|Fallback 4| Claude

    ToolExecNode --> WebSearch
    ToolExecNode --> FileParser
    ToolExecNode --> Calculator
    ToolExecNode --> Vision

    GAIAAgent -->|Answers| API
    API --> QEndpoint
    API --> SEndpoint
    SEndpoint -->|Score| GradioUI

    style GAIAAgent fill:#ffcccc
    style LLMClient fill:#fff4cc
    style GradioUI fill:#cce5ff
    style API fill:#d9f2d9
```

## Project Specification

**Project Context:**

This is a course assignment template for building an AI agent that passes the GAIA benchmark (General AI Assistants). The project was recently started as a learning-focused workspace to support iterative agent development and experimentation.

**Current State:**

- **Status:** Stage 5 Complete - Performance Optimization
- **Development Progress:**
  - Stage 1-2: Basic infrastructure and LangGraph setup βœ…
  - Stage 3: Multi-provider LLM integration βœ…
  - Stage 4: Tool system and MVP (10% GAIA score: 2/20 questions) βœ…
  - Stage 5: Performance optimization (retry logic, Groq integration, improved prompts) βœ…
- **Current Performance:** Testing in progress (target: 25% accuracy, 5/20 questions)
- **Next Stage:** Stage 6 - Advanced optimizations based on Stage 5 results

**Data & Workflows:**

- **Input Data:** GAIA test questions fetched from external scoring API (`agents-course-unit4-scoring.hf.space`)
- **Processing:** BasicAgent class processes questions and generates answers
- **Output:** Agent responses submitted to scoring endpoint for evaluation
- **Development Workflow:**
  1. Local development and testing
  2. Deploy to Hugging Face Space
  3. Submit via integrated evaluation UI

**User Workflow Diagram:**

```mermaid
---
config:
  layout: fixed
---
flowchart TB
    Start(["Student starts assignment"]) --> Clone["Clone HF Space template"]
    Clone --> LocalDev["Local development:<br>Implement BasicAgent logic"]
    LocalDev --> LocalTest{"Test locally?"}
    LocalTest -- Yes --> RunLocal["Run app locally"]
    RunLocal --> Debug{"Works?"}
    Debug -- No --> LocalDev
    Debug -- Yes --> Deploy["Deploy to HF Space"]
    LocalTest -- Skip --> Deploy
    Deploy --> Login["Login with HF OAuth"]
    Login --> RunEval@{ label: "Click 'Run Evaluation'<br>button in UI" }
    RunEval --> FetchQ["System fetches GAIA<br>questions from API"]
    FetchQ --> RunAgent["Agent processes<br>each question"]
    RunAgent --> Submit["Submit answers<br>to scoring API"]
    Submit --> Display["Display score<br>and results"]
    Display --> Iterate{"Satisfied with<br>score?"}
    Iterate -- "No - improve agent" --> LocalDev
    Iterate -- Yes --> Complete(["Assignment complete"])

    RunEval@{ shape: rect}
    style Start fill:#e1f5e1
    style LocalDev fill:#fff4e1
    style Deploy fill:#e1f0ff
    style RunAgent fill:#ffe1f0
    style Complete fill:#e1f5e1
```

**Technical Architecture:**

- **Platform:** Hugging Face Spaces with OAuth integration
- **Framework:** Gradio for UI, Requests for API communication
- **Core Component:** BasicAgent class (student-customizable template)
- **Evaluation Infrastructure:** Pre-built orchestration (question fetching, submission, scoring display)
- **Deployment:** HF Space with environment variables (SPACE_ID, SPACE_HOST)

**Requirements & Constraints:**

- **Constraint Type:** Minimal at current stage
- **Infrastructure:** Must run on Hugging Face Spaces platform
- **Integration:** Fixed scoring API endpoints (cannot modify evaluation system)
- **Flexibility:** Students have full freedom to design agent capabilities

**Integration Points:**

- **External API:** `https://agents-course-unit4-scoring.hf.space`
  - `/questions` endpoint: Fetch GAIA test questions
  - `/submit` endpoint: Submit answers and receive scores
- **Authentication:** Hugging Face OAuth for student identification
- **Deployment:** HF Space runtime environment variables

**Development Goals:**

- **Primary:** Achieve competitive GAIA benchmark performance through systematic optimization
- **Focus:** Multi-tier LLM architecture with free-tier prioritization to minimize costs
- **Key Features:**
  - 4-tier LLM fallback for quota resilience (Gemini β†’ HF β†’ Groq β†’ Claude)
  - Exponential backoff retry logic for quota/rate limit errors
  - UI-based LLM provider selection for easy A/B testing in cloud
  - Comprehensive tool system (web search, file parsing, calculator, vision)
  - Graceful error handling and degradation
  - Extensive test coverage (99 tests)
- **Documentation:** Full dev record workflow tracking all decisions and changes

## Key Features

### LLM Provider Selection (UI-Based)

**Local Testing (.env configuration):**

```bash
LLM_PROVIDER=gemini          # Options: gemini, huggingface, groq, claude
ENABLE_LLM_FALLBACK=false    # Disable fallback for debugging single provider
```

**Cloud Testing (HuggingFace Spaces):**

- Use UI dropdowns in Test & Debug tab or Full Evaluation tab
- Select from: Gemini, HuggingFace, Groq, Claude
- Toggle fallback behavior with checkbox
- No environment variable changes needed, instant provider switching

**Benefits:**

- Easy A/B testing between providers
- Clear visibility which LLM is used
- Isolated testing for debugging
- Production safety with fallback enabled

### Retry Logic

- **Exponential backoff:** 3 attempts with 1s, 2s, 4s delays
- **Error detection:** 429 status, quota errors, rate limits
- **Scope:** All LLM calls (planning, tool selection, synthesis)

### Tool System

**Web Search (Tavily/Exa):**

- Factual information, current events, statistics
- Wikipedia, company info, people

**File Parser:**

- PDF, Excel, Word, CSV, Text, Images
- Handles uploaded files and local paths

**Calculator:**

- Safe expression evaluation
- Arithmetic, algebra, trigonometry, logarithms
- Functions: sqrt, sin, cos, log, abs, etc.

**Vision:**

- Multimodal image/video analysis
- Describe content, identify objects, read text
- YouTube video understanding

### Performance Optimizations (Stage 5)

- Few-shot prompting for improved tool selection
- Graceful vision question skip when quota exhausted
- Relaxed calculator validation (returns error dicts instead of crashes)
- Improved tool descriptions with "Use when..." guidance
- Config-based provider debugging

## GAIA Benchmark Results

**Baseline (Stage 4):** 10% accuracy (2/20 questions correct)

**Stage 5 Target:** 25% accuracy (5/20 questions correct)

- Status: Testing in progress
- Expected improvements from retry logic, Groq integration, improved prompts

**Test Coverage:** 99 passing tests (~2min 40sec runtime)

> **Note:** This project implements the **Course Leaderboard** (20 questions, 30% target). See [GAIA Submission Guide](../agentbee/docs/gaia_submission_guide.md) for distinction between Course and Official GAIA leaderboards.

## Workflow

### Dev Record Workflow

**Philosophy:** Dev records are the single source of truth. CHANGELOG/PLAN/TODO are temporary workspace files.

**Dev Record Types:**

- 🐞 **Issue:** Problem-solving, bug fixes, error resolution
- πŸ”¨ **Development:** Feature development, enhancements, new functionality

### Session Start Workflow

#### Phase 1: Planning (Explicit)

1. **Create or identify dev record:** `dev/dev_YYMMDD_##_concise_title.md`
   - Choose type: 🐞 Issue or πŸ”¨ Development
2. **Create PLAN.md ONLY:** Use `/plan` command or write directly
   - Document implementation approach, steps, files to modify
   - DO NOT create TODO.md or CHANGELOG.md yet

#### Phase 2: Development (Automatic)

3. **Create TODO.md:** Automatically populate as you start implementing
   - Track tasks in real-time using TodoWrite tool
   - Mark in_progress/completed as you work
4. **Create CHANGELOG.md:** Automatically populate as you make changes
   - Record file modifications/creations/deletions as they happen
5. **Work on solution:** Update all three files during development

### Session End Workflow

#### Phase 3: Completion (Manual)

After AI completes all work and updates PLAN/TODO/CHANGELOG:

- AI stops and waits for user review (Checkpoint 3)
- User reviews PLAN.md, TODO.md, and CHANGELOG.md
- User manually runs `/update-dev dev_YYMMDD_##` when satisfied

When /update-dev runs:

1. Distills PLAN decisions β†’ dev record "Key Decisions" section
2. Distills TODO deliverables β†’ dev record "Outcome" section
3. Distills CHANGELOG changes β†’ dev record "Changelog" section
4. Empties PLAN.md, TODO.md, CHANGELOG.md back to templates
5. Marks dev record status as βœ… Resolved

### AI Context Loading Protocol

**MANDATORY - Execute in exact order. NO delegating to sub-agents for initial context.**

**Phase 1: Current State (What's happening NOW)**

1. **Read workspace files:**

   - `CHANGELOG.md` - Active session changes (reverse chronological, newest first)
   - `PLAN.md` - Current implementation plan (if exists)
   - `TODO.md` - Active task tracking (if exists)

2. **Read actual outputs (CRITICAL - verify claims, don't trust summaries):**
   - Latest files in `output/` folder (sorted by timestamp, newest first)
   - For GAIA projects: Read latest `output/gaia_results_*.json` completely
     - Check `metadata.score_percent` and `metadata.correct_count`
     - Read ALL `results[].submitted_answer` to understand failure patterns
     - Identify error categories (vision failures, tool errors, wrong answers)
   - For test projects: Read latest test output logs
   - **Purpose:** Ground truth of what ACTUALLY happened, not what was claimed

**Phase 2: Recent History (What was done recently)**

3. **Read last 3 dev records from `dev/` folder:**
   - Sort by filename (newest `dev_YYMMDD_##_title.md` first)
   - Read: Problem Description, Key Decisions, Outcome, Changelog
   - **Cross-verify:** Compare dev record claims with actual output files
   - **Red flag:** If dev record says "25% accuracy" but latest JSON shows "0%", prioritize JSON truth

**Phase 3: Project Structure (How it works)**

4. **Read README.md sections in order:**

   - Section 1: Overview (purpose, objectives)
   - Section 2: Architecture (tech stack, components, diagrams)
   - Section 3: Specification (current state, workflows, requirements)
   - Section 4: Workflow (this protocol)

5. **Read CLAUDE.md:**
   - Project-specific coding standards
   - Usually empty (inherits from global ~/.claude/CLAUDE.md)

**Phase 4: Code Structure (Critical files)**

6. **Identify critical files from README.md Architecture section:**
   - Note main entry points (e.g., `app.py`)
   - Note core logic files (e.g., `src/agent/graph.py`, `src/agent/llm_client.py`)
   - Note tool implementations (e.g., `src/tools/*.py`)
   - **DO NOT read these yet** - only note their locations for later reference

**Verification Checklist (Before claiming "I have context"):**

- [ ] I personally read CHANGELOG.md, PLAN.md, TODO.md (not delegated)
- [ ] I personally read latest output files (JSON results, test logs, etc.)
- [ ] I know the ACTUAL current accuracy/status from output files
- [ ] I read last 3 dev records and cross-verified claims with output data
- [ ] I read README.md sections 1-4 completely
- [ ] I can answer: "What is the current status and why?"
- [ ] I can answer: "What were the last 3 major changes and their outcomes?"
- [ ] I can answer: "What specific problems exist based on latest outputs?"

**Anti-Patterns (NEVER do these):**

- ❌ Delegate initial context loading to Explore/Task agents
- ❌ Trust dev record claims without verifying against output files
- ❌ Skip reading actual output data (JSON results, logs, test outputs)
- ❌ Claim "I have context" after only reading summaries
- ❌ Read code files before understanding current state from outputs

**Context Priority:** Latest Outputs (ground truth) > CHANGELOG (active work) > Dev Records (history) > README (structure)