Spaces:
Running
Running
add markdown parser (#5)
Browse files- cfg.py +8 -13
- embed_documents.py +1 -6
- markdown_parser.py +92 -0
cfg.py
CHANGED
@@ -35,9 +35,9 @@ hf_hub_download(
|
|
35 |
extract_zip(zip_file_path=HUB_DB_FILE, output_path="deeplake_store")
|
36 |
|
37 |
example_questions = [
|
38 |
-
"What
|
39 |
-
"What is
|
40 |
-
"What is
|
41 |
]
|
42 |
|
43 |
|
@@ -50,12 +50,10 @@ buster_cfg = BusterConfig(
|
|
50 |
"embedding_model": "text-embedding-ada-002",
|
51 |
"use_reranking": True,
|
52 |
"invalid_question_response": "This question does not seem relevant to my current knowledge.",
|
53 |
-
"check_question_prompt": """You are
|
54 |
-
|
55 |
-
Users will
|
56 |
-
|
57 |
-
More general questions are not considered valid, even if you might know the response.
|
58 |
-
A user will submit a question. Respond 'true' if it is valid, respond 'false' if it is invalid.
|
59 |
|
60 |
For example:
|
61 |
|
@@ -65,7 +63,7 @@ true
|
|
65 |
Q: What is the meaning of life?
|
66 |
false
|
67 |
|
68 |
-
A user will submit a question. Respond 'true' if it is valid, respond 'false' if it is invalid.""",
|
69 |
"completion_kwargs": {
|
70 |
"model": "gpt-3.5-turbo",
|
71 |
"stream": False,
|
@@ -130,9 +128,6 @@ A user will submit a question. Respond 'true' if it is valid, respond 'false' if
|
|
130 |
},
|
131 |
)
|
132 |
|
133 |
-
# initialize buster with the config in cfg.py (adapt to your needs) ...
|
134 |
-
# buster_cfg = cfg.buster_cfg
|
135 |
-
|
136 |
|
137 |
def setup_buster(buster_cfg):
|
138 |
retriever: Retriever = DeepLakeRetriever(**buster_cfg.retriever_cfg)
|
|
|
35 |
extract_zip(zip_file_path=HUB_DB_FILE, output_path="deeplake_store")
|
36 |
|
37 |
example_questions = [
|
38 |
+
"What is the LLama model?",
|
39 |
+
"What is a LLM?",
|
40 |
+
"What is an embedding?",
|
41 |
]
|
42 |
|
43 |
|
|
|
50 |
"embedding_model": "text-embedding-ada-002",
|
51 |
"use_reranking": True,
|
52 |
"invalid_question_response": "This question does not seem relevant to my current knowledge.",
|
53 |
+
"check_question_prompt": """You are a chatbot, answering questions about large language models and artificial intelligence.
|
54 |
+
Users will ask all sorts of questions, and some might be tangentially related.
|
55 |
+
Users will learn to build LLM-powered apps, with LangChain & Deep Lake among other technologies.
|
56 |
+
As long as a question is somewhat related to the topic, respond 'true'. If a question is completely unrelated, respond 'false'.
|
|
|
|
|
57 |
|
58 |
For example:
|
59 |
|
|
|
63 |
Q: What is the meaning of life?
|
64 |
false
|
65 |
|
66 |
+
A user will now submit a question. Respond 'true' if it is valid, respond 'false' if it is invalid.""",
|
67 |
"completion_kwargs": {
|
68 |
"model": "gpt-3.5-turbo",
|
69 |
"stream": False,
|
|
|
128 |
},
|
129 |
)
|
130 |
|
|
|
|
|
|
|
131 |
|
132 |
def setup_buster(buster_cfg):
|
133 |
retriever: Retriever = DeepLakeRetriever(**buster_cfg.retriever_cfg)
|
embed_documents.py
CHANGED
@@ -3,16 +3,11 @@ from buster.documents_manager import DeepLakeDocumentsManager
|
|
3 |
|
4 |
if __name__ == "__main__":
|
5 |
vector_store_path = "deeplake_store"
|
6 |
-
chunk_file = "
|
7 |
overwrite = True
|
8 |
|
9 |
df = pd.read_csv(chunk_file)
|
10 |
|
11 |
-
# some pre-processing based on the latest file provided
|
12 |
-
df["url"] = df["source"]
|
13 |
-
df["source"] = "towardsai_blog"
|
14 |
-
df = df.dropna()
|
15 |
-
|
16 |
dm = DeepLakeDocumentsManager(vector_store_path, overwrite=overwrite)
|
17 |
dm.batch_add(df)
|
18 |
zipped_file_path = dm.to_zip()
|
|
|
3 |
|
4 |
if __name__ == "__main__":
|
5 |
vector_store_path = "deeplake_store"
|
6 |
+
chunk_file = "langchain_course.csv"
|
7 |
overwrite = True
|
8 |
|
9 |
df = pd.read_csv(chunk_file)
|
10 |
|
|
|
|
|
|
|
|
|
|
|
11 |
dm = DeepLakeDocumentsManager(vector_store_path, overwrite=overwrite)
|
12 |
dm.batch_add(df)
|
13 |
zipped_file_path = dm.to_zip()
|
markdown_parser.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import pandas as pd
|
4 |
+
import tiktoken
|
5 |
+
from langchain.text_splitter import MarkdownHeaderTextSplitter
|
6 |
+
|
7 |
+
|
8 |
+
def num_tokens_from_string(string: str, encoding_name: str = "cl100k_base") -> int:
|
9 |
+
encoding = tiktoken.get_encoding(encoding_name)
|
10 |
+
num_tokens = len(encoding.encode(string))
|
11 |
+
return num_tokens
|
12 |
+
|
13 |
+
|
14 |
+
def drop_outlier_chunks(df: pd.DataFrame, max_tokens_by_chunk: int = 4500):
|
15 |
+
# drops chunks with abnormally high token counts, usually they contain lots of links
|
16 |
+
filtered_df = df[df.content.apply(num_tokens_from_string) < max_tokens_by_chunk]
|
17 |
+
outliers_df = df[df.content.apply(num_tokens_from_string) >= max_tokens_by_chunk]
|
18 |
+
print(f"Dropping {len(df) - len(filtered_df)} outlier chunks")
|
19 |
+
print(f"Dropped outliers: {outliers_df.content.to_list()}")
|
20 |
+
return filtered_df
|
21 |
+
|
22 |
+
|
23 |
+
def find_md_files(folder_path):
|
24 |
+
"""Recursively find .md files, extract content and use filename as title."""
|
25 |
+
md_files = []
|
26 |
+
|
27 |
+
for root, _, files in os.walk(folder_path):
|
28 |
+
for file in files:
|
29 |
+
if file.endswith(".md"):
|
30 |
+
file_path = os.path.join(root, file)
|
31 |
+
title = os.path.splitext(file)[0]
|
32 |
+
with open(file_path, "r", encoding="utf-8") as md_file:
|
33 |
+
content = md_file.read()
|
34 |
+
md_files.append({"title": title, "content": content})
|
35 |
+
|
36 |
+
return md_files
|
37 |
+
|
38 |
+
|
39 |
+
def split_string_by_max_words(input_string, max_words):
|
40 |
+
words = input_string.split()
|
41 |
+
return [" ".join(words[i : i + max_words]) for i in range(0, len(words), max_words)]
|
42 |
+
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
folder_path = "/path/to/folder/with/md_content/"
|
46 |
+
folder_path = "/Users/jeremypinto/Downloads/d22d1e98-345f-490d-870e-3b082938741c_Export-0a33c13f-6d42-4a94-8f23-7459e7b2c024"
|
47 |
+
|
48 |
+
md_files = find_md_files(folder_path)
|
49 |
+
|
50 |
+
headers_to_split_on = [
|
51 |
+
("#", "#"),
|
52 |
+
("##", "##"),
|
53 |
+
]
|
54 |
+
|
55 |
+
markdown_splitter = MarkdownHeaderTextSplitter(
|
56 |
+
headers_to_split_on=headers_to_split_on
|
57 |
+
)
|
58 |
+
|
59 |
+
chunks = []
|
60 |
+
|
61 |
+
from tqdm import tqdm
|
62 |
+
|
63 |
+
for md_file in tqdm(md_files):
|
64 |
+
md_title = md_file["title"]
|
65 |
+
md_raw_content = md_file["content"]
|
66 |
+
md_header_splits = markdown_splitter.split_text(md_raw_content)
|
67 |
+
|
68 |
+
for split in md_header_splits:
|
69 |
+
# add the headers back to the content
|
70 |
+
headers = "\n".join(
|
71 |
+
[
|
72 |
+
k + " " + v
|
73 |
+
for k, v in zip(split.metadata.keys(), split.metadata.values())
|
74 |
+
]
|
75 |
+
)
|
76 |
+
|
77 |
+
substrings = split_string_by_max_words(split.page_content, max_words=600)
|
78 |
+
for substring in substrings:
|
79 |
+
chunk = {
|
80 |
+
"title": md_title,
|
81 |
+
"content": headers + "\n" + substring,
|
82 |
+
"source": "TAI Course",
|
83 |
+
"url": "https://learn.activeloop.ai/courses/langchain/",
|
84 |
+
}
|
85 |
+
chunks.append(chunk)
|
86 |
+
|
87 |
+
df = pd.DataFrame(chunks)
|
88 |
+
|
89 |
+
df = drop_outlier_chunks(df, max_tokens_by_chunk=2000)
|
90 |
+
|
91 |
+
print(f"Exported {len(df)} chunks from {len(md_files)} articles.")
|
92 |
+
df.to_csv("langchain_course.csv")
|