An intelligent multi-LLM FAQ answering system with reflective validation. This agent generates answers to FAQ questions using Groq's LLaMA 3.3 (70B), then validates them using a local Ollama LLaMA 3.1 (8B) model. Invalid answers automatically retry up to 3 times before being marked as unanswerable.
- Dual-Model Architecture: Separate LLMs for generation and validation
- Generator: Groq LLaMA 3.3-70b (answer creation)
- Validator: Local Ollama LLaMA 3.1-8b (quality assurance)
- Reflective Loop: Automatic retry mechanism for invalid answers (up to 3 attempts)
- Structured Outputs: Pydantic-validated responses for both generation and validation
- LangGraph Orchestration: State-machine-based workflow with conditional routing
- Comprehensive Logging: Detailed console output tracking each step
- JSON I/O: Read questions from
inputs/input.json, write results tooutputs/output.json
| Component | Purpose |
|---|---|
graph.py |
LangGraph state machine; defines nodes, edges, and routing logic |
state.py |
Pydantic models for structured outputs; shared state schema |
llm.py |
LLM initialization; Groq + Ollama configuration |
main.py |
Entry point; loads input, runs graph, saves output |
nodes/ |
Four LangGraph nodes: load, generate, validate, save |
- load_question_node: Picks next question; resets per-question state
- generate_node: Sends question to Groq LLM; parses structured answer
- validate_node: Sends answer to Ollama LLM; judges validity
- save_result_node: Records result (answered/no_answer); advances queue
- Python 3.10+
- Groq API Key (free tier available at groq.com)
- Ollama installed locally with
llama3.1:8bmodel pulledollama pull llama3.1:8b
-
Clone/download the project:
cd reflective-agent -
Create a Python virtual environment:
python -m venv venv source venv/bin/activate # Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables: Create a
.envfile in the project root:GROQ_API_KEY=your_groq_api_key_here OLLAMA_BASE_URL=http://localhost:11434 # Optional; defaults to this
python main.pyOutput:
- Console: Summary with β (answered) / β (no_answer) symbols per question
- JSON: Detailed results written to
outputs/output.json
reflective-agent/
βββ main.py # Entry point
βββ graph.py # LangGraph definition
βββ state.py # Pydantic models & shared state
βββ llm.py # LLM configuration
βββ requirements.txt # Python dependencies
βββ .env # API keys (not in repo)
βββ nodes/
β βββ load_question_node.py # Load next question
β βββ generate_node.py # Groq generation
β βββ validate_node.py # Ollama validation
β βββ save_result_node.py # Result persistence
βββ inputs/
β βββ input.json # FAQ questions (faqQuestion, Question)
βββ outputs/
β βββ output.json # Generated answers + validation results
βββ graph_image.png # Mermaid diagram of LangGraph flow
[
{
"faqQuestion": 1,
"Question": "What is TensorFlow?"
},
{
"faqQuestion": 2,
"Question": "What is the difference between supervised and unsupervised learning?"
}
]{
"totalQuestions": 2,
"processedAt": "2026-05-12T10:18:13.994790+00:00",
"results": [
{
"faqQuestion": 1,
"Question": "What is TensorFlow?",
"iterationCount": 1,
"status": "answered",
"Answer": "TensorFlow is an open-source machine learning library...",
"checkedAnswer": true,
"remarks": "Accurate and informative."
},
{
"faqQuestion": 3,
"Question": "What is a neural network?",
"iterationCount": 3,
"status": "no_answer",
"Answer": "-",
"checkedAnswer": false,
"remarks": "Max retries exhausted without valid answer."
}
]
}Edit graph.py to adjust max retries:
MAX_RETRIES = 3 # Change this valueGroq (Generation):
- Model:
llama-3.3-70b-versatile - Temperature:
0.3(low for factual answers) - Max tokens:
1024
Ollama (Validation):
- Model:
llama3.1:8b - Temperature:
0.1(very low for strict validation)
Edit llm.py to modify these settings.
- Agent loads FAQ questions from JSON
- For each question:
- Sends to Groq LLM with a generation prompt
- Groq returns structured answer (text + confidence score)
- Validates with Ollama LLM using a validation prompt
- If invalid and retries remain: try again with original question
- If valid or retries exhausted: save result and move to next question
- Compilation: Graph generates Mermaid diagram (
graph_image.png) - Output: All results written to
outputs/output.jsonwith timestamps
-
Create
nodes/my_node.py:from state import FAQAgentState def my_node(state: FAQAgentState) -> FAQAgentState: # Your logic here return {**state, ...}
-
Register in
graph.py:graph.add_node("my_node", my_node) graph.add_edge("previous_node", "my_node")
Edit the *_PROMPT templates in nodes/generate_node.py and nodes/validate_node.py.
| Package | Purpose |
|---|---|
langchain |
LLM abstraction layer |
langgraph |
State machine orchestration |
langchain-groq |
Groq integration |
langchain-ollama |
Ollama integration |
pydantic |
Data validation & structured outputs |
python-dotenv |
Environment variable management |
| Issue | Solution |
|---|---|
GROQ_API_KEY not found |
Add GROQ_API_KEY to .env file |
Ollama connection failed |
Ensure Ollama is running (ollama serve) and model is pulled |
llama3.1:8b not found |
Run ollama pull llama3.1:8b |
| Input file not found | Ensure inputs/input.json exists in project root |
============================================================
FAQ Reflection Agent β Starting
============================================================
[Main] Loaded 5 questions from inputs/input.json
[Main] Running graph for 5 question(s)...
============================================================
[LoadQuestion] Processing FAQ #1: What is TensorFlow?
============================================================
[GenerateNode] Q1 | Attempt 1/3 | Question: What is TensorFlow?
[GenerateNode] Generated answer (confidence=0.92): TensorFlow is an open-source...
[ValidateNode] Q1 | Attempt 1/3 | Validating answer...
[ValidateNode] Valid
[SaveResultNode] Q1 | Saving result...
...
============================================================
Summary
============================================================
β
Q1 | What is TensorFlow? β Answered in 1 iteration
β
Q2 | What is supervised learning? β Answered in 1 iteration
β
Q3 | What is neural networks? β Answered in 1 iteration
β
Q4 | What is gradient descent? β Answered in 1 iteration
β
Q5 | What is transfer learning? β Answered in 1 iteration
[Main] Results saved β outputs/output.json
This project is provided as-is for educational and training purposes.
