mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-24 15:43:54 +00:00
fix: Resolve AttributeError
in Google Cloud Enterprise Search retriever (#8872)
- Reverting some of the changes made in https://github.com/langchain-ai/langchain/pull/8369
This commit is contained in:
parent
b2eb4ff0fc
commit
fad26e79a3
@ -116,22 +116,37 @@ class GoogleCloudEnterpriseSearchRetriever(BaseRetriever):
|
|||||||
self, results: Sequence[SearchResult]
|
self, results: Sequence[SearchResult]
|
||||||
) -> List[Document]:
|
) -> List[Document]:
|
||||||
"""Converts a sequence of search results to a list of LangChain documents."""
|
"""Converts a sequence of search results to a list of LangChain documents."""
|
||||||
|
from google.protobuf.json_format import MessageToDict
|
||||||
|
|
||||||
documents: List[Document] = []
|
documents: List[Document] = []
|
||||||
|
|
||||||
for result in results:
|
for result in results:
|
||||||
derived_struct_data = result.document.derived_struct_data
|
document_dict = MessageToDict(
|
||||||
doc_metadata = result.document.struct_data
|
result.document._pb, preserving_proto_field_name=True
|
||||||
doc_metadata.source = derived_struct_data.link or ""
|
)
|
||||||
doc_metadata.id = result.document.id
|
derived_struct_data = document_dict.get("derived_struct_data", None)
|
||||||
|
if not derived_struct_data:
|
||||||
|
continue
|
||||||
|
|
||||||
|
doc_metadata = document_dict.get("struct_data", {})
|
||||||
|
doc_metadata["id"] = document_dict["id"]
|
||||||
|
|
||||||
|
chunk_type = (
|
||||||
|
"extractive_answers"
|
||||||
|
if self.get_extractive_answers
|
||||||
|
else "extractive_segments"
|
||||||
|
)
|
||||||
|
|
||||||
|
for chunk in getattr(derived_struct_data, chunk_type, []):
|
||||||
|
doc_metadata["source"] = derived_struct_data.get("link", "")
|
||||||
|
|
||||||
|
if chunk_type == "extractive_answers":
|
||||||
|
doc_metadata["source"] += f":{chunk.get('pageNumber', '')}"
|
||||||
|
|
||||||
for chunk in (
|
|
||||||
derived_struct_data.extractive_answers
|
|
||||||
or derived_struct_data.extractive_segments
|
|
||||||
):
|
|
||||||
if hasattr(chunk, "page_number"):
|
|
||||||
doc_metadata.source += f":{chunk.page_number}"
|
|
||||||
documents.append(
|
documents.append(
|
||||||
Document(page_content=chunk.content, metadata=doc_metadata)
|
Document(
|
||||||
|
page_content=chunk.get("content", ""), metadata=doc_metadata
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
return documents
|
return documents
|
||||||
|
Loading…
Reference in New Issue
Block a user