Spaces:
Runtime error
Runtime error
Update chat_engine.py
Browse files- chat_engine.py +73 -0
chat_engine.py
CHANGED
|
@@ -178,5 +178,78 @@ def get_chat_engine(files, api_type, progress=gr.Progress()):
|
|
| 178 |
similarity_top_k=15)
|
| 179 |
progress(1, desc="LLM Created")
|
| 180 |
return chat_engine, query_engine, "LLM Created"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
|
|
|
|
| 178 |
similarity_top_k=15)
|
| 179 |
progress(1, desc="LLM Created")
|
| 180 |
return chat_engine, query_engine, "LLM Created"
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
def get_new_chat_engine(files, api_type):
|
| 184 |
+
|
| 185 |
+
llm = get_api_type(api_type)
|
| 186 |
+
Settings.llm = llm
|
| 187 |
+
embed_model = MistralAIEmbedding(model_name='mistral-embed', api_key=mistral_api_key)
|
| 188 |
+
Settings.embed_model = embed_model
|
| 189 |
+
|
| 190 |
+
documents = SimpleDirectoryReader(input_files=files).load_data()
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
splitter = TokenTextSplitter(
|
| 194 |
+
chunk_size=1024,
|
| 195 |
+
chunk_overlap=20,
|
| 196 |
+
separator=" ",
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
nodes = splitter.get_nodes_from_documents(documents)
|
| 200 |
+
index = VectorStoreIndex(nodes)
|
| 201 |
+
|
| 202 |
+
chat_text_qa_msgs = [
|
| 203 |
+
ChatMessage(
|
| 204 |
+
role=MessageRole.SYSTEM,
|
| 205 |
+
content=(
|
| 206 |
+
"""
|
| 207 |
+
% You are an expert on developing websites for contractors and explaining your expertise to a general audience.
|
| 208 |
+
% If a character or word limit is mentioned in the prompt, ADHERE TO IT.
|
| 209 |
+
% For example, if a user wants a summary of a business less than 750 characters, the summary must be less than 750 characters.
|
| 210 |
+
"""
|
| 211 |
+
|
| 212 |
+
),
|
| 213 |
+
),
|
| 214 |
+
ChatMessage(
|
| 215 |
+
role=MessageRole.USER,
|
| 216 |
+
content=(
|
| 217 |
+
"""
|
| 218 |
+
% You are an expert on developing websites for contractors and explaining your expertise to a general audience.
|
| 219 |
+
% Goal: Given the Context below, give a detailed and thorough answer to the following question without mentioning where you found the answer: {query_str}
|
| 220 |
+
|
| 221 |
+
% Context:
|
| 222 |
+
```{context_str}```
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
% Instructions:"
|
| 226 |
+
Answer in a friendly manner.
|
| 227 |
+
Do not answer any questions that have no relevance to the context provided.
|
| 228 |
+
Do not include any instructions in your response.
|
| 229 |
+
Do not mention the context provided in your answer
|
| 230 |
+
ANSWER WITHOUT MENTIONING THE PROVIDED DOCUMENTS
|
| 231 |
+
YOU ARE NOT PERMITTED TO GIVE PAGE NUMBERS IN YOUR ANSWER UNDER ANY CIRCUMSTANCE
|
| 232 |
+
"""
|
| 233 |
+
),
|
| 234 |
+
),
|
| 235 |
+
]
|
| 236 |
+
|
| 237 |
+
text_qa_template = ChatPromptTemplate(chat_text_qa_msgs)
|
| 238 |
+
|
| 239 |
+
reorder = LongContextReorder()
|
| 240 |
+
# postprocessor = SimilarityPostprocessor(similarity_cutoff=0.7)
|
| 241 |
+
rerank = RankGPTRerank(top_n=5, llm=OpenAI(model="gpt-4o-mini"))
|
| 242 |
+
chat_engine = index.as_chat_engine('condense_plus_context',
|
| 243 |
+
text_qa_prompt=text_qa_template,
|
| 244 |
+
node_postprocessors=[
|
| 245 |
+
reorder,
|
| 246 |
+
MetadataReplacementPostProcessor(target_metadata_key="window"),
|
| 247 |
+
rerank
|
| 248 |
+
],
|
| 249 |
+
similarity_top_k=15,
|
| 250 |
+
streaming=True)
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
return chat_engine
|
| 254 |
|
| 255 |
|