-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restructure and testing different decision layer structures
- Loading branch information
1 parent
ad200b0
commit 30a50b4
Showing
17 changed files
with
420 additions
and
50 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
Empty file.
This file contains 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,18 @@ | ||
from pydantic import BaseModel | ||
|
||
from semantic_router.retrievers import BaseRetriever | ||
from semantic_router.rankers import BaseRanker | ||
from semantic_router.schema import Decision | ||
|
||
|
||
class BaseMatcher(BaseModel): | ||
retriever: BaseRetriever | None | ||
ranker: BaseRanker | None | ||
top_k: int | None | ||
top_n: int | None | ||
|
||
class Config: | ||
arbitrary_types_allowed = True | ||
|
||
def __call__(self, query: str, decisions: list[Decision]) -> str: | ||
raise NotImplementedError("Subclasses must implement this method") |
This file contains 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 @@ | ||
from semantic_router import rankers |
This file contains 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,59 @@ | ||
import numpy as np | ||
|
||
from semantic_router.rankers import ( | ||
BaseRanker, | ||
CohereRanker | ||
) | ||
from semantic_router.retrievers import ( | ||
BaseRetriever, | ||
CohereRetriever | ||
) | ||
from semantic_router.matchers import BaseMatcher | ||
from semantic_router.schema import Decision | ||
|
||
|
||
class TwoStageMatcher(BaseMatcher): | ||
def __init__( | ||
self, | ||
retriever: BaseRetriever | None, | ||
ranker: BaseRanker | None, | ||
top_k: int = 25, | ||
top_n: int = 5 | ||
): | ||
super().__init__( | ||
retriever=retriever, ranker=ranker, top_k=top_k, top_n=top_n | ||
) | ||
if retriever is None: | ||
retriever = CohereRetriever( | ||
name="embed-english-v3.0", | ||
top_k=top_k | ||
) | ||
if ranker is None: | ||
ranker = CohereRanker( | ||
name="rerank-english-v2.0", | ||
top_n=top_n | ||
) | ||
|
||
def __call__(self, query: str, decisions: list[Decision]) -> str: | ||
pass | ||
|
||
def add(self, decision: Decision): | ||
self._add_decision(decision=decision) | ||
|
||
def _add_decision(self, decision: Decision): | ||
# create embeddings for first stage | ||
embeds = self.retriever(decision.utterances) | ||
# create a decision array for decision categories | ||
if self.categories is None: | ||
self.categories = np.array([decision.name] * len(embeds)) | ||
else: | ||
str_arr = np.array([decision.name] * len(embeds)) | ||
self.categories = np.concatenate([self.categories, str_arr]) | ||
# create utterance array (the index) | ||
if self.index is None: | ||
self.index = np.array(embeds) | ||
else: | ||
embed_arr = np.array(embeds) | ||
self.index = np.concatenate([self.index, embed_arr]) | ||
|
||
|
Empty file.
This file contains 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,12 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class BaseRanker(BaseModel): | ||
name: str | ||
top_n: int = 5 | ||
|
||
class Config: | ||
arbitrary_types_allowed = True | ||
|
||
def __call__(self, query: str, docs: list[str]) -> list[str]: | ||
raise NotImplementedError("Subclasses must implement this method") |
Oops, something went wrong.