-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathingest.py
196 lines (168 loc) · 6.5 KB
/
ingest.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"""Load html from files, clean up, split, ingest into Weaviate."""
import logging
import os
import re
from typing import Optional
import weaviate
from bs4 import BeautifulSoup, SoupStrainer
from langchain.document_loaders import RecursiveUrlLoader, SitemapLoader
from langchain.indexes import SQLRecordManager, index
from langchain.utils.html import PREFIXES_TO_IGNORE_REGEX, SUFFIXES_TO_IGNORE_REGEX
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_weaviate import WeaviateVectorStore
from backend.constants import WEAVIATE_DOCS_INDEX_NAME
from backend.embeddings import get_embeddings_model
from backend.parser import langchain_docs_extractor
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def metadata_extractor(
meta: dict, soup: BeautifulSoup, title_suffix: Optional[str] = None
) -> dict:
title_element = soup.find("title")
description_element = soup.find("meta", attrs={"name": "description"})
html_element = soup.find("html")
title = title_element.get_text() if title_element else ""
if title_suffix is not None:
title += title_suffix
return {
"source": meta["loc"],
"title": title,
"description": description_element.get("content", "")
if description_element
else "",
"language": html_element.get("lang", "") if html_element else "",
**meta,
}
def load_langchain_docs():
return SitemapLoader(
"https://python.langchain.com/sitemap.xml",
filter_urls=["https://python.langchain.com/"],
parsing_function=langchain_docs_extractor,
default_parser="lxml",
bs_kwargs={
"parse_only": SoupStrainer(
name=("article", "title", "html", "lang", "content")
),
},
meta_function=metadata_extractor,
).load()
def load_langgraph_docs():
return SitemapLoader(
"https://langchain-ai.github.io/langgraph/sitemap.xml",
parsing_function=simple_extractor,
default_parser="lxml",
bs_kwargs={"parse_only": SoupStrainer(name=("article", "title"))},
meta_function=lambda meta, soup: metadata_extractor(
meta, soup, title_suffix=" | 🦜🕸️LangGraph"
),
).load()
def load_langsmith_docs():
return RecursiveUrlLoader(
url="https://docs.smith.langchain.com/",
max_depth=8,
extractor=simple_extractor,
prevent_outside=True,
use_async=True,
timeout=600,
# Drop trailing / to avoid duplicate pages.
link_regex=(
f"href=[\"']{PREFIXES_TO_IGNORE_REGEX}((?:{SUFFIXES_TO_IGNORE_REGEX}.)*?)"
r"(?:[\#'\"]|\/[\#'\"])"
),
check_response_status=True,
).load()
def simple_extractor(html: str | BeautifulSoup) -> str:
if isinstance(html, str):
soup = BeautifulSoup(html, "lxml")
elif isinstance(html, BeautifulSoup):
soup = html
else:
raise ValueError(
"Input should be either BeautifulSoup object or an HTML string"
)
return re.sub(r"\n\n+", "\n\n", soup.text).strip()
def load_api_docs():
return RecursiveUrlLoader(
url="https://api.python.langchain.com/en/latest/",
max_depth=8,
extractor=simple_extractor,
prevent_outside=True,
use_async=True,
timeout=600,
# Drop trailing / to avoid duplicate pages.
link_regex=(
f"href=[\"']{PREFIXES_TO_IGNORE_REGEX}((?:{SUFFIXES_TO_IGNORE_REGEX}.)*?)"
r"(?:[\#'\"]|\/[\#'\"])"
),
check_response_status=True,
exclude_dirs=(
"https://api.python.langchain.com/en/latest/_sources",
"https://api.python.langchain.com/en/latest/_modules",
),
).load()
def ingest_docs():
WEAVIATE_URL = os.environ["WEAVIATE_URL"]
WEAVIATE_API_KEY = os.environ["WEAVIATE_API_KEY"]
RECORD_MANAGER_DB_URL = os.environ["RECORD_MANAGER_DB_URL"]
text_splitter = RecursiveCharacterTextSplitter(chunk_size=4000, chunk_overlap=200)
embedding = get_embeddings_model()
with weaviate.connect_to_weaviate_cloud(
cluster_url=WEAVIATE_URL,
auth_credentials=weaviate.classes.init.Auth.api_key(WEAVIATE_API_KEY),
skip_init_checks=True,
) as weaviate_client:
vectorstore = WeaviateVectorStore(
client=weaviate_client,
index_name=WEAVIATE_DOCS_INDEX_NAME,
text_key="text",
embedding=embedding,
attributes=["source", "title"],
)
record_manager = SQLRecordManager(
f"weaviate/{WEAVIATE_DOCS_INDEX_NAME}", db_url=RECORD_MANAGER_DB_URL
)
record_manager.create_schema()
docs_from_documentation = load_langchain_docs()
logger.info(f"Loaded {len(docs_from_documentation)} docs from documentation")
docs_from_api = load_api_docs()
logger.info(f"Loaded {len(docs_from_api)} docs from API")
docs_from_langsmith = load_langsmith_docs()
logger.info(f"Loaded {len(docs_from_langsmith)} docs from LangSmith")
docs_from_langgraph = load_langgraph_docs()
logger.info(f"Loaded {len(docs_from_langgraph)} docs from LangGraph")
docs_transformed = text_splitter.split_documents(
docs_from_documentation
+ docs_from_api
+ docs_from_langsmith
+ docs_from_langgraph
)
docs_transformed = [
doc for doc in docs_transformed if len(doc.page_content) > 10
]
# We try to return 'source' and 'title' metadata when querying vector store and
# Weaviate will error at query time if one of the attributes is missing from a
# retrieved document.
for doc in docs_transformed:
if "source" not in doc.metadata:
doc.metadata["source"] = ""
if "title" not in doc.metadata:
doc.metadata["title"] = ""
indexing_stats = index(
docs_transformed,
record_manager,
vectorstore,
cleanup="full",
source_id_key="source",
force_update=(os.environ.get("FORCE_UPDATE") or "false").lower() == "true",
)
logger.info(f"Indexing stats: {indexing_stats}")
num_vecs = (
weaviate_client.collections.get(WEAVIATE_DOCS_INDEX_NAME)
.aggregate.over_all()
.total_count
)
logger.info(
f"LangChain now has this many vectors: {num_vecs}",
)
if __name__ == "__main__":
ingest_docs()