forked from Archives/langchain
Dev2049/pinecone try except (#4424)
Co-authored-by: Harrison Chase <hw.chase.17@gmail.com> Co-authored-by: Bernie G <bernie.gandin2@gmail.com>
This commit is contained in:
parent
2b14036126
commit
5c8e12558d
@ -1,6 +1,7 @@
|
|||||||
"""Wrapper around Pinecone vector database."""
|
"""Wrapper around Pinecone vector database."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
import uuid
|
import uuid
|
||||||
from typing import Any, Callable, Iterable, List, Optional, Tuple
|
from typing import Any, Callable, Iterable, List, Optional, Tuple
|
||||||
|
|
||||||
@ -8,6 +9,8 @@ from langchain.docstore.document import Document
|
|||||||
from langchain.embeddings.base import Embeddings
|
from langchain.embeddings.base import Embeddings
|
||||||
from langchain.vectorstores.base import VectorStore
|
from langchain.vectorstores.base import VectorStore
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Pinecone(VectorStore):
|
class Pinecone(VectorStore):
|
||||||
"""Wrapper around Pinecone vector database.
|
"""Wrapper around Pinecone vector database.
|
||||||
@ -120,8 +123,14 @@ class Pinecone(VectorStore):
|
|||||||
)
|
)
|
||||||
for res in results["matches"]:
|
for res in results["matches"]:
|
||||||
metadata = res["metadata"]
|
metadata = res["metadata"]
|
||||||
text = metadata.pop(self._text_key)
|
if self._text_key in metadata:
|
||||||
docs.append((Document(page_content=text, metadata=metadata), res["score"]))
|
text = metadata.pop(self._text_key)
|
||||||
|
score = res["score"]
|
||||||
|
docs.append((Document(page_content=text, metadata=metadata), score))
|
||||||
|
else:
|
||||||
|
logger.warning(
|
||||||
|
f"Found document with no `{self._text_key}` key. Skipping."
|
||||||
|
)
|
||||||
return docs
|
return docs
|
||||||
|
|
||||||
def similarity_search(
|
def similarity_search(
|
||||||
@ -143,22 +152,10 @@ class Pinecone(VectorStore):
|
|||||||
Returns:
|
Returns:
|
||||||
List of Documents most similar to the query and score for each
|
List of Documents most similar to the query and score for each
|
||||||
"""
|
"""
|
||||||
if namespace is None:
|
docs_and_scores = self.similarity_search_with_score(
|
||||||
namespace = self._namespace
|
query, k=k, filter=filter, namespace=namespace, **kwargs
|
||||||
query_obj = self._embedding_function(query)
|
|
||||||
docs = []
|
|
||||||
results = self._index.query(
|
|
||||||
[query_obj],
|
|
||||||
top_k=k,
|
|
||||||
include_metadata=True,
|
|
||||||
namespace=namespace,
|
|
||||||
filter=filter,
|
|
||||||
)
|
)
|
||||||
for res in results["matches"]:
|
return [doc for doc, _ in docs_and_scores]
|
||||||
metadata = res["metadata"]
|
|
||||||
text = metadata.pop(self._text_key)
|
|
||||||
docs.append(Document(page_content=text, metadata=metadata))
|
|
||||||
return docs
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_texts(
|
def from_texts(
|
||||||
|
Loading…
Reference in New Issue
Block a user