mirror of
https://github.com/Mintplex-Labs/langchain-python.git
synced 2026-07-20 05:43:59 -04:00
160bfae93f
This **partially** addresses https://github.com/hwchase17/langchain/issues/1524, but it's also useful for some of our use cases. This `DocstoreFn` allows to lookup a document given a function that accepts the `search` string without the need to implement a custom `Docstore`. This could be useful when: * you don't want to implement a `Docstore` just to provide a custom `search` * it's expensive to construct an `InMemoryDocstore`/dict * you retrieve documents from remote sources * you just want to reuse existing objects
31 lines
912 B
Python
31 lines
912 B
Python
from typing import Callable, Union
|
|
|
|
from langchain.docstore.base import Docstore
|
|
from langchain.schema import Document
|
|
|
|
|
|
class DocstoreFn(Docstore):
|
|
"""
|
|
Langchain Docstore via arbitrary lookup function.
|
|
|
|
This is useful when:
|
|
* it's expensive to construct an InMemoryDocstore/dict
|
|
* you retrieve documents from remote sources
|
|
* you just want to reuse existing objects
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
lookup_fn: Callable[[str], Union[Document, str]],
|
|
):
|
|
self._lookup_fn = lookup_fn
|
|
|
|
def search(self, search: str) -> Document:
|
|
r = self._lookup_fn(search)
|
|
if isinstance(r, str):
|
|
# NOTE: assume the search string is the source ID
|
|
return Document(page_content=r, metadata={"source": search})
|
|
elif isinstance(r, Document):
|
|
return r
|
|
raise ValueError(f"Unexpected type of document {type(r)}")
|