Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Semantic Router
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
MachineLearning
aurelio-labs
Semantic Router
Commits
cc04e3b4
Unverified
Commit
cc04e3b4
authored
1 year ago
by
Bogdan Buduroiu
Browse files
Options
Downloads
Patches
Plain Diff
Removes redundant splitters file
parent
398f26b9
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
semantic_router/utils/splitters.py
+0
-97
0 additions, 97 deletions
semantic_router/utils/splitters.py
with
0 additions
and
97 deletions
semantic_router/utils/splitters.py
deleted
100644 → 0
+
0
−
97
View file @
398f26b9
from
typing
import
Any
,
List
,
Literal
,
Optional
import
numpy
as
np
from
pydantic.v1
import
BaseModel
from
semantic_router.encoders
import
BaseEncoder
class
DocumentSplit
(
BaseModel
):
docs
:
List
[
Any
]
is_triggered
:
bool
=
False
triggered_score
:
Optional
[
float
]
=
None
def
semantic_splitter
(
encoder
:
BaseEncoder
,
docs
:
List
[
Any
],
threshold
:
float
,
split_method
:
Literal
[
"
consecutive_similarity_drop
"
,
"
cumulative_similarity_drop
"
]
=
"
consecutive_similarity_drop
"
,
)
->
List
[
DocumentSplit
]:
"""
Splits a list of documents base on semantic similarity changes.
Method 1:
"
consecutive_similarity_drop
"
- This method splits documents based on
the changes in similarity scores between consecutive documents.
Method 2:
"
cumulative_similarity_drop
"
- This method segments the documents based
on the changes in cumulative similarity score of the documents within the same
split.
Args:
encoder (BaseEncoder): Encoder for document embeddings.
docs (List[str]): Documents to split.
threshold (float): The similarity drop value that will trigger a new document
split.
split_method (str): The method to use for splitting.
Returns:
Dict[str, List[str]]: Splits with corresponding documents.
"""
total_docs
=
len
(
docs
)
splits
=
[]
curr_split_start_idx
=
0
curr_split_num
=
1
if
split_method
==
"
consecutive_similarity_drop
"
:
doc_embeds
=
encoder
(
docs
)
norm_embeds
=
doc_embeds
/
np
.
linalg
.
norm
(
doc_embeds
,
axis
=
1
,
keepdims
=
True
)
sim_matrix
=
np
.
matmul
(
norm_embeds
,
norm_embeds
.
T
)
for
idx
in
range
(
1
,
total_docs
):
curr_sim_score
=
sim_matrix
[
idx
-
1
][
idx
]
if
idx
<
len
(
sim_matrix
)
and
curr_sim_score
<
threshold
:
splits
.
append
(
DocumentSplit
(
docs
=
docs
[
curr_split_start_idx
:
idx
],
is_triggered
=
True
,
triggered_score
=
curr_sim_score
,
)
)
curr_split_start_idx
=
idx
curr_split_num
+=
1
elif
split_method
==
"
cumulative_similarity_drop
"
:
for
idx
in
range
(
1
,
total_docs
):
if
idx
+
1
<
total_docs
:
curr_split_docs
=
"
\n
"
.
join
(
docs
[
curr_split_start_idx
:
idx
+
1
])
next_doc
=
docs
[
idx
+
1
]
curr_split_docs_embed
=
encoder
([
curr_split_docs
])[
0
]
next_doc_embed
=
encoder
([
next_doc
])[
0
]
curr_sim_score
=
np
.
dot
(
curr_split_docs_embed
,
next_doc_embed
)
/
(
np
.
linalg
.
norm
(
curr_split_docs_embed
)
*
np
.
linalg
.
norm
(
next_doc_embed
)
)
if
curr_sim_score
<
threshold
:
splits
.
append
(
DocumentSplit
(
docs
=
docs
[
curr_split_start_idx
:
idx
+
1
],
is_triggered
=
True
,
triggered_score
=
curr_sim_score
,
)
)
curr_split_start_idx
=
idx
+
1
curr_split_num
+=
1
else
:
raise
ValueError
(
"
Invalid
'
split_method
'
. Choose either
'
consecutive_similarity_drop
'
or
"
"
'
cumulative_similarity_drop
'
.
"
)
splits
.
append
(
DocumentSplit
(
docs
=
docs
[
curr_split_start_idx
:]))
return
splits
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment