forked from pmarkun/redeiro-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.py
24 lines (18 loc) · 909 Bytes
/
memory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from haystack.document_stores import InMemoryDocumentStore
from haystack.utils import convert_files_to_docs
from haystack.nodes import TfidfRetriever
# Set up the document store
document_store = InMemoryDocumentStore()
def index_documents_from_folder(folder_path: str):
# Read text files from the folder and convert them to dictionaries
documents = convert_files_to_docs(dir_path=folder_path, clean_func=None, split_paragraphs=True)
# Index the documents in the document store
document_store.write_documents(documents)
def get_memory_snippets(query: str, top_n: int):
# Set up the retriever
retriever = TfidfRetriever(document_store)
# Retrieve top_n documents
retrieved_docs = retriever.retrieve(query, top_k=top_n)
# Extract text snippets from the retrieved documents
memory_snippets = [doc.content for doc in retrieved_docs]
return memory_snippets