更新與刪除向量

使用 Pinecone 構建 AI 應用

James Chapman

Curriculum Manager, DataCamp

隨時保持新鮮…

  • 保持資料為最新
  • 最佳化查詢效能
  • 維護資料完整性

資料從擷取、使用、驗證,到更新或移除的生命週期。

使用 Pinecone 構建 AI 應用

更新向量值

index.fetch(ids=['1'])
{'namespace': '',
 'usage': {'read_units': 1},
 'vectors': {'1': {'id': '1',
                   'metadata': {"genre": "action", "year": 2023},
                   'values': [-0.0131468913, ...]}
            }
}
使用 Pinecone 構建 AI 應用

更新向量值

index.update(

id="1",
values=[0.370695321, ...]
)
  • 確保:清單長度 = 索引維度
1 https://docs.pinecone.io/docs/update-data
使用 Pinecone 構建 AI 應用

更新向量值

index.fetch(ids=['1'])
{'namespace': '',
 'usage': {'read_units': 1},
 'vectors': {'1': {'id': '1',
                   'metadata': {"genre": "action", "year": 2023},
                   'values': [0.370695321, ...]}
            }
}
使用 Pinecone 構建 AI 應用

更新向量中繼資料

index.update(
    id="1",
    set_metadata={"genre": "comedy", "rating": 5}
)
1 https://docs.pinecone.io/docs/update-data
使用 Pinecone 構建 AI 應用

更新向量中繼資料

index.fetch(ids=['1'])
{'namespace': '',
 'usage': {'read_units': 1},
 'vectors': {'1': {'id': '1',
                   'metadata': {"genre": "comedy", "year": 2023, "rating": 5},
                   'values': [0.370695321, ...]}
            }
}
使用 Pinecone 構建 AI 應用

同時更新數值與中繼資料

index.update(
    id="1",
    values=[-0.31956, ...],
    set_metadata={"genre": "thriller", "ratings": 4}
)
1 https://docs.pinecone.io/docs/update-data
使用 Pinecone 構建 AI 應用

刪除向量

index.delete(
    ids=["1", "2"]
)
1 https://docs.pinecone.io/docs/delete-data
使用 Pinecone 構建 AI 應用

依中繼資料刪除向量

index.delete(
    filter={
        "genre": {"$eq": "action"},
    }
)
使用 Pinecone 構建 AI 應用

刪除命名空間內的向量

index.delete(delete_all=True, namespace='namespace1')
  • 注意:也會一併移除該命名空間!
1 https://docs.pinecone.io/docs/delete-data
使用 Pinecone 構建 AI 應用

一起來練習吧!

使用 Pinecone 構建 AI 應用

Preparing Video For Download...