Replies: 3 comments 5 replies
-
🤖 Hello, Thank you for your question. Unfortunately, there is no direct way to use the The The If you want to achieve this functionality, you would need to create a custom class that inherits from You can refer to the source code of the I hope this helps. If you have any other questions, feel free to ask. Best regards. SourcesThis response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
Beta Was this translation helpful? Give feedback.
-
For the React Docstore Agent in langchain is there any way to pass you own data(pdf,docx,etc) instead of using Wikipedia as docstore? |
Beta Was this translation helpful? Give feedback.
-
The easiest way is to just create your own docstore which has access to a database instead of a local dict. Here is an example implementation of a docstore that connects to a Postgres database to retrieve parent documents from a table called "your_table": from typing import (
AsyncIterator,
Iterator,
List,
Optional,
Sequence,
Tuple,
TypeVar,
)
from langchain_core.documents import Document
from langchain.schema import BaseStore
from psycopg.rows import dict_row # Optional: to return rows as dictionaries
# Example implementation of a PostgresStore
from postgres import PostgresStore
V = TypeVar("V")
class CustomStore(BaseStore[str, str]):
def __init__(self, postgres_store: PostgresStore):
self.postgres_store = postgres_store
def mget(self, keys: Sequence[str]) -> List[Optional[str]]:
"""" This function returns parent documents from a database."""
try:
# Create a cursor object
with self.postgres_store.cursor(row_factory=dict_row) as cursor: # Optional: Use dict_row to get rows as dictionaries
# Create the SQL query using the IN clause
# Generate the placeholders for the number of IDs
placeholders = ', '.join(['%s'] * len(keys))
query = f"""
SELECT xxx
FROM your_table
WHERE doc_id IN ({placeholders})
ORDER by your_item_index;
"""
# Execute the query with the list of IDs
cursor.execute(query, keys)
# Fetch all results
snippets = cursor.fetchall()
# Check if any items were found
if snippets:
ids = {}
documents = {}
# Create a dictionary of the results.
# every id is a key and the content is an array of the values:
for snippet in snippets:
doc_id = snippet["doc_id"]
if doc_id not in documents:
documents[doc_id] = Document(
page_content="",
metadata={
"doc_id": doc_id,
"snippet_ids": []
}
)
documents[doc_id].page_content += snippet["document"]
documents[doc_id].metadata["snippet_ids"].append(snippet["id"])
docs = [documents[key] for key in documents]
# Test it:
# for d in docs:
# print(d.page_content)
# print("\n" + "-" * 20 + "\n")
return docs
else:
return []
except Exception as e:
print(e)
return []
# These are dummy methods from BaseStore:
async def amget(self, keys: Sequence[str]) -> List[Optional[V]]:
raise NotImplementedError
def mset(self, key_value_pairs: Sequence[Tuple[str, V]]) -> None:
raise NotImplementedError
async def amset(self, key_value_pairs: Sequence[Tuple[str, V]]) -> None:
raise NotImplementedError
def mdelete(self, keys: Sequence[str]) -> None:
raise NotImplementedError
async def amdelete(self, keys: Sequence[str]) -> None:
raise NotImplementedError
def yield_keys(self, prefix: Optional[str] = None) -> Iterator[str]:
raise NotImplementedError
async def ayield_keys(self, prefix: Optional[str] = None) -> AsyncIterator[str]:
raise NotImplementedError You can have a look at the (docstore) BaseStore class here: I hope that helps! If you have any further questions, please let me know. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This is the code I have right now:
Instead of using the
is there a way to use the vectorstore to retrieve the similar chunk?
When I try to put docstore=vectorstore in the retriever it returns the error
Beta Was this translation helpful? Give feedback.
All reactions