Skip to content

Commit

Permalink
update tests and removed WIP modules/objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescalam committed Dec 2, 2023
1 parent c70d086 commit 42dea8d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 53 deletions.
9 changes: 0 additions & 9 deletions semantic_router/encoders/huggingface.py

This file was deleted.

Empty file.
12 changes: 0 additions & 12 deletions semantic_router/rankers/base.py

This file was deleted.

31 changes: 0 additions & 31 deletions semantic_router/rankers/cohere.py

This file was deleted.

21 changes: 21 additions & 0 deletions tests/unit/encoders/test_bm25.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest

from semantic_router.encoders import BM25Encoder


@pytest.fixture
def bm25_encoder():
return BM25Encoder()


class TestBM25Encoder:
def test_initialization(self):
self.bm25_encoder = BM25Encoder()
assert len(self.bm25_encoder.idx_mapping) != 0

def test_call_method(self):
result = self.bm25_encoder(["test"])
assert isinstance(result, list), "Result should be a list"
assert all(
isinstance(sublist, list) for sublist in result
), "Each item in result should be a list"
70 changes: 69 additions & 1 deletion tests/unit/test_layer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from semantic_router.encoders import BaseEncoder, CohereEncoder, OpenAIEncoder
from semantic_router.layer import DecisionLayer # Replace with the actual module name
from semantic_router.layer import DecisionLayer, HybridDecisionLayer # Replace with the actual module name
from semantic_router.schema import Decision


Expand Down Expand Up @@ -111,5 +111,73 @@ def test_failover_score_threshold(self, base_encoder):
decision_layer = DecisionLayer(encoder=base_encoder)
assert decision_layer.score_threshold == 0.82

class TestHybridDecisionLayer:
def test_initialization(self, openai_encoder, decisions):
decision_layer = HybridDecisionLayer(encoder=openai_encoder, decisions=decisions)
assert decision_layer.score_threshold == 0.82
assert len(decision_layer.index) == 5
assert len(set(decision_layer.categories)) == 2

def test_initialization_different_encoders(self, cohere_encoder, openai_encoder):
decision_layer_cohere = HybridDecisionLayer(encoder=cohere_encoder)
assert decision_layer_cohere.score_threshold == 0.3

decision_layer_openai = HybridDecisionLayer(encoder=openai_encoder)
assert decision_layer_openai.score_threshold == 0.82

def test_add_decision(self, openai_encoder):
decision_layer = HybridDecisionLayer(encoder=openai_encoder)
decision = Decision(name="Decision 3", utterances=["Yes", "No"])
decision_layer.add(decision)
assert len(decision_layer.index) == 2
assert len(set(decision_layer.categories)) == 1

def test_add_multiple_decisions(self, openai_encoder, decisions):
decision_layer = HybridDecisionLayer(encoder=openai_encoder)
for decision in decisions:
decision_layer.add(decision)
assert len(decision_layer.index) == 5
assert len(set(decision_layer.categories)) == 2

def test_query_and_classification(self, openai_encoder, decisions):
decision_layer = HybridDecisionLayer(encoder=openai_encoder, decisions=decisions)
query_result = decision_layer("Hello")
assert query_result in ["Decision 1", "Decision 2"]

def test_query_with_no_index(self, openai_encoder):
decision_layer = HybridDecisionLayer(encoder=openai_encoder)
assert decision_layer("Anything") is None

def test_semantic_classify(self, openai_encoder, decisions):
decision_layer = HybridDecisionLayer(encoder=openai_encoder, decisions=decisions)
classification, score = decision_layer._semantic_classify(
[
{"decision": "Decision 1", "score": 0.9},
{"decision": "Decision 2", "score": 0.1},
]
)
assert classification == "Decision 1"
assert score == [0.9]

def test_semantic_classify_multiple_decisions(self, openai_encoder, decisions):
decision_layer = HybridDecisionLayer(encoder=openai_encoder, decisions=decisions)
classification, score = decision_layer._semantic_classify(
[
{"decision": "Decision 1", "score": 0.9},
{"decision": "Decision 2", "score": 0.1},
{"decision": "Decision 1", "score": 0.8},
]
)
assert classification == "Decision 1"
assert score == [0.9, 0.8]

def test_pass_threshold(self, openai_encoder):
decision_layer = HybridDecisionLayer(encoder=openai_encoder)
assert not decision_layer._pass_threshold([], 0.5)
assert decision_layer._pass_threshold([0.6, 0.7], 0.5)

def test_failover_score_threshold(self, base_encoder):
decision_layer = HybridDecisionLayer(encoder=base_encoder)
assert decision_layer.score_threshold == 0.82

# Add more tests for edge cases and error handling as needed.

0 comments on commit 42dea8d

Please sign in to comment.