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
c40fb281
Commit
c40fb281
authored
4 months ago
by
João Galego
Browse files
Options
Downloads
Patches
Plain Diff
Added support for multimodal inputs and model-specific inference params
parent
a0f61923
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
semantic_router/encoders/bedrock.py
+35
-10
35 additions, 10 deletions
semantic_router/encoders/bedrock.py
with
35 additions
and
10 deletions
semantic_router/encoders/bedrock.py
+
35
−
10
View file @
c40fb281
...
...
@@ -17,7 +17,7 @@ Classes:
"""
import
json
from
typing
import
List
,
Optional
,
Any
from
typing
import
Dict
,
List
,
Optional
,
Any
,
Union
import
os
from
time
import
sleep
import
tiktoken
...
...
@@ -138,11 +138,12 @@ class BedrockEncoder(DenseEncoder):
)
from
err
return
bedrock_client
def
__call__
(
self
,
docs
:
List
[
str
]
)
->
List
[
List
[
float
]]:
def
__call__
(
self
,
docs
:
List
[
Union
[
str
,
Dict
]],
model_kwargs
:
Optional
[
Dict
]
=
None
)
->
List
[
List
[
float
]]:
"""
Generates embeddings for the given documents.
Args:
docs: A list of strings representing the documents to embed.
model_kwargs: A dictionary of model-specific inference parameters.
Returns:
A list of lists, where each inner list contains the embedding values for a
...
...
@@ -168,11 +169,25 @@ class BedrockEncoder(DenseEncoder):
embeddings
=
[]
if
self
.
name
and
"
amazon
"
in
self
.
name
:
for
doc
in
docs
:
embedding_body
=
json
.
dumps
(
{
"
inputText
"
:
doc
,
}
)
embedding_body
=
{}
if
isinstance
(
doc
,
dict
):
embedding_body
[
'
inputText
'
]
=
doc
.
get
(
'
text
'
)
embedding_body
[
'
inputImage
'
]
=
doc
.
get
(
'
image
'
)
# expects a base64-encoded image
else
:
embedding_body
[
'
inputText
'
]
=
doc
# Add model-specific inference parameters
if
model_kwargs
:
embedding_body
=
embedding_body
|
model_kwargs
# Clean up null values
embedding_body
=
{
k
:
v
for
k
,
v
in
embedding_body
.
items
()
if
v
}
# Format payload
embedding_body
=
json
.
dumps
(
embedding_body
)
response
=
self
.
client
.
invoke_model
(
body
=
embedding_body
,
modelId
=
self
.
name
,
...
...
@@ -184,9 +199,19 @@ class BedrockEncoder(DenseEncoder):
elif
self
.
name
and
"
cohere
"
in
self
.
name
:
chunked_docs
=
self
.
chunk_strings
(
docs
)
for
chunk
in
chunked_docs
:
chunk
=
json
.
dumps
(
{
"
texts
"
:
chunk
,
"
input_type
"
:
self
.
input_type
}
)
chunk
=
{
'
texts
'
:
chunk
,
'
input_type
'
:
self
.
input_type
}
# Add model-specific inference parameters
# Note: if specified, input_type will be overwritten by model_kwargs
if
model_kwargs
:
chunk
=
chunk
|
model_kwargs
# Format payload
chunk
=
json
.
dumps
(
chunk
)
response
=
self
.
client
.
invoke_model
(
body
=
chunk
,
modelId
=
self
.
name
,
...
...
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