Skip to content

Commit 1913320

Browse files
Feature/add acreom loader (#5780)
adding new loader for [acreom](https://acreom.com) vaults. It's based on the Obsidian loader with some additional text processing for acreom specific markdown elements. @eyurtsev please take a look! --------- Co-authored-by: rlm <[email protected]>
1 parent ae76e47 commit 1913320

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "e310c8dc-acd0-48d2-801c-f37ce99acd2d",
6+
"metadata": {},
7+
"source": [
8+
"# acreom"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "04a2c95d-4114-431e-904a-32d79005c28b",
14+
"metadata": {},
15+
"source": [
16+
"[acreom](https://acreom.com) is a dev-first knowledge base with tasks running on local markdown files.\n",
17+
"\n",
18+
"Below is an example on how to load a local acreom vault into Langchain. As the local vault in acreom is a folder of plain text .md files, the loader requires the path to the directory. \n",
19+
"\n",
20+
"Vault files may contain some metadata which is stored as a YAML header. These values will be added to the document’s metadata if `collect_metadata` is set to true. "
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"id": "0169bee5-aa7a-4ec7-b7e7-b3bb2e58f3bb",
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"from langchain.document_loaders import AcreomLoader"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"id": "c1b49ab3-616b-4149-bef5-7559d65d3d2b",
37+
"metadata": {},
38+
"outputs": [],
39+
"source": [
40+
"loader = AcreomLoader('<path-to-acreom-vault>', collect_metadata=False)"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": null,
46+
"id": "3127a018-9c1c-4886-8321-f5666d970a95",
47+
"metadata": {},
48+
"outputs": [],
49+
"source": [
50+
"docs = loader.load()"
51+
]
52+
}
53+
],
54+
"metadata": {
55+
"kernelspec": {
56+
"display_name": "Python 3 (ipykernel)",
57+
"language": "python",
58+
"name": "python3"
59+
},
60+
"language_info": {
61+
"codemirror_mode": {
62+
"name": "ipython",
63+
"version": 3
64+
},
65+
"file_extension": ".py",
66+
"mimetype": "text/x-python",
67+
"name": "python",
68+
"nbconvert_exporter": "python",
69+
"pygments_lexer": "ipython3",
70+
"version": "3.10.10"
71+
}
72+
},
73+
"nbformat": 4,
74+
"nbformat_minor": 5
75+
}

langchain/document_loaders/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""All different types of document loaders."""
22

3+
from langchain.document_loaders.acreom import AcreomLoader
34
from langchain.document_loaders.airbyte_json import AirbyteJSONLoader
45
from langchain.document_loaders.airtable import AirtableLoader
56
from langchain.document_loaders.apify_dataset import ApifyDatasetLoader
@@ -136,6 +137,7 @@
136137
TelegramChatLoader = TelegramChatFileLoader
137138

138139
__all__ = [
140+
"AcreomLoader",
139141
"AZLyricsLoader",
140142
"AirbyteJSONLoader",
141143
"AirtableLoader",
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""Loader that loads acreom vault from a directory."""
2+
import re
3+
from pathlib import Path
4+
from typing import Iterator, List
5+
6+
from langchain.docstore.document import Document
7+
from langchain.document_loaders.base import BaseLoader
8+
9+
10+
class AcreomLoader(BaseLoader):
11+
FRONT_MATTER_REGEX = re.compile(r"^---\n(.*?)\n---\n", re.MULTILINE | re.DOTALL)
12+
13+
def __init__(
14+
self, path: str, encoding: str = "UTF-8", collect_metadata: bool = True
15+
):
16+
"""Initialize with path."""
17+
self.file_path = path
18+
self.encoding = encoding
19+
self.collect_metadata = collect_metadata
20+
21+
def _parse_front_matter(self, content: str) -> dict:
22+
"""Parse front matter metadata from the content and return it as a dict."""
23+
if not self.collect_metadata:
24+
return {}
25+
match = self.FRONT_MATTER_REGEX.search(content)
26+
front_matter = {}
27+
if match:
28+
lines = match.group(1).split("\n")
29+
for line in lines:
30+
if ":" in line:
31+
key, value = line.split(":", 1)
32+
front_matter[key.strip()] = value.strip()
33+
else:
34+
# Skip lines without a colon
35+
continue
36+
return front_matter
37+
38+
def _remove_front_matter(self, content: str) -> str:
39+
"""Remove front matter metadata from the given content."""
40+
if not self.collect_metadata:
41+
return content
42+
return self.FRONT_MATTER_REGEX.sub("", content)
43+
44+
def _process_acreom_content(self, content: str) -> str:
45+
# remove acreom specific elements from content that
46+
# do not contribute to the context of current document
47+
content = re.sub("\s*-\s\[\s\]\s.*|\s*\[\s\]\s.*", "", content) # rm tasks
48+
content = re.sub("#", "", content) # rm hashtags
49+
content = re.sub("\[\[.*?\]\]", "", content) # rm doclinks
50+
return content
51+
52+
def lazy_load(self) -> Iterator[Document]:
53+
ps = list(Path(self.file_path).glob("**/*.md"))
54+
55+
for p in ps:
56+
with open(p, encoding=self.encoding) as f:
57+
text = f.read()
58+
59+
front_matter = self._parse_front_matter(text)
60+
text = self._remove_front_matter(text)
61+
62+
text = self._process_acreom_content(text)
63+
64+
metadata = {
65+
"source": str(p.name),
66+
"path": str(p),
67+
**front_matter,
68+
}
69+
70+
yield Document(page_content=text, metadata=metadata)
71+
72+
def load(self) -> List[Document]:
73+
return list(self.lazy_load())

0 commit comments

Comments
 (0)