AzureSearch Vector Store: Moving the usage of additional_fields into context of it's definition (bug fix from python error) (#8551)

Description: Using Azure Cognitive Search as a VectorStore. Calling the
`add_texts` method throws an error if there is no metadata property
specified. The `additional_fields` field is set in an `if` statement and
then is used later outside the if statement. This PR just moves the
declaration of `additional_fields` below and puts the usage of it in
context.

Issue: https://github.com/langchain-ai/langchain/issues/8544

Tagging @rlancemartin, @eyurtsev as this is related to Vector stores.

`make format`, `make lint`, `make spellcheck`, and `make test` have been
run
This commit is contained in:
Kevin Buckley 2023-07-31 20:25:57 -04:00 committed by GitHub
parent 8d2344db43
commit 8061994c61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -269,12 +269,6 @@ class AzureSearch(VectorStore):
metadata = metadatas[i] if metadatas else {}
# Add data to index
# Additional metadata to fields mapping
if metadata:
additional_fields = {
k: v
for k, v in metadata.items()
if k in [x.name for x in self.fields]
}
doc = {
"@search.action": "upload",
FIELDS_ID: key,
@ -284,6 +278,12 @@ class AzureSearch(VectorStore):
).tolist(),
FIELDS_METADATA: json.dumps(metadata),
}
if metadata:
additional_fields = {
k: v
for k, v in metadata.items()
if k in [x.name for x in self.fields]
}
doc.update(additional_fields)
data.append(doc)
ids.append(key)