acpotts commited on
Commit
6a9b7c2
·
1 Parent(s): 0be6f64

dockerfile

Browse files
Files changed (2) hide show
  1. Dockerfile +0 -2
  2. app.py +58 -26
Dockerfile CHANGED
@@ -1,5 +1,3 @@
1
-
2
- AIM version:
3
  FROM python:3.9
4
  RUN useradd -m -u 1000 user
5
  USER user
 
 
 
1
  FROM python:3.9
2
  RUN useradd -m -u 1000 user
3
  USER user
app.py CHANGED
@@ -11,6 +11,9 @@ from langchain_core.prompts import PromptTemplate
11
  from langchain.schema.output_parser import StrOutputParser
12
  from langchain.schema.runnable import RunnablePassthrough
13
  from langchain.schema.runnable.config import RunnableConfig
 
 
 
14
 
15
  # GLOBAL SCOPE - ENTIRE APPLICATION HAS ACCESS TO VALUES SET IN THIS SCOPE #
16
  # ---- ENV VARIABLES ---- #
@@ -44,42 +47,73 @@ documents = text_loader.load()
44
 
45
  ### 2. CREATE TEXT SPLITTER AND SPLIT DOCUMENTS
46
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=30)
47
- split_documents = text_splitter.split_documents(documents)
 
48
 
49
  ### 3. LOAD HUGGINGFACE EMBEDDINGS
50
- hf_embeddings = HuggingFaceEndpointEmbeddings(
51
- model=HF_EMBED_ENDPOINT, #HERE
52
  task="feature-extraction",
53
  huggingfacehub_api_token=os.environ["HF_TOKEN"],
54
  )
55
 
56
- if os.path.exists("./data/vectorstore"):
57
- vectorstore = FAISS.load_local(
58
- "./data/vectorstore",
59
- hf_embeddings,
60
- allow_dangerous_deserialization=True # this is necessary to load the vectorstore from disk as it's stored as a `.pkl` file.
61
- )
62
- hf_retriever = vectorstore.as_retriever()
63
- print("Loaded Vectorstore")
64
- else:
 
 
 
 
65
  print("Indexing Files")
66
- os.makedirs("./data/vectorstore", exist_ok=True)
67
- ### 4. INDEX FILES
68
- ### NOTE: REMEMBER TO BATCH THE DOCUMENTS WITH MAXIMUM BATCH SIZE = 32
69
- for i in range(0, len(split_documents), 32):
70
- if i == 0:
71
- vectorstore = FAISS.from_documents(split_documents[i:i+32], hf_embeddings)
72
- continue
73
- vectorstore.add_documents(split_documents[i:i+32])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
- hf_retriever = vectorstore.as_retriever()
76
 
77
  # -- AUGMENTED -- #
78
  """
79
  1. Define a String Template
80
  2. Create a Prompt Template from the String Template
81
  """
82
- ### 1. DEFINE STRING TEMPLATE
83
  RAG_PROMPT_TEMPLATE = """\
84
  <|start_header_id|>system<|end_header_id|>
85
  You are a helpful assistant. You answer user questions based on provided context. If you can't answer the question with the provided context, say you don't know.<|eot_id|>
@@ -94,16 +128,14 @@ Context:
94
  <|start_header_id|>assistant<|end_header_id|>
95
  """
96
 
97
- ### 2. CREATE PROMPT TEMPLATE
98
  rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
99
 
100
-
101
  # -- GENERATION -- #
102
  """
103
  1. Create a HuggingFaceEndpoint for the LLM
104
  """
105
  ### 1. CREATE HUGGINGFACE ENDPOINT FOR LLM
