# ValueError：查询向量维度（384）与索引维度（768）不匹配

- **ID:** `llm/embedding-dimension-mismatch-in-vector-search`
- **领域:** llm
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

用于查询编码的嵌入模型与用于构建向量索引的模型输出维度不同，通常是由于模型切换（例如，从text-embedding-ada-002切换到较小的模型）。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| openai==1.10.0 | active | — | — |
| pinecone-client==3.0.0 | active | — | — |
| chromadb==0.4.22 | active | — | — |
| text-embedding-ada-002 | active | — | — |
| all-MiniLM-L6-v2 | active | — | — |
| sentence-transformers==2.2.2 | active | — | — |

## 解决方案

1. ```
   使用与查询相同的嵌入模型重新嵌入所有文档。例如，如果使用text-embedding-ada-002（1536维度），确保索引和查询都使用该模型。
   ```
2. ```
   使用维度无关的向量数据库（例如，带有自动检测功能的ChromaDB），通过存储元数据并在查询时过滤来处理不同维度。
   ```

## 无效尝试

- **** — Resizing the query vector by padding with zeros or truncating corrupts the embedding and leads to poor search results. (90% 失败率)
- **** — Re-indexing all documents with the new model is correct but often skipped due to time constraints; partial re-indexing causes inconsistency. (70% 失败率)
- **** — Assuming the vector database automatically handles dimension conversion leads to silent failures or errors. (95% 失败率)
