Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿค– Autonomous AI Agent โ†’ Word Document Generator

Give it one sentence. It plans its own tasks, does them, and hands you a polished Microsoft Word document.

Python FastAPI Groq Pydantic Tests License

An autonomous agent that understands a natural-language request, plans its own execution, grounds itself in company knowledge via RAG, writes each section with an LLM, and produces a professional .docx โ€” all behind a clean FastAPI endpoint.


โœจ Highlights

  • ๐Ÿง  Truly autonomous โ€” the agent designs its own task list & document structure per request (no fixed templates).
  • ๐Ÿ“š Simple RAG โ€” grounds every document in real company docs using local TF-IDF retrieval (no vector DB, no external service).
  • ๐Ÿ” Self-checking โ€” a reflection pass lets the agent catch and add a missing section before writing.
  • ๐Ÿ›ก๏ธ Production-minded โ€” request validation, safety guardrails, retry with backoff, and an offline fallback so it never hard-crashes.
  • ๐Ÿ“ Polished output โ€” styled Word documents (cover, metadata table, assumptions, headings, bullets, footer).
  • ๐Ÿงพ Fully typed โ€” Pydantic contracts end-to-end; defensive JSON parsing of LLM output.
  • โœ… Runs with zero setup โ€” works offline in mock mode; add a free Groq key for real AI-authored prose.

๐ŸŽฌ What it does

        POST /agent  { "request": "Create a project plan for our new mobile app" }
                                     โ”‚
                                     โ–ผ
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚                      AutonomousAgent.run()                        โ”‚
   โ”‚                                                                    โ”‚
   โ”‚  1. ๐Ÿ›ก๏ธ  GUARDRAILS   validate & sanitise the request               โ”‚
   โ”‚  2. ๐Ÿ“š  RETRIEVE      RAG: pull relevant company knowledge  โ—„โ”€โ”€ Simple RAG
   โ”‚  3. ๐Ÿง   PLAN          LLM designs doc type + section list   โ—„โ”€โ”€ Multi-step planning
   โ”‚  4. ๐Ÿ”  REFLECT       self-check: any missing section?             โ”‚
   โ”‚  5. โš™๏ธ   EXECUTE       write each section (grounded on RAG)         โ”‚
   โ”‚  6. ๐Ÿ“  ASSEMBLE      render a polished .docx (python-docx)         โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                     โ”‚
      returns: self-generated task list + summary + assumptions + .docx

Every planned step is tracked as a TaskStep with a live status (pending โ†’ done / failed), so the API returns the agent's actual TODO list โ€” exactly what an autonomous agent should expose.


๐Ÿ—บ๏ธ Architecture

Layer Responsibility Tech
1 ยท Input POST /agent accepts {"request": "..."} FastAPI
2 ยท Validation length/blank checks + prompt-injection & safety filter Pydantic v2
3 ยท RAG retrieve top-k relevant chunks from knowledge/ TF-IDF + cosine (pure Python)
4 ยท Planning design doc type, title, sections, assumptions Groq ยท Llama 3.3 70B
5 ยท Reflection self-check the plan, add a missing section LLM critic
6 ยท Execution write each section via the tool registry write_section tool
7 ยท Parsing strip fences, extract & validate JSON _safe_json + Pydantic
8 ยท Reliability retry with backoff, offline mock fallback custom LLMClient
9 ยท Document render polished Word file python-docx
10 ยท Output task list + summary + download link FastAPI response

๐Ÿ“Ž A visual version of this diagram (with the full tech stack and reasoning) is in ARCHITECTURE.html โ€” open it in any browser.


๐Ÿš€ Quick start

# 1. Install
pip install -r requirements.txt

# 2. (Optional) add a FREE Groq key for real AI prose โ€” runs offline without it
cp .env.example .env      # paste a key from https://console.groq.com/keys

# 3a. Run the two demo test cases (best for a quick look)
python demo.py

# 3b. โ€ฆor start the API
python run.py             # โ†’ http://127.0.0.1:8000/docs  (interactive Swagger UI)

Call the API:

curl -X POST http://127.0.0.1:8000/agent \
  -H "Content-Type: application/json" \
  -d '{"request":"Create a project plan for launching our retail mobile app"}'

The response includes the task list and a download_url for the generated .docx (also saved in outputs/).


๐Ÿงฉ API reference

Method Endpoint Description
POST /agent Run the agent on a request; returns task list, summary & document link
GET /download/{filename} Download a generated .docx
GET /health Liveness + which LLM provider is active
GET / Redirects to the interactive docs
Example response (click to expand)
{
  "request": "Create a project plan for launching our retail mobile app",
  "doc_type": "Project Plan",
  "title": "Northwind One Mobile App Launch Project Plan",
  "message": "Completed a 'Project Plan' โ€ฆ across 8 sections in 9.8s.",
  "assumptions": ["Timeline assumes a standard business quarter โ€ฆ"],
  "task_list": [
    {"id": 1, "description": "Retrieve relevant company knowledge (RAG)", "tool": "knowledge_base", "status": "done"},
    {"id": 2, "description": "Analyse request and design document plan", "tool": "plan_document", "status": "done"},
    {"id": 3, "description": "Self-check plan for missing sections", "tool": "reflect_on_plan", "status": "done"},
    {"id": 4, "description": "Write section: Executive Summary", "tool": "write_section", "status": "done"}
  ],
  "download_url": "/download/northwind-one-mobile-app-launch-project-plan.docx",
  "provider_used": "groq"
}

๐Ÿ† The two engineering improvements

The assignment requires one. This project implements the primary one thoroughly and adds a second, plus supporting robustness.

