Commit 6d71d8e05fb6db7766552e2793e19b900c8c8196
1 parent
24edc208
多模态模型配置
Showing
9 changed files
with
19 additions
and
16 deletions
Show diff stats
config/config.yaml
| @@ -364,16 +364,18 @@ services: | @@ -364,16 +364,18 @@ services: | ||
| 364 | device: "cuda" | 364 | device: "cuda" |
| 365 | batch_size: 32 | 365 | batch_size: 32 |
| 366 | normalize_embeddings: true | 366 | normalize_embeddings: true |
| 367 | - # 服务内图片后端(embedding 进程启动时读取) | 367 | + # 服务内图片后端(embedding 进程启动时读取;cnclip gRPC 与 6008 须同一 model_name) |
| 368 | + # Chinese-CLIP:ViT-H-14 → 1024 维,ViT-L-14 → 768 维。须与 mappings/search_products.json 中 | ||
| 369 | + # image_embedding.vector.dims 一致(当前索引为 1024 → 默认 ViT-H-14)。 | ||
| 368 | image_backend: "clip_as_service" # clip_as_service | local_cnclip | 370 | image_backend: "clip_as_service" # clip_as_service | local_cnclip |
| 369 | image_backends: | 371 | image_backends: |
| 370 | clip_as_service: | 372 | clip_as_service: |
| 371 | server: "grpc://127.0.0.1:51000" | 373 | server: "grpc://127.0.0.1:51000" |
| 372 | - model_name: "CN-CLIP/ViT-L-14" | 374 | + model_name: "CN-CLIP/ViT-H-14" |
| 373 | batch_size: 8 | 375 | batch_size: 8 |
| 374 | normalize_embeddings: true | 376 | normalize_embeddings: true |
| 375 | local_cnclip: | 377 | local_cnclip: |
| 376 | - model_name: "ViT-L-14" | 378 | + model_name: "ViT-H-14" |
| 377 | device: null | 379 | device: null |
| 378 | batch_size: 8 | 380 | batch_size: 8 |
| 379 | normalize_embeddings: true | 381 | normalize_embeddings: true |
config/loader.py
| @@ -532,12 +532,12 @@ class AppConfigLoader: | @@ -532,12 +532,12 @@ class AppConfigLoader: | ||
| 532 | image_backends = { | 532 | image_backends = { |
| 533 | "clip_as_service": { | 533 | "clip_as_service": { |
| 534 | "server": "grpc://127.0.0.1:51000", | 534 | "server": "grpc://127.0.0.1:51000", |
| 535 | - "model_name": "CN-CLIP/ViT-L-14", | 535 | + "model_name": "CN-CLIP/ViT-H-14", |
| 536 | "batch_size": 8, | 536 | "batch_size": 8, |
| 537 | "normalize_embeddings": True, | 537 | "normalize_embeddings": True, |
| 538 | }, | 538 | }, |
| 539 | "local_cnclip": { | 539 | "local_cnclip": { |
| 540 | - "model_name": "ViT-L-14", | 540 | + "model_name": "ViT-H-14", |
| 541 | "device": None, | 541 | "device": None, |
| 542 | "batch_size": 8, | 542 | "batch_size": 8, |
| 543 | "normalize_embeddings": True, | 543 | "normalize_embeddings": True, |
docs/CNCLIP_SERVICE说明文档.md
| @@ -76,8 +76,8 @@ python3 -c "from embeddings.config import CONFIG; print(CONFIG.CLIP_AS_SERVICE_M | @@ -76,8 +76,8 @@ python3 -c "from embeddings.config import CONFIG; print(CONFIG.CLIP_AS_SERVICE_M | ||
| 76 | 临时覆盖模型: | 76 | 临时覆盖模型: |
| 77 | 77 | ||
| 78 | ```bash | 78 | ```bash |
| 79 | -./scripts/start_cnclip_service.sh --model-name CN-CLIP/ViT-L-14 | ||
| 80 | -CNCLIP_MODEL_NAME=CN-CLIP/ViT-H-14 ./scripts/service_ctl.sh start cnclip | 79 | +./scripts/start_cnclip_service.sh --model-name CN-CLIP/ViT-L-14 # 768 维,需与索引 dims 一致 |
| 80 | +CNCLIP_MODEL_NAME=CN-CLIP/ViT-H-14 ./scripts/service_ctl.sh start cnclip # 默认 1024 维(与 mappings 中 image_embedding 一致) | ||
| 81 | ``` | 81 | ``` |
| 82 | 82 | ||
| 83 | ### 5.4 停止服务 | 83 | ### 5.4 停止服务 |
docs/搜索API对接指南-07-微服务接口(Embedding-Reranker-Translation).md
| @@ -102,7 +102,7 @@ curl -X POST "http://localhost:6008/embed/image?normalize=true&priority=1" \ | @@ -102,7 +102,7 @@ curl -X POST "http://localhost:6008/embed/image?normalize=true&priority=1" \ | ||
| 102 | 102 | ||
| 103 | #### 7.1.3 `POST /embed/clip_text` — CN-CLIP 文本多模态向量(与图片同空间) | 103 | #### 7.1.3 `POST /embed/clip_text` — CN-CLIP 文本多模态向量(与图片同空间) |
| 104 | 104 | ||
| 105 | -将**自然语言短语**编码为向量,与 `POST /embed/image` 输出的图向量**处于同一向量空间**(Chinese-CLIP 文本塔 / 图塔),用于 **以文搜图**、与 ES `image_embedding` 对齐的 KNN 等。 | 105 | +将**自然语言短语**编码为向量,与 `POST /embed/image` 输出的图向量**处于同一向量空间**(Chinese-CLIP 文本塔 / 图塔),用于 **以文搜图**、与 ES `image_embedding` 对齐的 KNN 等。默认配置为 **ViT-H-14**,向量长度 **1024**(与 `mappings/search_products.json` 中 `image_embedding.vector.dims` 一致);若改为 ViT-L-14 则为 768 维,须同步索引映射与全量重索引。 |
| 106 | 106 | ||
| 107 | 与 **7.1.1** 的 `POST /embed/text`(TEI/BGE,语义检索)**不是同一模型、不是同一空间**,请勿混用。 | 107 | 与 **7.1.1** 的 `POST /embed/text`(TEI/BGE,语义检索)**不是同一模型、不是同一空间**,请勿混用。 |
| 108 | 108 |
embeddings/README.md
| @@ -92,7 +92,7 @@ | @@ -92,7 +92,7 @@ | ||
| 92 | 3. **配置**(`embeddings/config.py` 或环境变量): | 92 | 3. **配置**(`embeddings/config.py` 或环境变量): |
| 93 | - `USE_CLIP_AS_SERVICE=true`(默认) | 93 | - `USE_CLIP_AS_SERVICE=true`(默认) |
| 94 | - `CLIP_AS_SERVICE_SERVER=grpc://127.0.0.1:51000` | 94 | - `CLIP_AS_SERVICE_SERVER=grpc://127.0.0.1:51000` |
| 95 | - - `CLIP_AS_SERVICE_MODEL_NAME=CN-CLIP/ViT-L-14` | 95 | + - `CLIP_AS_SERVICE_MODEL_NAME=CN-CLIP/ViT-H-14`(与 `config/config.yaml` 中 `services.embedding.image_backends` 一致;换 ViT-L 时为 768 维,须同步改 ES 映射) |
| 96 | - `scripts/start_cnclip_service.sh` 默认会读取同一个 `CLIP_AS_SERVICE_MODEL_NAME`,也可用 `CNCLIP_MODEL_NAME` 或 `--model-name` 临时覆盖 | 96 | - `scripts/start_cnclip_service.sh` 默认会读取同一个 `CLIP_AS_SERVICE_MODEL_NAME`,也可用 `CNCLIP_MODEL_NAME` 或 `--model-name` 临时覆盖 |
| 97 | 97 | ||
| 98 | ### 性能与压测(沿用仓库脚本) | 98 | ### 性能与压测(沿用仓库脚本) |
embeddings/clip_as_service_encoder.py
| @@ -38,7 +38,8 @@ def _normalize_image_url(url: str) -> str: | @@ -38,7 +38,8 @@ def _normalize_image_url(url: str) -> str: | ||
| 38 | class ClipAsServiceImageEncoder: | 38 | class ClipAsServiceImageEncoder: |
| 39 | """ | 39 | """ |
| 40 | Image embedding encoder using clip-as-service Client. | 40 | Image embedding encoder using clip-as-service Client. |
| 41 | - Encodes image URLs in batch; returns 1024-dim vectors (server model must match). | 41 | + Vector length follows the loaded Chinese-CLIP model (e.g. 1024 for ViT-H-14, 768 for ViT-L-14); |
| 42 | + must match ``services.embedding.image_backends.*.model_name`` and ES ``image_embedding.vector.dims``. | ||
| 42 | """ | 43 | """ |
| 43 | 44 | ||
| 44 | def __init__( | 45 | def __init__( |
| @@ -122,7 +123,7 @@ class ClipAsServiceImageEncoder: | @@ -122,7 +123,7 @@ class ClipAsServiceImageEncoder: | ||
| 122 | return out | 123 | return out |
| 123 | 124 | ||
| 124 | def encode_image_from_url(self, url: str, normalize_embeddings: bool = True) -> np.ndarray: | 125 | def encode_image_from_url(self, url: str, normalize_embeddings: bool = True) -> np.ndarray: |
| 125 | - """Encode a single image URL and return one 1024-dim vector.""" | 126 | + """Encode a single image URL and return one float32 vector (length = model embedding dim).""" |
| 126 | results = self.encode_image_urls([url], batch_size=1, normalize_embeddings=normalize_embeddings) | 127 | results = self.encode_image_urls([url], batch_size=1, normalize_embeddings=normalize_embeddings) |
| 127 | if not results: | 128 | if not results: |
| 128 | raise RuntimeError("clip-as-service returned empty result for single image URL") | 129 | raise RuntimeError("clip-as-service returned empty result for single image URL") |
embeddings/clip_model.py
| @@ -16,7 +16,7 @@ from cn_clip.clip import load_from_name | @@ -16,7 +16,7 @@ from cn_clip.clip import load_from_name | ||
| 16 | import cn_clip.clip as clip | 16 | import cn_clip.clip as clip |
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | -DEFAULT_MODEL_NAME = "ViT-L-14" # "ViT-H-14", "ViT-L-14-336" | 19 | +DEFAULT_MODEL_NAME = "ViT-H-14" # ViT-H-14: 1024-dim; ViT-L-14: 768-dim — 须与 config 与 ES image_embedding.dims 一致 |
| 20 | MODEL_DOWNLOAD_DIR = "/data/" | 20 | MODEL_DOWNLOAD_DIR = "/data/" |
| 21 | 21 | ||
| 22 | 22 |
embeddings/config.py
| @@ -34,9 +34,9 @@ class EmbeddingConfig(object): | @@ -34,9 +34,9 @@ class EmbeddingConfig(object): | ||
| 34 | 34 | ||
| 35 | self.USE_CLIP_AS_SERVICE = services.image_backend == "clip_as_service" | 35 | self.USE_CLIP_AS_SERVICE = services.image_backend == "clip_as_service" |
| 36 | self.CLIP_AS_SERVICE_SERVER = str(image_backend.get("server") or "grpc://127.0.0.1:51000") | 36 | self.CLIP_AS_SERVICE_SERVER = str(image_backend.get("server") or "grpc://127.0.0.1:51000") |
| 37 | - self.CLIP_AS_SERVICE_MODEL_NAME = str(image_backend.get("model_name") or "CN-CLIP/ViT-L-14") | 37 | + self.CLIP_AS_SERVICE_MODEL_NAME = str(image_backend.get("model_name") or "CN-CLIP/ViT-H-14") |
| 38 | 38 | ||
| 39 | - self.IMAGE_MODEL_NAME = str(image_backend.get("model_name") or "ViT-L-14") | 39 | + self.IMAGE_MODEL_NAME = str(image_backend.get("model_name") or "ViT-H-14") |
| 40 | self.IMAGE_DEVICE = image_backend.get("device") # type: Optional[str] | 40 | self.IMAGE_DEVICE = image_backend.get("device") # type: Optional[str] |
| 41 | self.IMAGE_BATCH_SIZE = int(image_backend.get("batch_size", 8)) | 41 | self.IMAGE_BATCH_SIZE = int(image_backend.get("batch_size", 8)) |
| 42 | self.IMAGE_NORMALIZE_EMBEDDINGS = bool(image_backend.get("normalize_embeddings", True)) | 42 | self.IMAGE_NORMALIZE_EMBEDDINGS = bool(image_backend.get("normalize_embeddings", True)) |
scripts/start_cnclip_service.sh
| @@ -47,7 +47,7 @@ resolve_default_model_name() { | @@ -47,7 +47,7 @@ resolve_default_model_name() { | ||
| 47 | fi | 47 | fi |
| 48 | fi | 48 | fi |
| 49 | done | 49 | done |
| 50 | - echo "CN-CLIP/ViT-L-14" | 50 | + echo "CN-CLIP/ViT-H-14" |
| 51 | } | 51 | } |
| 52 | 52 | ||
| 53 | # 默认配置 | 53 | # 默认配置 |
| @@ -80,7 +80,7 @@ show_help() { | @@ -80,7 +80,7 @@ show_help() { | ||
| 80 | echo " $0 # 使用默认配置启动" | 80 | echo " $0 # 使用默认配置启动" |
| 81 | echo " $0 --port 52000 --device cuda # 指定 CUDA 模式,端口 52000" | 81 | echo " $0 --port 52000 --device cuda # 指定 CUDA 模式,端口 52000" |
| 82 | echo " $0 --port 52000 --device cpu # 显式使用 CPU 模式" | 82 | echo " $0 --port 52000 --device cpu # 显式使用 CPU 模式" |
| 83 | - echo " $0 --model-name CN-CLIP/ViT-L-14 # 临时覆盖模型" | 83 | + echo " $0 --model-name CN-CLIP/ViT-H-14 # 临时覆盖模型" |
| 84 | echo " $0 --replicas 2 # 启动2个副本(需8-10GB显存)" | 84 | echo " $0 --replicas 2 # 启动2个副本(需8-10GB显存)" |
| 85 | echo "" | 85 | echo "" |
| 86 | echo "说明:" | 86 | echo "说明:" |