-
Notifications
You must be signed in to change notification settings - Fork 2
Topic relevance validator #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9ae1fb7
76f98f7
d717740
dfb3c03
9d7d6dc
aaa2e7d
dfeff2a
4cf6b86
d8fe009
1090a96
efa3641
2813efb
6916fb4
7a770e8
ca44f62
540b930
baa13b1
26c43f6
4b14581
9bd379a
faa0cfd
50a6c4b
7f99434
638ed26
1ccce38
c4b11d0
d8c36fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ SENTRY_DSN= | |
|
|
||
| DOCKER_IMAGE_BACKEND=kaapi-guardrails-backend | ||
|
|
||
| OPENAI_API_KEY="<ADD-KEY>" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Description: Check if tests mock OpenAI or require real API key
echo "=== Checking for OpenAI mocking in tests ==="
rg -n -C3 'mock.*openai|patch.*openai|monkeypatch.*openai' --type=py --glob='**/test*.py'
echo -e "\n=== Checking if OPENAI_API_KEY is referenced in test files ==="
rg -n -C3 'OPENAI_API_KEY' --type=py --glob='**/test*.py'
echo -e "\n=== Checking if new validators use OpenAI client ==="
rg -n -C3 'OpenAI\(|openai\.' --type=py backend/app/core/validators/
echo -e "\n=== Checking CI workflow for OPENAI_API_KEY replacement ==="
rg -n 'OPENAI_API_KEY' .github/workflows/Repository: ProjectTech4DevAI/kaapi-guardrails Length of output: 301 🏁 Script executed: # Check CI workflow directly
cat .github/workflows/continuous_integration.yml
# Search for the validators mentioned
fd -type f -name "*.py" | xargs grep -l "Topic.*Relevance\|LLM.*Critic" 2>/dev/null
# Check validators directory structure
find . -type d -name "validators" 2>/dev/null | head -20
# Search for OpenAI imports more broadly
rg -n "from openai\|import openai" --type=py
# Check environment variable usage in test setup
rg -n "OPENAI_API_KEY\|os\.environ\|getenv" --type=py backend/app/core/validators/ 2>/dev/null || echo "Path not found, checking alternatives"
# List what's in core directory
find backend -type d -name "core" 2>/dev/nullRepository: ProjectTech4DevAI/kaapi-guardrails Length of output: 2664 🏁 Script executed: # List validators in core directory
ls -la backend/app/core/validators/
# List test validators
ls -la backend/app/tests/validators/
# Search for OpenAI usage more carefully (fix the type flag)
rg -n "from openai\|import openai\|OpenAI" --glob='**/*.py' backend/app/
# Check conftest and test setup files
find backend -name "conftest.py" -o -name "pytest.ini" -o -name "pyproject.toml" | head -10
# Check if there's test configuration
cat backend/pyproject.toml 2>/dev/null | grep -A 20 "\[tool.pytest"Repository: ProjectTech4DevAI/kaapi-guardrails Length of output: 1240 🏁 Script executed: # Check topic_relevance.py implementation
cat backend/app/core/validators/topic_relevance.py
# Check conftest for mocking setup
cat backend/app/tests/conftest.py
# Search for LLM Critic validator
find backend -name "*llm*" -o -name "*critic*" 2>/dev/null
# Check if tests are mocking or patching openai
rg "mock\|patch\|monkeypatch" backend/app/tests/ --glob='*.py' -A 3 -B 1
# Check environment handling in tests
cat backend/app/tests/pytest.iniRepository: ProjectTech4DevAI/kaapi-guardrails Length of output: 9039 🏁 Script executed: # Search for LLM Critic validator
find backend -name "*critic*" -type f
# Check if any test files import or use TopicRelevance
rg "TopicRelevance\|topic-relevance\|topic_relevance" backend/app/tests/ --glob='*.py'
# Check validators.json to see what's registered
cat backend/app/core/validators/validators.json
# Check if topic_relevance is imported in validators __init__
cat backend/app/core/validators/__init__.py 2>/dev/null || echo "No __init__.py"
# Check what happens when validator is missing OpenAI key - look for error handling
rg "OPENAI_API_KEY" backend/ --glob='*.py' -B 2 -A 2
# Check if there are integration tests or if validators are imported anywhere
rg "from.*validators import\|import.*validators" backend/app/ --glob='*.py' | head -20Repository: ProjectTech4DevAI/kaapi-guardrails Length of output: 943 🏁 Script executed: # Check the llm_critic_safety_validator_config.py file
cat backend/app/core/validators/config/llm_critic_safety_validator_config.py
# Check for any usage of topic_relevance or llm_critic validators in actual code
rg "topic_relevance|llm_critic" backend/app/ --glob='*.py' --glob='*.json' -B 2 -A 2 | head -100
# Check if validators are instantiated/used during tests
rg "ValidatorConfig\|ValidatorPayload\|validate\(" backend/app/tests/ --glob='*.py' -B 1 -A 1 | head -80
# Check the actual seed_data to see what validators are being tested
find backend -name "seed_data.py" -exec cat {} \;Repository: ProjectTech4DevAI/kaapi-guardrails Length of output: 10613 🏁 Script executed: # Check what _validate_with_guard actually does - does it instantiate validators?
cat backend/app/tests/test_validate_with_guard.py | head -200
# Check the actual validation route implementation
cat backend/app/api/routes/guardrails.py | grep -A 50 "def _validate_with_guard"
# Check seed_data.json to see what validators are seeded
cat backend/app/tests/seed_data.jsonRepository: ProjectTech4DevAI/kaapi-guardrails Length of output: 10410 🏁 Script executed: # Check if TopicRelevance or LLMCritic validators are ever instantiated without mocking
rg "TopicRelevance\(|LLMCritic\(" backend/app/tests/ --glob='*.py'
# Check the build_guard function to understand how validators are instantiated
find backend -name "*.py" -exec grep -l "def build_guard" {} \;
# Check if the validators are tested directly
find backend -path "*/tests/*" -name "*topic*" -o -name "*critic*" 2>/dev/null
# Check how Guard is built and if validators are instantiated
rg "Guard\(" backend/app/ --glob='*.py' -B 3 -A 3 | head -150Repository: ProjectTech4DevAI/kaapi-guardrails Length of output: 4487 🏁 Script executed: # Check the test_build_guard.py to see if TopicRelevance is tested
cat backend/app/tests/test_build_guard.py
# Check test_topic_relevance_configs_api_integration.py
cat backend/app/tests/test_topic_relevance_configs_api_integration.py
# Check if any test actually calls build_guard without mocking it
rg "build_guard" backend/app/tests/ --glob='*.py' | grep -v "patch\|Mock"
# Check guardrail_controller.py for more context
cat backend/app/core/guardrail_controller.pyRepository: ProjectTech4DevAI/kaapi-guardrails Length of output: 13104 🏁 Script executed: # Check if there's an llm_critic.py validator file (as opposed to the config class)
find backend -name "*llm_critic*" -type f
# Check what's in the config file more carefully
cat backend/app/core/validators/config/llm_critic_safety_validator_config.py
# Verify which validators are actually defined locally vs imported from hub
ls -la backend/app/core/validators/*.py
# Check if any non-mocked test actually instantiates validators
rg "\.build\(\)" backend/app/tests/ --glob='*.py' -B 2 -A 2
# Look for any test configuration that uses TopicRelevance without mocking
rg "TopicRelevance|llm_critic" backend/app/tests/ --glob='*.py' -B 3 -A 3 | head -200Repository: ProjectTech4DevAI/kaapi-guardrails Length of output: 8117 Update the CI workflow to populate OPENAI_API_KEY from secrets or mock OpenAI calls in tests. The TopicRelevance and LLMCritic validators require a real OpenAI API key to function (via Either:
🧰 Tools🪛 dotenv-linter (4.0.0)[warning] 24-24: [QuoteCharacter] The value has quote characters (', ") (QuoteCharacter) 🤖 Prompt for AI Agents |
||
| GUARDRAILS_HUB_API_KEY="<ADD-KEY>" | ||
| # SHA-256 hex digest of your bearer token (64 lowercase hex chars) | ||
| AUTH_TOKEN="<ADD-HASH-TOKEN>" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| """Added topic_relevance table | ||
|
|
||
| Revision ID: 006 | ||
| Revises: 005 | ||
| Create Date: 2026-03-05 00:00:00.000000 | ||
|
|
||
| """ | ||
|
|
||
| from typing import Sequence, Union | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision: str = "006" | ||
| down_revision = "005" | ||
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| op.create_table( | ||
| "topic_relevance", | ||
| sa.Column("id", sa.Uuid(), nullable=False), | ||
| sa.Column("organization_id", sa.Integer(), nullable=False), | ||
| sa.Column("project_id", sa.Integer(), nullable=False), | ||
| sa.Column("name", sa.String(), nullable=False), | ||
| sa.Column("description", sa.String(), nullable=False), | ||
rkritika1508 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| sa.Column("prompt_schema_version", sa.Integer(), nullable=False), | ||
| sa.Column("configuration", sa.Text(), nullable=False), | ||
| sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true()), | ||
| sa.Column("created_at", sa.DateTime(), nullable=False), | ||
| sa.Column("updated_at", sa.DateTime(), nullable=False), | ||
| sa.PrimaryKeyConstraint("id"), | ||
| sa.UniqueConstraint( | ||
| "organization_id", | ||
| "project_id", | ||
| "prompt_schema_version", | ||
| "configuration", | ||
| name="uq_topic_relevance_config_org_project_prompt", | ||
| ), | ||
| ) | ||
|
|
||
| op.create_index( | ||
| "idx_topic_relevance_organization", "topic_relevance", ["organization_id"] | ||
| ) | ||
| op.create_index("idx_topic_relevance_project", "topic_relevance", ["project_id"]) | ||
| op.create_index( | ||
| "idx_topic_relevance_prompt_schema_version", | ||
| "topic_relevance", | ||
| ["prompt_schema_version"], | ||
| ) | ||
| op.create_index("idx_topic_relevance_is_active", "topic_relevance", ["is_active"]) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| op.drop_table("topic_relevance") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| Creates a topic relevance configuration for the tenant resolved from `X-API-KEY`. | ||
|
|
||
| Behavior notes: | ||
| - Stores a topic relevance preset with `name`, `prompt_schema_version`, and `configuration`. | ||
| - `configuration` is a plain text scope sub-prompt (string). | ||
| - Tenant scope is enforced from the API key context. | ||
| - Duplicate configurations are rejected. | ||
|
|
||
| Common failure cases: | ||
| - Missing or invalid API key. | ||
| - Payload schema validation errors. | ||
| - Topic relevance with the same configuration already exists. | ||
|
|
||
| ## Field glossary | ||
|
|
||
| **`configuration`** | ||
| A plain text string describing the topic scope the assistant is allowed to handle. This is injected into the LLM critic evaluation prompt at the `{{TOPIC_CONFIGURATION}}` placeholder to define what is considered in-scope. | ||
|
|
||
| Example: | ||
| ``` | ||
| This assistant only answers questions about maternal health and pregnancy care for NGO beneficiaries. It should not respond to questions about politics, general medicine unrelated to pregnancy, or financial topics. | ||
| ``` | ||
rkritika1508 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| **`prompt_schema_version`** | ||
| An integer selecting the versioned prompt template used to evaluate scope violations (e.g., `1` → `v1.md`). Controls the structure and wording of the LLM critic assessment prompt. Defaults to `1`. Only increment this when a new prompt template version has been added to the system. | ||
|
|
||
| Example: `1` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| Deletes a topic relevance configuration by id for the tenant resolved from `X-API-KEY`. | ||
|
|
||
| Behavior notes: | ||
| - Tenant scope is enforced from the API key context. | ||
|
|
||
| Common failure cases: | ||
| - Missing or invalid API key. | ||
| - Topic relevance preset not found in tenant's scope. | ||
rkritika1508 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| Fetches a single topic relevance configuration by id for the tenant resolved from `X-API-KEY`. | ||
|
|
||
| Behavior notes: | ||
| - Tenant scope is enforced from the API key context. | ||
|
|
||
| Common failure cases: | ||
| - Missing or invalid API key. | ||
| - Topic relevance preset not found in tenant's scope. | ||
rkritika1508 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - Invalid id format. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| Lists topic relevance configurations for the tenant resolved from `X-API-KEY`. | ||
|
|
||
| Behavior notes: | ||
| - Supports pagination via `offset` and `limit`. | ||
| - `offset` defaults to `0`. | ||
| - `limit` is optional; when omitted, no limit is applied. | ||
| - Tenant scope is enforced from the API key context. | ||
|
|
||
| Common failure cases: | ||
| - Missing or invalid API key. | ||
| - Invalid pagination values. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| Partially updates a topic relevance configuration by id for the tenant resolved from `X-API-KEY`. | ||
|
|
||
| Behavior notes: | ||
| - Supports patch-style updates; omitted fields remain unchanged. | ||
| - `configuration` should be provided as a plain text scope sub-prompt (string). | ||
| - Tenant scope is enforced from the API key context. | ||
| - Duplicate configurations are rejected. | ||
|
|
||
| Common failure cases: | ||
| - Missing or invalid API key. | ||
| - Topic relevance preset not found in tenant's scope. | ||
| - Payload schema validation errors. | ||
| - Topic relevance with the same configuration already exists. |
Uh oh!
There was an error while loading. Please reload this page.