# ValueError：嵌入维度不匹配：索引维度为 1536，但新嵌入维度为 768。请重建索引或设置 allow_dangerous_deserialization=True。

- **ID:** `llm/llamaindex-embedding-dim-mismatch-update`
- **领域:** llm
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

LlamaIndex 向量存储索引是使用一个嵌入模型（例如 text-embedding-ada-002，维度 1536）构建的，但正在使用不同模型（例如 text-embedding-3-small，维度 768）的嵌入进行更新。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| llama-index 0.10.0 | active | — | — |
| text-embedding-ada-002 | active | — | — |
| text-embedding-3-small | active | — | — |
| OpenAI API 2024-02-15 | active | — | — |

## 解决方案

1. ```
   使用新的嵌入模型从头重建索引。代码示例：`from llama_index.core import VectorStoreIndex, SimpleDirectoryReader; documents = SimpleDirectoryReader('data').load_data(); index = VectorStoreIndex.from_documents(documents, embed_model='text-embedding-3-small'); index.storage_context.persist('new_index')`。
   ```
2. ```
   在向量数据库（例如 Chroma）中创建具有正确维度的新集合，并重新插入所有文档。
   ```

## 无效尝试

- **** — This flag allows loading a potentially malicious pickle file; it does not fix the dimension mismatch. The index will still reject new embeddings. (100% 失败率)
- **** — Padding/truncating corrupts the embedding space, leading to meaningless similarity scores and broken retrieval. (95% 失败率)
- **** — OpenAI deprecated text-embedding-ada-002; the old model may still be accessible but returns different embeddings over time due to model updates. (60% 失败率)
