Update appChatbot.py
Browse files- appChatbot.py +42 -0
appChatbot.py
CHANGED
@@ -16,6 +16,10 @@ from langchain.vectorstores import Chroma
|
|
16 |
from langchain.document_loaders import PyPDFLoader
|
17 |
from fastapi.encoders import jsonable_encoder
|
18 |
|
|
|
|
|
|
|
|
|
19 |
"""
|
20 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
21 |
"""
|
@@ -31,6 +35,44 @@ embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
|
31 |
ABS_PATH = os.path.dirname(os.path.abspath(__file__))
|
32 |
DB_DIR = os.path.join(ABS_PATH, "db")
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
vectorstore = None
|
35 |
|
36 |
def replace_newlines_and_spaces(text):
|
|
|
16 |
from langchain.document_loaders import PyPDFLoader
|
17 |
from fastapi.encoders import jsonable_encoder
|
18 |
|
19 |
+
from langchain.embeddings import HuggingFaceInstructEmbeddings
|
20 |
+
from langchain.vectorstores.faiss import FAISS
|
21 |
+
from huggingface_hub import snapshot_download
|
22 |
+
|
23 |
"""
|
24 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
25 |
"""
|
|
|
35 |
ABS_PATH = os.path.dirname(os.path.abspath(__file__))
|
36 |
DB_DIR = os.path.join(ABS_PATH, "db")
|
37 |
|
38 |
+
cache_dir=f"{book}_cache"
|
39 |
+
|
40 |
+
vectorstore = snapshot_download(repo_id="calmgoose/book-embeddings",
|
41 |
+
repo_type="dataset",
|
42 |
+
revision="main",
|
43 |
+
allow_patterns=f"books/{BOOK}/*", # to download only the one book
|
44 |
+
cache_dir=cache_dir,
|
45 |
+
)
|
46 |
+
# get path to the `vectorstore` folder that you just downloaded
|
47 |
+
# we'll look inside the `cache_dir` for the folder we want
|
48 |
+
target_dir = BOOK
|
49 |
+
|
50 |
+
# Walk through the directory tree recursively
|
51 |
+
for root, dirs, files in os.walk(cache_dir):
|
52 |
+
# Check if the target directory is in the list of directories
|
53 |
+
if target_dir in dirs:
|
54 |
+
# Get the full path of the target directory
|
55 |
+
target_path = os.path.join(root, target_dir)
|
56 |
+
|
57 |
+
# load embeddings
|
58 |
+
# this is what was used to create embeddings for the book
|
59 |
+
embeddings = HuggingFaceInstructEmbeddings(
|
60 |
+
embed_instruction="Represent the book passage for retrieval: ",
|
61 |
+
query_instruction="Represent the question for retrieving supporting texts from the book passage: "
|
62 |
+
)
|
63 |
+
|
64 |
+
# load vector store to use with langchain
|
65 |
+
docsearch = FAISS.load_local(folder_path=target_path, embeddings=embeddings)
|
66 |
+
|
67 |
+
# similarity search
|
68 |
+
question = "Who is big brother?"
|
69 |
+
search = docsearch.similarity_search(question, k=4)
|
70 |
+
|
71 |
+
for item in search:
|
72 |
+
print(item.page_content)
|
73 |
+
print(f"From page: {item.metadata['page']}")
|
74 |
+
print("---")
|
75 |
+
|
76 |
vectorstore = None
|
77 |
|
78 |
def replace_newlines_and_spaces(text):
|