-
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.
Merge pull request #150 from aurelio-labs/simonas/dynamic-splitter
feat: Rolling window splitter
- Loading branch information
Showing
19 changed files
with
1,151 additions
and
122 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
format: | ||
poetry run black --target-version py39 . | ||
poetry run black --target-version py39 -l 88 . | ||
poetry run ruff --select I --fix . | ||
|
||
PYTHON_FILES=. | ||
lint: PYTHON_FILES=. | ||
lint_diff: PYTHON_FILES=$(shell git diff --name-only --diff-filter=d main | grep -E '\.py$$') | ||
|
||
lint lint_diff: | ||
poetry run black --target-version py39 $(PYTHON_FILES) --check | ||
poetry run black --target-version py39 -l 88 $(PYTHON_FILES) --check | ||
poetry run ruff . | ||
poetry run mypy $(PYTHON_FILES) | ||
|
||
test: | ||
poetry run pytest -vv -n 20 --cov=semantic_router --cov-report=term-missing --cov-report=xml --cov-fail-under=80 | ||
poetry run pytest -vv -n 20 --cov=semantic_router --cov-report=term-missing --cov-report=xml |
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
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
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
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
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
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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from typing import List, Optional | ||
|
||
import requests | ||
|
||
from semantic_router.llms import BaseLLM | ||
|
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
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,11 @@ | ||
from semantic_router.splitters.base import BaseSplitter | ||
from semantic_router.splitters.consecutive_sim import ConsecutiveSimSplitter | ||
from semantic_router.splitters.cumulative_sim import CumulativeSimSplitter | ||
from semantic_router.splitters.rolling_window import RollingWindowSplitter | ||
|
||
__all__ = [ | ||
"BaseSplitter", | ||
"ConsecutiveSimSplitter", | ||
"CumulativeSimSplitter", | ||
"RollingWindowSplitter", | ||
] |
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 |
---|---|---|
@@ -1,14 +1,38 @@ | ||
from typing import Any, List | ||
from typing import List | ||
|
||
from pydantic.v1 import BaseModel | ||
from colorama import Fore, Style | ||
from pydantic.v1 import BaseModel, Extra | ||
|
||
from semantic_router.encoders import BaseEncoder | ||
from semantic_router.schema import DocumentSplit | ||
|
||
|
||
class BaseSplitter(BaseModel): | ||
name: str | ||
encoder: BaseEncoder | ||
score_threshold: float | ||
|
||
def __call__(self, docs: List[Any]) -> List[List[float]]: | ||
class Config: | ||
extra = Extra.allow | ||
|
||
def __call__(self, docs: List[str]) -> List[DocumentSplit]: | ||
raise NotImplementedError("Subclasses must implement this method") | ||
|
||
def print(self, document_splits: List[DocumentSplit]) -> None: | ||
colors = [Fore.RED, Fore.GREEN, Fore.BLUE, Fore.MAGENTA] | ||
for i, split in enumerate(document_splits): | ||
color = colors[i % len(colors)] | ||
colored_content = f"{color}{split.content}{Style.RESET_ALL}" | ||
if split.is_triggered: | ||
triggered = f"{split.triggered_score:.2f}" | ||
elif i == len(document_splits) - 1: | ||
triggered = "final split" | ||
else: | ||
triggered = "token limit" | ||
print( | ||
f"Split {i + 1}, " | ||
f"tokens {split.token_count}, " | ||
f"triggered by: {triggered}" | ||
) | ||
print(colored_content) | ||
print("-" * 88) | ||
print("\n") |
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
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
Oops, something went wrong.