我是云新手,正在尝试为 Azure AI 搜索创建矢量嵌入。这是我的代码片段:
# Define the index fields
client = SearchIndexClient(endpoint, credential)
fields = [
SimpleField(name="id", type=SearchFieldDataType.String, key=True, sortable=True,
filterable=True, facetable=True),
SimpleField(name="originalbalancedue", type=SearchFieldDataType.Double,
sortable=True, filterable=True, facetable=True),
SimpleField(name="adjustedbalancedue", type=SearchFieldDataType.Double,
sortable=True, filterable=True, facetable=True),
SimpleField(name="feeamount", type=SearchFieldDataType.Double, sortable=True,
filterable=True, facetable=True),
SearchableField(name="result", type=SearchFieldDataType.Double, sortable=True,
filterable=True)
]
index = SearchIndex(name=index_name, fields=fields, vector_search=vector_search)
result = client.create_or_update_index(index)
print(f'{result.name} created')
搜索索引创建成功。
现在尝试将文本和嵌入插入到向量存储中:
with open('worthiness_with_result_small.json', 'r') as f:
content = f.read().strip()
if content:
documents = json.loads(content)
print(f"loaded {len(documents)} documents")
else:
print("The file is empty.")
search_client = SearchClient(endpoint=endpoint, index_name=index_name,
credential=credential)
result = search_client.upload_documents(documents)
print(f"Uploaded {len(documents)} documents")
我的 data 在 worthiness_with_result_small.json 文件中看起来像:
[{“id”:“425001”,“原始余额”:1684269.59,“调整后余额”:1683369.59,“费用金额”:6659.1199999999998900,“结果”:5759.1199999999998900 }]
我收到错误:
HttpResponseError: () The request is invalid. Details: Cannot convert the literal '5759.12' to the expected type 'Edm.String'.
Code:
Message: The request is invalid. Details: Cannot convert the literal '5759.12' to the expected type 'Edm.String'.
知道我在这里做错了什么吗?
我认为问题在于您正在使用
SearchableField
作为 double
数据类型。请注意,只能搜索 String
和 String (Collection)
数据类型。
请尝试更改:
SearchableField(name="result", type=SearchFieldDataType.Double, sortable=True,
filterable=True)
到
SimpleField(name="result", type=SearchFieldDataType.Double, sortable=True,
filterable=True)