Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -37,21 +37,18 @@ HF_TOKEN = os.environ["HF_TOKEN"]
|
|
37 |
3. Load HuggingFace Embeddings (remember to use the URL we set above)
|
38 |
4. Index Files if they do not exist, otherwise load the vectorstore
|
39 |
"""
|
40 |
-
|
41 |
-
|
42 |
-
text_loader = TextLoader("./data/paul_graham_essays.txt")
|
43 |
-
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,
|
52 |
task="feature-extraction",
|
53 |
huggingfacehub_api_token=HF_TOKEN,
|
54 |
)
|
|
|
55 |
vectorstore_path = "./data/vectorstore"
|
56 |
index_file = os.path.join(vectorstore_path, "index.faiss")
|
57 |
|
@@ -76,7 +73,8 @@ else:
|
|
76 |
vectorstore.add_documents(batch)
|
77 |
vectorstore.save_local(vectorstore_path)
|
78 |
print("Vectorstore created and saved")
|
79 |
-
|
|
|
80 |
hf_retriever = vectorstore.as_retriever()
|
81 |
|
82 |
# -- AUGMENTED -- #
|
@@ -84,7 +82,6 @@ hf_retriever = vectorstore.as_retriever()
|
|
84 |
1. Define a String Template
|
85 |
2. Create a Prompt Template from the String Template
|
86 |
"""
|
87 |
-
### 1. DEFINE STRING TEMPLATE
|
88 |
RAG_PROMPT_TEMPLATE = """\
|
89 |
<|start_header_id|>system<|end_header_id|>
|
90 |
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|>
|
@@ -99,21 +96,19 @@ Context:
|
|
99 |
<|start_header_id|>assistant<|end_header_id|>
|
100 |
"""
|
101 |
|
102 |
-
### 2. CREATE PROMPT TEMPLATE
|
103 |
rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
|
104 |
|
105 |
# -- GENERATION -- #
|
106 |
"""
|
107 |
1. Create a HuggingFaceEndpoint for the LLM
|
108 |
"""
|
109 |
-
### 1. CREATE HUGGINGFACE ENDPOINT FOR LLM
|
110 |
hf_llm = HuggingFaceEndpoint(
|
111 |
endpoint_url=HF_LLM_ENDPOINT,
|
112 |
max_new_tokens=512,
|
113 |
top_k=10,
|
114 |
top_p=0.95,
|
115 |
-
temperature=0.
|
116 |
-
repetition_penalty=1.
|
117 |
huggingfacehub_api_token=HF_TOKEN,
|
118 |
)
|
119 |
|
@@ -139,7 +134,6 @@ async def start_chat():
|
|
139 |
The user session is a dictionary that is unique to each user session, and is stored in the memory of the server.
|
140 |
"""
|
141 |
|
142 |
-
### BUILD LCEL RAG CHAIN THAT ONLY RETURNS TEXT
|
143 |
lcel_rag_chain = (
|
144 |
{"context": itemgetter("query") | hf_retriever, "query": itemgetter("query")}
|
145 |
| rag_prompt | hf_llm
|
@@ -160,7 +154,7 @@ async def main(message: cl.Message):
|
|
160 |
|
161 |
msg = cl.Message(content="")
|
162 |
|
163 |
-
|
164 |
{"query": message.content},
|
165 |
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
166 |
):
|
|
|
37 |
3. Load HuggingFace Embeddings (remember to use the URL we set above)
|
38 |
4. Index Files if they do not exist, otherwise load the vectorstore
|
39 |
"""
|
40 |
+
document_loader = TextLoader("./data/paul_graham_essays.txt")
|
41 |
+
documents = document_loader.load()
|
|
|
|
|
42 |
|
|
|
43 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=30)
|
44 |
split_documents = text_splitter.split_documents(documents)
|
45 |
|
|
|
46 |
hf_embeddings = HuggingFaceEndpointEmbeddings(
|
47 |
model=HF_EMBED_ENDPOINT,
|
48 |
task="feature-extraction",
|
49 |
huggingfacehub_api_token=HF_TOKEN,
|
50 |
)
|
51 |
+
|
52 |
vectorstore_path = "./data/vectorstore"
|
53 |
index_file = os.path.join(vectorstore_path, "index.faiss")
|
54 |
|
|
|
73 |
vectorstore.add_documents(batch)
|
74 |
vectorstore.save_local(vectorstore_path)
|
75 |
print("Vectorstore created and saved")
|
76 |
+
|
77 |
+
|
78 |
hf_retriever = vectorstore.as_retriever()
|
79 |
|
80 |
# -- AUGMENTED -- #
|
|
|
82 |
1. Define a String Template
|
83 |
2. Create a Prompt Template from the String Template
|
84 |
"""
|
|
|
85 |
RAG_PROMPT_TEMPLATE = """\
|
86 |
<|start_header_id|>system<|end_header_id|>
|
87 |
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|>
|
|
|
96 |
<|start_header_id|>assistant<|end_header_id|>
|
97 |
"""
|
98 |
|
|
|
99 |
rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
|
100 |
|
101 |
# -- GENERATION -- #
|
102 |
"""
|
103 |
1. Create a HuggingFaceEndpoint for the LLM
|
104 |
"""
|
|
|
105 |
hf_llm = HuggingFaceEndpoint(
|
106 |
endpoint_url=HF_LLM_ENDPOINT,
|
107 |
max_new_tokens=512,
|
108 |
top_k=10,
|
109 |
top_p=0.95,
|
110 |
+
temperature=0.3,
|
111 |
+
repetition_penalty=1.15,
|
112 |
huggingfacehub_api_token=HF_TOKEN,
|
113 |
)
|
114 |
|
|
|
134 |
The user session is a dictionary that is unique to each user session, and is stored in the memory of the server.
|
135 |
"""
|
136 |
|
|
|
137 |
lcel_rag_chain = (
|
138 |
{"context": itemgetter("query") | hf_retriever, "query": itemgetter("query")}
|
139 |
| rag_prompt | hf_llm
|
|
|
154 |
|
155 |
msg = cl.Message(content="")
|
156 |
|
157 |
+
for chunk in await cl.make_async(lcel_rag_chain.stream)(
|
158 |
{"query": message.content},
|
159 |
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
160 |
):
|