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
a16175ce
Commit
a16175ce
authored
11 months ago
by
Alessio Serra (ext.)
Browse files
Options
Downloads
Patches
Plain Diff
added HFEncoderEndpoint
parent
103a5e88
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/encoders/hfendpointencoder.py
+53
-0
53 additions, 0 deletions
semantic_router/encoders/hfendpointencoder.py
with
53 additions
and
0 deletions
semantic_router/encoders/hfendpointencoder.py
0 → 100644
+
53
−
0
View file @
a16175ce
from
typing
import
List
,
Optional
import
requests
from
semantic_router.encoders
import
BaseEncoder
class
HFEndpointEncoder
(
BaseEncoder
):
huggingface_url
:
Optional
[
str
]
=
None
huggingface_api_key
:
Optional
[
str
]
=
None
score_threshold
:
float
=
0.8
def
__init__
(
self
,
name
:
Optional
[
str
]
=
"
hugging_face_custom_endpoint
"
,
huggingface_url
:
Optional
[
str
]
=
None
,
huggingface_api_key
:
Optional
[
str
]
=
None
,
score_threshold
:
float
=
0.8
):
super
().
__init__
(
name
=
name
,
huggingface_url
=
huggingface_url
,
huggingface_api_key
=
huggingface_api_key
,
score_threshold
=
score_threshold
)
huggingface_url
=
huggingface_url
huggingface_api_key
=
huggingface_api_key
score_threshold
=
score_threshold
if
huggingface_url
is
None
:
raise
ValueError
(
"
HuggingFace endpoint url cannot be
'
None
'
.
"
)
if
huggingface_api_key
is
None
:
raise
ValueError
(
"
HuggingFace API key cannot be
'
None
'
.
"
)
try
:
self
.
query
({
"
inputs
"
:
"
Hello World!
"
,
"
parameters
"
:
{}
})
pass
except
Exception
as
e
:
raise
ValueError
(
f
"
HuggingFace endpoint client failed to initialize. Error:
{
e
}
"
)
from
e
def
__call__
(
self
,
docs
:
List
[
str
])
->
List
[
List
[
float
]]:
embeddings
=
[]
for
d
in
docs
:
try
:
output
=
self
.
query
({
"
inputs
"
:
d
,
"
parameters
"
:
{}
})
embeddings
.
append
(
output
[
0
])
except
Exception
as
e
:
raise
ValueError
(
f
"
No embeddings returned. Error!
"
)
return
embeddings
def
query
(
self
,
payload
):
API_URL
=
self
.
huggingface_url
headers
=
{
"
Accept
"
:
"
application/json
"
,
"
Authorization
"
:
f
"
Bearer
{
self
.
huggingface_api_key
}
"
,
"
Content-Type
"
:
"
application/json
"
}
response
=
requests
.
post
(
API_URL
,
headers
=
headers
,
json
=
payload
)
return
response
.
json
()
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