diff --git a/features/steps/semantic_context_search_steps.py b/features/steps/semantic_context_search_steps.py index c6fe8480b..8106022f3 100644 --- a/features/steps/semantic_context_search_steps.py +++ b/features/steps/semantic_context_search_steps.py @@ -9,10 +9,10 @@ from cleveragents.application.services.embedding_provider import ( SimpleWordEmbeddingProvider, cosine_similarity, ) -from cleveragents.domain.models.acms.crp import FragmentProvenance from cleveragents.domain.models.core.context_fragment import ( ContextBudget, ContextFragment, + FragmentProvenance, ) @@ -128,11 +128,19 @@ def step_have_threshold(context, threshold): @when("I filter fragments by similarity threshold") def step_filter_by_threshold(context): - """Filter fragments by threshold.""" + """Filter fragments by threshold. + + Computes similarity inline so the step works whether or not a prior + "rank fragments" step ran. Scenarios that filter directly (without an + intermediate ranking step) would otherwise hit an AttributeError on + ``context.ranked_fragments``. + """ + scored = [ + (frag, cosine_similarity(context.query_embedding, frag["embedding"])) + for frag in context.fragments + ] context.filtered_fragments = [ - (frag, sim) - for frag, sim in context.ranked_fragments - if sim >= context.threshold + (frag, sim) for frag, sim in scored if sim >= context.threshold ] @@ -214,8 +222,14 @@ def step_check_ranking(context): @given("I have an embedding provider configuration") def step_have_config(context): - """Initialize embedding provider configuration.""" - context.config = { + """Initialize embedding provider configuration. + + Stored under ``embedding_config`` rather than ``config`` because Behave's + ``Context`` reserves ``config`` for its own Configuration object — setting + ``context.config`` raises ``KeyError`` from Behave's scope-tracking + ``__setattr__``. + """ + context.embedding_config = { "provider_type": "simple_word", "vocab_size": 100, } @@ -224,9 +238,9 @@ def step_have_config(context): @when("I create a semantic strategy with the configuration") def step_create_strategy_with_config(context): """Create strategy with configuration.""" - if context.config["provider_type"] == "simple_word": + if context.embedding_config["provider_type"] == "simple_word": context.strategy_provider = SimpleWordEmbeddingProvider( - vocab_size=context.config["vocab_size"] + vocab_size=context.embedding_config["vocab_size"] )