Update app.py
Browse files
app.py
CHANGED
@@ -40,20 +40,16 @@ HF_TOKEN = os.environ["HF_TOKEN"]
|
|
40 |
3. Load HuggingFace Embeddings (remember to use the URL we set above)
|
41 |
4. Index Files if they do not exist, otherwise load the vectorstore
|
42 |
"""
|
43 |
-
|
44 |
-
### NOTE: PAY ATTENTION TO THE PATH THEY ARE IN.
|
45 |
-
text_loader = TextLoader("./paul-graham-to-kindle/paul_graham_essays.txt")
|
46 |
documents = document_loader.load()
|
47 |
|
48 |
-
### 2. CREATE TEXT SPLITTER AND SPLIT DOCUMENTS
|
49 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=30)
|
50 |
split_documents = text_splitter.split_documents(documents)
|
51 |
|
52 |
-
### 3. LOAD HUGGINGFACE EMBEDDINGS
|
53 |
hf_embeddings = HuggingFaceEndpointEmbeddings(
|
54 |
-
model=
|
55 |
task="feature-extraction",
|
56 |
-
huggingfacehub_api_token=
|
57 |
)
|
58 |
|
59 |
async def add_documents_async(vectorstore, documents):
|
@@ -113,7 +109,6 @@ hf_retriever = asyncio.run(run())
|
|
113 |
1. Define a String Template
|
114 |
2. Create a Prompt Template from the String Template
|
115 |
"""
|
116 |
-
### 1. DEFINE STRING TEMPLATE
|
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,24 +123,20 @@ Context:
|
|
128 |
<|start_header_id|>assistant<|end_header_id|>
|
129 |
"""
|
130 |
|
131 |
-
|
132 |
-
### 2. CREATE PROMPT TEMPLATE
|
133 |
rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
|
134 |
|
135 |
# -- GENERATION -- #
|
136 |
"""
|
137 |
1. Create a HuggingFaceEndpoint for the LLM
|
138 |
"""
|
139 |
-
### 1. CREATE HUGGINGFACE ENDPOINT FOR LLM
|
140 |
hf_llm = HuggingFaceEndpoint(
|
141 |
-
endpoint_url=
|
142 |
max_new_tokens=512,
|
143 |
top_k=10,
|
144 |
top_p=0.95,
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
huggingfacehub_api_token=os.environ["HF_TOKEN"]
|
149 |
)
|
150 |
|
151 |
@cl.author_rename
|
@@ -170,8 +161,10 @@ async def start_chat():
|
|
170 |
The user session is a dictionary that is unique to each user session, and is stored in the memory of the server.
|
171 |
"""
|
172 |
|
173 |
-
|
174 |
-
|
|
|
|
|
175 |
|
176 |
cl.user_session.set("lcel_rag_chain", lcel_rag_chain)
|
177 |
|
@@ -188,7 +181,7 @@ async def main(message: cl.Message):
|
|
188 |
|
189 |
msg = cl.Message(content="")
|
190 |
|
191 |
-
|
192 |
{"query": message.content},
|
193 |
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
194 |
):
|
|
|
40 |
3. Load HuggingFace Embeddings (remember to use the URL we set above)
|
41 |
4. Index Files if they do not exist, otherwise load the vectorstore
|
42 |
"""
|
43 |
+
document_loader = TextLoader("./data/paul_graham_essays.txt")
|
|
|
|
|
44 |
documents = document_loader.load()
|
45 |
|
|
|
46 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=30)
|
47 |
split_documents = text_splitter.split_documents(documents)
|
48 |
|
|
|
49 |
hf_embeddings = HuggingFaceEndpointEmbeddings(
|
50 |
+
model=HF_EMBED_ENDPOINT,
|
51 |
task="feature-extraction",
|
52 |
+
huggingfacehub_api_token=HF_TOKEN,
|
53 |
)
|
54 |
|
55 |
async def add_documents_async(vectorstore, documents):
|
|
|
109 |
1. Define a String Template
|
110 |
2. Create a Prompt Template from the String Template
|
111 |
"""
|
|
|
112 |
RAG_PROMPT_TEMPLATE = """\
|
113 |
<|start_header_id|>system<|end_header_id|>
|
114 |
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|>
|
|
|
123 |
<|start_header_id|>assistant<|end_header_id|>
|
124 |
"""
|
125 |
|
|
|
|
|
126 |
rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
|
127 |
|
128 |
# -- GENERATION -- #
|
129 |
"""
|
130 |
1. Create a HuggingFaceEndpoint for the LLM
|
131 |
"""
|
|
|
132 |
hf_llm = HuggingFaceEndpoint(
|
133 |
+
endpoint_url=HF_LLM_ENDPOINT,
|
134 |
max_new_tokens=512,
|
135 |
top_k=10,
|
136 |
top_p=0.95,
|
137 |
+
temperature=0.3,
|
138 |
+
repetition_penalty=1.15,
|
139 |
+
huggingfacehub_api_token=HF_TOKEN,
|
|
|
140 |
)
|
141 |
|
142 |
@cl.author_rename
|
|
|
161 |
The user session is a dictionary that is unique to each user session, and is stored in the memory of the server.
|
162 |
"""
|
163 |
|
164 |
+
lcel_rag_chain = (
|
165 |
+
{"context": itemgetter("query") | hf_retriever, "query": itemgetter("query")}
|
166 |
+
| rag_prompt | hf_llm
|
167 |
+
)
|
168 |
|
169 |
cl.user_session.set("lcel_rag_chain", lcel_rag_chain)
|
170 |
|
|
|
181 |
|
182 |
msg = cl.Message(content="")
|
183 |
|
184 |
+
for chunk in await cl.make_async(lcel_rag_chain.stream)(
|
185 |
{"query": message.content},
|
186 |
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
187 |
):
|