Skip to content

Instantly share code, notes, and snippets.

@thigm85
Last active May 13, 2022 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thigm85/f10ed59ae4d2c5099477292987373b56 to your computer and use it in GitHub Desktop.
Save thigm85/f10ed59ae4d2c5099477292987373b56 to your computer and use it in GitHub Desktop.
# 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