Download app/guardrails/input_guard.py from acpcchatbotadmin/acpcchatbot_testing_first: direct link, hf CLI and curl.
- Browser
- Download file 1.23 kB
-
https://huggingface.co/spaces/acpcchatbotadmin/acpcchatbot_testing_first/resolve/main/app/guardrails/input_guard.py
- Command line
-
hf download hf://spaces/acpcchatbotadmin/acpcchatbot_testing_first/app/guardrails/input_guard.py
-
curl -L -o input_guard.py https://huggingface.co/spaces/acpcchatbotadmin/acpcchatbot_testing_first/resolve/main/app/guardrails/input_guard.py
1.23 kB
| import logging | |
| from langchain_core.messages import HumanMessage | |
| from langchain_openai import ChatOpenAI | |
| from app.agent.prompts import INTENT_CLASSIFICATION_PROMPT | |
| from app.config import settings | |
| logger = logging.getLogger(__name__) | |
| def classify_query(query: str) -> str: | |
| """ | |
| Classify query as ACPC_RELATED, OFF_TOPIC, or INJECTION. | |
| Returns one of those three strings. | |
| """ | |
| llm = ChatOpenAI( | |
| model=settings.llm_model, | |
| api_key=settings.openai_api_key, | |
| base_url=settings.openai_api_base, | |
| temperature=0.0, | |
| ) | |
| prompt = INTENT_CLASSIFICATION_PROMPT.format(query=query) | |
| response = llm.invoke([HumanMessage(content=prompt)]) | |
| classification = response.content.strip().upper() | |
| if classification not in ("ACPC_RELATED", "OFF_TOPIC", "INJECTION"): | |
| logger.warning("Unexpected classification '%s' — defaulting to ACPC_RELATED", classification) | |
| return "ACPC_RELATED" | |
| logger.info("Input guard classification: %s", classification) | |
| return classification | |
| def is_allowed(query: str) -> bool: | |
| """Returns True if query should be processed by the RAG pipeline.""" | |
| return classify_query(query) == "ACPC_RELATED" | |