Skip to content

Commit ff67345

Browse files
[cp 2.6] function sdk
Signed-off-by: junjie.jiang <[email protected]>
1 parent 1766729 commit ff67345

File tree

12 files changed

+671
-139
lines changed

12 files changed

+671
-139
lines changed

examples/function_edit.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from pymilvus import (
2+
MilvusClient,
3+
Function, DataType, FunctionType,
4+
)
5+
6+
collection_name = "text_embedding"
7+
8+
milvus_client = MilvusClient("http://localhost:19530")
9+
10+
has_collection = milvus_client.has_collection(collection_name, timeout=5)
11+
if has_collection:
12+
milvus_client.drop_collection(collection_name)
13+
14+
schema = milvus_client.create_schema()
15+
schema.add_field("id", DataType.INT64, is_primary=True, auto_id=False)
16+
schema.add_field("document", DataType.VARCHAR, max_length=9000)
17+
schema.add_field("dense", DataType.FLOAT_VECTOR, dim=1536)
18+
19+
text_embedding_function = Function(
20+
name="openai",
21+
function_type=FunctionType.TEXTEMBEDDING,
22+
input_field_names=["document"],
23+
output_field_names="dense",
24+
params={
25+
"provider": "openai",
26+
"model_name": "text-embedding-3-small",
27+
}
28+
)
29+
30+
schema.add_function(text_embedding_function)
31+
32+
index_params = milvus_client.prepare_index_params()
33+
index_params.add_index(
34+
field_name="dense",
35+
index_name="dense_index",
36+
index_type="AUTOINDEX",
37+
metric_type="IP",
38+
)
39+
40+
ret = milvus_client.create_collection(collection_name, schema=schema, index_params=index_params, consistency_level="Strong")
41+
42+
ret = milvus_client.describe_collection(collection_name)
43+
print(ret["functions"][0])
44+
45+
text_embedding_function.params["user"] = "user123"
46+
47+
milvus_client.alter_collection_function(collection_name, "openai", text_embedding_function)
48+
49+
ret = milvus_client.describe_collection(collection_name)
50+
print(ret["functions"][0])
51+
52+
milvus_client.drop_collection_function(collection_name, "openai")
53+
54+
ret = milvus_client.describe_collection(collection_name)
55+
print(ret["functions"])
56+
57+
text_embedding_function.params["user"] = "user1234"
58+
59+
milvus_client.add_collection_function(collection_name, text_embedding_function)
60+
61+
ret = milvus_client.describe_collection(collection_name)
62+
print(ret["functions"][0])

pymilvus/client/async_grpc_handler.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,60 @@ async def add_collection_field(
13051305
)
13061306
check_status(status)
13071307

1308+
@retry_on_rpc_failure()
1309+
async def drop_collection_function(
1310+
self,
1311+
collection_name: str,
1312+
function_name: str,
1313+
timeout: Optional[float] = None,
1314+
**kwargs,
1315+
):
1316+
await self.ensure_channel_ready()
1317+
check_pass_param(collection_name=collection_name, timeout=timeout)
1318+
request = Prepare.drop_collection_function_request(collection_name, function_name)
1319+
1320+
status = await self._async_stub.DropCollectionFunction(
1321+
request, timeout=timeout, metadata=_api_level_md(**kwargs)
1322+
)
1323+
check_status(status)
1324+
1325+
@retry_on_rpc_failure()
1326+
async def add_collection_function(
1327+
self,
1328+
collection_name: str,
1329+
function: Function,
1330+
timeout: Optional[float] = None,
1331+
**kwargs,
1332+
):
1333+
await self.ensure_channel_ready()
1334+
check_pass_param(collection_name=collection_name, timeout=timeout)
1335+
request = Prepare.add_collection_function_request(collection_name, function)
1336+
1337+
status = await self._async_stub.AddCollectionFunction(
1338+
request, timeout=timeout, metadata=_api_level_md(**kwargs)
1339+
)
1340+
check_status(status)
1341+
1342+
@retry_on_rpc_failure()
1343+
async def alter_collection_function(
1344+
self,
1345+
collection_name: str,
1346+
function_name: str,
1347+
function: Function,
1348+
timeout: Optional[float] = None,
1349+
**kwargs,
1350+
):
1351+
await self.ensure_channel_ready()
1352+
check_pass_param(collection_name=collection_name, timeout=timeout)
1353+
request = Prepare.alter_collection_function_request(
1354+
collection_name, function_name, function
1355+
)
1356+
1357+
status = await self._async_stub.AlterCollectionFunction(
1358+
request, timeout=timeout, metadata=_api_level_md(**kwargs)
1359+
)
1360+
check_status(status)
1361+
13081362
@retry_on_rpc_failure()
13091363
async def list_indexes(self, collection_name: str, timeout: Optional[float] = None, **kwargs):
13101364
await self.ensure_channel_ready()