1๏ธโƒฃ Primary โ€” Multi-step planning

The agent does not follow a hard-coded template. It calls the LLM to design its own plan for each request โ€” the document type, title, audience, and the exact set of sections โ€” then executes that plan step-by-step, tracking status per step.

  • Generalises โ€” one codebase makes a project plan, SOP, technical design, meeting minutes, etc.
  • Handles ambiguity โ€” for vague/conflicting requests it records the assumptions it made.
  • Observable โ€” the plan becomes a task list with per-step status.
  • Adaptive โ€” a reflection pass adds a missing section before writing.

2๏ธโƒฃ Second โ€” Simple RAG

Before planning, the agent retrieves the most relevant chunks from a local knowledge/ corpus (company profile, brand style guide, product & ops facts) and injects them into the prompts.

Proof it works: an AI-chatbot request produces a Technical Design Document that automatically includes a "Data Privacy & Security" section โ€” because the retrieved brand style guide mandates it for technical designs. The agent grounded its structure in retrieved policy, not guesswork.

๐Ÿงฐ Supporting robustness

  • Retry & fallback โ€” retries transient LLM failures with backoff; falls back to a deterministic mock so the pipeline always completes.
  • Guardrails โ€” Pydantic request validation + a prompt-injection/safety check.

๐Ÿ“š How the RAG works (in plain terms)

Your request  โ”€โ–บ  chunk the knowledge/ files  โ”€โ–บ  score each chunk (TF-IDF)
      โ”€โ–บ  pick the top-3 most relevant  โ”€โ–บ  inject as "COMPANY KNOWLEDGE" in the prompt
      โ”€โ–บ  the LLM writes grounded in real facts & rules

Swap the three mock files in knowledge/ for a real company's documents and the agent grounds every output in them โ€” no code change needed. The Retriever interface is intentionally small so the scorer can be upgraded to dense embeddings later.


๐Ÿงฑ Tech stack โ€” and why

Concern Choice Why
Web API FastAPI + Uvicorn Async, typed, free Swagger docs
LLM Groq ยท Llama 3.3 70B Free, extremely fast, no credit card
LLM calls requests (direct REST) No LangChain weight โ€” lighter & fully explainable
Validation / typing Pydantic v2 Free validation + typed structured output
RAG TF-IDF + cosine (pure Python) Local, deterministic, zero heavy deps
Document python-docx Programmatic, styled .docx
Reliability retry + mock fallback Degrade-don't-crash
Tests pytest (mock mode) Deterministic, fast, free

๐Ÿ“ Project structure

Autonomus_AI_Agent/
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ main.py        # FastAPI app โ€” /agent, /download, /health
โ”‚   โ”œโ”€โ”€ agent.py       # Plan โ†’ Reflect โ†’ Execute orchestrator + guardrails
โ”‚   โ”œโ”€โ”€ tools.py       # Tool registry: plan_document, write_section, reflect_on_plan
โ”‚   โ”œโ”€โ”€ rag.py         # Simple RAG โ€” TF-IDF retriever over knowledge/
โ”‚   โ”œโ”€โ”€ llm.py         # Provider abstraction (Groq/Gemini/mock) + retry + fallback
โ”‚   โ”œโ”€โ”€ docgen.py      # Polished .docx rendering (python-docx)
โ”‚   โ”œโ”€โ”€ schemas.py     # Typed Pydantic contract
โ”‚   โ””โ”€โ”€ config.py      # Env-based settings; auto-selects mock when no key
โ”œโ”€โ”€ knowledge/         # Company-context docs the agent retrieves from (RAG)
โ”œโ”€โ”€ tests/             # Pytest smoke + unit tests (no API key needed)
โ”œโ”€โ”€ demo.py            # Runs the two required test cases
โ”œโ”€โ”€ run.py             # Launcher โ†’ http://127.0.0.1:8000/docs
โ”œโ”€โ”€ ARCHITECTURE.html  # Visual architecture + tech-stack diagram
โ””โ”€โ”€ requirements.txt

๐Ÿงช Testing

python -m pytest -q      # 7 tests โ€” deterministic, run in mock mode, no key required

Covers request validation, the guardrail, plan generation, RAG retrieval & ranking, document creation on disk, and the defensive JSON parser.


๐Ÿž Debugging insight

Problem: the planner asks the LLM for strict JSON, but models sometimes wrap it in ```code fences or prepend prose like "Here is your plan" โ€” crashing json.loads and the whole run.

Root cause: LLM output format is not guaranteed.

Fix: a defensive parser (_safe_json) that strips fences, and if that fails, extracts the substring between the first { and last }; returns {} as a last resort instead of crashing.

Lesson: treat LLM output as untrusted and parse it defensively.


โš–๏ธ Key tradeoff โ€” autonomous planning vs. fixed workflow

A fixed template is fast and 100% predictable, but can't handle document-type variety or ambiguity. This project chose an LLM planner for adaptability, then bounded its non-determinism with typed schemas, defensive JSON parsing, a self-check pass, and a mock fallback โ€” keeping the flexibility while making it safe and reliable.


๐Ÿ”ฎ Future improvements

  • Swap TF-IDF for dense embeddings + a vector store for larger corpora.
  • Conversation memory for multi-turn document refinement.
  • Streaming progress of the task list over WebSockets.
  • Export to PDF / templated brand themes.

๐Ÿ“„ License

MIT โ€” free to use and adapt.

Built to demonstrate autonomous agent design, planning & reasoning, tool orchestration, RAG, and clean API engineering.

About

Autonomous AI agent (FastAPI + Groq) that plans its own tasks, uses RAG for grounding, and generates polished Microsoft Word documents from a single natural-language request.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages