Last active
May 13, 2022 13:41
-
-
Save thigm85/f10ed59ae4d2c5099477292987373b56 to your computer and use it in GitHub Desktop.
Code to reproduce https://github.com/vespa-engine/pyvespa/issues/327
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
# clone pyvespa repo: git clone git@github.com:vespa-engine/pyvespa.git | |
# Install pyvespa from master branch - pip install -e .[full] | |
# save model.onnx file on the working directory | |
from vespa.package import ApplicationPackage, Field, OnnxModel, QueryTypeField, RankProfile, Function, FieldSet, SecondPhaseRanking | |
# | |
# Create the application package - it assumes you have model.onnx file on the working directory | |
# | |
app_package = ApplicationPackage(name="crossencoder") | |
app_package.query_profile_type.add_fields( | |
QueryTypeField( | |
name="ranking.features.query(text_embedding)", | |
type="tensor<foat>(x[1], y[512])" | |
) | |
) | |
app_package.schema.add_fields( | |
Field(name="temp", type="string", indexing=["index"]), | |
Field( | |
name="video_embedding", | |
type="tensor<float>(x[1], y[12], z[512])", | |
indexing=["attribute"] | |
), | |
) | |
app_package.schema.add_field_set(FieldSet(name="default", fields=["temp"])) | |
app_package.schema.add_model( | |
OnnxModel( | |
model_name="cross_encoder", | |
model_file_path="model.onnx", | |
inputs={ | |
"video_embedding": "attribute(video_embedding)", | |
"text_embedding": "query(text_embedding)" | |
}, | |
outputs={"similarity": "similarity"} | |
) | |
) | |
app_package.schema.add_rank_profile( | |
RankProfile( | |
name="default", | |
first_phase="similarity", | |
second_phase=SecondPhaseRanking(expression="similarity", rerank_count=10), | |
functions=[Function(name="similarity", expression="onnx(cross_encoder).similarity{d0:0,d1:0}")], | |
summary_features=[ | |
"similarity", | |
"attribute(video_embedding)", | |
"query(text_embedding)" | |
] | |
) | |
) | |
# | |
# Deploy the application package | |
# | |
from vespa.deployment import VespaDocker | |
vespa_docker = VespaDocker() | |
app = vespa_docker.deploy(application_package=app_package) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment