File size: 40,009 Bytes
b3d711f | 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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 | from typing import TYPE_CHECKING
from tenacity import retry, stop_after_attempt, retry_if_exception, wait_fixed
from chromadb.api import ServerAPI
if TYPE_CHECKING:
from chromadb.api.models.AttachedFunction import AttachedFunction
from chromadb.api.collection_configuration import (
CreateCollectionConfiguration,
UpdateCollectionConfiguration,
create_collection_configuration_to_json,
)
from chromadb.auth import UserIdentity
from chromadb.config import DEFAULT_DATABASE, DEFAULT_TENANT, Settings, System
from chromadb.db.system import SysDB
from chromadb.quota import QuotaEnforcer, Action
from chromadb.rate_limit import RateLimitEnforcer
from chromadb.segment import SegmentManager
from chromadb.execution.executor.abstract import Executor
from chromadb.execution.expression.operator import Scan, Filter, Limit, KNN, Projection
from chromadb.execution.expression.plan import CountPlan, GetPlan, KNNPlan
from chromadb.telemetry.opentelemetry import (
add_attributes_to_current_span,
OpenTelemetryClient,
OpenTelemetryGranularity,
trace_method,
)
from chromadb.telemetry.product import ProductTelemetryClient
from chromadb.ingest import Producer
from chromadb.types import Collection as CollectionModel
from chromadb import __version__
from chromadb.errors import (
InvalidDimensionException,
NotFoundError,
VersionMismatchError,
)
from chromadb.api.types import (
CollectionMetadata,
IDs,
Embeddings,
Metadatas,
Documents,
ReadLevel,
Schema,
URIs,
Where,
WhereDocument,
Include,
GetResult,
QueryResult,
SearchResult,
validate_metadata,
validate_update_metadata,
validate_where,
validate_where_document,
validate_batch,
IncludeMetadataDocuments,
IncludeMetadataDocumentsDistances,
DeleteResult,
)
from chromadb.telemetry.product.events import (
CollectionAddEvent,
CollectionDeleteEvent,
CollectionGetEvent,
CollectionUpdateEvent,
CollectionQueryEvent,
ClientCreateCollectionEvent,
)
import chromadb.types as t
from typing import (
Optional,
Sequence,
Generator,
List,
Any,
Dict,
Callable,
TypeVar,
Tuple,
)
from overrides import override
from uuid import UUID, uuid4
from functools import wraps
import time
import logging
import re
from chromadb.execution.expression.plan import Search
T = TypeVar("T", bound=Callable[..., Any])
logger = logging.getLogger(__name__)
# mimics s3 bucket requirements for naming
def check_index_name(index_name: str) -> None:
msg = (
"Expected collection name that "
"(1) contains 3-63 characters, "
"(2) starts and ends with an alphanumeric character, "
"(3) otherwise contains only alphanumeric characters, underscores or hyphens (-), "
"(4) contains no two consecutive periods (..) and "
"(5) is not a valid IPv4 address, "
f"got {index_name}"
)
if len(index_name) < 3 or len(index_name) > 63:
raise ValueError(msg)
if not re.match("^[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]$", index_name):
raise ValueError(msg)
if ".." in index_name:
raise ValueError(msg)
if re.match("^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$", index_name):
raise ValueError(msg)
def rate_limit(func: T) -> T:
@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
self = args[0]
return self._rate_limit_enforcer.rate_limit(func)(*args, **kwargs)
return wrapper # type: ignore
class SegmentAPI(ServerAPI):
"""API implementation utilizing the new segment-based internal architecture"""
_settings: Settings
_sysdb: SysDB
_manager: SegmentManager
_executor: Executor
_producer: Producer
_product_telemetry_client: ProductTelemetryClient
_opentelemetry_client: OpenTelemetryClient
_tenant_id: str
_topic_ns: str
_rate_limit_enforcer: RateLimitEnforcer
def __init__(self, system: System):
super().__init__(system)
self._settings = system.settings
self._sysdb = self.require(SysDB)
self._manager = self.require(SegmentManager)
self._executor = self.require(Executor)
self._quota_enforcer = self.require(QuotaEnforcer)
self._product_telemetry_client = self.require(ProductTelemetryClient)
self._opentelemetry_client = self.require(OpenTelemetryClient)
self._producer = self.require(Producer)
self._rate_limit_enforcer = self._system.require(RateLimitEnforcer)
@override
def heartbeat(self) -> int:
return int(time.time_ns())
@trace_method("SegmentAPI.create_database", OpenTelemetryGranularity.OPERATION)
@override
def create_database(self, name: str, tenant: str = DEFAULT_TENANT) -> None:
if len(name) < 3:
raise ValueError("Database name must be at least 3 characters long")
self._quota_enforcer.enforce(
action=Action.CREATE_DATABASE,
tenant=tenant,
name=name,
)
self._sysdb.create_database(
id=uuid4(),
name=name,
tenant=tenant,
)
@trace_method("SegmentAPI.get_database", OpenTelemetryGranularity.OPERATION)
@override
def get_database(self, name: str, tenant: str = DEFAULT_TENANT) -> t.Database:
return self._sysdb.get_database(name=name, tenant=tenant)
@trace_method("SegmentAPI.delete_database", OpenTelemetryGranularity.OPERATION)
@override
def delete_database(self, name: str, tenant: str = DEFAULT_TENANT) -> None:
self._sysdb.delete_database(name=name, tenant=tenant)
@trace_method("SegmentAPI.list_databases", OpenTelemetryGranularity.OPERATION)
@override
def list_databases(
self,
limit: Optional[int] = None,
offset: Optional[int] = None,
tenant: str = DEFAULT_TENANT,
) -> Sequence[t.Database]:
return self._sysdb.list_databases(limit=limit, offset=offset, tenant=tenant)
@trace_method("SegmentAPI.create_tenant", OpenTelemetryGranularity.OPERATION)
@override
def create_tenant(self, name: str) -> None:
if len(name) < 3:
raise ValueError("Tenant name must be at least 3 characters long")
self._sysdb.create_tenant(
name=name,
)
@override
def get_user_identity(self) -> UserIdentity:
return UserIdentity(
user_id="",
tenant=DEFAULT_TENANT,
databases=[DEFAULT_DATABASE],
)
@trace_method("SegmentAPI.get_tenant", OpenTelemetryGranularity.OPERATION)
@override
def get_tenant(self, name: str) -> t.Tenant:
return self._sysdb.get_tenant(name=name)
# TODO: Actually fix CollectionMetadata type to remove type: ignore flags. This is
# necessary because changing the value type from `Any` to`` `Union[str, int, float]`
# causes the system to somehow convert all values to strings.
@trace_method("SegmentAPI.create_collection", OpenTelemetryGranularity.OPERATION)
@override
@rate_limit
def create_collection(
self,
name: str,
schema: Optional[Schema] = None,
configuration: Optional[CreateCollectionConfiguration] = None,
metadata: Optional[CollectionMetadata] = None,
get_or_create: bool = False,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> CollectionModel:
if metadata is not None:
validate_metadata(metadata)
# TODO: remove backwards compatibility in naming requirements
check_index_name(name)
self._quota_enforcer.enforce(
action=Action.CREATE_COLLECTION,
tenant=tenant,
name=name,
metadata=metadata,
)
id = uuid4()
model = CollectionModel(
id=id,
name=name,
metadata=metadata,
serialized_schema=None,
configuration_json=create_collection_configuration_to_json(
configuration or CreateCollectionConfiguration(), metadata
),
tenant=tenant,
database=database,
dimension=None,
)
# TODO: Let sysdb create the collection directly from the model
coll, created = self._sysdb.create_collection(
id=model.id,
name=model.name,
schema=schema,
configuration=configuration or CreateCollectionConfiguration(),
segments=[], # Passing empty till backend changes are deployed.
metadata=model.metadata,
dimension=None, # This is lazily populated on the first add
get_or_create=get_or_create,
tenant=tenant,
database=database,
)
if created:
segments = self._manager.prepare_segments_for_new_collection(coll)
for segment in segments:
self._sysdb.create_segment(segment)
else:
logger.debug(
f"Collection {name} already exists, returning existing collection."
)
# TODO: This event doesn't capture the get_or_create case appropriately
# TODO: Re-enable embedding function tracking in create_collection
self._product_telemetry_client.capture(
ClientCreateCollectionEvent(
collection_uuid=str(id),
# embedding_function=embedding_function.__class__.__name__,
)
)
add_attributes_to_current_span({"collection_uuid": str(id)})
return coll
@trace_method(
"SegmentAPI.get_or_create_collection", OpenTelemetryGranularity.OPERATION
)
@override
@rate_limit
def get_or_create_collection(
self,
name: str,
schema: Optional[Schema] = None,
configuration: Optional[CreateCollectionConfiguration] = None,
metadata: Optional[CollectionMetadata] = None,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> CollectionModel:
return self.create_collection(
name=name,
schema=schema,
metadata=metadata,
configuration=configuration,
get_or_create=True,
tenant=tenant,
database=database,
)
# TODO: Actually fix CollectionMetadata type to remove type: ignore flags. This is
# necessary because changing the value type from `Any` to`` `Union[str, int, float]`
# causes the system to somehow convert all values to strings
@trace_method("SegmentAPI.get_collection", OpenTelemetryGranularity.OPERATION)
@override
@rate_limit
def get_collection(
self,
name: Optional[str] = None,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> CollectionModel:
existing = self._sysdb.get_collections(
name=name, tenant=tenant, database=database
)
if existing:
return existing[0]
else:
raise NotFoundError(f"Collection {name} does not exist.")
@trace_method("SegmentAPI.get_collection_by_id", OpenTelemetryGranularity.OPERATION)
@override
@rate_limit
def get_collection_by_id(
self,
collection_id: UUID,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> CollectionModel:
existing = self._sysdb.get_collections(
id=collection_id, tenant=tenant, database=database
)
if existing:
return existing[0]
else:
raise NotFoundError(f"Collection {collection_id} does not exist.")
@trace_method("SegmentAPI.list_collection", OpenTelemetryGranularity.OPERATION)
@override
@rate_limit
def list_collections(
self,
limit: Optional[int] = None,
offset: Optional[int] = None,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> Sequence[CollectionModel]:
self._quota_enforcer.enforce(
action=Action.LIST_COLLECTIONS,
tenant=tenant,
limit=limit,
)
return self._sysdb.get_collections(
limit=limit, offset=offset, tenant=tenant, database=database
)
@trace_method("SegmentAPI.count_collections", OpenTelemetryGranularity.OPERATION)
@override
@rate_limit
def count_collections(
self,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> int:
return self._sysdb.count_collections(tenant=tenant, database=database)
@trace_method("SegmentAPI._modify", OpenTelemetryGranularity.OPERATION)
@override
@rate_limit
def _modify(
self,
id: UUID,
new_name: Optional[str] = None,
new_metadata: Optional[CollectionMetadata] = None,
new_configuration: Optional[UpdateCollectionConfiguration] = None,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> None:
if new_name:
# backwards compatibility in naming requirements (for now)
check_index_name(new_name)
if new_metadata:
validate_update_metadata(new_metadata)
# Ensure the collection exists
_ = self._get_collection(id)
self._quota_enforcer.enforce(
action=Action.UPDATE_COLLECTION,
tenant=tenant,
name=new_name,
metadata=new_metadata,
)
# TODO eventually we'll want to use OptionalArgument and Unspecified in the
# signature of `_modify` but not changing the API right now.
if new_name and new_metadata and new_configuration:
self._sysdb.update_collection(
id,
name=new_name,
metadata=new_metadata,
configuration=new_configuration,
)
elif new_name and new_metadata:
self._sysdb.update_collection(id, name=new_name, metadata=new_metadata)
elif new_name and new_configuration:
self._sysdb.update_collection(
id, name=new_name, configuration=new_configuration
)
elif new_metadata and new_configuration:
self._sysdb.update_collection(
id, metadata=new_metadata, configuration=new_configuration
)
elif new_name:
self._sysdb.update_collection(id, name=new_name)
elif new_metadata:
self._sysdb.update_collection(id, metadata=new_metadata)
elif new_configuration:
self._sysdb.update_collection(id, configuration=new_configuration)
@override
def _fork(
self,
collection_id: UUID,
new_name: str,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> CollectionModel:
raise NotImplementedError(
"Collection forking is not implemented for SegmentAPI"
)
@override
def _fork_count(
self,
collection_id: UUID,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> int:
raise NotImplementedError(
"Fork count is not implemented for SegmentAPI"
)
@override
def _get_indexing_status(
self,
collection_id: UUID,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> "IndexingStatus":
raise NotImplementedError("Indexing status is not implemented for SegmentAPI")
@override
def _search(
self,
collection_id: UUID,
searches: List[Search],
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
read_level: ReadLevel = ReadLevel.INDEX_AND_WAL,
) -> SearchResult:
raise NotImplementedError("Search is not implemented for SegmentAPI")
@trace_method("SegmentAPI.delete_collection", OpenTelemetryGranularity.OPERATION)
@override
@rate_limit
def delete_collection(
self,
name: str,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> None:
existing = self._sysdb.get_collections(
name=name, tenant=tenant, database=database
)
if existing:
self._manager.delete_segments(existing[0].id)
self._sysdb.delete_collection(
existing[0].id, tenant=tenant, database=database
)
else:
raise ValueError(f"Collection {name} does not exist.")
@trace_method("SegmentAPI._add", OpenTelemetryGranularity.OPERATION)
@override
@rate_limit
def _add(
self,
ids: IDs,
collection_id: UUID,
embeddings: Embeddings,
metadatas: Optional[Metadatas] = None,
documents: Optional[Documents] = None,
uris: Optional[URIs] = None,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> bool:
coll = self._get_collection(collection_id)
self._manager.hint_use_collection(collection_id, t.Operation.ADD)
validate_batch(
(ids, embeddings, metadatas, documents, uris),
{"max_batch_size": self.get_max_batch_size()},
)
records_to_submit = list(
_records(
t.Operation.ADD,
ids=ids,
embeddings=embeddings,
metadatas=metadatas,
documents=documents,
uris=uris,
)
)
self._validate_embedding_record_set(coll, records_to_submit)
self._quota_enforcer.enforce(
action=Action.ADD,
tenant=tenant,
ids=ids,
embeddings=embeddings,
metadatas=metadatas,
documents=documents,
uris=uris,
collection_id=collection_id,
)
self._producer.submit_embeddings(collection_id, records_to_submit)
self._product_telemetry_client.capture(
CollectionAddEvent(
collection_uuid=str(collection_id),
add_amount=len(ids),
with_metadata=len(ids) if metadatas is not None else 0,
with_documents=len(ids) if documents is not None else 0,
with_uris=len(ids) if uris is not None else 0,
)
)
return True
@trace_method("SegmentAPI._update", OpenTelemetryGranularity.OPERATION)
@override
@rate_limit
def _update(
self,
collection_id: UUID,
ids: IDs,
embeddings: Optional[Embeddings] = None,
metadatas: Optional[Metadatas] = None,
documents: Optional[Documents] = None,
uris: Optional[URIs] = None,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> bool:
coll = self._get_collection(collection_id)
self._manager.hint_use_collection(collection_id, t.Operation.UPDATE)
validate_batch(
(ids, embeddings, metadatas, documents, uris),
{"max_batch_size": self.get_max_batch_size()},
)
records_to_submit = list(
_records(
t.Operation.UPDATE,
ids=ids,
embeddings=embeddings,
metadatas=metadatas,
documents=documents,
uris=uris,
)
)
self._validate_embedding_record_set(coll, records_to_submit)
self._quota_enforcer.enforce(
action=Action.UPDATE,
tenant=tenant,
ids=ids,
embeddings=embeddings,
metadatas=metadatas,
documents=documents,
uris=uris,
)
self._producer.submit_embeddings(collection_id, records_to_submit)
self._product_telemetry_client.capture(
CollectionUpdateEvent(
collection_uuid=str(collection_id),
update_amount=len(ids),
with_embeddings=len(embeddings) if embeddings else 0,
with_metadata=len(metadatas) if metadatas else 0,
with_documents=len(documents) if documents else 0,
with_uris=len(uris) if uris else 0,
)
)
return True
@trace_method("SegmentAPI._upsert", OpenTelemetryGranularity.OPERATION)
@override
@rate_limit
def _upsert(
self,
collection_id: UUID,
ids: IDs,
embeddings: Embeddings,
metadatas: Optional[Metadatas] = None,
documents: Optional[Documents] = None,
uris: Optional[URIs] = None,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> bool:
coll = self._get_collection(collection_id)
self._manager.hint_use_collection(collection_id, t.Operation.UPSERT)
validate_batch(
(ids, embeddings, metadatas, documents, uris),
{"max_batch_size": self.get_max_batch_size()},
)
records_to_submit = list(
_records(
t.Operation.UPSERT,
ids=ids,
embeddings=embeddings,
metadatas=metadatas,
documents=documents,
uris=uris,
)
)
self._validate_embedding_record_set(coll, records_to_submit)
self._quota_enforcer.enforce(
action=Action.UPSERT,
tenant=tenant,
ids=ids,
embeddings=embeddings,
metadatas=metadatas,
documents=documents,
uris=uris,
collection_id=collection_id,
)
self._producer.submit_embeddings(collection_id, records_to_submit)
return True
@trace_method("SegmentAPI._get", OpenTelemetryGranularity.OPERATION)
@retry( # type: ignore[misc]
retry=retry_if_exception(lambda e: isinstance(e, VersionMismatchError)),
wait=wait_fixed(2),
stop=stop_after_attempt(5),
reraise=True,
)
@override
@rate_limit
def _get(
self,
collection_id: UUID,
ids: Optional[IDs] = None,
where: Optional[Where] = None,
limit: Optional[int] = None,
offset: Optional[int] = None,
where_document: Optional[WhereDocument] = None,
include: Include = IncludeMetadataDocuments,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> GetResult:
add_attributes_to_current_span(
{
"collection_id": str(collection_id),
"ids_count": len(ids) if ids else 0,
}
)
scan = self._scan(collection_id)
# TODO: Replace with unified validation
if where is not None:
validate_where(where)
if where_document is not None:
validate_where_document(where_document)
self._quota_enforcer.enforce(
action=Action.GET,
tenant=tenant,
ids=ids,
where=where,
where_document=where_document,
limit=limit,
)
ids_amount = len(ids) if ids else 0
self._product_telemetry_client.capture(
CollectionGetEvent(
collection_uuid=str(collection_id),
ids_count=ids_amount,
limit=limit if limit else 0,
include_metadata=ids_amount if "metadatas" in include else 0,
include_documents=ids_amount if "documents" in include else 0,
include_uris=ids_amount if "uris" in include else 0,
)
)
return self._executor.get(
GetPlan(
scan,
Filter(ids, where, where_document),
Limit(offset or 0, limit),
Projection(
"documents" in include,
"embeddings" in include,
"metadatas" in include,
False,
"uris" in include,
),
)
)
@trace_method("SegmentAPI._delete", OpenTelemetryGranularity.OPERATION)
@override
@rate_limit
def _delete(
self,
collection_id: UUID,
ids: Optional[IDs] = None,
where: Optional[Where] = None,
where_document: Optional[WhereDocument] = None,
limit: Optional[int] = None,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> DeleteResult:
add_attributes_to_current_span(
{
"collection_id": str(collection_id),
"ids_count": len(ids) if ids else 0,
}
)
# TODO: Replace with unified validation
if where is not None:
validate_where(where)
if where_document is not None:
validate_where_document(where_document)
# You must have at least one of non-empty ids, where, or where_document.
if (
(ids is None or (ids is not None and len(ids) == 0))
and (where is None or (where is not None and len(where) == 0))
and (
where_document is None
or (where_document is not None and len(where_document) == 0)
)
):
raise ValueError(
"""
You must provide either ids, where, or where_document to delete. If
you want to delete all data in a collection you can delete the
collection itself using the delete_collection method. Or alternatively,
you can get() all the relevant ids and then delete them.
"""
)
scan = self._scan(collection_id)
self._quota_enforcer.enforce(
action=Action.DELETE,
tenant=tenant,
ids=ids,
where=where,
where_document=where_document,
)
self._manager.hint_use_collection(collection_id, t.Operation.DELETE)
if (where or where_document) or not ids:
ids_to_delete = self._executor.get(
GetPlan(scan, Filter(ids, where, where_document))
)["ids"]
else:
ids_to_delete = ids
# Apply limit if specified (validated upstream, but enforce defensively)
if limit is not None:
if not isinstance(limit, int) or isinstance(limit, bool) or limit < 0:
raise ValueError("limit must be a non-negative integer")
if where is None and where_document is None:
raise ValueError(
"limit can only be specified when a where or where_document clause is provided"
)
ids_to_delete = ids_to_delete[:limit]
if len(ids_to_delete) == 0:
return DeleteResult(deleted=0)
records_to_submit = list(
_records(operation=t.Operation.DELETE, ids=ids_to_delete)
)
self._validate_embedding_record_set(scan.collection, records_to_submit)
self._producer.submit_embeddings(collection_id, records_to_submit)
deleted_count = len(ids_to_delete)
self._product_telemetry_client.capture(
CollectionDeleteEvent(
collection_uuid=str(collection_id), delete_amount=deleted_count
)
)
return DeleteResult(deleted=deleted_count)
@trace_method("SegmentAPI._count", OpenTelemetryGranularity.OPERATION)
@retry( # type: ignore[misc]
retry=retry_if_exception(lambda e: isinstance(e, VersionMismatchError)),
wait=wait_fixed(2),
stop=stop_after_attempt(5),
reraise=True,
)
@override
@rate_limit
def _count(
self,
collection_id: UUID,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
read_level: ReadLevel = ReadLevel.INDEX_AND_WAL,
) -> int:
add_attributes_to_current_span({"collection_id": str(collection_id)})
return self._executor.count(CountPlan(self._scan(collection_id)))
@trace_method("SegmentAPI._query", OpenTelemetryGranularity.OPERATION)
# We retry on version mismatch errors because the version of the collection
# may have changed between the time we got the version and the time we
# actually query the collection on the FE. We are fine with fixed
# wait time because the version mismatch error is not a error due to
# network issues or other transient issues. It is a result of the
# collection being updated between the time we got the version and
# the time we actually query the collection on the FE.
@retry( # type: ignore[misc]
retry=retry_if_exception(lambda e: isinstance(e, VersionMismatchError)),
wait=wait_fixed(2),
stop=stop_after_attempt(5),
reraise=True,
)
@override
@rate_limit
def _query(
self,
collection_id: UUID,
query_embeddings: Embeddings,
ids: Optional[IDs] = None,
n_results: int = 10,
where: Optional[Where] = None,
where_document: Optional[WhereDocument] = None,
include: Include = IncludeMetadataDocumentsDistances,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> QueryResult:
add_attributes_to_current_span(
{
"collection_id": str(collection_id),
"n_results": n_results,
"where": str(where),
}
)
query_amount = len(query_embeddings)
ids_amount = len(ids) if ids else 0
self._product_telemetry_client.capture(
CollectionQueryEvent(
collection_uuid=str(collection_id),
query_amount=query_amount,
filtered_ids_amount=ids_amount,
n_results=n_results,
with_metadata_filter=query_amount if where is not None else 0,
with_document_filter=query_amount if where_document is not None else 0,
include_metadatas=query_amount if "metadatas" in include else 0,
include_documents=query_amount if "documents" in include else 0,
include_uris=query_amount if "uris" in include else 0,
include_distances=query_amount if "distances" in include else 0,
)
)
# TODO: Replace with unified validation
if where is not None:
validate_where(where)
if where_document is not None:
validate_where_document(where_document)
scan = self._scan(collection_id)
for embedding in query_embeddings:
self._validate_dimension(scan.collection, len(embedding), update=False)
self._quota_enforcer.enforce(
action=Action.QUERY,
tenant=tenant,
where=where,
where_document=where_document,
query_embeddings=query_embeddings,
n_results=n_results,
)
return self._executor.knn(
KNNPlan(
scan,
KNN(query_embeddings, n_results),
Filter(None, where, where_document),
Projection(
"documents" in include,
"embeddings" in include,
"metadatas" in include,
"distances" in include,
"uris" in include,
),
)
)
@trace_method("SegmentAPI._peek", OpenTelemetryGranularity.OPERATION)
@override
@rate_limit
def _peek(
self,
collection_id: UUID,
n: int = 10,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> GetResult:
add_attributes_to_current_span({"collection_id": str(collection_id)})
return self._get(collection_id, limit=n) # type: ignore
@override
def get_version(self) -> str:
return __version__
@override
def reset_state(self) -> None:
pass
@override
def reset(self) -> bool:
self._system.reset_state()
return True
@override
def get_settings(self) -> Settings:
return self._settings
@override
def get_max_batch_size(self) -> int:
return self._producer.max_batch_size
@override
def attach_function(
self,
function_id: str,
name: str,
input_collection_id: UUID,
output_collection: str,
params: Optional[Dict[str, Any]] = None,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> Tuple["AttachedFunction", bool]:
"""Attached functions are not supported in the Segment API (local embedded mode)."""
raise NotImplementedError(
"Attached functions are only supported when connecting to a Chroma server via HttpClient. "
"The Segment API (embedded mode) does not support attached function operations."
)
@override
def get_attached_function(
self,
name: str,
input_collection_id: UUID,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> "AttachedFunction":
"""Attached functions are not supported in the Segment API (local embedded mode)."""
raise NotImplementedError(
"Attached functions are only supported when connecting to a Chroma server via HttpClient. "
"The Segment API (embedded mode) does not support attached function operations."
)
@override
def detach_function(
self,
name: str,
input_collection_id: UUID,
delete_output: bool = False,
tenant: str = DEFAULT_TENANT,
database: str = DEFAULT_DATABASE,
) -> bool:
"""Attached functions are not supported in the Segment API (local embedded mode)."""
raise NotImplementedError(
"Attached functions are only supported when connecting to a Chroma server via HttpClient. "
"The Segment API (embedded mode) does not support attached function operations."
)
# TODO: This could potentially cause race conditions in a distributed version of the
# system, since the cache is only local.
# TODO: promote collection -> topic to a base class method so that it can be
# used for channel assignment in the distributed version of the system.
@trace_method(
"SegmentAPI._validate_embedding_record_set", OpenTelemetryGranularity.ALL
)
def _validate_embedding_record_set(
self, collection: t.Collection, records: List[t.OperationRecord]
) -> None:
"""Validate the dimension of an embedding record before submitting it to the system."""
add_attributes_to_current_span({"collection_id": str(collection["id"])})
for record in records:
if record["embedding"] is not None:
self._validate_dimension(
collection, len(record["embedding"]), update=True
)
# This method is intentionally left untraced because otherwise it can emit thousands of spans for requests containing many embeddings.
def _validate_dimension(
self, collection: t.Collection, dim: int, update: bool
) -> None:
"""Validate that a collection supports records of the given dimension. If update
is true, update the collection if the collection doesn't already have a
dimension."""
if collection["dimension"] is None:
if update:
id = collection.id
self._sysdb.update_collection(id=id, dimension=dim)
collection["dimension"] = dim
elif collection["dimension"] != dim:
raise InvalidDimensionException(
f"Embedding dimension {dim} does not match collection dimensionality {collection['dimension']}"
)
else:
return # all is well
@trace_method("SegmentAPI._get_collection", OpenTelemetryGranularity.ALL)
def _get_collection(self, collection_id: UUID) -> t.Collection:
collections = self._sysdb.get_collections(id=collection_id)
if not collections or len(collections) == 0:
raise NotFoundError(f"Collection {collection_id} does not exist.")
return collections[0]
@trace_method("SegmentAPI._scan", OpenTelemetryGranularity.OPERATION)
def _scan(self, collection_id: UUID) -> Scan:
collection_and_segments = self._sysdb.get_collection_with_segments(
collection_id
)
# For now collection should have exactly one segment per scope:
# - Local scopes: vector, metadata
# - Distributed scopes: vector, metadata, record
scope_to_segment = {
segment["scope"]: segment for segment in collection_and_segments["segments"]
}
return Scan(
collection=collection_and_segments["collection"],
knn=scope_to_segment[t.SegmentScope.VECTOR],
metadata=scope_to_segment[t.SegmentScope.METADATA],
# Local chroma do not have record segment, and this is not used by the local executor
record=scope_to_segment.get(t.SegmentScope.RECORD, None), # type: ignore[arg-type]
)
def _records(
operation: t.Operation,
ids: IDs,
embeddings: Optional[Embeddings] = None,
metadatas: Optional[Metadatas] = None,
documents: Optional[Documents] = None,
uris: Optional[URIs] = None,
) -> Generator[t.OperationRecord, None, None]:
"""Convert parallel lists of embeddings, metadatas and documents to a sequence of
SubmitEmbeddingRecords"""
# Presumes that callers were invoked via Collection model, which means
# that we know that the embeddings, metadatas and documents have already been
# normalized and are guaranteed to be consistently named lists.
if embeddings == []:
embeddings = None
for i, id in enumerate(ids):
metadata = None
if metadatas:
metadata = metadatas[i]
if documents:
document = documents[i]
if metadata:
metadata = {**metadata, "chroma:document": document}
else:
metadata = {"chroma:document": document}
if uris:
uri = uris[i]
if metadata:
metadata = {**metadata, "chroma:uri": uri}
else:
metadata = {"chroma:uri": uri}
record = t.OperationRecord(
id=id,
embedding=embeddings[i] if embeddings is not None else None,
encoding=t.ScalarEncoding.FLOAT32, # Hardcode for now
metadata=metadata,
operation=operation,
)
yield record
|