MCPcopy
hub / github.com/langchain-ai/langchain / DocstoreExplorer

Class DocstoreExplorer

libs/langchain/langchain/agents/react/base.py:73–119  ·  view source on GitHub ↗

Class to assist with exploration of a document store.

Source from the content-addressed store, hash-verified

71
72@deprecated("0.1.0", removal="0.3.0")
73class DocstoreExplorer:
74 """Class to assist with exploration of a document store."""
75
76 def __init__(self, docstore: Docstore):
77 """Initialize with a docstore, and set initial document to None."""
78 self.docstore = docstore
79 self.document: Optional[Document] = None
80 self.lookup_str = ""
81 self.lookup_index = 0
82
83 def search(self, term: str) -> str:
84 """Search for a term in the docstore, and if found save."""
85 result = self.docstore.search(term)
86 if isinstance(result, Document):
87 self.document = result
88 return self._summary
89 else:
90 self.document = None
91 return result
92
93 def lookup(self, term: str) -> str:
94 """Lookup a term in document (if saved)."""
95 if self.document is None:
96 raise ValueError("Cannot lookup without a successful search first")
97 if term.lower() != self.lookup_str:
98 self.lookup_str = term.lower()
99 self.lookup_index = 0
100 else:
101 self.lookup_index += 1
102 lookups = [p for p in self._paragraphs if self.lookup_str in p.lower()]
103 if len(lookups) == 0:
104 return "No Results"
105 elif self.lookup_index >= len(lookups):
106 return "No More Results"
107 else:
108 result_prefix = f"(Result {self.lookup_index + 1}/{len(lookups)})"
109 return f"{result_prefix} {lookups[self.lookup_index]}"
110
111 @property
112 def _summary(self) -> str:
113 return self._paragraphs[0]
114
115 @property
116 def _paragraphs(self) -> List[str]:
117 if self.document is None:
118 raise ValueError("Cannot get paragraphs without a document")
119 return self.document.page_content.split("\n\n")
120
121
122@deprecated("0.1.0", removal="0.3.0")

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected