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
|
import json import time
from pymilvus import ( connections, utility, FieldSchema, CollectionSchema, DataType, Collection, AnnSearchRequest, WeightedRanker ) from pymilvus.exceptions import MilvusException from pymilvus.model.hybrid import BGEM3EmbeddingFunction
DATA_PATH = "../../../data/灭神纪/战斗场景.json" COLLECTION_NAME = "wukong_hybrid_v4" MILVUS_URI = "http://82.157.193.65:19530" BATCH_SIZE = 50 DEVICE = "cpu"
print("脚本开始执行...")
print(f"1. 正在从 {DATA_PATH} 加载数据...") try: with open(DATA_PATH, 'r', encoding='utf-8') as f: dataset = json.load(f) except FileNotFoundError: print(f"错误: 数据文件 {DATA_PATH} 未找到。请检查路径。") exit() except json.JSONDecodeError: print(f"错误: 数据文件 {DATA_PATH} JSON 格式错误。") exit()
docs = [] metadata = [] for item in dataset.get('data', []): text_parts = [item.get('title', ''), item.get('description', '')] if 'combat_details' in item and isinstance(item['combat_details'], dict): text_parts.extend(item['combat_details'].get('combat_style', [])) text_parts.extend(item['combat_details'].get('abilities_used', [])) if 'scene_info' in item and isinstance(item['scene_info'], dict): text_parts.extend([ item['scene_info'].get('location', ''), item['scene_info'].get('environment', ''), item['scene_info'].get('time_of_day', '') ]) docs.append(' '.join(filter(None, [str(part).strip() for part in text_parts if part]))) metadata.append(item)
if not docs: print("错误: 未能从数据文件中加载任何文档。请检查文件内容和结构。") exit() print(f"数据加载完成,共 {len(docs)} 条文档。")
print("2. 正在生成向量...") try: ef = BGEM3EmbeddingFunction(use_fp16=False, device=DEVICE) docs_to_embed = docs print(f"将为 {len(docs_to_embed)} 条文档生成向量...") docs_embeddings = ef(docs_to_embed) print("向量生成完成。") print(f" 密集向量维度: {ef.dim['dense']}") if "sparse" in docs_embeddings and docs_embeddings["sparse"].shape[0] > 0: print(f" 稀疏向量类型 (整体): {type(docs_embeddings['sparse'])}") first_sparse_vector_row_obj = docs_embeddings['sparse'][0] print(f" 第一个稀疏向量 (行对象类型): {type(first_sparse_vector_row_obj)}") print(f" 第一个稀疏向量 (行对象形状): {first_sparse_vector_row_obj.shape}") if hasattr(first_sparse_vector_row_obj, 'col') and hasattr(first_sparse_vector_row_obj, 'data'): print(f" 第一个稀疏向量 (部分列索引/col): {first_sparse_vector_row_obj.col[:5]}") print(f" 第一个稀疏向量 (部分数据/data): {first_sparse_vector_row_obj.data[:5]}") elif hasattr(first_sparse_vector_row_obj, 'indices') and hasattr(first_sparse_vector_row_obj, 'data'): print(f" 第一个稀疏向量 (部分索引/indices): {first_sparse_vector_row_obj.indices[:5]}") print(f" 第一个稀疏向量 (部分数据/data): {first_sparse_vector_row_obj.data[:5]}") else: print(" 无法直接获取第一个稀疏向量的列索引和数据属性。") else: print("警告: 未生成稀疏向量或稀疏向量为空。")
except Exception as e: print(f"生成向量时发生错误: {e}") exit()
print(f"3. 正在连接 Milvus (URI: {MILVUS_URI})...") try: connections.connect(uri=MILVUS_URI) print("成功连接到 Milvus。") except MilvusException as e: print(f"连接 Milvus 失败: {e}") exit()
print(f"4. 正在准备集合 '{COLLECTION_NAME}'...") fields = [ FieldSchema(name="pk", dtype=DataType.VARCHAR, is_primary=True, auto_id=True, max_length=100), FieldSchema(name="text", dtype=DataType.VARCHAR, max_length=65535), FieldSchema(name="id", dtype=DataType.VARCHAR, max_length=100), FieldSchema(name="title", dtype=DataType.VARCHAR, max_length=512), FieldSchema(name="category", dtype=DataType.VARCHAR, max_length=128), FieldSchema(name="location", dtype=DataType.VARCHAR, max_length=256), FieldSchema(name="environment", dtype=DataType.VARCHAR, max_length=128), FieldSchema(name="sparse_vector", dtype=DataType.SPARSE_FLOAT_VECTOR), FieldSchema(name="dense_vector", dtype=DataType.FLOAT_VECTOR, dim=ef.dim["dense"]) ] schema = CollectionSchema(fields, description="Wukong Hybrid Search Collection v4")
try: if utility.has_collection(COLLECTION_NAME): print(f"集合 '{COLLECTION_NAME}' 已存在,正在删除...") utility.drop_collection(COLLECTION_NAME) print(f"集合 '{COLLECTION_NAME}' 删除成功。") time.sleep(1)
print(f"正在创建集合 '{COLLECTION_NAME}'...") collection = Collection(name=COLLECTION_NAME, schema=schema, consistency_level="Strong") print(f"集合 '{COLLECTION_NAME}' 创建成功。")
print("正在为 sparse_vector 创建索引 (SPARSE_INVERTED_INDEX, IP)...") collection.create_index("sparse_vector", {"index_type": "SPARSE_INVERTED_INDEX", "metric_type": "IP"}) print("sparse_vector 索引创建成功。") time.sleep(0.5)
print("正在为 dense_vector 创建索引 (AUTOINDEX, IP)...") collection.create_index("dense_vector", {"index_type": "AUTOINDEX", "metric_type": "IP"}) print("dense_vector 索引创建成功。") time.sleep(0.5)
print(f"正在加载集合 '{COLLECTION_NAME}'...") collection.load() print(f"集合 '{COLLECTION_NAME}' 加载成功。")
except MilvusException as e: print(f"创建或加载集合/索引时发生 Milvus 错误: {e}") exit() except Exception as e: print(f"创建或加载集合/索引时发生未知错误: {e}") exit()
print("5. 正在准备插入数据...") num_docs_to_insert = len(docs_to_embed) try: for i in range(0, num_docs_to_insert, BATCH_SIZE): end_idx = min(i + BATCH_SIZE, num_docs_to_insert) batch_data = [] print(f" 正在准备批次 {i // BATCH_SIZE + 1} (索引 {i} 到 {end_idx - 1})...")
for j in range(i, end_idx): item_metadata = metadata[j]
sparse_row_obj = docs_embeddings["sparse"][j] if hasattr(sparse_row_obj, 'col') and hasattr(sparse_row_obj, 'data'): milvus_sparse_vector = {int(idx_col): float(val) for idx_col, val in zip(sparse_row_obj.col, sparse_row_obj.data)} elif hasattr(sparse_row_obj, 'indices') and hasattr(sparse_row_obj, 'data'): milvus_sparse_vector = {int(idx_col): float(val) for idx_col, val in zip(sparse_row_obj.indices, sparse_row_obj.data)} else: print(f"警告: 无法识别的稀疏行对象类型 {type(sparse_row_obj)} 在索引 {j}。跳过此条。") continue
doc_text = docs_to_embed[j] if len(doc_text) > 65530: doc_text = doc_text[:65530]
title_text = item_metadata.get("title", "N/A") if len(title_text) > 500: title_text = title_text[:500]
batch_data.append({ "text": doc_text, "id": str(item_metadata.get("id", f"unknown_id_{j}")), "title": title_text, "category": item_metadata.get("category", "N/A"), "location": item_metadata.get("scene_info", {}).get("location", "N/A"), "environment": item_metadata.get("scene_info", {}).get("environment", "N/A"), "sparse_vector": milvus_sparse_vector, "dense_vector": docs_embeddings["dense"][j].tolist() })
if not batch_data: print(f" 批次 {i // BATCH_SIZE + 1} 为空,跳过插入。") continue
print(f" 正在插入批次 {i // BATCH_SIZE + 1} ({len(batch_data)} 条记录)...") insert_result = collection.insert(batch_data) print(f" 批次 {i // BATCH_SIZE + 1} 插入成功, 主键: {insert_result.primary_keys[:5]}...") collection.flush() print(f" 批次 {i // BATCH_SIZE + 1} flush 完成。") time.sleep(0.5)
print(f"所有数据插入完成。总共 {collection.num_entities} 条实体。")
except MilvusException as e: print(f"插入数据时发生 Milvus 错误: {e}") if 'batch_data' in locals() and batch_data: print("问题批次的第一条数据(部分):") print(f" Text: {batch_data[0]['text'][:100]}...") print(f" ID: {batch_data[0]['id']}") print(f" Title: {batch_data[0]['title']}") exit() except Exception as e: print(f"插入数据时发生未知错误: {e}") if 'batch_data' in locals() and batch_data: print("问题批次的第一条数据(部分):") print(f" Text: {batch_data[0]['text'][:100]}...") exit()
def hybrid_search(query, category=None, environment=None, limit=5, weights=None): if weights is None: weights = {"sparse": 0.5, "dense": 0.5}
print(f"\n6. 执行混合搜索: '{query}'") print(f" Category: {category}, Environment: {environment}, Limit: {limit}, Weights: {weights}")
try: query_embeddings = ef([query])
conditions = [] if category: conditions.append(f'category == "{category}"') if environment: conditions.append(f'environment == "{environment}"') expr = " && ".join(conditions) if conditions else None print(f" 过滤表达式: {expr}")
search_params_dense = {"metric_type": "IP", "params": {}} search_params_sparse = {"metric_type": "IP", "params": {}}
if expr: search_params_dense["expr"] = expr search_params_sparse["expr"] = expr
dense_req = AnnSearchRequest( data=[query_embeddings["dense"][0].tolist()], anns_field="dense_vector", param=search_params_dense, limit=limit )
query_sparse_row_obj = query_embeddings["sparse"][0] if hasattr(query_sparse_row_obj, 'col') and hasattr(query_sparse_row_obj, 'data'): query_milvus_sparse_vector = {int(idx): float(val) for idx, val in zip(query_sparse_row_obj.col, query_sparse_row_obj.data)} elif hasattr(query_sparse_row_obj, 'indices') and hasattr(query_sparse_row_obj, 'data'): query_milvus_sparse_vector = {int(idx): float(val) for idx, val in zip(query_sparse_row_obj.indices, query_sparse_row_obj.data)} else: print(f"错误: 无法识别的查询稀疏向量类型 {type(query_sparse_row_obj)}。") return []
sparse_req = AnnSearchRequest( data=[query_milvus_sparse_vector], anns_field="sparse_vector", param=search_params_sparse, limit=limit )
rerank = WeightedRanker(weights["sparse"], weights["dense"])
print(" 发送混合搜索请求到 Milvus...") results = collection.hybrid_search( reqs=[sparse_req, dense_req], rerank=rerank, limit=limit, output_fields=["text", "id", "title", "category", "location", "environment", "pk"] )
print(" 搜索完成。结果:") if not results or not results[0]: print(" 未找到结果。") return []
processed_results = [] for hit in results[0]: processed_results.append({ "id": hit.entity.get("id"), "pk": hit.id, "title": hit.entity.get("title"), "text_preview": hit.entity.get("text", "")[:200] + "...", "category": hit.entity.get("category"), "location": hit.entity.get("location"), "environment": hit.entity.get("environment"), "distance": hit.distance }) return processed_results
except MilvusException as e: print(f"混合搜索时发生 Milvus 错误: {e}") return [] except Exception as e: print(f"混合搜索时发生未知错误: {e}") return []
if collection.num_entities > 0: print("\n开始示例搜索...") search_results = hybrid_search("孙悟空的战斗技巧", category="神魔大战", limit=3) if search_results: for res in search_results: print(f" - PK: {res['pk']}, Title: {res['title']}, Distance: {res['distance']:.4f}") print(f" Category: {res['category']}, Location: {res['location']}") print(f" Preview: {res['text_preview']}\n")
search_results_filtered = hybrid_search("火焰山的战斗", environment="火山", limit=2) if search_results_filtered: for res in search_results_filtered: print(f" - PK: {res['pk']}, Title: {res['title']}, Distance: {res['distance']:.4f}") print(f" Category: {res['category']}, Location: {res['location']}, Environment: {res['environment']}") print(f" Preview: {res['text_preview']}\n") else: print("\n集合中没有实体,跳过示例搜索。")
print("\n脚本执行完毕。")
|