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
d3bffff0
Commit
d3bffff0
authored
1 year ago
by
Ismail Ashraq
Browse files
Options
Downloads
Patches
Plain Diff
semantic splitter
parent
412d74cf
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
semantic_router/schema.py
+22
-0
22 additions, 0 deletions
semantic_router/schema.py
semantic_router/utils/splitters.py
+71
-0
71 additions, 0 deletions
semantic_router/utils/splitters.py
with
93 additions
and
0 deletions
semantic_router/schema.py
+
22
−
0
View file @
d3bffff0
...
@@ -9,6 +9,8 @@ from semantic_router.encoders import (
...
@@ -9,6 +9,8 @@ from semantic_router.encoders import (
OpenAIEncoder
,
OpenAIEncoder
,
)
)
from
semantic_router.utils.splitters
import
semantic_splitter
class
EncoderType
(
Enum
):
class
EncoderType
(
Enum
):
HUGGINGFACE
=
"
huggingface
"
HUGGINGFACE
=
"
huggingface
"
...
@@ -41,3 +43,23 @@ class Encoder:
...
@@ -41,3 +43,23 @@ class Encoder:
def
__call__
(
self
,
texts
:
list
[
str
])
->
list
[
list
[
float
]]:
def
__call__
(
self
,
texts
:
list
[
str
])
->
list
[
list
[
float
]]:
return
self
.
model
(
texts
)
return
self
.
model
(
texts
)
class
Message
(
BaseModel
):
role
:
str
content
:
str
class
Conversation
(
BaseModel
):
messages
:
list
[
Message
]
def
split_by_topic
(
self
,
encoder
:
BaseEncoder
,
threshold
:
float
=
0.5
,
split_method
:
str
=
"
consecutive_similarity_drop
"
,
):
docs
=
[
f
"
{
m
.
role
}
:
{
m
.
content
}
"
for
m
in
self
.
messages
]
return
semantic_splitter
(
encoder
=
encoder
,
docs
=
docs
,
threshold
=
threshold
,
split_method
=
split_method
)
This diff is collapsed.
Click to expand it.
semantic_router/utils/splitters.py
0 → 100644
+
71
−
0
View file @
d3bffff0
import
numpy
as
np
from
semantic_router.encoders
import
BaseEncoder
def
semantic_splitter
(
encoder
:
BaseEncoder
,
docs
:
list
[
str
],
threshold
:
float
,
split_method
:
str
=
"
consecutive_similarity_drop
"
,
)
->
dict
[
str
,
list
[
str
]]:
"""
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
):
if
idx
<
len
(
sim_matrix
)
and
sim_matrix
[
idx
-
1
][
idx
]
<
threshold
:
splits
[
f
"
split
{
curr_split_num
}
"
]
=
docs
[
curr_split_start_idx
:
idx
]
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
]
similarity
=
np
.
dot
(
curr_split_docs_embed
,
next_doc_embed
)
/
(
np
.
linalg
.
norm
(
curr_split_docs_embed
)
*
np
.
linalg
.
norm
(
next_doc_embed
)
)
if
similarity
<
threshold
:
splits
[
f
"
split
{
curr_split_num
}
"
]
=
docs
[
curr_split_start_idx
:
idx
+
1
]
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
[
f
"
split
{
curr_split_num
}
"
]
=
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