-
Notifications
You must be signed in to change notification settings - Fork 4
Topic relevance improvements #132
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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
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
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
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
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
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
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 |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| [pytest] | ||
| asyncio_mode = auto | ||
| asyncio_mode = auto | ||
| markers = | ||
| integration: tests that hit the full HTTP stack with a real database | ||
| llm_live: tests that make real LLM calls (require OPENAI_API_KEY) |
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
107 changes: 107 additions & 0 deletions
107
backend/app/tests/validators/test_topic_relevance_llm_live.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,107 @@ | ||
| """ | ||
| Live integration tests for TopicRelevanceLLM — these call the real LLM and are | ||
| skipped automatically when OPENAI_API_KEY is not set or is a placeholder value. | ||
|
|
||
| Run them explicitly with: | ||
| pytest -m llm_live | ||
| or in any environment that has OPENAI_API_KEY configured. | ||
| """ | ||
| import os | ||
|
|
||
| import pytest | ||
| from guardrails.validators import FailResult, PassResult | ||
|
|
||
| from app.core.validators.topic_relevance_llm import TopicRelevanceLLM | ||
|
|
||
| pytestmark = pytest.mark.llm_live | ||
|
|
||
| _needs_key = pytest.mark.skipif( | ||
| not os.environ.get("OPENAI_API_KEY", "").startswith("sk-"), | ||
| reason="OPENAI_API_KEY not set or not a valid key — skipping live LLM tests", | ||
| ) | ||
|
|
||
| _COOKING_SCOPE = "Only answer questions about cooking and recipes." | ||
| _HEALTH_SCOPE = "Only answer questions about general health and wellness." | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def cooking_validator(): | ||
| return TopicRelevanceLLM(system_prompt=_COOKING_SCOPE) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def health_validator(): | ||
| return TopicRelevanceLLM(system_prompt=_HEALTH_SCOPE) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # In-scope queries — model should return score >= threshold (PassResult) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @_needs_key | ||
| def test_live_in_scope_query_passes(cooking_validator): | ||
| result = cooking_validator._validate("How do I make pasta carbonara?") | ||
|
|
||
| assert isinstance(result, PassResult) | ||
| assert result.metadata["scope_score"] >= 2 | ||
|
|
||
|
|
||
| @_needs_key | ||
| def test_live_in_scope_query_exposes_score_metadata(cooking_validator): | ||
| result = cooking_validator._validate("What temperature should I bake bread at?") | ||
|
|
||
| assert isinstance(result, PassResult) | ||
| assert "scope_score" in result.metadata | ||
| assert result.metadata["scope_score"] in (1, 2, 3) | ||
|
|
||
|
rkritika1508 marked this conversation as resolved.
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Out-of-scope queries — model should return score < threshold (FailResult) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @_needs_key | ||
| def test_live_out_of_scope_query_fails(cooking_validator): | ||
| result = cooking_validator._validate("What is the capital of France?") | ||
|
|
||
| assert isinstance(result, FailResult) | ||
| assert "outside the allowed topic scope" in result.error_message | ||
|
|
||
|
|
||
| @_needs_key | ||
| def test_live_out_of_scope_score_is_exposed_in_metadata(cooking_validator): | ||
| result = cooking_validator._validate("Who won the cricket World Cup?") | ||
|
|
||
| assert isinstance(result, FailResult) | ||
| assert "scope_score" in result.metadata | ||
| assert result.metadata["scope_score"] in (1, 2, 3) | ||
|
|
||
|
rkritika1508 marked this conversation as resolved.
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # JSON response format — exercises _extract_first_json_object on real output | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @_needs_key | ||
| def test_live_response_parsed_without_error(health_validator): | ||
| """The LLM returns JSON that _extract_first_json_object must parse correctly, | ||
| regardless of whether the model wraps it in a markdown fence or adds prose.""" | ||
| result = health_validator._validate("How much water should I drink per day?") | ||
|
|
||
| assert isinstance(result, (PassResult, FailResult)) | ||
| assert "scope_score" in result.metadata | ||
|
|
||
|
|
||
| @_needs_key | ||
| def test_live_different_scope_gives_different_verdict( | ||
| cooking_validator, health_validator | ||
| ): | ||
| """The same off-topic query fails both validators, confirming scope config is wired.""" | ||
| query = "Explain quantum entanglement." | ||
|
|
||
| cooking_result = cooking_validator._validate(query) | ||
| health_result = health_validator._validate(query) | ||
|
|
||
| assert isinstance(cooking_result, FailResult) | ||
| assert isinstance(health_result, FailResult) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.