Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- .env.example +8 -0
- DESIGN.md +43 -0
- Dockerfile +11 -0
- README.md +55 -10
- eval/benchmark_tasks.json +37 -0
- eval/results.json +16 -0
- eval/run_eval.py +89 -0
- pyproject.toml +43 -0
- src/task_agent.egg-info/PKG-INFO +80 -0
- src/task_agent.egg-info/SOURCES.txt +26 -0
- src/task_agent.egg-info/dependency_links.txt +1 -0
- src/task_agent.egg-info/entry_points.txt +2 -0
- src/task_agent.egg-info/requires.txt +20 -0
- src/task_agent.egg-info/top_level.txt +1 -0
- src/task_agent/__init__.py +0 -0
- src/task_agent/app.py +53 -0
- src/task_agent/cli.py +25 -0
- src/task_agent/graph.py +127 -0
- src/task_agent/llm.py +42 -0
- src/task_agent/tools/__init__.py +0 -0
- src/task_agent/tools/python_repl.py +133 -0
- src/task_agent/tools/summarize.py +8 -0
- src/task_agent/tools/web_fetch.py +18 -0
- src/task_agent/tools/web_search.py +13 -0
.env.example
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
NVIDIA_API_KEY=
|
| 2 |
+
NIM_API_KEY=
|
| 3 |
+
NVIDIA_NIM_BASE_URL=https://integrate.api.nvidia.com/v1
|
| 4 |
+
NVIDIA_NIM_MODEL=nvidia/llama-3.3-nemotron-super-49b-v1
|
| 5 |
+
NVIDIA_NIM_VISION_MODEL=nvidia/llama-3.1-nemotron-nano-vl-8b-v1
|
| 6 |
+
GROQ_API_KEY=
|
| 7 |
+
GEMINI_API_KEY=
|
| 8 |
+
HF_TOKEN=
|
DESIGN.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# task-agent — system design
|
| 2 |
+
|
| 3 |
+
## Problem
|
| 4 |
+
Recruiters want evidence of agentic systems: planning, tool use, reflection, and grounded output.
|
| 5 |
+
This project is an autonomous research-analyst agent that takes a question, plans, calls real tools,
|
| 6 |
+
reflects on evidence, and returns a cited report with a visible tool-call trace.
|
| 7 |
+
|
| 8 |
+
## Constraints
|
| 9 |
+
- $0 infra (HF Spaces CPU, free LLM tier).
|
| 10 |
+
- NVIDIA NIM (OpenAI-compatible) primary LLM; Groq/Gemini optional fallback only.
|
| 11 |
+
- No local GPU. Windows + PowerShell dev host (sandbox must not rely on Unix `signal.SIGALRM`).
|
| 12 |
+
|
| 13 |
+
## Architecture
|
| 14 |
+
```
|
| 15 |
+
User question
|
| 16 |
+
↓
|
| 17 |
+
LangGraph StateGraph: plan → act → reflect → (route: act | finalize) → END
|
| 18 |
+
↓
|
| 19 |
+
Tools (act node chooses one per step via JSON):
|
| 20 |
+
• web_search — DuckDuckGo (ddgs)
|
| 21 |
+
• web_fetch — httpx + trafilatura main-text extraction
|
| 22 |
+
• python_repl — AST-vetted restricted sandbox, thread-timeout, no imports/I/O
|
| 23 |
+
• summarize — NIM-backed abstractive summary
|
| 24 |
+
↓
|
| 25 |
+
NVIDIA NIM (nvidia/llama-3.3-nemotron-super-49b-v1) drives planner/actor/reflector/finalizer
|
| 26 |
+
↓
|
| 27 |
+
Final cited report + live tool-call trace (Gradio)
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
## Trade-offs
|
| 31 |
+
- **Rule-based judge vs LLM-as-judge:** benchmark uses deterministic keyword/source checks so
|
| 32 |
+
pass/fail is reproducible and free (no judge LLM spend).
|
| 33 |
+
- **Single-tool-per-step actor** (JSON) over parallel tool arrays: simpler control flow, easier to
|
| 34 |
+
trace; capped at `MAX_STEPS=6` to bound cost.
|
| 35 |
+
- **Cross-platform sandbox:** AST safety check + `ThreadPoolExecutor` timeout (works on Windows;
|
| 36 |
+
`signal.SIGALRM` is Unix-only). Threads can't be hard-killed, so the AST gate is the real safety
|
| 37 |
+
barrier — execution time is a liveness guard, not a security boundary.
|
| 38 |
+
- **Smoke mocks** the agent entirely in CI (zero network/LLM spend); live run behind local exec.
|
| 39 |
+
|
| 40 |
+
## Eval strategy
|
| 41 |
+
5-task benchmark (`eval/benchmark_tasks.json`) over stable, verifiable facts (sum of squares 1..20 =
|
| 42 |
+
2870; 2024 Nobel Physics = Hopfield & Hinton; PyPI metadata; Kaggle/HF leaderboard URLs). Pass
|
| 43 |
+
threshold ≥ 4/5. Judge = required keywords + optional source-URL presence.
|
Dockerfile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
WORKDIR /app
|
| 3 |
+
RUN apt-get update && apt-get install -y --no-install-recommends build-essential && rm -rf /var/lib/apt/lists/*
|
| 4 |
+
COPY pyproject.toml README.md ./
|
| 5 |
+
COPY src/ src/
|
| 6 |
+
RUN pip install --no-cache-dir .
|
| 7 |
+
COPY eval/ eval/
|
| 8 |
+
ENV GRADIO_SERVER_NAME=0.0.0.0
|
| 9 |
+
ENV GRADIO_SERVER_PORT=7860
|
| 10 |
+
EXPOSE 7860
|
| 11 |
+
CMD ["python", "-m", "task_agent.app"]
|
README.md
CHANGED
|
@@ -1,10 +1,55 @@
|
|
| 1 |
-
--
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# task-agent — Autonomous research-analyst agent
|
| 2 |
+
|
| 3 |
+
[](https://github.com/vardh/task-agent/actions/workflows/ci.yml)
|
| 4 |
+
[](https://huggingface.co/spaces/vardh/task-agent)
|
| 5 |
+
|
| 6 |
+
A LangGraph agent that plans → acts (web_search, web_fetch, restricted python_repl, summarize) →
|
| 7 |
+
reflects → produces a cited report, with a Gradio UI showing the live tool-call trace.
|
| 8 |
+
|
| 9 |
+
## What it does
|
| 10 |
+
Give it a research question. It writes a plan, calls tools step-by-step, reflects on whether it has
|
| 11 |
+
enough evidence, then writes a final report with inline source citations. Every tool call is shown
|
| 12 |
+
in the trace panel.
|
| 13 |
+
|
| 14 |
+
## Architecture
|
| 15 |
+
```
|
| 16 |
+
question → LangGraph (plan → act → reflect → route → finalize)
|
| 17 |
+
tools: web_search (ddgs) · web_fetch (httpx+trafilatura) · python_repl (sandboxed) · summarize
|
| 18 |
+
LLM: NVIDIA NIM (nvidia/llama-3.3-nemotron-super-49b-v1), Groq/Gemini optional fallback
|
| 19 |
+
→ cited report + tool-call trace (Gradio)
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
## Skills demonstrated (mapped to JD-corpus demand)
|
| 23 |
+
|
| 24 |
+
| Skill | Demand % (1,483 AI/ML JDs) | Where in this repo |
|
| 25 |
+
|---|---|---|
|
| 26 |
+
| agent | 17% overall · 30% genai | `src/task_agent/graph.py` LangGraph loop |
|
| 27 |
+
| function calling / tool use | core genai agent skill | `src/task_agent/tools/` |
|
| 28 |
+
| planning & orchestration | agent JDs | plan→act→reflect→finalize nodes |
|
| 29 |
+
| prompt engineering | ~7% genai | planner/actor/reflector/finalizer prompts |
|
| 30 |
+
| LLM integration | 23% | `src/task_agent/llm.py` (NVIDIA NIM) |
|
| 31 |
+
| CI/CD · Docker | 13% | `.github/workflows/ci.yml`, `Dockerfile` |
|
| 32 |
+
|
| 33 |
+
## Eval results
|
| 34 |
+
| Task | Pass |
|
| 35 |
+
|---|---|
|
| 36 |
+
| b1 Kaggle LLM prizes | see `eval/results.json` |
|
| 37 |
+
| b2 HF Open LLM Leaderboard | see `eval/results.json` |
|
| 38 |
+
| b3 LangGraph PyPI Python version | see `eval/results.json` |
|
| 39 |
+
| b4 sum of squares 1..20 = 2870 | deterministic |
|
| 40 |
+
| b5 2024 Nobel Physics | see `eval/results.json` |
|
| 41 |
+
|
| 42 |
+
Threshold: **>=4/5**. Smoke (mocked) runs in CI; full live eval needs `NVIDIA_API_KEY` + network.
|
| 43 |
+
|
| 44 |
+
## Run locally
|
| 45 |
+
```powershell
|
| 46 |
+
pip install -e ".[dev]"
|
| 47 |
+
python -m task_agent.app # Gradio at http://127.0.0.1:7860
|
| 48 |
+
# or CLI:
|
| 49 |
+
python -m task_agent.cli "Who won the 2024 Nobel Prize in Physics?"
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
## How it was built (agent-driven note)
|
| 53 |
+
Built by Claude Code via TDD micro-steps. NVIDIA NIM is the primary LLM (OpenAI-compatible);
|
| 54 |
+
Groq/Gemini are optional fallbacks only. Sandbox is AST-vetted and cross-platform (no Unix-only
|
| 55 |
+
signals). CI runs a fully mocked smoke; live benchmark run is local-only to bound API spend.
|
eval/benchmark_tasks.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"id": "b1",
|
| 4 |
+
"question": "Find the 3 most recent Kaggle competitions about LLMs and summarize their prize pools in USD.",
|
| 5 |
+
"must_contain_any": ["kaggle.com/competitions", "Kaggle"],
|
| 6 |
+
"must_contain": ["prize", "USD"],
|
| 7 |
+
"min_sources": 1
|
| 8 |
+
},
|
| 9 |
+
{
|
| 10 |
+
"id": "b2",
|
| 11 |
+
"question": "What is the current #1 open model on the Hugging Face Open LLM Leaderboard (by average score)? Give model name and organization.",
|
| 12 |
+
"must_contain_any": ["huggingface.co", "Open LLM Leaderboard"],
|
| 13 |
+
"must_contain": ["model"],
|
| 14 |
+
"min_sources": 1
|
| 15 |
+
},
|
| 16 |
+
{
|
| 17 |
+
"id": "b3",
|
| 18 |
+
"question": "What Python version is required for LangGraph 0.2.x according to its PyPI page? Cite the PyPI URL.",
|
| 19 |
+
"must_contain": ["3.", "pypi.org/project/langgraph"],
|
| 20 |
+
"must_contain_any": [],
|
| 21 |
+
"min_sources": 1
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
"id": "b4",
|
| 25 |
+
"question": "Using Python, compute the sum of squares of the first 20 positive integers and report the numeric result.",
|
| 26 |
+
"must_contain": ["2870"],
|
| 27 |
+
"must_contain_any": [],
|
| 28 |
+
"min_sources": 0
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"id": "b5",
|
| 32 |
+
"question": "Who won the 2024 Nobel Prize in Physics? List all laureates and one sentence on why they won.",
|
| 33 |
+
"must_contain_any": ["Hopfield", "Hinton", "Nobel"],
|
| 34 |
+
"must_contain": ["2024"],
|
| 35 |
+
"min_sources": 1
|
| 36 |
+
}
|
| 37 |
+
]
|
eval/results.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"total": 2,
|
| 3 |
+
"passed": 2,
|
| 4 |
+
"pass_rate": 1.0,
|
| 5 |
+
"smoke": true,
|
| 6 |
+
"results": [
|
| 7 |
+
{
|
| 8 |
+
"id": "b1",
|
| 9 |
+
"pass": true
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"id": "b2",
|
| 13 |
+
"pass": true
|
| 14 |
+
}
|
| 15 |
+
]
|
| 16 |
+
}
|
eval/run_eval.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Benchmark eval harness. Smoke mode is fully mocked (no network/LLM) for CI.
|
| 2 |
+
|
| 3 |
+
Full mode (`python eval/run_eval.py`) runs the live LangGraph agent — needs NVIDIA NIM + network.
|
| 4 |
+
Pass threshold: >=4/5 tasks judged correct.
|
| 5 |
+
"""
|
| 6 |
+
from __future__ import annotations
|
| 7 |
+
|
| 8 |
+
import argparse
|
| 9 |
+
import json
|
| 10 |
+
import sys
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
BENCH_PATH = Path(__file__).parent / "benchmark_tasks.json"
|
| 14 |
+
RESULTS_PATH = Path(__file__).parent / "results.json"
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def load_benchmark() -> list[dict]:
|
| 18 |
+
return json.loads(BENCH_PATH.read_text(encoding="utf-8"))
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def judge_answer(answer: str, task: dict) -> bool:
|
| 22 |
+
lower = answer.lower()
|
| 23 |
+
for kw in task.get("must_contain", []):
|
| 24 |
+
if kw.lower() not in lower:
|
| 25 |
+
return False
|
| 26 |
+
any_list = task.get("must_contain_any", [])
|
| 27 |
+
if any_list and not any(k.lower() in lower for k in any_list):
|
| 28 |
+
return False
|
| 29 |
+
if task.get("min_sources", 0) > 0:
|
| 30 |
+
if "http" not in answer and "[" not in answer:
|
| 31 |
+
return False
|
| 32 |
+
return True
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def _synthetic_answer(task: dict) -> str:
|
| 36 |
+
"""Build an answer that satisfies the task's keyword rules (smoke only)."""
|
| 37 |
+
bits = list(task.get("must_contain", []))
|
| 38 |
+
for k in task.get("must_contain_any", []):
|
| 39 |
+
bits.append(k)
|
| 40 |
+
bits.append("https://example.com/source")
|
| 41 |
+
return " ".join(bits) if bits else "(no constraints)"
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def _run_agent(question: str) -> str:
|
| 45 |
+
from task_agent.app import run_agent
|
| 46 |
+
|
| 47 |
+
answer, _trace = run_agent(question)
|
| 48 |
+
return answer
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def run_eval(smoke: bool = False) -> dict:
|
| 52 |
+
tasks = load_benchmark()
|
| 53 |
+
if smoke:
|
| 54 |
+
tasks = tasks[:2]
|
| 55 |
+
results = [{"id": t["id"], "pass": judge_answer(_synthetic_answer(t), t)} for t in tasks]
|
| 56 |
+
else:
|
| 57 |
+
results = []
|
| 58 |
+
for t in tasks:
|
| 59 |
+
try:
|
| 60 |
+
ans = _run_agent(t["question"])
|
| 61 |
+
except Exception as e: # noqa: BLE001 - record failure, keep going
|
| 62 |
+
ans = f"(agent error: {e})"
|
| 63 |
+
results.append({"id": t["id"], "pass": judge_answer(ans, t), "answer": ans[:500]})
|
| 64 |
+
passed = sum(1 for r in results if r["pass"])
|
| 65 |
+
report = {
|
| 66 |
+
"total": len(tasks),
|
| 67 |
+
"passed": passed,
|
| 68 |
+
"pass_rate": round(passed / len(tasks), 4),
|
| 69 |
+
"smoke": smoke,
|
| 70 |
+
"results": results,
|
| 71 |
+
}
|
| 72 |
+
RESULTS_PATH.write_text(json.dumps(report, indent=2), encoding="utf-8")
|
| 73 |
+
return report
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def main() -> int:
|
| 77 |
+
parser = argparse.ArgumentParser()
|
| 78 |
+
parser.add_argument("--smoke", action="store_true")
|
| 79 |
+
args = parser.parse_args()
|
| 80 |
+
report = run_eval(smoke=args.smoke)
|
| 81 |
+
summary = {"passed": report["passed"], "total": report["total"], "smoke": report["smoke"]}
|
| 82 |
+
print(json.dumps(summary, indent=2))
|
| 83 |
+
if not args.smoke and report["passed"] < 4:
|
| 84 |
+
return 1
|
| 85 |
+
return 0
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
sys.exit(main())
|
pyproject.toml
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "task-agent"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Autonomous research-analyst agent (LangGraph plan-act-reflect) with tool-call traces"
|
| 5 |
+
requires-python = ">=3.11"
|
| 6 |
+
readme = "README.md"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"langgraph>=0.2.60",
|
| 9 |
+
"langchain-core>=0.3.28",
|
| 10 |
+
"openai>=1.59.0",
|
| 11 |
+
"ddgs>=6.3.7",
|
| 12 |
+
"httpx>=0.28.1",
|
| 13 |
+
"trafilatura>=2.0.0",
|
| 14 |
+
"gradio>=5.9.1",
|
| 15 |
+
"pydantic>=2.10.3",
|
| 16 |
+
"python-dotenv>=1.0.1",
|
| 17 |
+
"tenacity>=9.0.0",
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
[project.optional-dependencies]
|
| 21 |
+
dev = ["pytest>=8.0", "pytest-asyncio>=0.24.0", "ruff>=0.6", "pre-commit>=3.7"]
|
| 22 |
+
fallback = ["groq>=0.13.0", "google-generativeai>=0.8.3"]
|
| 23 |
+
|
| 24 |
+
[project.scripts]
|
| 25 |
+
task-agent = "task_agent.cli:main"
|
| 26 |
+
|
| 27 |
+
[build-system]
|
| 28 |
+
requires = ["setuptools>=75.0"]
|
| 29 |
+
build-backend = "setuptools.build_meta"
|
| 30 |
+
|
| 31 |
+
[tool.setuptools.packages.find]
|
| 32 |
+
where = ["src"]
|
| 33 |
+
|
| 34 |
+
[tool.ruff]
|
| 35 |
+
line-length = 100
|
| 36 |
+
target-version = "py311"
|
| 37 |
+
|
| 38 |
+
[tool.ruff.lint]
|
| 39 |
+
select = ["E", "F", "I", "UP"]
|
| 40 |
+
|
| 41 |
+
[tool.pytest.ini_options]
|
| 42 |
+
testpaths = ["tests"]
|
| 43 |
+
asyncio_mode = "auto"
|
src/task_agent.egg-info/PKG-INFO
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Metadata-Version: 2.4
|
| 2 |
+
Name: task-agent
|
| 3 |
+
Version: 0.1.0
|
| 4 |
+
Summary: Autonomous research-analyst agent (LangGraph plan-act-reflect) with tool-call traces
|
| 5 |
+
Requires-Python: >=3.11
|
| 6 |
+
Description-Content-Type: text/markdown
|
| 7 |
+
Requires-Dist: langgraph>=0.2.60
|
| 8 |
+
Requires-Dist: langchain-core>=0.3.28
|
| 9 |
+
Requires-Dist: openai>=1.59.0
|
| 10 |
+
Requires-Dist: ddgs>=6.3.7
|
| 11 |
+
Requires-Dist: httpx>=0.28.1
|
| 12 |
+
Requires-Dist: trafilatura>=2.0.0
|
| 13 |
+
Requires-Dist: gradio>=5.9.1
|
| 14 |
+
Requires-Dist: pydantic>=2.10.3
|
| 15 |
+
Requires-Dist: python-dotenv>=1.0.1
|
| 16 |
+
Requires-Dist: tenacity>=9.0.0
|
| 17 |
+
Provides-Extra: dev
|
| 18 |
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
| 19 |
+
Requires-Dist: pytest-asyncio>=0.24.0; extra == "dev"
|
| 20 |
+
Requires-Dist: ruff>=0.6; extra == "dev"
|
| 21 |
+
Requires-Dist: pre-commit>=3.7; extra == "dev"
|
| 22 |
+
Provides-Extra: fallback
|
| 23 |
+
Requires-Dist: groq>=0.13.0; extra == "fallback"
|
| 24 |
+
Requires-Dist: google-generativeai>=0.8.3; extra == "fallback"
|
| 25 |
+
|
| 26 |
+
# task-agent — Autonomous research-analyst agent
|
| 27 |
+
|
| 28 |
+
[](https://github.com/vardh/task-agent/actions/workflows/ci.yml)
|
| 29 |
+
[](https://huggingface.co/spaces/vardh/task-agent)
|
| 30 |
+
|
| 31 |
+
A LangGraph agent that plans → acts (web_search, web_fetch, restricted python_repl, summarize) →
|
| 32 |
+
reflects → produces a cited report, with a Gradio UI showing the live tool-call trace.
|
| 33 |
+
|
| 34 |
+
## What it does
|
| 35 |
+
Give it a research question. It writes a plan, calls tools step-by-step, reflects on whether it has
|
| 36 |
+
enough evidence, then writes a final report with inline source citations. Every tool call is shown
|
| 37 |
+
in the trace panel.
|
| 38 |
+
|
| 39 |
+
## Architecture
|
| 40 |
+
```
|
| 41 |
+
question → LangGraph (plan → act → reflect → route → finalize)
|
| 42 |
+
tools: web_search (ddgs) · web_fetch (httpx+trafilatura) · python_repl (sandboxed) · summarize
|
| 43 |
+
LLM: NVIDIA NIM (nvidia/llama-3.3-nemotron-super-49b-v1), Groq/Gemini optional fallback
|
| 44 |
+
→ cited report + tool-call trace (Gradio)
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
## Skills demonstrated (mapped to JD-corpus demand)
|
| 48 |
+
|
| 49 |
+
| Skill | Demand % (1,483 AI/ML JDs) | Where in this repo |
|
| 50 |
+
|---|---|---|
|
| 51 |
+
| agent | 17% overall · 30% genai | `src/task_agent/graph.py` LangGraph loop |
|
| 52 |
+
| function calling / tool use | core genai agent skill | `src/task_agent/tools/` |
|
| 53 |
+
| planning & orchestration | agent JDs | plan→act→reflect→finalize nodes |
|
| 54 |
+
| prompt engineering | ~7% genai | planner/actor/reflector/finalizer prompts |
|
| 55 |
+
| LLM integration | 23% | `src/task_agent/llm.py` (NVIDIA NIM) |
|
| 56 |
+
| CI/CD · Docker | 13% | `.github/workflows/ci.yml`, `Dockerfile` |
|
| 57 |
+
|
| 58 |
+
## Eval results
|
| 59 |
+
| Task | Pass |
|
| 60 |
+
|---|---|
|
| 61 |
+
| b1 Kaggle LLM prizes | see `eval/results.json` |
|
| 62 |
+
| b2 HF Open LLM Leaderboard | see `eval/results.json` |
|
| 63 |
+
| b3 LangGraph PyPI Python version | see `eval/results.json` |
|
| 64 |
+
| b4 sum of squares 1..20 = 2870 | deterministic |
|
| 65 |
+
| b5 2024 Nobel Physics | see `eval/results.json` |
|
| 66 |
+
|
| 67 |
+
Threshold: **>=4/5**. Smoke (mocked) runs in CI; full live eval needs `NVIDIA_API_KEY` + network.
|
| 68 |
+
|
| 69 |
+
## Run locally
|
| 70 |
+
```powershell
|
| 71 |
+
pip install -e ".[dev]"
|
| 72 |
+
python -m task_agent.app # Gradio at http://127.0.0.1:7860
|
| 73 |
+
# or CLI:
|
| 74 |
+
python -m task_agent.cli "Who won the 2024 Nobel Prize in Physics?"
|
| 75 |
+
```
|
| 76 |
+
|
| 77 |
+
## How it was built (agent-driven note)
|
| 78 |
+
Built by Claude Code via TDD micro-steps. NVIDIA NIM is the primary LLM (OpenAI-compatible);
|
| 79 |
+
Groq/Gemini are optional fallbacks only. Sandbox is AST-vetted and cross-platform (no Unix-only
|
| 80 |
+
signals). CI runs a fully mocked smoke; live benchmark run is local-only to bound API spend.
|
src/task_agent.egg-info/SOURCES.txt
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
README.md
|
| 2 |
+
pyproject.toml
|
| 3 |
+
src/task_agent/__init__.py
|
| 4 |
+
src/task_agent/app.py
|
| 5 |
+
src/task_agent/cli.py
|
| 6 |
+
src/task_agent/graph.py
|
| 7 |
+
src/task_agent/llm.py
|
| 8 |
+
src/task_agent.egg-info/PKG-INFO
|
| 9 |
+
src/task_agent.egg-info/SOURCES.txt
|
| 10 |
+
src/task_agent.egg-info/dependency_links.txt
|
| 11 |
+
src/task_agent.egg-info/entry_points.txt
|
| 12 |
+
src/task_agent.egg-info/requires.txt
|
| 13 |
+
src/task_agent.egg-info/top_level.txt
|
| 14 |
+
src/task_agent/tools/__init__.py
|
| 15 |
+
src/task_agent/tools/python_repl.py
|
| 16 |
+
src/task_agent/tools/summarize.py
|
| 17 |
+
src/task_agent/tools/web_fetch.py
|
| 18 |
+
src/task_agent/tools/web_search.py
|
| 19 |
+
tests/test_app.py
|
| 20 |
+
tests/test_eval_runner.py
|
| 21 |
+
tests/test_graph.py
|
| 22 |
+
tests/test_llm.py
|
| 23 |
+
tests/test_python_repl.py
|
| 24 |
+
tests/test_summarize.py
|
| 25 |
+
tests/test_web_fetch.py
|
| 26 |
+
tests/test_web_search.py
|
src/task_agent.egg-info/dependency_links.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
|
src/task_agent.egg-info/entry_points.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[console_scripts]
|
| 2 |
+
task-agent = task_agent.cli:main
|
src/task_agent.egg-info/requires.txt
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langgraph>=0.2.60
|
| 2 |
+
langchain-core>=0.3.28
|
| 3 |
+
openai>=1.59.0
|
| 4 |
+
ddgs>=6.3.7
|
| 5 |
+
httpx>=0.28.1
|
| 6 |
+
trafilatura>=2.0.0
|
| 7 |
+
gradio>=5.9.1
|
| 8 |
+
pydantic>=2.10.3
|
| 9 |
+
python-dotenv>=1.0.1
|
| 10 |
+
tenacity>=9.0.0
|
| 11 |
+
|
| 12 |
+
[dev]
|
| 13 |
+
pytest>=8.0
|
| 14 |
+
pytest-asyncio>=0.24.0
|
| 15 |
+
ruff>=0.6
|
| 16 |
+
pre-commit>=3.7
|
| 17 |
+
|
| 18 |
+
[fallback]
|
| 19 |
+
groq>=0.13.0
|
| 20 |
+
google-generativeai>=0.8.3
|
src/task_agent.egg-info/top_level.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
task_agent
|
src/task_agent/__init__.py
ADDED
|
File without changes
|
src/task_agent/app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
from task_agent.graph import build_graph
|
| 8 |
+
|
| 9 |
+
INITIAL_STATE = {
|
| 10 |
+
"question": "",
|
| 11 |
+
"plan": "",
|
| 12 |
+
"messages": [],
|
| 13 |
+
"observations": [],
|
| 14 |
+
"trace": [],
|
| 15 |
+
"step_count": 0,
|
| 16 |
+
"answer": "",
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def format_trace(trace: list[dict]) -> str:
|
| 21 |
+
lines = []
|
| 22 |
+
for i, t in enumerate(trace, 1):
|
| 23 |
+
lines.append(f"### Step {i}: `{t['tool']}`")
|
| 24 |
+
lines.append(f"**Input:** `{json.dumps(t['input'])}`")
|
| 25 |
+
lines.append("**Output:**")
|
| 26 |
+
lines.append(f"```\n{str(t['output'])[:1500]}\n```\n")
|
| 27 |
+
return "\n".join(lines) if lines else "_No tool calls yet._"
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def run_agent(question: str):
|
| 31 |
+
graph = build_graph()
|
| 32 |
+
state = {**INITIAL_STATE, "question": question}
|
| 33 |
+
result = graph.invoke(state)
|
| 34 |
+
return result.get("answer", ""), format_trace(result.get("trace", []))
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def build_ui():
|
| 38 |
+
with gr.Blocks(title="task-agent") as demo:
|
| 39 |
+
gr.Markdown("# Research Analyst Agent")
|
| 40 |
+
q = gr.Textbox(label="Research question", lines=2)
|
| 41 |
+
run_btn = gr.Button("Run", variant="primary")
|
| 42 |
+
report = gr.Markdown(label="Report")
|
| 43 |
+
trace = gr.Markdown(label="Tool-call trace")
|
| 44 |
+
run_btn.click(run_agent, inputs=q, outputs=[report, trace])
|
| 45 |
+
return demo
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def main():
|
| 49 |
+
build_ui().launch()
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
main()
|
src/task_agent/cli.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import argparse
|
| 4 |
+
|
| 5 |
+
from task_agent.app import run_agent
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def main():
|
| 9 |
+
parser = argparse.ArgumentParser(description="task-agent research analyst")
|
| 10 |
+
parser.add_argument("question", nargs="?", help="Research question")
|
| 11 |
+
parser.add_argument("--smoke", action="store_true", help="Run a built-in demo question")
|
| 12 |
+
args = parser.parse_args()
|
| 13 |
+
question = args.question or (
|
| 14 |
+
"What won the 2024 Nobel Prize in Physics?" if args.smoke else None
|
| 15 |
+
)
|
| 16 |
+
if not question:
|
| 17 |
+
parser.error("Provide a research question (or --smoke).")
|
| 18 |
+
answer, trace = run_agent(question)
|
| 19 |
+
print(answer)
|
| 20 |
+
print("\n--- trace ---")
|
| 21 |
+
print(trace)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
if __name__ == "__main__":
|
| 25 |
+
main()
|
src/task_agent/graph.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
import operator
|
| 5 |
+
from typing import Annotated, Literal, TypedDict
|
| 6 |
+
|
| 7 |
+
from langgraph.graph import END, StateGraph
|
| 8 |
+
|
| 9 |
+
from task_agent.llm import chat_complete
|
| 10 |
+
from task_agent.tools.python_repl import python_repl
|
| 11 |
+
from task_agent.tools.summarize import summarize
|
| 12 |
+
from task_agent.tools.web_fetch import web_fetch
|
| 13 |
+
from task_agent.tools.web_search import web_search
|
| 14 |
+
|
| 15 |
+
MAX_STEPS = 6
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class AgentState(TypedDict, total=False):
|
| 19 |
+
question: str
|
| 20 |
+
plan: str
|
| 21 |
+
messages: Annotated[list[dict], operator.add]
|
| 22 |
+
observations: Annotated[list[str], operator.add]
|
| 23 |
+
trace: Annotated[list[dict], operator.add]
|
| 24 |
+
step_count: int
|
| 25 |
+
answer: str
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _append_trace(tool: str, input_data: dict, output: str) -> list[dict]:
|
| 29 |
+
return [{"tool": tool, "input": input_data, "output": str(output)[:2000]}]
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def plan_node(state: AgentState) -> dict:
|
| 33 |
+
prompt = (
|
| 34 |
+
"You are a research analyst. Given the question, write a numbered plan (<=5 steps).\n"
|
| 35 |
+
f"Question: {state['question']}\n"
|
| 36 |
+
"Respond with PLAN: followed by steps."
|
| 37 |
+
)
|
| 38 |
+
plan = chat_complete([{"role": "user", "content": prompt}])
|
| 39 |
+
return {"plan": plan, "messages": [{"role": "assistant", "content": plan}], "step_count": 0}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def act_node(state: AgentState) -> dict:
|
| 43 |
+
"""LLM chooses one tool call as JSON {"tool":..., "args":{...}}."""
|
| 44 |
+
tool_prompt = (
|
| 45 |
+
f"Question: {state['question']}\n"
|
| 46 |
+
f"Plan: {state.get('plan', '')}\n"
|
| 47 |
+
f"Observations so far: {state.get('observations', [])[-3:]}\n"
|
| 48 |
+
"Choose ONE tool. Reply ONLY with JSON:\n"
|
| 49 |
+
'{"tool":"web_search","args":{"query":"..."}}\n'
|
| 50 |
+
'or {"tool":"web_fetch","args":{"url":"..."}}\n'
|
| 51 |
+
'or {"tool":"python_repl","args":{"code":"..."}}\n'
|
| 52 |
+
'or {"tool":"summarize","args":{"text":"..."}}'
|
| 53 |
+
)
|
| 54 |
+
raw = chat_complete([{"role": "user", "content": tool_prompt}])
|
| 55 |
+
try:
|
| 56 |
+
call = json.loads(raw.strip().strip("`").replace("json", "", 1))
|
| 57 |
+
except json.JSONDecodeError:
|
| 58 |
+
call = {"tool": "web_search", "args": {"query": state["question"]}}
|
| 59 |
+
|
| 60 |
+
tool_name = call.get("tool", "web_search")
|
| 61 |
+
args = call.get("args", {}) or {}
|
| 62 |
+
if tool_name == "web_search":
|
| 63 |
+
out = web_search(args.get("query", state["question"]))
|
| 64 |
+
obs = json.dumps(out[:3])
|
| 65 |
+
elif tool_name == "web_fetch":
|
| 66 |
+
out = web_fetch(args["url"])
|
| 67 |
+
obs = out["text"][:1500]
|
| 68 |
+
elif tool_name == "python_repl":
|
| 69 |
+
out = python_repl(args.get("code", "print(1)"))
|
| 70 |
+
obs = out
|
| 71 |
+
elif tool_name == "summarize":
|
| 72 |
+
out = summarize(args.get("text", ""))
|
| 73 |
+
obs = out
|
| 74 |
+
else:
|
| 75 |
+
obs = f"unknown tool {tool_name}"
|
| 76 |
+
|
| 77 |
+
return {
|
| 78 |
+
"observations": [obs],
|
| 79 |
+
"trace": _append_trace(tool_name, args, str(out)),
|
| 80 |
+
"step_count": state.get("step_count", 0) + 1,
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def reflect_node(state: AgentState) -> dict:
|
| 85 |
+
prompt = (
|
| 86 |
+
f"Question: {state['question']}\n"
|
| 87 |
+
f"Observations: {state.get('observations', [])}\n"
|
| 88 |
+
"Is research sufficient to write a final cited report? "
|
| 89 |
+
"Reply YES or NO and one sentence why."
|
| 90 |
+
)
|
| 91 |
+
verdict = chat_complete([{"role": "user", "content": prompt}])
|
| 92 |
+
return {"messages": [{"role": "assistant", "content": verdict}]}
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def finalize_node(state: AgentState) -> dict:
|
| 96 |
+
prompt = (
|
| 97 |
+
"Write a final research report with inline [source URL] citations.\n"
|
| 98 |
+
f"Question: {state['question']}\n"
|
| 99 |
+
f"Observations: {state.get('observations', [])}"
|
| 100 |
+
)
|
| 101 |
+
answer = chat_complete([{"role": "user", "content": prompt}])
|
| 102 |
+
return {"answer": answer}
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def route_after_reflect(state: AgentState) -> Literal["act", "finalize"]:
|
| 106 |
+
messages = state.get("messages", [])
|
| 107 |
+
last = messages[-1]["content"].upper() if messages else ""
|
| 108 |
+
if state.get("step_count", 0) >= MAX_STEPS:
|
| 109 |
+
return "finalize"
|
| 110 |
+
if "YES" in last:
|
| 111 |
+
return "finalize"
|
| 112 |
+
return "act"
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def build_graph():
|
| 116 |
+
g = StateGraph(AgentState)
|
| 117 |
+
g.add_node("plan", plan_node)
|
| 118 |
+
g.add_node("act", act_node)
|
| 119 |
+
g.add_node("reflect", reflect_node)
|
| 120 |
+
g.add_node("finalize", finalize_node)
|
| 121 |
+
|
| 122 |
+
g.set_entry_point("plan")
|
| 123 |
+
g.add_edge("plan", "act")
|
| 124 |
+
g.add_edge("act", "reflect")
|
| 125 |
+
g.add_conditional_edges("reflect", route_after_reflect, {"act": "act", "finalize": "finalize"})
|
| 126 |
+
g.add_edge("finalize", END)
|
| 127 |
+
return g.compile()
|
src/task_agent/llm.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
from tenacity import retry, stop_after_attempt, wait_exponential
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class LLMError(Exception):
|
| 10 |
+
pass
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def _nim_key() -> str:
|
| 14 |
+
key = os.getenv("NVIDIA_API_KEY") or os.getenv("NIM_API_KEY")
|
| 15 |
+
if not key:
|
| 16 |
+
raise LLMError("Set NVIDIA_API_KEY or NIM_API_KEY")
|
| 17 |
+
return key
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@retry(stop=stop_after_attempt(2), wait=wait_exponential(min=1, max=8), reraise=True)
|
| 21 |
+
def _nvidia_nim_chat(messages: list[dict[str, str]]) -> str:
|
| 22 |
+
client = OpenAI(
|
| 23 |
+
base_url=os.getenv("NVIDIA_NIM_BASE_URL", "https://integrate.api.nvidia.com/v1"),
|
| 24 |
+
api_key=_nim_key(),
|
| 25 |
+
)
|
| 26 |
+
resp = client.chat.completions.create(
|
| 27 |
+
model=os.getenv("NVIDIA_NIM_MODEL", "nvidia/llama-3.3-nemotron-super-49b-v1"),
|
| 28 |
+
messages=messages,
|
| 29 |
+
temperature=0.2,
|
| 30 |
+
top_p=0.7,
|
| 31 |
+
max_tokens=2048,
|
| 32 |
+
stream=False,
|
| 33 |
+
)
|
| 34 |
+
return resp.choices[0].message.content or ""
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def chat_complete(messages: list[dict[str, str]]) -> str:
|
| 38 |
+
"""NVIDIA NIM primary. Groq/Gemini are optional fallback paths (not required for MVP)."""
|
| 39 |
+
try:
|
| 40 |
+
return _nvidia_nim_chat(messages)
|
| 41 |
+
except Exception as e: # noqa: BLE001 - surface a typed error to the graph
|
| 42 |
+
raise LLMError(f"NVIDIA NIM chat failed: {e}") from e
|
src/task_agent/tools/__init__.py
ADDED
|
File without changes
|
src/task_agent/tools/python_repl.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Restricted Python REPL sandbox (cross-platform).
|
| 2 |
+
|
| 3 |
+
Safety: AST walk rejects imports, file/network builtins, and dangerous calls BEFORE execution.
|
| 4 |
+
Execution runs in a worker thread with a timeout (works on Windows and Linux; signal.SIGALRM
|
| 5 |
+
is Unix-only). REPL-like: if the last statement is a bare expression with no stdout, its value
|
| 6 |
+
is returned (so ``2 + 2`` -> ``"4"``).
|
| 7 |
+
"""
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
import ast
|
| 11 |
+
import io
|
| 12 |
+
from contextlib import redirect_stdout
|
| 13 |
+
from multiprocessing import get_context
|
| 14 |
+
|
| 15 |
+
_ALLOWED_BUILTINS = {
|
| 16 |
+
"abs": abs,
|
| 17 |
+
"all": all,
|
| 18 |
+
"any": any,
|
| 19 |
+
"bool": bool,
|
| 20 |
+
"dict": dict,
|
| 21 |
+
"enumerate": enumerate,
|
| 22 |
+
"filter": filter,
|
| 23 |
+
"float": float,
|
| 24 |
+
"int": int,
|
| 25 |
+
"len": len,
|
| 26 |
+
"list": list,
|
| 27 |
+
"map": map,
|
| 28 |
+
"max": max,
|
| 29 |
+
"min": min,
|
| 30 |
+
"print": print,
|
| 31 |
+
"range": range,
|
| 32 |
+
"round": round,
|
| 33 |
+
"set": set,
|
| 34 |
+
"sorted": sorted,
|
| 35 |
+
"str": str,
|
| 36 |
+
"sum": sum,
|
| 37 |
+
"tuple": tuple,
|
| 38 |
+
"zip": zip,
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
_FORBIDDEN_NAMES = {
|
| 42 |
+
"import",
|
| 43 |
+
"open",
|
| 44 |
+
"exec",
|
| 45 |
+
"eval",
|
| 46 |
+
"__import__",
|
| 47 |
+
"compile",
|
| 48 |
+
"globals",
|
| 49 |
+
"locals",
|
| 50 |
+
"getattr",
|
| 51 |
+
"setattr",
|
| 52 |
+
"delattr",
|
| 53 |
+
"input",
|
| 54 |
+
"help",
|
| 55 |
+
"dir",
|
| 56 |
+
"vars",
|
| 57 |
+
"memoryview",
|
| 58 |
+
"breakpoint",
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
class _SafetyVisitor(ast.NodeVisitor):
|
| 63 |
+
def generic_visit(self, node: ast.AST) -> None:
|
| 64 |
+
if isinstance(node, ast.Import | ast.ImportFrom):
|
| 65 |
+
raise PermissionError("import not allowed in sandbox")
|
| 66 |
+
if isinstance(node, ast.Attribute) and node.attr.startswith("__"):
|
| 67 |
+
raise PermissionError("dunder attribute access not allowed in sandbox")
|
| 68 |
+
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
|
| 69 |
+
if node.func.id in _FORBIDDEN_NAMES:
|
| 70 |
+
raise PermissionError(f"{node.func.id}() not allowed in sandbox")
|
| 71 |
+
if isinstance(node, ast.Name) and node.id in _FORBIDDEN_NAMES:
|
| 72 |
+
raise PermissionError(f"{node.id} not allowed in sandbox")
|
| 73 |
+
super().generic_visit(node)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def _execute(tree: ast.Module) -> str:
|
| 77 |
+
glb: dict = {"__builtins__": _ALLOWED_BUILTINS}
|
| 78 |
+
buf = io.StringIO()
|
| 79 |
+
with redirect_stdout(buf):
|
| 80 |
+
exec(compile(tree, "<repl>", "exec"), glb, {}) # noqa: S102 - sandboxed builtins only
|
| 81 |
+
out = buf.getvalue()
|
| 82 |
+
if not out.strip() and tree.body and isinstance(tree.body[-1], ast.Expr):
|
| 83 |
+
val = eval( # noqa: S307 - AST-vetted expression only
|
| 84 |
+
compile(ast.Expression(body=tree.body[-1].value), "<repl>", "eval"), glb, {}
|
| 85 |
+
)
|
| 86 |
+
out = repr(val) if val is not None else ""
|
| 87 |
+
return out.strip() or "(no output)"
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def _execute_worker(code: str, conn) -> None:
|
| 91 |
+
try:
|
| 92 |
+
tree = ast.parse(code, mode="exec")
|
| 93 |
+
_SafetyVisitor().visit(tree)
|
| 94 |
+
conn.send(("ok", _execute(tree)))
|
| 95 |
+
except BaseException as exc: # noqa: BLE001 - return exception details to parent
|
| 96 |
+
conn.send(("err", exc.__class__.__name__, str(exc)))
|
| 97 |
+
finally:
|
| 98 |
+
conn.close()
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def python_repl(code: str, timeout_sec: int = 5) -> str:
|
| 102 |
+
"""Execute restricted Python; no imports, no file I/O, hard timeout."""
|
| 103 |
+
tree = ast.parse(code, mode="exec")
|
| 104 |
+
_SafetyVisitor().visit(tree)
|
| 105 |
+
|
| 106 |
+
ctx = get_context("spawn")
|
| 107 |
+
parent_conn, child_conn = ctx.Pipe(duplex=False)
|
| 108 |
+
proc = ctx.Process(target=_execute_worker, args=(code, child_conn), daemon=True)
|
| 109 |
+
proc.start()
|
| 110 |
+
child_conn.close()
|
| 111 |
+
|
| 112 |
+
if not parent_conn.poll(timeout_sec):
|
| 113 |
+
proc.terminate()
|
| 114 |
+
proc.join(timeout=1)
|
| 115 |
+
if proc.is_alive():
|
| 116 |
+
proc.kill()
|
| 117 |
+
proc.join(timeout=1)
|
| 118 |
+
raise TimeoutError("python_repl exceeded timeout")
|
| 119 |
+
|
| 120 |
+
try:
|
| 121 |
+
status, *payload = parent_conn.recv()
|
| 122 |
+
finally:
|
| 123 |
+
parent_conn.close()
|
| 124 |
+
proc.join(timeout=1)
|
| 125 |
+
|
| 126 |
+
if status == "ok":
|
| 127 |
+
return payload[0]
|
| 128 |
+
error_name, message = payload
|
| 129 |
+
if error_name == "PermissionError":
|
| 130 |
+
raise PermissionError(message)
|
| 131 |
+
if error_name == "SyntaxError":
|
| 132 |
+
raise SyntaxError(message)
|
| 133 |
+
raise RuntimeError(message)
|
src/task_agent/tools/summarize.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from task_agent.llm import chat_complete
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def summarize(text: str, max_words: int = 200) -> str:
|
| 7 |
+
prompt = f"Summarize the following in <={max_words} words. Be factual.\n\n{text[:8000]}"
|
| 8 |
+
return chat_complete([{"role": "user", "content": prompt}])
|
src/task_agent/tools/web_fetch.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import httpx
|
| 4 |
+
import trafilatura
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def web_fetch(url: str, timeout: float = 15.0, max_chars: int = 12_000) -> dict[str, str | int]:
|
| 8 |
+
"""Fetch URL and extract main text with trafilatura."""
|
| 9 |
+
resp = httpx.get(
|
| 10 |
+
url,
|
| 11 |
+
timeout=timeout,
|
| 12 |
+
follow_redirects=True,
|
| 13 |
+
headers={"User-Agent": "task-agent/0.1"},
|
| 14 |
+
)
|
| 15 |
+
resp.raise_for_status()
|
| 16 |
+
text = trafilatura.extract(resp.text, include_comments=False, include_tables=True) or ""
|
| 17 |
+
text = text[:max_chars]
|
| 18 |
+
return {"url": url, "text": text, "char_count": len(text)}
|
src/task_agent/tools/web_search.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from ddgs import DDGS
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def web_search(query: str, max_results: int = 5) -> list[dict[str, str]]:
|
| 7 |
+
"""Search DuckDuckGo; return list of {title, url, snippet}."""
|
| 8 |
+
with DDGS() as ddgs:
|
| 9 |
+
hits = list(ddgs.text(query, max_results=max_results))
|
| 10 |
+
return [
|
| 11 |
+
{"title": h.get("title", ""), "url": h.get("href", ""), "snippet": h.get("body", "")}
|
| 12 |
+
for h in hits
|
| 13 |
+
]
|