A conversational chatbot powered by Meta-Llama-3-8B-Instruct via the HuggingFace Inference API, with real-time safety validation using TrustGuard and an LLM-as-Judge β no GPU required.
Every message goes through two LLM calls:
You
β
βΌ
Llama-3 (chatbot) β generates the answer
β
βΌ
Llama-3 (safety judge) β evaluates the answer
β
βΌ
TrustGuard β approves or blocks
β
βΌ
Bot reply β
or π Blocked
The judge uses the same model but a different system prompt, asking it to evaluate the chatbot's response for safety before it reaches you. Instead of hardcoded rules, the judge understands context β it allows coding help, math, and general knowledge, while blocking hate speech, violence, explicit content, and malware.
- π¬ Conversational memory β the full chat history is sent with every request so the bot remembers context
- π‘οΈ LLM-as-Judge β a second LLM call validates every response before it is shown
- β‘ No GPU needed β runs entirely through the HuggingFace Inference API
- π Follow-up shortcuts β type
yes,more,ok,continue, orgo onto automatically expand on the last reply - π Secure config β API token loaded from a
.envfile viapython-dotenv
- Python 3.9+
uvpackage manager- A HuggingFace account with API token
- Access granted to meta-llama/Meta-Llama-3-8B-Instruct (free, requires accepting the license)
# macOS / Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
# Windows
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"git clone https://github.com/your-username/chatguard.git
cd chatguarduv init
uv add trustguard huggingface_hub python-dotenvcp .env.example .envThen open .env and add your HuggingFace token:
HF_TOKEN=hf_your_token_here
Get your token at: https://huggingface.co/settings/tokens
uv run python chatbot.pyπ€ Chatbot (HF API + LLM Judge) | type 'quit' to exit
You: what is a binary search tree?
Bot: A binary search tree (BST) is a data structure where each node has ...
You: more
Bot: To expand further β insertion in a BST works by comparing ...
You: quit
Instead of repeating context, just type one of these to expand on the last reply:
| Keyword | Action |
|---|---|
yes |
Expand on last reply |
more |
Expand on last reply |
ok |
Expand on last reply |
continue |
Expand on last reply |
go on |
Expand on last reply |
chatguard/
βββ chatbot.py # Main application
βββ .env # Your API token (never commit this)
βββ .env.example # Template for .env
βββ .gitignore
βββ README.md
The LLMJudge class extends TrustGuard's BaseJudge. It sends the chatbot's response to the LLM with this instruction:
"Mark a response as unsafe ONLY if it contains: hate speech, violence, self-harm, explicit sexual content, or real malware/exploits. Coding help, math, and general knowledge are always SAFE."
The judge returns a structured verdict:
{
"safe": true,
"reason": "General coding help β always safe",
"confidence": 0.95
}If safe is false, TrustGuard blocks the response and shows the reason.
| Package | Purpose |
|---|---|
huggingface_hub |
HuggingFace Inference API client |
trustguard |
Output validation and judge framework |
python-dotenv |
Load .env config file |
uv add trustguard huggingface_hub python-dotenvMake sure your .env is never committed:
.env
__pycache__/
.venv/
MIT