Spaces:
Running
on
T4
Running
on
T4
File size: 6,685 Bytes
8ce4d25 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
#!/usr/bin/env python3
import argparse
from vespa.package import (
ApplicationPackage,
Field,
Schema,
Document,
HNSW,
RankProfile,
Function,
AuthClient,
Parameter,
FieldSet,
SecondPhaseRanking,
)
from vespa.deployment import VespaCloud
import os
def main():
parser = argparse.ArgumentParser(description="Deploy Vespa application")
parser.add_argument("--tenant_name", required=True, help="Vespa Cloud tenant name")
parser.add_argument(
"--vespa_application_name", required=True, help="Vespa application name"
)
parser.add_argument(
"--token_id_write", required=True, help="Vespa Cloud token ID for write access"
)
parser.add_argument(
"--token_id_read", required=True, help="Vespa Cloud token ID for read access"
)
args = parser.parse_args()
tenant_name = args.tenant_name
vespa_app_name = args.vespa_application_name
token_id_write = args.token_id_write
token_id_read = args.token_id_read
# Define the Vespa schema
colpali_schema = Schema(
name="pdf_page",
document=Document(
fields=[
Field(
name="id",
type="string",
indexing=["summary", "index"],
match=["word"],
),
Field(name="url", type="string", indexing=["summary", "index"]),
Field(
name="title",
type="string",
indexing=["summary", "index"],
match=["text"],
index="enable-bm25",
),
Field(
name="page_number", type="int", indexing=["summary", "attribute"]
),
Field(name="image", type="raw", indexing=["summary"]),
Field(
name="text",
type="string",
indexing=["summary", "index"],
match=["text"],
index="enable-bm25",
),
Field(
name="embedding",
type="tensor<int8>(patch{}, v[16])",
indexing=[
"attribute",
"index",
], # adds HNSW index for candidate retrieval.
ann=HNSW(
distance_metric="hamming",
max_links_per_node=32,
neighbors_to_explore_at_insert=400,
),
),
]
),
fieldsets=[
FieldSet(name="default", fields=["title", "url", "page_number", "text"]),
FieldSet(name="image", fields=["image"]),
],
)
# Define rank profiles
colpali_profile = RankProfile(
name="default",
inputs=[("query(qt)", "tensor<float>(querytoken{}, v[128])")],
functions=[
Function(
name="max_sim",
expression="""
sum(
reduce(
sum(
query(qt) * unpack_bits(attribute(embedding)) , v
),
max, patch
),
querytoken
)
""",
),
Function(name="bm25_score", expression="bm25(title) + bm25(text)"),
],
first_phase="bm25_score",
second_phase=SecondPhaseRanking(expression="max_sim", rerank_count=10),
)
colpali_schema.add_rank_profile(colpali_profile)
# Add retrieval-and-rerank rank profile
input_query_tensors = []
MAX_QUERY_TERMS = 64
for i in range(MAX_QUERY_TERMS):
input_query_tensors.append((f"query(rq{i})", "tensor<int8>(v[16])"))
input_query_tensors.append(("query(qt)", "tensor<float>(querytoken{}, v[128])"))
input_query_tensors.append(("query(qtb)", "tensor<int8>(querytoken{}, v[16])"))
colpali_retrieval_profile = RankProfile(
name="retrieval-and-rerank",
inputs=input_query_tensors,
functions=[
Function(
name="max_sim",
expression="""
sum(
reduce(
sum(
query(qt) * unpack_bits(attribute(embedding)) , v
),
max, patch
),
querytoken
)
""",
),
Function(
name="max_sim_binary",
expression="""
sum(
reduce(
1/(1 + sum(
hamming(query(qtb), attribute(embedding)) ,v)
),
max,
patch
),
querytoken
)
""",
),
],
first_phase="max_sim_binary",
second_phase=SecondPhaseRanking(expression="max_sim", rerank_count=10),
)
colpali_schema.add_rank_profile(colpali_retrieval_profile)
# Create the Vespa application package
vespa_application_package = ApplicationPackage(
name=vespa_app_name,
schema=[colpali_schema],
auth_clients=[
AuthClient(
id="mtls", # Note that you still need to include the mtls client.
permissions=["read", "write"],
parameters=[Parameter("certificate", {"file": "security/clients.pem"})],
),
AuthClient(
id="token_write",
permissions=["read", "write"],
parameters=[Parameter("token", {"id": token_id_write})],
),
AuthClient(
id="token_read",
permissions=["read"],
parameters=[Parameter("token", {"id": token_id_read})],
),
],
)
vespa_team_api_key = os.getenv("VESPA_TEAM_API_KEY")
# Deploy the application to Vespa Cloud
vespa_cloud = VespaCloud(
tenant=tenant_name,
application=vespa_app_name,
key_content=vespa_team_api_key,
application_package=vespa_application_package,
)
app = vespa_cloud.deploy()
# Output the endpoint URL
endpoint_url = vespa_cloud.get_token_endpoint()
print(f"Application deployed. Token endpoint URL: {endpoint_url}")
if __name__ == "__main__":
main()
|