106
- hf_llm = HuggingFaceEndpoint(
107
  endpoint_url=f"{HF_LLM_ENDPOINT}",
108
  max_new_tokens=512,
109
  top_k=10,
@@ -111,7 +143,7 @@ hf_llm = HuggingFaceEndpoint(
111
  typical_p=0.95,
112
  temperature=0.01,
113
  repetition_penalty=1.03,
114
- huggingfacehub_api_token=HF_TOKEN
115
  )
116
 
117
  @cl.author_rename
 
11
  from langchain.schema.output_parser import StrOutputParser
12
  from langchain.schema.runnable import RunnablePassthrough
13
  from langchain.schema.runnable.config import RunnableConfig
14
+ from tqdm.asyncio import tqdm_asyncio
15
+ import asyncio
16
+ from tqdm.asyncio import tqdm
17
 
18
  # GLOBAL SCOPE - ENTIRE APPLICATION HAS ACCESS TO VALUES SET IN THIS SCOPE #
19
  # ---- ENV VARIABLES ---- #
 
47
 
48
  ### 2. CREATE TEXT SPLITTER AND SPLIT DOCUMENTS
49
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=30)
50
+ split_documents = split_documents = text_splitter.split_documents(documents)
51
+ print(len(split_documents))
52
 
53
  ### 3. LOAD HUGGINGFACE EMBEDDINGS
54
+ hf_embeddings = HuggingFaceEndpointEmbeddings(
55
+ model=HF_EMBED_ENDPOINT,
56
  task="feature-extraction",
57
  huggingfacehub_api_token=os.environ["HF_TOKEN"],
58
  )
59
 
60
+ async def add_documents_async(vectorstore, documents):
61
+ await vectorstore.aadd_documents(documents)
62
+
63
+ async def process_batch(vectorstore, batch, is_first_batch, pbar):
64
+ if is_first_batch:
65
+ result = await FAISS.afrom_documents(batch, hf_embeddings)
66
+ else:
67
+ await add_documents_async(vectorstore, batch)
68
+ result = vectorstore
69
+ pbar.update(len(batch))
70
+ return result
71
+
72
+ async def main():
73
  print("Indexing Files")
74
+
75
+ vectorstore = None
76
+ batch_size = 32
77
+
78
+ batches = [split_documents[i:i+batch_size] for i in range(0, len(split_documents), batch_size)]
79
+
80
+ async def process_all_batches():
81
+ nonlocal vectorstore
82
+ tasks = []
83
+ pbars = []
84
+
85
+ for i, batch in enumerate(batches):
86
+ pbar = tqdm(total=len(batch), desc=f"Batch {i+1}/{len(batches)}", position=i)
87
+ pbars.append(pbar)
88
+
89
+ if i == 0:
90
+ vectorstore = await process_batch(None, batch, True, pbar)
91
+ else:
92
+ tasks.append(process_batch(vectorstore, batch, False, pbar))
93
+
94
+ if tasks:
95
+ await asyncio.gather(*tasks)
96
+
97
+ for pbar in pbars:
98
+ pbar.close()
99
+
100
+ await process_all_batches()
101
+
102
+ hf_retriever = vectorstore.as_retriever()
103
+ print("\nIndexing complete. Vectorstore is ready for use.")
104
+ return hf_retriever
105
+
106
+ async def run():
107
+ retriever = await main()
108
+ return retriever
109
 
110
+ hf_retriever = asyncio.run(run())
111
 
112
  # -- AUGMENTED -- #
113
  """
114
  1. Define a String Template
115
  2. Create a Prompt Template from the String Template
116
  """
 
117
  RAG_PROMPT_TEMPLATE = """\
118
  <|start_header_id|>system<|end_header_id|>
119
  You are a helpful assistant. You answer user questions based on provided context. If you can't answer the question with the provided context, say you don't know.<|eot_id|>
 
128
  <|start_header_id|>assistant<|end_header_id|>
129
  """
130
 
 
131
  rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
132
 
 
133
  # -- GENERATION -- #
134
  """
135
  1. Create a HuggingFaceEndpoint for the LLM
136
  """
137
  ### 1. CREATE HUGGINGFACE ENDPOINT FOR LLM
138
+ hf_llm = HuggingFaceEndpoint(
139
  endpoint_url=f"{HF_LLM_ENDPOINT}",
140
  max_new_tokens=512,
141
  top_k=10,
 
143
  typical_p=0.95,
144
  temperature=0.01,
145
  repetition_penalty=1.03,
146
+ huggingfacehub_api_token=os.environ["HF_TOKEN"]
147
  )
148
 
149
  @cl.author_rename