Compare commits

...

4 Commits

Author SHA1 Message Date
Harrison Chase
69415734db cr 2023-01-10 07:45:06 -08:00
Harrison Chase
4018892f8a cr 2023-01-10 07:44:28 -08:00
Bernie G
d39ab27969
Fix KeyError in pinecone wrapper (#568)
Pinecone can return multiple matches with different text keys in the
metadata, and the current behavior will throw a KeyError if one pops up
that isn't the expected text key. This PR only selects the matches with
the correct text key.
2023-01-10 07:43:39 -08:00
Harrison Chase
7a32cde5ff logging 2023-01-10 07:43:13 -08:00

View File

@ -1,6 +1,7 @@
"""Wrapper around Pinecone vector database."""
from __future__ import annotations
import logging
import uuid
from typing import Any, Callable, Iterable, List, Optional
@ -8,6 +9,8 @@ from langchain.docstore.document import Document
from langchain.embeddings.base import Embeddings
from langchain.vectorstores.base import VectorStore
logger = logging.getLogger(__name__)
class Pinecone(VectorStore):
"""Wrapper around Pinecone vector database.
@ -78,8 +81,13 @@ class Pinecone(VectorStore):
results = self._index.query([query_obj], top_k=k, include_metadata=True)
for res in results["matches"]:
metadata = res["metadata"]
text = metadata.pop(self._text_key)
docs.append(Document(page_content=text, metadata=metadata))
if self._text_key in metadata:
text = metadata.pop(self._text_key)
docs.append(Document(page_content=text, metadata=metadata))
else:
logger.warning(
f"Found document with no `{self._text_key}` key. Skipping."
)
return docs
@classmethod