mirror of
https://github.com/Mintplex-Labs/langchain-python.git
synced 2026-07-19 21:33:31 -04:00
315b0c09c6
this will break atm but wanted to get thoughts on implementation. 1. should add() be on docstore interface? 2. should InMemoryDocstore change to take a list of documents as init? (makes this slightly easier to implement in FAISS -- if we think it is less clean then could expose a method to get the number of documents currently in the dict, and perform the logic of creating the necessary dictionary in the FAISS.add_texts method. Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
28 lines
972 B
Python
28 lines
972 B
Python
"""Simple in memory docstore in the form of a dict."""
|
|
from typing import Dict, Union
|
|
|
|
from langchain.docstore.base import AddableMixin, Docstore
|
|
from langchain.docstore.document import Document
|
|
|
|
|
|
class InMemoryDocstore(Docstore, AddableMixin):
|
|
"""Simple in memory docstore in the form of a dict."""
|
|
|
|
def __init__(self, _dict: Dict[str, Document]):
|
|
"""Initialize with dict."""
|
|
self._dict = _dict
|
|
|
|
def add(self, texts: Dict[str, Document]) -> None:
|
|
"""Add texts to in memory dictionary."""
|
|
overlapping = set(texts).intersection(self._dict)
|
|
if overlapping:
|
|
raise ValueError(f"Tried to add ids that already exist: {overlapping}")
|
|
self._dict = dict(self._dict, **texts)
|
|
|
|
def search(self, search: str) -> Union[str, Document]:
|
|
"""Search via direct lookup."""
|
|
if search not in self._dict:
|
|
return f"ID {search} not found."
|
|
else:
|
|
return self._dict[search]
|