-
Notifications
You must be signed in to change notification settings - Fork 3
pattern.json enhanced with ready-to-use responses API payload section #68
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
filip-komarzyniec
wants to merge
1
commit into
main
Choose a base branch
from
enhance_rag_pattern_output_with_responses_api_payload
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
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
251 changes: 251 additions & 0 deletions
251
tests/unit/ai4rag/core/experiment/test_stream_pattern.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,251 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # Copyright IBM Corp. 2026 | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # ----------------------------------------------------------------------------- | ||
| """Unit tests for AI4RAGExperiment._stream_finished_pattern payload construction.""" | ||
|
|
||
| import pandas as pd | ||
| import pytest | ||
| from langchain_core.documents import Document | ||
|
|
||
| from ai4rag.core.experiment.experiment import AI4RAGExperiment | ||
| from ai4rag.core.experiment.results import EvaluationResult, ExperimentResults | ||
| from ai4rag.core.hpo.random_opt import RandomOptSettings | ||
| from ai4rag.search_space.src.parameter import Parameter | ||
| from ai4rag.search_space.src.search_space import AI4RAGSearchSpace | ||
| from ai4rag.utils.constants import AI4RAGParamNames | ||
| from ai4rag.utils.event_handler import LocalEventHandler | ||
| from dev_utils.mocks import MockedEmbeddingModel, MockedFoundationModel, MockedOGXClient | ||
|
|
||
| _EMBEDDING_DIMENSION = 64 | ||
|
|
||
|
|
||
| def _make_search_space(fm, em): | ||
| return AI4RAGSearchSpace( | ||
| vector_store_type="chroma", | ||
| params=[ | ||
| Parameter(name="foundation_model", param_type="C", values=[fm]), | ||
| Parameter(name="embedding_model", param_type="C", values=[em]), | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| def _make_evaluation_result( | ||
| vector_io_provider_type="chroma::local", | ||
| search_mode="vector", | ||
| window_size=None, | ||
| ranker_strategy=None, | ||
| ranker_k=None, | ||
| ranker_alpha=None, | ||
| ): | ||
| return EvaluationResult( | ||
| pattern_name="Pattern1", | ||
| collection="test-collection-abc", | ||
| indexing_params={ | ||
| "chunking": { | ||
| AI4RAGParamNames.CHUNKING_METHOD: "recursive", | ||
| AI4RAGParamNames.CHUNK_SIZE: 512, | ||
| AI4RAGParamNames.CHUNK_OVERLAP: 64, | ||
| }, | ||
| "embedding": { | ||
| "model_id": "mock-em-0", | ||
| "distance_metric": "cosine", | ||
| }, | ||
| }, | ||
| rag_params={ | ||
| "retrieval": { | ||
| AI4RAGParamNames.RETRIEVAL_METHOD: "simple", | ||
| AI4RAGParamNames.NUMBER_OF_CHUNKS: 3, | ||
| AI4RAGParamNames.SEARCH_MODE: search_mode, | ||
| AI4RAGParamNames.WINDOW_SIZE: window_size, | ||
| AI4RAGParamNames.RANKER_STRATEGY: ranker_strategy, | ||
| AI4RAGParamNames.RANKER_K: ranker_k, | ||
| AI4RAGParamNames.RANKER_ALPHA: ranker_alpha, | ||
| }, | ||
| "generation": { | ||
| "model_id": "mock-fm-0", | ||
| "context_template_text": "Context: {context}", | ||
| "user_message_text": "Answer: {question}", | ||
| "system_message_text": "You are a helpful assistant.", | ||
| }, | ||
| "vector_io_provider_type": vector_io_provider_type, | ||
| }, | ||
| scores={ | ||
| "scores": {"answer_correctness": {"mean": 0.5}}, | ||
| "question_scores": {"answer_correctness": {"q0": 0.5}}, | ||
| }, | ||
| execution_time=10.0, | ||
| final_score=0.5, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def foundation_model(): | ||
| return MockedFoundationModel(model_id="mock-fm-0", params=None) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def embedding_model(): | ||
| return MockedEmbeddingModel( | ||
| model_id="mock-em-0", | ||
| params={"embedding_dimension": _EMBEDDING_DIMENSION}, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def minimal_documents(): | ||
| return [Document(page_content="Test content.", metadata={"document_id": "doc_0"})] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def minimal_benchmark(): | ||
| return pd.DataFrame( | ||
| { | ||
| "question": ["What is test?"], | ||
| "correct_answers": [["Test content."]], | ||
| "correct_answer_document_ids": [["doc_0"]], | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def _make_chroma_experiment(foundation_model, embedding_model, minimal_documents, minimal_benchmark, mocker): | ||
| event_handler = mocker.MagicMock(spec=LocalEventHandler) | ||
| experiment = AI4RAGExperiment( | ||
| documents=minimal_documents, | ||
| benchmark_data=minimal_benchmark, | ||
| search_space=_make_search_space(foundation_model, embedding_model), | ||
| vector_store_type="chroma", | ||
| optimizer_settings=RandomOptSettings(max_evals=1), | ||
| event_handler=event_handler, | ||
| ) | ||
| experiment.results = ExperimentResults() | ||
| return experiment | ||
|
|
||
|
|
||
| def _make_ogx_experiment(foundation_model, embedding_model, minimal_documents, minimal_benchmark, mocker): | ||
| event_handler = mocker.MagicMock(spec=LocalEventHandler) | ||
| experiment = AI4RAGExperiment( | ||
| documents=minimal_documents, | ||
| benchmark_data=minimal_benchmark, | ||
| search_space=_make_search_space(foundation_model, embedding_model), | ||
| vector_store_type="ogx", | ||
| optimizer_settings=RandomOptSettings(max_evals=1), | ||
| event_handler=event_handler, | ||
| client=MockedOGXClient(), | ||
| ogx_vector_io_provider_id="test-provider", | ||
| ) | ||
| experiment.results = ExperimentResults() | ||
| return experiment | ||
|
|
||
|
|
||
| class TestStreamFinishedPatternChroma: | ||
|
|
||
| def test_chroma_payload_excludes_responses_template( | ||
| self, foundation_model, embedding_model, minimal_documents, minimal_benchmark, mocker | ||
| ): | ||
| experiment = _make_chroma_experiment( | ||
| foundation_model, embedding_model, minimal_documents, minimal_benchmark, mocker | ||
| ) | ||
| eval_result = _make_evaluation_result(vector_io_provider_type="chroma::local") | ||
|
|
||
| experiment._stream_finished_pattern(eval_result, []) | ||
|
|
||
| experiment.event_handler.on_pattern_creation.assert_called_once() | ||
| payload = experiment.event_handler.on_pattern_creation.call_args.kwargs["payload"] | ||
|
|
||
| assert "responses_template" not in payload | ||
| binding = payload["settings"]["vector_store_binding"] | ||
| assert "provider_id" in binding | ||
| assert "provider_type" in binding | ||
| assert "vector_store_id" in binding | ||
| assert "vector_store_name" in binding | ||
| assert binding["provider_type"] == "chroma::local" | ||
|
|
||
| def test_chroma_payload_has_required_top_level_keys( | ||
| self, foundation_model, embedding_model, minimal_documents, minimal_benchmark, mocker | ||
| ): | ||
| experiment = _make_chroma_experiment( | ||
| foundation_model, embedding_model, minimal_documents, minimal_benchmark, mocker | ||
| ) | ||
| eval_result = _make_evaluation_result() | ||
|
|
||
| experiment._stream_finished_pattern(eval_result, []) | ||
|
|
||
| payload = experiment.event_handler.on_pattern_creation.call_args.kwargs["payload"] | ||
| expected_keys = {"pattern_name", "scores", "execution_time", "final_score", "schema_version", "producer", "settings", "iteration"} | ||
| assert expected_keys.issubset(payload.keys()) | ||
|
|
||
|
|
||
| class TestStreamFinishedPatternOGX: | ||
|
|
||
| def test_ogx_payload_includes_responses_template( | ||
| self, foundation_model, embedding_model, minimal_documents, minimal_benchmark, mocker | ||
| ): | ||
| experiment = _make_ogx_experiment( | ||
| foundation_model, embedding_model, minimal_documents, minimal_benchmark, mocker | ||
| ) | ||
| eval_result = _make_evaluation_result(vector_io_provider_type="mock_provider") | ||
|
|
||
| experiment._stream_finished_pattern(eval_result, []) | ||
|
|
||
| payload = experiment.event_handler.on_pattern_creation.call_args.kwargs["payload"] | ||
|
|
||
| assert "responses_template" in payload | ||
| responses = payload["responses_template"] | ||
| assert responses["model"] == "mock-fm-0" | ||
| assert responses["tools"][0]["type"] == "file_search" | ||
| assert responses["tools"][0]["vector_store_ids"] == ["test-collection-abc"] | ||
| assert responses["include"] == ["file_search_call.results"] | ||
| assert responses["stream"] is False | ||
| assert responses["store"] is True | ||
|
|
||
|
|
||
| class TestStreamFinishedPatternRetrieval: | ||
|
|
||
| def test_hybrid_retrieval_includes_ranker_fields( | ||
| self, foundation_model, embedding_model, minimal_documents, minimal_benchmark, mocker | ||
| ): | ||
| experiment = _make_chroma_experiment( | ||
| foundation_model, embedding_model, minimal_documents, minimal_benchmark, mocker | ||
| ) | ||
| eval_result = _make_evaluation_result( | ||
| search_mode="hybrid", | ||
| ranker_strategy="rrf", | ||
| ranker_k=60, | ||
| ranker_alpha=0.5, | ||
| ) | ||
|
|
||
| experiment._stream_finished_pattern(eval_result, []) | ||
|
|
||
| payload = experiment.event_handler.on_pattern_creation.call_args.kwargs["payload"] | ||
| retrieval = payload["settings"]["retrieval"] | ||
| assert retrieval["search_mode"] == "hybrid" | ||
| assert retrieval["ranker_strategy"] == "rrf" | ||
| assert retrieval["ranker_k"] == 60 | ||
| assert retrieval["ranker_alpha"] == 0.5 | ||
|
|
||
| def test_window_size_included_when_set( | ||
| self, foundation_model, embedding_model, minimal_documents, minimal_benchmark, mocker | ||
| ): | ||
| experiment = _make_chroma_experiment( | ||
| foundation_model, embedding_model, minimal_documents, minimal_benchmark, mocker | ||
| ) | ||
| eval_result = _make_evaluation_result(window_size=2) | ||
|
|
||
| experiment._stream_finished_pattern(eval_result, []) | ||
|
|
||
| payload = experiment.event_handler.on_pattern_creation.call_args.kwargs["payload"] | ||
| assert payload["settings"]["retrieval"]["window_size"] == 2 | ||
|
|
||
| def test_window_size_excluded_when_none( | ||
| self, foundation_model, embedding_model, minimal_documents, minimal_benchmark, mocker | ||
| ): | ||
| experiment = _make_chroma_experiment( | ||
| foundation_model, embedding_model, minimal_documents, minimal_benchmark, mocker | ||
| ) | ||
| eval_result = _make_evaluation_result(window_size=None) | ||
|
|
||
| experiment._stream_finished_pattern(eval_result, []) | ||
|
|
||
| payload = experiment.event_handler.on_pattern_creation.call_args.kwargs["payload"] | ||
| assert "window_size" not in payload["settings"]["retrieval"] |
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.
The ADR specified here the name as assigned in the ConfigMap. It proves really problematic to obtain. How can we simplify this @LukaszCmielowski