diff --git a/docs/devel_doc/openapi.json b/docs/devel_doc/openapi.json index f3af6766d..595114a1c 100644 --- a/docs/devel_doc/openapi.json +++ b/docs/devel_doc/openapi.json @@ -16281,6 +16281,29 @@ "title": "OkpConfiguration", "description": "OKP (Offline Knowledge Portal) provider configuration.\n\nControls provider-specific behaviour for the OKP vector store.\nOnly relevant when ``\"okp\"`` is listed in ``rag.retrieval.inline.sources``\nor ``rag.retrieval.tool.sources``." }, + "OkpProductFilter": { + "properties": { + "product": { + "type": "string", + "title": "Product" + }, + "versions": { + "items": { + "type": "string" + }, + "type": "array", + "title": "Versions", + "default": [] + } + }, + "additionalProperties": false, + "type": "object", + "required": [ + "product" + ], + "title": "OkpProductFilter", + "description": "One product restriction for the OKP MCP ``search`` tool.\n\nThe server matches ``product`` exactly and OR-combines ``versions``.\nAn empty ``versions`` tuple matches that product at any version.\nSeveral filters on one source are OR-combined.\n\nAttributes:\n product: Product slug, for example ``openshift_container_platform``.\n versions: Versions to include, for example ``(\"4.19\", \"4.20\")``." + }, "OpenAIInferenceProviderConfig": { "properties": { "api_key": { @@ -19008,6 +19031,37 @@ } } ] + }, + "okp_products": { + "anyOf": [ + { + "items": { + "$ref": "#/components/schemas/OkpProductFilter" + }, + "type": "array" + }, + { + "type": "null" + } + ], + "title": "Okp Products", + "description": "Optional list of product restrictions for the OKP knowledge source. Each entry names one product. Versions are OR-combined, and an empty versions list matches every version of that product. Several entries are OR-combined. When omitted, OKP is not restricted by product.", + "examples": [ + [ + { + "product": "openshift_container_platform", + "versions": [ + "4.19", + "4.20" + ] + } + ], + [ + { + "product": "rhel" + } + ] + ] } }, "additionalProperties": false, @@ -19016,7 +19070,7 @@ "query" ], "title": "QueryRequest", - "description": "Model representing a request for the LLM (Language Model).\n\nAttributes:\n query: The query string.\n conversation_id: The optional conversation ID (UUID).\n provider: The optional provider.\n model: The optional model.\n system_prompt: The optional system prompt.\n attachments: The optional attachments.\n no_tools: Whether to bypass all tools and MCP servers (default: False).\n generate_topic_summary: Whether to generate topic summary for new conversations.\n media_type: The optional media type for response format (application/json or text/plain).\n vector_store_ids: The optional list of specific vector store IDs to query for RAG.\n shield_ids: The optional list of configured shield names to apply.\n solr: Optional Solr inline RAG options (mode, filters) or legacy filter-only dict.", + "description": "Model representing a request for the LLM (Language Model).\n\nAttributes:\n query: The query string.\n conversation_id: The optional conversation ID (UUID).\n provider: The optional provider.\n model: The optional model.\n system_prompt: The optional system prompt.\n attachments: The optional attachments.\n no_tools: Whether to bypass all tools and MCP servers (default: False).\n generate_topic_summary: Whether to generate topic summary for new conversations.\n media_type: The optional media type for response format (application/json or text/plain).\n vector_store_ids: The optional list of specific vector store IDs to query for RAG.\n shield_ids: The optional list of configured shield names to apply.\n solr: Optional Solr inline RAG options (mode, filters) or legacy filter-only dict.\n okp_products: The optional list of product restrictions for the OKP knowledge source.", "examples": [ { "attachments": [ diff --git a/src/agent/capabilities/knowledge/factory.py b/src/agent/capabilities/knowledge/factory.py index af1485184..199881ac0 100644 --- a/src/agent/capabilities/knowledge/factory.py +++ b/src/agent/capabilities/knowledge/factory.py @@ -1,14 +1,17 @@ """Factory for building knowledge capabilities from knowledge source settings.""" +from dataclasses import replace from typing import Optional from sentence_transformers import CrossEncoder from agent.types.settings import KnowledgeSourceSettings +from agent.types.settings.knowledge import KnowledgeSettings, OKPKnowledgeSourceSettings from configuration import configuration from knowledge.capability import Knowledge from knowledge.registry import KnowledgeSourceRegistry from knowledge.sources.base import KnowledgeSource +from knowledge.sources.okp import OkpKnowledgeSource from log import get_logger from providers.inference.errors import ( ModelNotAvailableError, @@ -25,7 +28,7 @@ class KnowledgeFactory: # pylint: disable=too-few-public-methods @staticmethod def build_capabilities( - settings: list[KnowledgeSourceSettings], + settings: KnowledgeSettings, ) -> Optional[Knowledge]: """Build a knowledge capability from knowledge source settings. @@ -35,8 +38,8 @@ def build_capabilities( configuration. Each source's score weight is applied after merged matches are ranked. When inline reranking is enabled, the cross-encoder is loaded from the inference provider registry and - attached to the capability. OKP search mode and the chunk filter are - fixed on the registered OKP source. + attached to the capability. OKP product filters from the request are + applied on a copy of the registered OKP source. Parameters: settings: Per-request knowledge source selection. @@ -48,7 +51,7 @@ def build_capabilities( cannot be resolved. """ - if sources := _get_enabled_sources(settings): + if sources := _get_enabled_sources(settings.sources): return Knowledge( sources=sources, reranker=_get_reranker(), @@ -68,7 +71,8 @@ def _get_enabled_sources( Returns: Registered sources whose settings are enabled. Disabled settings and - unknown ids are skipped. Unknown ids are logged. + unknown ids are skipped. Unknown ids are logged. An OKP source with + request product filters is a copy of the registered source. """ registry = KnowledgeSourceRegistry() @@ -83,6 +87,14 @@ def _get_enabled_sources( setting.id, ) continue + if isinstance(setting, OKPKnowledgeSourceSettings) and setting.products: + if isinstance(source, OkpKnowledgeSource): + source = replace(source, products=tuple(setting.products)) + else: + logger.warning( + "Knowledge source '%s' is not an OKP source; ignoring product filters", + setting.id, + ) sources.append(source) return sources diff --git a/src/agent/factory.py b/src/agent/factory.py index 911cb2cfc..e54a25555 100644 --- a/src/agent/factory.py +++ b/src/agent/factory.py @@ -71,7 +71,7 @@ async def create_agent( *(await MCPFactory.build_capabilities(settings.mcp_servers)), *ShieldFactory.build_capabilities(settings.shields), SkillFactory.build_capabilities(settings.skills), - KnowledgeFactory.build_capabilities(settings.knowledge_sources), + KnowledgeFactory.build_capabilities(settings.knowledge), CompactionFactory.build_capability(settings.compaction), StepPersistenceFactory.build_capability(), ) diff --git a/src/agent/types/settings/agent.py b/src/agent/types/settings/agent.py index 88d35d944..accae924f 100644 --- a/src/agent/types/settings/agent.py +++ b/src/agent/types/settings/agent.py @@ -8,7 +8,7 @@ from fastapi import Request from agent.types.settings.compaction import CompactionSettings -from agent.types.settings.knowledge import KnowledgeSourceSettings +from agent.types.settings.knowledge import KnowledgeSettings from agent.types.settings.mcp import MCPSettings from agent.types.settings.shield import ShieldSettings from agent.types.settings.skill import SkillSettings @@ -29,7 +29,7 @@ class AgentSettings: # pylint: disable=too-many-instance-attributes skills: list[SkillSettings] = field(default_factory=list) shields: list[ShieldSettings] = field(default_factory=list) - knowledge_sources: list[KnowledgeSourceSettings] = field(default_factory=list) + knowledge: KnowledgeSettings = field(default_factory=KnowledgeSettings) mcp_servers: list[MCPSettings] = field(default_factory=list) @classmethod @@ -64,6 +64,6 @@ def from_query_request( compaction=CompactionSettings.from_query_request(query_request), skills=SkillSettings.from_query_request(query_request), shields=ShieldSettings.from_query_request(query_request), - knowledge_sources=KnowledgeSourceSettings.from_query_request(query_request), + knowledge=KnowledgeSettings.from_query_request(query_request), mcp_servers=MCPSettings.from_query_request(request, query_request, token), ) diff --git a/src/agent/types/settings/knowledge.py b/src/agent/types/settings/knowledge.py index cd3c623f6..63e1cd0e4 100644 --- a/src/agent/types/settings/knowledge.py +++ b/src/agent/types/settings/knowledge.py @@ -2,9 +2,12 @@ from __future__ import annotations -from dataclasses import dataclass +from dataclasses import dataclass, field +from typing import Optional from configuration import configuration +from constants import OKP_RAG_ID +from knowledge.sources.okp import OkpProductFilter from models.api.requests import QueryRequest @@ -54,8 +57,28 @@ class KnowledgeSourceSettings: id: str enabled: bool = True + +@dataclass +class OKPKnowledgeSourceSettings(KnowledgeSourceSettings): + """Settings for an OKP knowledge source. + + Attributes: + products: Product restrictions from the query. ``None`` leaves the + registered source unrestricted. + + """ + + products: Optional[list[OkpProductFilter]] = None + + +@dataclass +class KnowledgeSettings: + """Settings for a knowledge source.""" + + sources: list[KnowledgeSourceSettings] = field(default_factory=list) + @classmethod - def from_query_request(cls, request: QueryRequest) -> list[KnowledgeSourceSettings]: + def from_query_request(cls, request: QueryRequest) -> KnowledgeSettings: """Build knowledge source settings for every configured source. Parameters: @@ -65,13 +88,21 @@ def from_query_request(cls, request: QueryRequest) -> list[KnowledgeSourceSettin Returns: One knowledge source setting per configured source, with - ``enabled`` set from the request. + ``enabled`` set from the request. The OKP setting also carries + ``request.okp_products``. """ - return [ - cls( - id=source_id, - enabled=_enabled_for_request(request.vector_store_ids, source_id), - ) - for source_id in _configured_knowledge_ids() - ] + sources: list[KnowledgeSourceSettings] = [] + for source_id in _configured_knowledge_ids(): + enabled = _enabled_for_request(request.vector_store_ids, source_id) + if source_id == OKP_RAG_ID: + sources.append( + OKPKnowledgeSourceSettings( + id=source_id, + enabled=enabled, + products=request.okp_products, + ) + ) + else: + sources.append(KnowledgeSourceSettings(id=source_id, enabled=enabled)) + return cls(sources=sources) diff --git a/src/models/api/requests/query.py b/src/models/api/requests/query.py index d0ff78e29..88f4b1344 100644 --- a/src/models/api/requests/query.py +++ b/src/models/api/requests/query.py @@ -5,6 +5,7 @@ from pydantic import BaseModel, Field, field_validator, model_validator from constants import IMAGE_CONTENT_TYPES, MEDIA_TYPE_JSON, MEDIA_TYPE_TEXT +from knowledge.sources.okp import OkpProductFilter from models.common.query import Attachment, SolrVectorSearchRequest from utils import suid @@ -25,6 +26,7 @@ class QueryRequest(BaseModel): vector_store_ids: The optional list of specific vector store IDs to query for RAG. shield_ids: The optional list of configured shield names to apply. solr: Optional Solr inline RAG options (mode, filters) or legacy filter-only dict. + okp_products: The optional list of product restrictions for the OKP knowledge source. """ @@ -123,6 +125,25 @@ class QueryRequest(BaseModel): ], ) + okp_products: Optional[list[OkpProductFilter]] = Field( + None, + description=( + "Optional list of product restrictions for the OKP knowledge source. " + "Each entry names one product. Versions are OR-combined, and an empty " + "versions list matches every version of that product. Several entries " + "are OR-combined. When omitted, OKP is not restricted by product." + ), + examples=[ + [ + { + "product": "openshift_container_platform", + "versions": ["4.19", "4.20"], + } + ], + [{"product": "rhel"}], + ], + ) + # provides examples for /docs endpoint model_config = { "extra": "forbid", diff --git a/tests/unit/agent/capabilities/knowledge/test_factory.py b/tests/unit/agent/capabilities/knowledge/test_factory.py index 68a486a36..7c49ca129 100644 --- a/tests/unit/agent/capabilities/knowledge/test_factory.py +++ b/tests/unit/agent/capabilities/knowledge/test_factory.py @@ -8,7 +8,10 @@ from agent.capabilities.knowledge.factory import KnowledgeFactory from agent.types.settings import KnowledgeSourceSettings +from agent.types.settings.knowledge import KnowledgeSettings, OKPKnowledgeSourceSettings +from constants import OKP_RAG_ID from knowledge.capability import Knowledge +from knowledge.sources.okp import OkpKnowledgeSource, OkpProductFilter from models.config import RerankerConfiguration from providers.inference.errors import ( ModelNotAvailableError, @@ -48,14 +51,19 @@ def _install( return registry, provider_registry +def _settings(*sources: KnowledgeSourceSettings) -> KnowledgeSettings: + """Wrap source settings in the object the factory accepts.""" + return KnowledgeSettings(sources=list(sources)) + + def test_no_enabled_sources_build_nothing(mocker: MockerFixture) -> None: """Disabled settings and an empty list produce no knowledge capability.""" registry, providers = _install(mocker, reranker=RerankerConfiguration(enabled=True)) - assert KnowledgeFactory.build_capabilities([]) is None + assert KnowledgeFactory.build_capabilities(KnowledgeSettings()) is None assert ( KnowledgeFactory.build_capabilities( - [KnowledgeSourceSettings(id="alpha", enabled=False)] + _settings(KnowledgeSourceSettings(id="alpha", enabled=False)) ) is None ) @@ -73,7 +81,7 @@ def test_unknown_sources_are_skipped( with caplog.at_level(logging.WARNING): capability = KnowledgeFactory.build_capabilities( - [KnowledgeSourceSettings(id="missing")] + _settings(KnowledgeSourceSettings(id="missing")) ) assert capability is None @@ -93,12 +101,12 @@ def test_enabled_sources_keep_settings_order(mocker: MockerFixture) -> None: registry.find_source.side_effect = {"beta": beta, "alpha": alpha}.get capability = KnowledgeFactory.build_capabilities( - [ + _settings( KnowledgeSourceSettings(id="beta"), KnowledgeSourceSettings(id="alpha", enabled=False), KnowledgeSourceSettings(id="missing"), KnowledgeSourceSettings(id="alpha"), - ] + ) ) assert isinstance(capability, Knowledge) @@ -123,7 +131,7 @@ def test_disabled_reranker_is_not_loaded(mocker: MockerFixture) -> None: _registry.find_source.return_value = source capability = KnowledgeFactory.build_capabilities( - [KnowledgeSourceSettings(id="alpha")] + _settings(KnowledgeSourceSettings(id="alpha")) ) assert isinstance(capability, Knowledge) @@ -140,7 +148,7 @@ def test_missing_reranker_configuration_skips_the_model( _registry.find_source.return_value = source capability = KnowledgeFactory.build_capabilities( - [KnowledgeSourceSettings(id="alpha")] + _settings(KnowledgeSourceSettings(id="alpha")) ) assert isinstance(capability, Knowledge) @@ -164,7 +172,7 @@ def test_enabled_reranker_is_attached(mocker: MockerFixture) -> None: providers.resolve_model_instance.return_value = model capability = KnowledgeFactory.build_capabilities( - [KnowledgeSourceSettings(id="alpha")] + _settings(KnowledgeSourceSettings(id="alpha")) ) assert isinstance(capability, Knowledge) @@ -204,10 +212,73 @@ def test_unresolved_reranker_leaves_sources_in_place( with caplog.at_level(logging.WARNING): capability = KnowledgeFactory.build_capabilities( - [KnowledgeSourceSettings(id="alpha")] + _settings(KnowledgeSourceSettings(id="alpha")) ) assert isinstance(capability, Knowledge) assert list(capability.sources) == [source] assert capability.reranker is None assert "Could not resolve cross-encoder" in caplog.text + + +def test_okp_product_filters_replace_the_registered_source( + mocker: MockerFixture, +) -> None: + """Request product filters are copied onto a new OKP source.""" + registered = OkpKnowledgeSource(name=OKP_RAG_ID, base_url="https://okp.example") + registry, _providers = _install(mocker, reranker=None) + registry.find_source.return_value = registered + products = [ + OkpProductFilter("openshift_container_platform", ("4.19", "4.20")), + OkpProductFilter("rhel"), + ] + + capability = KnowledgeFactory.build_capabilities( + _settings(OKPKnowledgeSourceSettings(id=OKP_RAG_ID, products=products)) + ) + + assert isinstance(capability, Knowledge) + source = capability.sources[0] + assert isinstance(source, OkpKnowledgeSource) + assert source is not registered + assert source.products == tuple(products) + assert registered.products == () + + +def test_okp_source_is_reused_when_the_request_sets_no_products( + mocker: MockerFixture, +) -> None: + """An OKP request with no product filters keeps the registered source.""" + registered = OkpKnowledgeSource(name=OKP_RAG_ID, base_url="https://okp.example") + registry, _providers = _install(mocker, reranker=None) + registry.find_source.return_value = registered + + capability = KnowledgeFactory.build_capabilities( + _settings(OKPKnowledgeSourceSettings(id=OKP_RAG_ID)) + ) + + assert isinstance(capability, Knowledge) + assert capability.sources[0] is registered + + +def test_product_filters_on_a_non_okp_source_are_ignored( + mocker: MockerFixture, caplog: pytest.LogCaptureFixture +) -> None: + """Product filters are not applied when the registered source is not OKP.""" + source = mocker.Mock() + registry, _providers = _install(mocker, reranker=None) + registry.find_source.return_value = source + + with caplog.at_level(logging.WARNING): + capability = KnowledgeFactory.build_capabilities( + _settings( + OKPKnowledgeSourceSettings( + id="alpha", + products=[OkpProductFilter("rhel")], + ) + ) + ) + + assert isinstance(capability, Knowledge) + assert capability.sources[0] is source + assert "is not an OKP source; ignoring product filters" in caplog.text diff --git a/tests/unit/agent/test_factory.py b/tests/unit/agent/test_factory.py index caa2ebd29..c32b0c78e 100644 --- a/tests/unit/agent/test_factory.py +++ b/tests/unit/agent/test_factory.py @@ -90,7 +90,7 @@ async def test_create_agent_assembles_resolved_capabilities( factories["mcp"].assert_awaited_once_with(settings.mcp_servers) factories["shields"].assert_called_once_with(settings.shields) factories["skills"].assert_called_once_with(settings.skills) - factories["knowledge"].assert_called_once_with(settings.knowledge_sources) + factories["knowledge"].assert_called_once_with(settings.knowledge) factories["compaction"].assert_called_once_with(settings.compaction) factories["persistence"].assert_called_once_with() agent_cls.assert_called_once_with( diff --git a/tests/unit/agent/types/settings/test_agent.py b/tests/unit/agent/types/settings/test_agent.py index dc101f8d8..be1295172 100644 --- a/tests/unit/agent/types/settings/test_agent.py +++ b/tests/unit/agent/types/settings/test_agent.py @@ -9,7 +9,10 @@ from agent.types.settings.agent import AgentSettings from agent.types.settings.compaction import CompactionSettings -from agent.types.settings.knowledge import KnowledgeSourceSettings +from agent.types.settings.knowledge import ( + KnowledgeSourceSettings, + OKPKnowledgeSourceSettings, +) from agent.types.settings.mcp import MCPSettings from agent.types.settings.shield import ShieldSettings from agent.types.settings.skill import SkillSettings @@ -127,9 +130,9 @@ def test_query_fields_select_the_agent(mocker: MockerFixture) -> None: ShieldSettings(id="topic", enabled=False), ShieldSettings(id="pii", enabled=True), ] - assert settings.knowledge_sources == [ + assert settings.knowledge.sources == [ KnowledgeSourceSettings(id="alpha", enabled=False), - KnowledgeSourceSettings(id=OKP_RAG_ID, enabled=True), + OKPKnowledgeSourceSettings(id=OKP_RAG_ID, enabled=True), ] assert settings.mcp_servers == [MCPSettings(id="docs", headers={}, enabled=True)] @@ -153,7 +156,7 @@ def test_omitted_query_fields_use_configuration_defaults( ) assert settings.skills == [] assert [shield.enabled for shield in settings.shields] == [True, True] - assert [source.enabled for source in settings.knowledge_sources] == [True, True] + assert [source.enabled for source in settings.knowledge.sources] == [True, True] def test_empty_selections_disable_optional_capabilities( @@ -173,5 +176,5 @@ def test_empty_selections_disable_optional_capabilities( assert settings.compaction.enabled is False assert settings.skills == [] assert [shield.enabled for shield in settings.shields] == [False, False] - assert [source.enabled for source in settings.knowledge_sources] == [False, False] + assert [source.enabled for source in settings.knowledge.sources] == [False, False] assert settings.mcp_servers == [MCPSettings(id="docs", headers={}, enabled=False)] diff --git a/tests/unit/agent/types/settings/test_knowledge.py b/tests/unit/agent/types/settings/test_knowledge.py index c45d12be6..854fd845d 100644 --- a/tests/unit/agent/types/settings/test_knowledge.py +++ b/tests/unit/agent/types/settings/test_knowledge.py @@ -4,8 +4,13 @@ from pytest_mock import MockerFixture -from agent.types.settings.knowledge import KnowledgeSourceSettings +from agent.types.settings.knowledge import ( + KnowledgeSettings, + KnowledgeSourceSettings, + OKPKnowledgeSourceSettings, +) from constants import OKP_RAG_ID +from knowledge.sources.okp import OkpProductFilter from models.api.requests import QueryRequest from models.config import ( ByokConfiguration, @@ -45,9 +50,16 @@ def _rag() -> RagConfiguration: ) -def _query(vector_store_ids: list[str] | None) -> QueryRequest: +def _query( + vector_store_ids: list[str] | None, + okp_products: list[OkpProductFilter] | None = None, +) -> QueryRequest: """Build a query that selects the given knowledge sources.""" - return QueryRequest(query="What is Kubernetes?", vector_store_ids=vector_store_ids) + return QueryRequest( + query="What is Kubernetes?", + vector_store_ids=vector_store_ids, + okp_products=okp_products, + ) def test_configured_sources_stay_enabled_when_the_query_omits_ids( @@ -56,12 +68,12 @@ def test_configured_sources_stay_enabled_when_the_query_omits_ids( """Omitted vector store ids leave every configured source enabled.""" _install(mocker, _rag()) - settings = KnowledgeSourceSettings.from_query_request(_query(None)) + settings = KnowledgeSettings.from_query_request(_query(None)) - assert settings == [ + assert settings.sources == [ KnowledgeSourceSettings(id="alpha", enabled=True), KnowledgeSourceSettings(id="beta", enabled=True), - KnowledgeSourceSettings(id=OKP_RAG_ID, enabled=True), + OKPKnowledgeSourceSettings(id=OKP_RAG_ID, enabled=True), ] @@ -69,12 +81,12 @@ def test_query_ids_enable_only_the_named_sources(mocker: MockerFixture) -> None: """A provided id list disables every configured source that is absent.""" _install(mocker, _rag()) - settings = KnowledgeSourceSettings.from_query_request(_query(["beta", "unknown"])) + settings = KnowledgeSettings.from_query_request(_query(["beta", "unknown"])) - assert settings == [ + assert settings.sources == [ KnowledgeSourceSettings(id="alpha", enabled=False), KnowledgeSourceSettings(id="beta", enabled=True), - KnowledgeSourceSettings(id=OKP_RAG_ID, enabled=False), + OKPKnowledgeSourceSettings(id=OKP_RAG_ID, enabled=False), ] @@ -82,12 +94,12 @@ def test_empty_id_list_disables_every_source(mocker: MockerFixture) -> None: """An empty vector store id list disables every configured source.""" _install(mocker, _rag()) - settings = KnowledgeSourceSettings.from_query_request(_query([])) + settings = KnowledgeSettings.from_query_request(_query([])) - assert settings == [ + assert settings.sources == [ KnowledgeSourceSettings(id="alpha", enabled=False), KnowledgeSourceSettings(id="beta", enabled=False), - KnowledgeSourceSettings(id=OKP_RAG_ID, enabled=False), + OKPKnowledgeSourceSettings(id=OKP_RAG_ID, enabled=False), ] @@ -95,4 +107,25 @@ def test_no_configured_sources_produce_no_settings(mocker: MockerFixture) -> Non """An empty RAG configuration yields no knowledge source settings.""" _install(mocker, RagConfiguration()) - assert KnowledgeSourceSettings.from_query_request(_query(None)) == [] + assert KnowledgeSettings.from_query_request(_query(None)).sources == [] + + +def test_okp_products_are_kept_on_the_okp_source(mocker: MockerFixture) -> None: + """Product filters from the query are stored only on the OKP setting.""" + _install(mocker, _rag()) + products = [ + OkpProductFilter("openshift_container_platform", ("4.19", "4.20")), + OkpProductFilter("rhel"), + ] + + settings = KnowledgeSettings.from_query_request(_query(None, products)) + + assert settings.sources[-1] == OKPKnowledgeSourceSettings( + id=OKP_RAG_ID, + enabled=True, + products=products, + ) + assert all( + not isinstance(source, OKPKnowledgeSourceSettings) + for source in settings.sources[:-1] + ) diff --git a/tests/unit/models/requests/test_query_request.py b/tests/unit/models/requests/test_query_request.py index 636a76259..4a8ba2ec4 100644 --- a/tests/unit/models/requests/test_query_request.py +++ b/tests/unit/models/requests/test_query_request.py @@ -2,6 +2,7 @@ import pytest +from knowledge.sources.okp import OkpProductFilter from models.api.requests import QueryRequest, StreamingInterruptRequest from models.common.query import Attachment, SolrVectorSearchRequest @@ -28,6 +29,7 @@ def test_constructor(self) -> None: assert qr.model is None assert qr.system_prompt is None assert qr.attachments is None + assert qr.okp_products is None def test_constructor_wrong_conversation_id(self) -> None: """Test the QueryRequest constructor with wrong conversation_id.""" @@ -187,6 +189,25 @@ def test_solr_structured_mode_and_filters(self) -> None: assert solr_request.mode == "hybrid" assert solr_request.filters == {"fq": ["x:y"]} + def test_okp_products_parse_into_filters(self) -> None: + """JSON product entries become a list of OKP product filters.""" + qr = QueryRequest.model_validate( + { + "query": "What is Kubernetes?", + "okp_products": [ + { + "product": "openshift_container_platform", + "versions": ["4.19", "4.20"], + }, + {"product": "rhel"}, + ], + } + ) + assert qr.okp_products == [ + OkpProductFilter("openshift_container_platform", ("4.19", "4.20")), + OkpProductFilter("rhel"), + ] + class TestQueryRequestEmptyQuery: """Validation of empty or whitespace-only queries (RSPEED-3909)."""