Retrieve vectors in Fusion mode (Python SDK V2)
Call the QueryVectorsFusion operation using the Python SDK V2 to search a Fusion mode vector index. This operation supports vector retrieval, scalar filtering, full-text search, and multi-way recall fusion.
Fusion Mode is currently in invitational preview and is available in the China (Shenzhen) and Indonesia (Jakarta) regions. For retrieving vectors from a Standard Mode index, see Query vectors using OSS SDK for Python 2.0.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default. The Alibaba Cloud account owner or an administrator must grant the required permissions by using a RAM policy or a bucket policy.
API | Action | Description |
QueryVectorsFusion | oss:QueryVectors and oss:GetVectors | Queries a Fusion-mode vector index. |
Method definition
query_vectors_fusion(request: QueryVectorsFusionRequest, **kwargs) → QueryVectorsFusionResultRequest parameters
Parameter | Type | Description |
request | QueryVectorsFusionRequest | The QueryVectorsFusionRequest object, which contains parameters such as bucket, index_name, knn, query, and retriever. For the meaning of each field, see the QueryVectorsFusion operation reference. |
Return values
Type | Description |
QueryVectorsFusionResult | The QueryVectorsFusionResult object, which includes status_code, request_id, vectors, next_token, and other information. |
Sample code
import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.vectors as oss_vectors
parser = argparse.ArgumentParser(description="query vectors fusion sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain name that other services can use to access OSS')
parser.add_argument('--index_name', help='The name of the fusion vector index.', required=True)
parser.add_argument('--account_id', help='The account ID.', required=True)
def main():
args = parser.parse_args()
# Load credentials from environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Using the SDK's default configuration
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
cfg.account_id = args.account_id
if args.endpoint is not None:
cfg.endpoint = args.endpoint
vector_client = oss_vectors.Client(cfg)
# QueryVectorsFusion accepts three shapes of query:
#
# knn nearest neighbor search. A single dict for one vector
# field, or a list of dicts for up to three of them. knn and
# query can share one request, and their scores add up.
# query scalar, full text and geo conditions. A logical operator
# takes the object form {"$and": {"clauses": [...]}}, and a
# full text match takes {"$textMatch": {"value": "..."}}.
# retriever combines several retrievers into one ranked result, for
# example an rrf node holding a knn leaf and a text leaf. It
# is mutually exclusive with knn, query and sort.
#
# The request below mixes knn and query: two vector recalls plus a brand
# and title boost, sorted by _score, which only accepts desc. Every hit
# carries a score.
result = vector_client.query_vectors_fusion(oss_vectors.models.QueryVectorsFusionRequest(
bucket=args.bucket,
index_name=args.index_name,
knn=[
{
"field": "text_vector",
"queryVector": [0.1] * 768,
"topK": 100,
"boost": 2.0
},
{
"field": "image_vector",
"queryVector": [0.1] * 512,
"topK": 100,
"boost": 0.5
}
],
query={
"$or": {
"clauses": [
{"brand": {"$in": {"value": ["AliBrand"], "boost": 1.0}}},
{"title": {"$textMatch": {"value": "headphones", "boost": 4.0}}}
]
}
},
return_metadata=True,
return_metadata_fields=["title", "brand", "price"],
sort=[{
"_score": {
"order": "desc"
}
}],
limit=20
))
# The third shape, retriever, cannot be mixed with knn, query or sort. An
# rrf fusion of a vector leaf and a full text leaf looks like this:
#
# result = vector_client.query_vectors_fusion(oss_vectors.models.QueryVectorsFusionRequest(
# bucket=args.bucket,
# index_name=args.index_name,
# retriever={
# "rrf": {
# "k": 50,
# "windowSize": 100,
# "retrievers": [
# {
# "retriever": {
# "knn": {
# "field": "text_vector",
# "queryVector": [0.1] * 768,
# "topK": 100
# }
# },
# "weight": 2.0
# },
# {
# "retriever": {
# "simple": {
# "query": {
# "title": {"$textMatch": {"value": "wireless headphones"}}
# }
# }
# },
# "weight": 0.5
# }
# ]
# }
# },
# limit=10,
# return_metadata=True,
# return_metadata_fields=["title", "brand"]
# ))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' next token: {result.next_token},'
)
if result.vectors:
for vector in result.vectors:
print(f'vector: {vector}')
if __name__ == "__main__":
main()