JaganathC commited on
Commit
064caab
·
verified ·
1 Parent(s): a722252

Upload 2 files

Browse files
Files changed (2) hide show
  1. indexing.py +83 -0
  2. prompt_template.json +5 -0
indexing.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Indexing with vector database
3
+ """
4
+
5
+ from pathlib import Path
6
+ import re
7
+
8
+ import chromadb
9
+
10
+ from unidecode import unidecode
11
+
12
+ from langchain_community.document_loaders import PyPDFLoader
13
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
14
+ from langchain_chroma import Chroma
15
+ from langchain_huggingface import HuggingFaceEmbeddings
16
+
17
+
18
+
19
+ # Load PDF document and create doc splits
20
+ def load_doc(list_file_path, chunk_size, chunk_overlap):
21
+ """Load PDF document and create doc splits"""
22
+
23
+ loaders = [PyPDFLoader(x) for x in list_file_path]
24
+ pages = []
25
+ for loader in loaders:
26
+ pages.extend(loader.load())
27
+ text_splitter = RecursiveCharacterTextSplitter(
28
+ chunk_size=chunk_size, chunk_overlap=chunk_overlap
29
+ )
30
+ doc_splits = text_splitter.split_documents(pages)
31
+ return doc_splits
32
+
33
+
34
+ # Generate collection name for vector database
35
+ # - Use filepath as input, ensuring unicode text
36
+ # - Handle multiple languages (arabic, chinese)
37
+ def create_collection_name(filepath):
38
+ """Create collection name for vector database"""
39
+
40
+ # Extract filename without extension
41
+ collection_name = Path(filepath).stem
42
+ # Fix potential issues from naming convention
43
+ ## Remove space
44
+ collection_name = collection_name.replace(" ", "-")
45
+ ## ASCII transliterations of Unicode text
46
+ collection_name = unidecode(collection_name)
47
+ ## Remove special characters
48
+ collection_name = re.sub("[^A-Za-z0-9]+", "-", collection_name)
49
+ ## Limit length to 50 characters
50
+ collection_name = collection_name[:50]
51
+ ## Minimum length of 3 characters
52
+ if len(collection_name) < 3:
53
+ collection_name = collection_name + "xyz"
54
+ ## Enforce start and end as alphanumeric character
55
+ if not collection_name[0].isalnum():
56
+ collection_name = "A" + collection_name[1:]
57
+ if not collection_name[-1].isalnum():
58
+ collection_name = collection_name[:-1] + "Z"
59
+ print("\n\nFilepath: ", filepath)
60
+ print("Collection name: ", collection_name)
61
+ return collection_name
62
+
63
+
64
+ # Create vector database
65
+ def create_db(splits, collection_name):
66
+ """Create embeddings and vector database"""
67
+
68
+ embedding = HuggingFaceEmbeddings(
69
+ model_name="sentence-transformers/paraphrase-multilingual-mpnet-base-v2",
70
+ # model_name="sentence-transformers/all-MiniLM-L6-v2",
71
+ # model_kwargs={"device": "cpu"},
72
+ # encode_kwargs={'normalize_embeddings': False}
73
+ )
74
+ chromadb.api.client.SharedSystemClient.clear_system_cache()
75
+ new_client = chromadb.EphemeralClient()
76
+ vectordb = Chroma.from_documents(
77
+ documents=splits,
78
+ embedding=embedding,
79
+ client=new_client,
80
+ collection_name=collection_name,
81
+ # persist_directory=default_persist_directory
82
+ )
83
+ return vectordb
prompt_template.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "title": "System prompt",
3
+ "prompt": "You are an assistant for question-answering tasks. Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. Keep the answer concise. Question: {question} \\n Context: {context} \\n Helpful Answer:"
4
+ }
5
+