How can i use ref2vec-centroid on v4 python client?
I use this code, but vector was empty:
import asyncio
import weaviate
from weaviate.classes.config import Configure
from weaviate.client import WeaviateAsyncClient
from weaviate.collections.classes.grpc import QueryReference
from weaviate.collections.classes.config_vectorizers import Multi2VecField
from weaviate.collections.classes.config import Property, DataType, ReferenceProperty
from core.settings import get_settings
settings = get_settings()
async def init_collections(client: WeaviateAsyncClient):
if await client.collections.exists('Short'):
return
if await client.collections.exists('UserInteractions'):
return
await client.collections.create(
"Short",
properties=[
Property(
name='object_id',
data_type=DataType.INT,
description="Unique identifier for the short.",
skip_vectorization=True,
),
Property(
name='description',
data_type=DataType.TEXT,
description="Short description of the content.",
),
Property(
name='category',
data_type=DataType.TEXT,
description="Category of the short.",
),
],
vectorizer_config=[
# Set a named vector
Configure.NamedVectors.multi2vec_bind(
name="shorts_vec",
text_fields=[
Multi2VecField(name='description', weight=0.6),
Multi2VecField(name='category', weight=0.4),
]
),
],
)
await client.collections.create(
"UserInteractions",
properties=[
Property(
name='object_id',
data_type=DataType.INT,
description="Unique identifier for the user."
),
],
references=[
ReferenceProperty(
name='liked_shorts',
target_collection="Short",
description="Short which liked by current user",
),
],
vectorizer_config=Configure.Vectorizer.ref2vec_centroid(
reference_properties=[
'liked_shorts'
]
),
)
async def main():
client: WeaviateAsyncClient = weaviate.use_async_with_local(
host=settings.WEAVIATE_HOST,
port=settings.WEAVIATE_PORT,
skip_init_checks=True
)
await client.connect()
try:
await init_collections(client=client)
short_collection = client.collections.get('Short')
user_collection = client.collections.get('UserInteractions')
short_uuid = await short_collection.data.insert(properties={
'object_id': 1,
'description': 'description1',
'category': 'category1'
}, uuid=uuid.uuid4())
print(f'created short: {short_uuid}')
user_uuid = await user_collection.data.insert(properties={'object_id': 1}, uuid=uuid.uuid4())
print(f'created user: {user_uuid}')
# creating cross-reference
await user_collection.data.reference_add(
from_uuid=user_uuid,
from_property='liked_shorts',
to=short_uuid
)
resp = await user_collection.query.fetch_object_by_id(
uuid=user_uuid,
include_vector=True,
return_references=QueryReference(
link_on='liked_shorts',
return_properties=["object_id"],
),
)
print(resp)
finally:
await client.close()
if __name__ == '__main__':
asyncio.run(main())
Output:
created short: 09e06d77-a1f6-4274-b0ae-485f532322fd
created user: 0c6ff736-ef4a-45e9-9e34-93ff67a53dfe
ObjectSingleReturn(uuid=_WeaviateUUIDInt('0c6ff736-ef4a-45e9-9e34-93ff67a53dfe'), metadata=MetadataSingleObjectReturn(creation_time=datetime.datetime(2025, 1, 6, 15, 20, 12, 837000, tzinfo=datetime.timezone.utc), last_update_time=datetime.datetime(2025, 1, 6, 15, 20, 12, 840000, tzinfo=datetime.timezone.utc), is_consistent=None), properties={'object_id': 1}, references={'liked_shorts': <weaviate.collections.classes.internal._CrossReference object at 0x7c28da11e610>}, vector={}, collection='UserInteractions')