pymilvus/client/grpc_handler.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,57 @@ def add_collection_field(
408408
)
409409
check_status(status)
410410

411+
@retry_on_rpc_failure()
412+
def drop_collection_function(
413+
self,
414+
collection_name: str,
415+
function_name: str,
416+
timeout: Optional[float] = None,
417+
**kwargs,
418+
):
419+
check_pass_param(collection_name=collection_name, timeout=timeout)
420+
request = Prepare.drop_collection_function_request(collection_name, function_name)
421+
422+
status = self._stub.DropCollectionFunction(
423+
request, timeout=timeout, metadata=_api_level_md(**kwargs)
424+
)
425+
check_status(status)
426+
427+
@retry_on_rpc_failure()
428+
def add_collection_function(
429+
self,
430+
collection_name: str,
431+
function: Function,
432+
timeout: Optional[float] = None,
433+
**kwargs,
434+
):
435+
check_pass_param(collection_name=collection_name, timeout=timeout)
436+
request = Prepare.add_collection_function_request(collection_name, function)
437+
438+
status = self._stub.AddCollectionFunction(
439+
request, timeout=timeout, metadata=_api_level_md(**kwargs)
440+
)
441+
check_status(status)
442+
443+
@retry_on_rpc_failure()
444+
def alter_collection_function(
445+
self,
446+
collection_name: str,
447+
function_name: str,
448+
function: Function,
449+
timeout: Optional[float] = None,
450+
**kwargs,
451+
):
452+
check_pass_param(collection_name=collection_name, timeout=timeout)
453+
request = Prepare.alter_collection_function_request(
454+
collection_name, function_name, function
455+
)
456+
457+
status = self._stub.AlterCollectionFunction(
458+
request, timeout=timeout, metadata=_api_level_md(**kwargs)
459+
)
460+
check_status(status)
461+
411462
@retry_on_rpc_failure()
412463
def alter_collection_properties(
413464
self, collection_name: str, properties: List, timeout: Optional[float] = None, **kwargs

pymilvus/client/prepare.py

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -242,16 +242,7 @@ def get_schema_from_collection_schema(
242242
schema.struct_array_fields.append(struct_schema)
243243

244244
for f in fields.functions:
245-
function_schema = schema_types.FunctionSchema(
246-
name=f.name,
247-
description=f.description,
248-
type=f.type,
249-
input_field_names=f.input_field_names,
250-
output_field_names=f.output_field_names,
251-
)
252-
for k, v in f.params.items():
253-
kv_pair = common_types.KeyValuePair(key=str(k), value=str(v))
254-
function_schema.params.append(kv_pair)
245+
function_schema = cls.convert_function_to_function_schema(f)
255246
schema.functions.append(function_schema)
256247

257248
return schema
@@ -364,6 +355,34 @@ def get_schema(
364355
def drop_collection_request(cls, collection_name: str) -> milvus_types.DropCollectionRequest:
365356
return milvus_types.DropCollectionRequest(collection_name=collection_name)
366357

358+
@classmethod
359+
def drop_collection_function_request(
360+
cls, collection_name: str, function_name: str
361+
) -> milvus_types.DropCollectionFunctionRequest:
362+
return milvus_types.DropCollectionFunctionRequest(
363+
collection_name=collection_name, function_name=function_name
364+
)
365+
366+
@classmethod
367+
def add_collection_function_request(
368+
cls, collection_name: str, f: Function
369+
) -> milvus_types.AddCollectionFunctionRequest:
370+
function_schema = cls.convert_function_to_function_schema(f)
371+
return milvus_types.AddCollectionFunctionRequest(
372+
collection_name=collection_name, functionSchema=function_schema
373+
)
374+
375+
@classmethod
376+
def alter_collection_function_request(
377+
cls, collection_name: str, function_name: str, f: Function
378+
) -> milvus_types.AlterCollectionFunctionRequest:
379+
function_schema = cls.convert_function_to_function_schema(f)
380+
return milvus_types.AlterCollectionFunctionRequest(
381+
collection_name=collection_name,
382+
function_name=function_name,
383+
functionSchema=function_schema,
384+
)
385+
367386
@classmethod
368387
def add_collection_field_request(
369388
cls,
@@ -2447,3 +2466,17 @@ def update_replicate_configuration_request(
24472466
return milvus_types.UpdateReplicateConfigurationRequest(
24482467
replicate_configuration=replicate_configuration
24492468
)
2469+
2470+
@staticmethod
2471+
def convert_function_to_function_schema(f: Function) -> schema_types.FunctionSchema:
2472+
function_schema = schema_types.FunctionSchema(
2473+
name=f.name,
2474+
description=f.description,
2475+
type=f.type,
2476+
input_field_names=f.input_field_names,
2477+
output_field_names=f.output_field_names,
2478+
)
2479+
for k, v in f.params.items():
2480+
kv_pair = common_types.KeyValuePair(key=str(k), value=str(v))
2481+
function_schema.params.append(kv_pair)
2482+
return function_schema

pymilvus/grpc_gen/common_pb2.py

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pymilvus/grpc_gen/common_pb2.pyi

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,6 @@ class ObjectPrivilege(int, metaclass=_enum_type_wrapper.EnumTypeWrapper):
356356
PrivilegeAddFileResource: _ClassVar[ObjectPrivilege]
357357
PrivilegeRemoveFileResource: _ClassVar[ObjectPrivilege]
358358
PrivilegeListFileResources: _ClassVar[ObjectPrivilege]
359-
PrivilegeAddCollectionFunction: _ClassVar[ObjectPrivilege]
360-
PrivilegeAlterCollectionFunction: _ClassVar[ObjectPrivilege]
361-
PrivilegeDropCollectionFunction: _ClassVar[ObjectPrivilege]
362359
PrivilegeUpdateReplicateConfiguration: _ClassVar[ObjectPrivilege]
363360

364361
class StateCode(int, metaclass=_enum_type_wrapper.EnumTypeWrapper):
@@ -708,9 +705,6 @@ PrivilegeAddCollectionField: ObjectPrivilege
708705
PrivilegeAddFileResource: ObjectPrivilege
709706
PrivilegeRemoveFileResource: ObjectPrivilege
710707
PrivilegeListFileResources: ObjectPrivilege
711-
PrivilegeAddCollectionFunction: ObjectPrivilege
712-
PrivilegeAlterCollectionFunction: ObjectPrivilege
713-
PrivilegeDropCollectionFunction: ObjectPrivilege
714708
PrivilegeUpdateReplicateConfiguration: ObjectPrivilege
715709
Initializing: StateCode
716710
Healthy: StateCode

pymilvus/grpc_gen/milvus_pb2.py

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pymilvus/milvus_client/async_milvus_client.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,85 @@ async def add_collection_field(
702702
**kwargs,
703703
)
704704

705+
async def add_collection_function(
706+
self, collection_name: str, function: Function, timeout: Optional[float] = None, **kwargs
707+
):
708+
"""Add a new function to the collection.
709+
710+
Args:
711+
collection_name(``string``): The name of collection.
712+
function(``Function``): The function schema.
713+
timeout (``float``, optional): A duration of time in seconds to allow for the RPC.
714+
If timeout is set to None, the client keeps waiting until the server
715+
responds or an error occurs.
716+
**kwargs (``dict``): Optional field params
717+
718+
Raises:
719+
MilvusException: If anything goes wrong
720+
"""
721+
conn = self._get_connection()
722+
await conn.add_collection_function(
723+
collection_name,
724+
function,
725+
timeout=timeout,
726+
**kwargs,
727+
)
728+
729+
async def alter_collection_function(
730+
self,
731+
collection_name: str,
732+
function_name: str,
733+
function: Function,
734+
timeout: Optional[float] = None,
735+
**kwargs,
736+
):
737+
"""Alter a function in the collection.
738+
739+
Args:
740+
collection_name(``string``): The name of collection.
741+
function_name(``string``): The function name that needs to be modified
742+
function(``Function``): The function schema.
743+
timeout (``float``, optional): A duration of time in seconds to allow for the RPC.
744+
If timeout is set to None, the client keeps waiting until the server
745+
responds or an error occurs.
746+
**kwargs (``dict``): Optional field params
747+
748+
Raises:
749+
MilvusException: If anything goes wrong
750+
"""
751+
conn = self._get_connection()
752+
await conn.alter_collection_function(
753+
collection_name,
754+
function_name,
755+
function,
756+
timeout=timeout,
757+
**kwargs,
758+
)
759+
760+
async def drop_collection_function(
761+
self, collection_name: str, function_name: str, timeout: Optional[float] = None, **kwargs
762+
):
763+
"""Drop a function from the collection.
764+
765+
Args:
766+
collection_name(``string``): The name of collection.
767+
function_name(``string``): The function name that needs to be dropped
768+
timeout (``float``, optional): A duration of time in seconds to allow for the RPC.
769+
If timeout is set to None, the client keeps waiting until the server
770+
responds or an error occurs.
771+
**kwargs (``dict``): Optional field params
772+
773+
Raises:
774+
MilvusException: If anything goes wrong
775+
"""
776+
conn = self._get_connection()
777+
await conn.drop_collection_function(
778+
collection_name,
779+
function_name,
780+
timeout=timeout,
781+
**kwargs,
782+
)
783+
705784
async def close(self):
706785
await connections.async_remove_connection(self._using)
707786

0 commit comments

Comments
 (0)