|
| 1 | +import os |
| 2 | +import random |
| 3 | + |
| 4 | +import pymilvus |
| 5 | +import pytest |
| 6 | +from opentelemetry.semconv_ai import Events, SpanAttributes, EventAttributes |
| 7 | + |
| 8 | +path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "milvus.db") |
| 9 | +milvus = pymilvus.MilvusClient(uri=path) |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def collection(): |
| 14 | + collection_name = "Colors" |
| 15 | + milvus.create_collection(collection_name=collection_name, dimension=5) |
| 16 | + yield collection_name |
| 17 | + milvus.drop_collection(collection_name=collection_name) |
| 18 | + |
| 19 | + |
| 20 | +def insert_data(collection): |
| 21 | + colors = [ |
| 22 | + "green", |
| 23 | + "blue", |
| 24 | + "yellow", |
| 25 | + "red", |
| 26 | + "black", |
| 27 | + "white", |
| 28 | + "purple", |
| 29 | + "pink", |
| 30 | + "orange", |
| 31 | + "grey", |
| 32 | + ] |
| 33 | + data = [ |
| 34 | + { |
| 35 | + "id": i, |
| 36 | + "vector": [random.uniform(-1, 1) for _ in range(5)], |
| 37 | + "color": random.choice(colors), |
| 38 | + "tag": random.randint(1000, 9999), |
| 39 | + } |
| 40 | + for i in range(1000) |
| 41 | + ] |
| 42 | + data += [ |
| 43 | + { |
| 44 | + "id": 1000, |
| 45 | + "vector": [random.uniform(-1, 1) for _ in range(5)], |
| 46 | + "color": "brown", |
| 47 | + "tag": 1234, |
| 48 | + }, |
| 49 | + { |
| 50 | + "id": 1001, |
| 51 | + "vector": [random.uniform(-1, 1) for _ in range(5)], |
| 52 | + "color": "brown", |
| 53 | + "tag": 5678, |
| 54 | + }, |
| 55 | + { |
| 56 | + "id": 1002, |
| 57 | + "vector": [random.uniform(-1, 1) for _ in range(5)], |
| 58 | + "color": "brown", |
| 59 | + "tag": 9101, |
| 60 | + }, |
| 61 | + ] |
| 62 | + for i in data: |
| 63 | + i["color_tag"] = "{}_{}".format(i["color"], i["tag"]) |
| 64 | + milvus.insert(collection_name=collection, data=data) |
| 65 | + |
| 66 | + |
| 67 | +def test_milvus_single_vector_search(exporter, collection): |
| 68 | + insert_data(collection) |
| 69 | + |
| 70 | + query_vectors = [ |
| 71 | + [random.uniform(-1, 1) for _ in range(5)], # Random query vector for the search |
| 72 | + ] |
| 73 | + search_params = {"radius": 0.5, "metric_type": "COSINE", "index_type": "IVF_FLAT"} |
| 74 | + milvus.search( |
| 75 | + collection_name=collection, |
| 76 | + data=query_vectors, |
| 77 | + anns_field="vector", |
| 78 | + search_params=search_params, |
| 79 | + output_fields=["color_tag"], |
| 80 | + limit=3, |
| 81 | + timeout=10, |
| 82 | + ) |
| 83 | + |
| 84 | + # Get finished spans |
| 85 | + spans = exporter.get_finished_spans() |
| 86 | + span = next(span for span in spans if span.name == "milvus.search") |
| 87 | + |
| 88 | + # Check the span attributes related to search |
| 89 | + assert span.attributes.get(SpanAttributes.VECTOR_DB_VENDOR) == "milvus" |
| 90 | + assert span.attributes.get(SpanAttributes.VECTOR_DB_OPERATION) == "search" |
| 91 | + assert ( |
| 92 | + span.attributes.get(SpanAttributes.MILVUS_SEARCH_COLLECTION_NAME) == collection |
| 93 | + ) |
| 94 | + assert span.attributes.get(SpanAttributes.MILVUS_SEARCH_OUTPUT_FIELDS_COUNT) == 1 |
| 95 | + assert span.attributes.get(SpanAttributes.MILVUS_SEARCH_LIMIT) == 3 |
| 96 | + assert span.attributes.get(SpanAttributes.MILVUS_SEARCH_TIMEOUT) == 10 |
| 97 | + assert span.attributes.get(SpanAttributes.MILVUS_SEARCH_ANNS_FIELD) == "vector" |
| 98 | + assert ( |
| 99 | + span.attributes.get(SpanAttributes.MILVUS_SEARCH_QUERY_VECTOR_DIMENSION) |
| 100 | + == "[5]" |
| 101 | + ) |
| 102 | + distances = [] |
| 103 | + ids = [] |
| 104 | + |
| 105 | + events = span.events |
| 106 | + |
| 107 | + for event in events: |
| 108 | + assert event.name == Events.DB_SEARCH_RESULT.value |
| 109 | + _id = event.attributes.get(EventAttributes.DB_SEARCH_RESULT_ID.value) |
| 110 | + distance = event.attributes.get(EventAttributes.DB_SEARCH_RESULT_DISTANCE.value) |
| 111 | + |
| 112 | + assert isinstance(_id, int) |
| 113 | + assert isinstance(distance, str) |
| 114 | + |
| 115 | + # Collect the distances and IDs for further computation |
| 116 | + distances.append( |
| 117 | + float(distance) |
| 118 | + ) # Convert the distance to a float for computation |
| 119 | + ids.append(_id) |
| 120 | + |
| 121 | + # Now compute dynamic stats from the distances |
| 122 | + total_matches = len(events) |
| 123 | + |
| 124 | + assert ( |
| 125 | + span.attributes.get(SpanAttributes.MILVUS_SEARCH_RESULT_COUNT) == total_matches |
| 126 | + ) |
| 127 | + |
| 128 | + |
| 129 | +def test_milvus_multiple_vector_search(exporter, collection): |
| 130 | + insert_data(collection) |
| 131 | + |
| 132 | + query_vectors = [ |
| 133 | + [random.uniform(-1, 1) for _ in range(5)], # Random query vector for the search |
| 134 | + [random.uniform(-1, 1) for _ in range(5)], # Another query vector |
| 135 | + [ |
| 136 | + random.uniform(-1, 1) for _ in range(5) |
| 137 | + ], # Another query vector (you can add more as needed) |
| 138 | + ] |
| 139 | + search_params = {"radius": 0.5, "metric_type": "COSINE", "index_type": "IVF_FLAT"} |
| 140 | + milvus.search( |
| 141 | + collection_name=collection, |
| 142 | + data=query_vectors, |
| 143 | + anns_field="vector", |
| 144 | + search_params=search_params, |
| 145 | + output_fields=["color_tag"], |
| 146 | + limit=3, |
| 147 | + timeout=10, |
| 148 | + ) |
| 149 | + |
| 150 | + # Get finished spans |
| 151 | + spans = exporter.get_finished_spans() |
| 152 | + span = next(span for span in spans if span.name == "milvus.search") |
| 153 | + |
| 154 | + # Check the span attributes related to search |
| 155 | + assert span.attributes.get(SpanAttributes.VECTOR_DB_VENDOR) == "milvus" |
| 156 | + assert span.attributes.get(SpanAttributes.VECTOR_DB_OPERATION) == "search" |
| 157 | + assert ( |
| 158 | + span.attributes.get(SpanAttributes.MILVUS_SEARCH_COLLECTION_NAME) == collection |
| 159 | + ) |
| 160 | + assert span.attributes.get(SpanAttributes.MILVUS_SEARCH_OUTPUT_FIELDS_COUNT) == 1 |
| 161 | + assert span.attributes.get(SpanAttributes.MILVUS_SEARCH_LIMIT) == 3 |
| 162 | + assert span.attributes.get(SpanAttributes.MILVUS_SEARCH_TIMEOUT) == 10 |
| 163 | + assert span.attributes.get(SpanAttributes.MILVUS_SEARCH_ANNS_FIELD) == "vector" |
| 164 | + assert ( |
| 165 | + span.attributes.get(SpanAttributes.MILVUS_SEARCH_QUERY_VECTOR_DIMENSION) |
| 166 | + == "[5, 5, 5]" |
| 167 | + ) |
| 168 | + |
| 169 | + distances_dict = {} |
| 170 | + ids_dict = {} |
| 171 | + |
| 172 | + events = span.events |
| 173 | + for event in events: |
| 174 | + assert event.name == Events.DB_SEARCH_RESULT.value |
| 175 | + query_idx = event.attributes.get( |
| 176 | + EventAttributes.DB_SEARCH_RESULT_QUERY_ID.value |
| 177 | + ) |
| 178 | + _id = event.attributes.get(EventAttributes.DB_SEARCH_RESULT_ID.value) |
| 179 | + distance = event.attributes.get(EventAttributes.DB_SEARCH_RESULT_DISTANCE.value) |
| 180 | + |
| 181 | + assert isinstance(_id, int) |
| 182 | + assert isinstance(distance, str) |
| 183 | + |
| 184 | + distance = float(distance) |
| 185 | + |
| 186 | + if query_idx not in distances_dict: |
| 187 | + distances_dict[query_idx] = [] |
| 188 | + ids_dict[query_idx] = [] |
| 189 | + |
| 190 | + distances_dict[query_idx].append(distance) |
| 191 | + ids_dict[query_idx].append(_id) |
| 192 | + |
| 193 | + for query_idx in distances_dict: |
| 194 | + distances = distances_dict[query_idx] |
| 195 | + |
| 196 | + total_matches = len(distances) |
| 197 | + |
| 198 | + count_key = f"{SpanAttributes.MILVUS_SEARCH_RESULT_COUNT}_{query_idx}" |
| 199 | + |
| 200 | + assert span.attributes.get(count_key) == total_matches |
0 commit comments