salmeida/langchain-agent
0
Gray Matter Research Agent
Agentic scientific research API that plans, searches, ranks evidence, and synthesizes grounded answers using Groq (Llama 3.3 70B), LangChain, FastAPI, arXiv, Wikipedia, web search, and a deterministic calculator.
Unlike a traditional RAG chatbot that retrieves once and answers, Gray Matter classifies intent, builds a research plan, runs multiple tools when needed, ranks evidence, synthesizes with citations, and verifies the answer before returning.
Architecture
flowchart TB
U[Client / Frontend] --> API[FastAPI]
API --> G[Research Graph]
G --> C[Intent Classifier]
C --> P[Planner]
P --> T[Multi-Tool Executor]
T --> A[ArXiv Ranked Search]
T --> W[Wikipedia]
T --> D[DuckDuckGo]
T --> M[Calculator]
T --> E[Evidence Ranker]
E --> S[Synthesizer + Groq]
S --> V[Verifier]
V --> API
API --> UPipeline steps
- Classify — LLM + heuristic fallback → intent, tools, depth, query rewrite
- Plan — 2–5 operational steps (not chain-of-thought)
- Execute — arXiv + web + Wikipedia + calculator as needed
- Rank — normalize sources into a common evidence format
- Synthesize — grounded answer with sources & limitations
- Verify — flag invented URLs, unsupported paper claims, false recency
vs Traditional RAG Chatbot
Quick start (local)
git clone https://github.com/sidnei-almeida/langchain-autonomous-agent.git
cd langchain-autonomous-agent
python -m venv venv && source venv/bin/activate
pip install -r requirements.txt
echo "GROQ_API_KEY=your_key" > .env
python app.pyOpen http://localhost:7860/docs
CLI
python -m agent "latest papers about agentic RAG"API examples
POST /api/query
curl -X POST http://localhost:7860/api/query \
-H "Content-Type: application/json" \
-d '{"question": "Explain quantum entanglement"}'POST /api/research (deep mode)
curl -X POST http://localhost:7860/api/research \
-H "Content-Type: application/json" \
-d '{"question": "Compare RAG and agentic RAG", "depth": "deep", "max_sources": 12}'Example response (abridged)
{
"answer": "…synthesis with Sources used section…",
"question": "latest papers about agentic RAG",
"tools_used": ["search_scientific_papers", "web_search"],
"intent": "mixed_research",
"research_plan": [
"Identify the scientific topic",
"Search arXiv for recent papers",
"Search web for current context",
"Synthesize answer with sources"
],
"papers": [
{
"title": "…",
"year": 2024,
"url": "https://arxiv.org/abs/…",
"relevanceScore": 18,
"whyItMatches": "…"
}
],
"sources": [
{
"title": "…",
"url": "https://…",
"source_type": "arxiv",
"relevance_score": 0.85,
"used_in_answer": true
}
],
"confidence": 0.78,
"limitations": [],
"follow_up_questions": ["Would you like a deeper summary of any specific paper?"],
"processing_time": 8.4
}Clarification example
Query: "paper about human interactions"
{
"answer": "Human interactions is broad. Do you mean human-computer interaction…",
"intent": "paper_search",
"confidence": 0.3,
"limitations": ["Query needs clarification before research can proceed."]
}Endpoints
Configuration
Project structure
agent/
state.py # ResearchState, EvidenceItem, IntentResult
router.py # Intent classifier (LLM + fallback)
planner.py # Research plan builder
tools.py # Multi-tool executor
evidence.py # Source ranking
synthesizer.py # Answer generation
verifier.py # Claim verification
graph.py # Pipeline orchestration
arxiv_search.py # Ranked arXiv with ambiguity detection
api.py # FastAPI routesScreenshots
<!-- Portfolio placeholders --> | Swagger UI | Research response | |---|---| | Add screenshot: `/docs` | Add screenshot: paper results JSON |
Limitations
- Heuristic + LLM routing may misclassify edge cases
- Groq rate limits apply on HF Spaces
- No persistent memory across sessions
- Calculator uses sandboxed
eval— not a security boundary for untrusted multi-tenant input - Verifier is rule-based, not a full fact-checker
Roadmap
- [ ] Native LangGraph tool-calling mode (feature flag)
- [ ] Streaming responses (
/api/research/stream) - [ ] Redis cache for arXiv queries
- [ ] Frontend: Gray Matter LABS chat UI
- [ ] Semantic reranker for evidence
Hugging Face Spaces
- SDK: Docker · Port: 7860
- Secret:
GROQ_API_KEY - Optional:
GRAY_MATTER_API_KEY,CORS_ORIGINS
See README_HF.md.
License
MIT — LICENSE · Maintainer: @sidnei-almeida
