-
Notifications
You must be signed in to change notification settings - Fork 2
Validator: Answer relevance custom LLM judge #109
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
Open
rkritika1508
wants to merge
4
commits into
main
Choose a base branch
from
feat/answer-relevance-llm-judge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
backend/app/alembic/versions/008_add_answer_relevance_prompt.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| """Add answer_relevance_prompt table | ||
|
|
||
| Revision ID: 008 | ||
| Revises: 007 | ||
| Create Date: 2026-05-08 00:00:00.000000 | ||
|
|
||
| """ | ||
|
|
||
| from typing import Sequence, Union | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
|
|
||
| revision: str = "008" | ||
| down_revision = "007" | ||
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| op.create_table( | ||
| "answer_relevance_prompt", | ||
| 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), | ||
| sa.Column("prompt_template", 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"), | ||
| ) | ||
|
|
||
| op.create_index( | ||
| "idx_answer_relevance_prompt_org", | ||
| "answer_relevance_prompt", | ||
| ["organization_id"], | ||
| ) | ||
| op.create_index( | ||
| "idx_answer_relevance_prompt_project", | ||
| "answer_relevance_prompt", | ||
| ["project_id"], | ||
| ) | ||
| op.create_index( | ||
| "idx_answer_relevance_prompt_is_active", | ||
| "answer_relevance_prompt", | ||
| ["is_active"], | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| op.drop_table("answer_relevance_prompt") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
backend/app/api/docs/answer_relevance_prompts/create_prompt.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| Creates an answer relevance prompt config for the tenant resolved from `X-API-KEY`. | ||
|
|
||
| Behavior notes: | ||
| - Stores a custom prompt template used by the `answer_relevance_custom_llm` validator to evaluate whether an LLM answer is relevant to a user query. | ||
| - Tenant scope is enforced from the API key context. | ||
| - `prompt_template` must contain both `{query}` and `{answer}` placeholders; the server rejects templates missing either. | ||
|
|
||
| Common failure cases: | ||
| - Missing or invalid API key. | ||
| - Payload schema validation errors. | ||
| - `prompt_template` is missing `{query}` or `{answer}` placeholder. | ||
|
|
||
| ## Field glossary | ||
|
|
||
| **`prompt_template`** | ||
| A string with `{query}` and `{answer}` placeholders. At validation time, the guardrail substitutes the user's query and the LLM's answer, then asks the model to respond `YES` (relevant) or `NO` (not relevant). | ||
|
|
||
| Default template used when no custom prompt is configured: | ||
| ``` | ||
| Query: {query} | ||
| Answer: {answer} | ||
|
|
||
| Does the answer fully satisfy the query and constraints? | ||
| Answer only YES or NO. | ||
| ``` | ||
|
|
||
| NGOs can customise this to add domain-specific constraints, language preferences, or stricter relevance criteria for their use case. | ||
|
|
||
| Example custom template: | ||
| ``` | ||
| You are evaluating a maternal health assistant. | ||
| Query: {query} | ||
| Answer: {answer} | ||
|
|
||
| Does the answer directly address the maternal health query with accurate information? | ||
| Answer only YES or NO. | ||
| ``` | ||
|
|
||
| **`name`** | ||
| Human-readable label for this prompt config (max 100 characters). | ||
|
|
||
| **`description`** | ||
| What this prompt evaluates (max 500 characters). | ||
10 changes: 10 additions & 0 deletions
10
backend/app/api/docs/answer_relevance_prompts/delete_prompt.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| Deletes an answer relevance prompt config by id for the tenant resolved from `X-API-KEY`. | ||
|
|
||
| Behavior notes: | ||
| - Tenant scope is enforced from the API key context. | ||
| - Deletion is permanent; any guardrail configs referencing this `custom_prompt_id` will fail to resolve at runtime after deletion. | ||
|
|
||
| Common failure cases: | ||
| - Missing or invalid API key. | ||
| - Prompt config not found in tenant's scope. | ||
| - Invalid id format. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| Fetches a single answer relevance prompt config by id for the tenant resolved from `X-API-KEY`. | ||
|
|
||
| Behavior notes: | ||
| - Tenant scope is enforced: only configs belonging to the resolved `organization_id` and `project_id` are accessible. | ||
|
|
||
| Common failure cases: | ||
| - Missing or invalid API key. | ||
| - Prompt config not found in tenant's scope. | ||
| - Invalid id format. |
12 changes: 12 additions & 0 deletions
12
backend/app/api/docs/answer_relevance_prompts/list_prompts.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| Lists answer relevance prompt configs for the tenant resolved from `X-API-KEY`. | ||
|
|
||
| Behavior notes: | ||
| - Returns all prompt configs scoped to the tenant's `organization_id` and `project_id`. | ||
| - Supports pagination via `offset` and `limit`. | ||
| - `offset` defaults to `0`. | ||
| - `limit` is optional; when omitted, no limit is applied. | ||
| - Results are ordered by `created_at` ascending, then `id`. | ||
|
|
||
| Common failure cases: | ||
| - Missing or invalid API key. | ||
| - Invalid pagination values. |
12 changes: 12 additions & 0 deletions
12
backend/app/api/docs/answer_relevance_prompts/update_prompt.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| Partially updates an answer relevance prompt config by id for the tenant resolved from `X-API-KEY`. | ||
|
|
||
| Behavior notes: | ||
| - Supports patch-style updates; omitted fields remain unchanged. | ||
| - Tenant scope is enforced from the API key context. | ||
| - If `prompt_template` is updated, it must still contain both `{query}` and `{answer}` placeholders. | ||
|
|
||
| Common failure cases: | ||
| - Missing or invalid API key. | ||
| - Prompt config not found in tenant's scope. | ||
| - Payload schema validation errors. | ||
| - Updated `prompt_template` is missing `{query}` or `{answer}` placeholder. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ Behavior notes: | |
| - For `ban_list`, `ban_list_id` can be resolved to `banned_words` from tenant ban list configs. | ||
| - For `topic_relevance`, `topic_relevance_config_id` is required and is resolved to `configuration` + `prompt_schema_version` from tenant topic relevance configs. Requires `OPENAI_API_KEY` to be configured; returns a validation failure with an explicit error if missing. | ||
| - For `llm_critic`, `OPENAI_API_KEY` must be configured; returns `success=false` with an explicit error if missing. | ||
| - For `answer_relevance_custom_llm`, `input` must be a JSON string `{"query": "...", "answer": "..."}`. Pass `custom_prompt_id` to use a tenant-stored prompt template, or `prompt_template` inline. Requires `OPENAI_API_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. Clarify precedence/mutual exclusivity for Line 11 says “or”, but doesn’t define behavior if clients send both. Please document whether they are mutually exclusive or which one wins. Suggested doc tweak-- For `answer_relevance_custom_llm`, `input` must be a JSON string `{"query": "...", "answer": "..."}`. Pass `custom_prompt_id` to use a tenant-stored prompt template, or `prompt_template` inline. Requires `OPENAI_API_KEY`.
+- For `answer_relevance_custom_llm`, `input` must be a JSON string `{"query": "...", "answer": "..."}`. Use `custom_prompt_id` for a tenant-stored prompt template or `prompt_template` inline, and document the behavior when both are provided (mutually exclusive vs precedence). Requires `OPENAI_API_KEY`.🤖 Prompt for AI Agents |
||
| - For `llamaguard_7b`, `policies` accepts human-readable policy names (see table below). If omitted, all policies are enforced by default. | ||
|
|
||
| | `policies` value | Policy enforced | | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add language identifiers to fenced code blocks to satisfy markdownlint.
Both fenced blocks should declare a language (e.g.,
text) to clear MD040 warnings.Proposed patch
@@
-
+textYou are evaluating a maternal health assistant.
Query: {query}
Answer: {answer}
Does the answer directly address the maternal health query with accurate information?
Answer only YES or NO.
Also applies to: 30-37
🧰 Tools
🪛 markdownlint-cli2 (0.22.1)
[warning] 19-19: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
🤖 Prompt for AI Agents