Chris4K commited on
Commit
62600e4
1 Parent(s): 2770f0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -3
app.py CHANGED
@@ -1,3 +1,6 @@
 
 
 
1
  #####################################
2
  ## BitsAndBytes
3
  #####################################
@@ -12,7 +15,7 @@ model_name = "bn22/Mistral-7B-Instruct-v0.1-sharded"
12
  # "HuggingFaceH4/zephyr-7b-beta"
13
 
14
  # function for loading 4-bit quantized model
15
- def load_quantized_model(model_name: str):
16
 
17
  model = HuggingFaceHub(
18
  repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
@@ -118,7 +121,7 @@ def get_context_retriever_chain(vector_store):
118
  # function for loading 4-bit quantized model
119
 
120
 
121
- llm = load_quantized_model(model_name)
122
 
123
  retriever = vector_store.as_retriever()
124
 
@@ -134,7 +137,7 @@ def get_context_retriever_chain(vector_store):
134
 
135
  def get_conversational_rag_chain(retriever_chain):
136
 
137
- llm = load_quantized_model(model_name)
138
 
139
  prompt = ChatPromptTemplate.from_messages([
140
  ("system", "Du bist ein freundlicher Mitarbeiter einens Call Center und beantwortest basierend auf dem Context. Benutze nur den Inhalt des Context. Antworte mit: Ich bin mir nicht sicher. Wenn die Antwort nicht aus dem Context hervorgeht. Antworte auf Deutsch, bitte? CONTEXT:\n\n{context}"),
@@ -195,6 +198,47 @@ def get_response(user_input):
195
 
196
  return response['answer']
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  def simple(text:str):
199
  return text +" hhhmmm "
200
 
 
1
+
2
+
3
+
4
  #####################################
5
  ## BitsAndBytes
6
  #####################################
 
15
  # "HuggingFaceH4/zephyr-7b-beta"
16
 
17
  # function for loading 4-bit quantized model
18
+ def load_model(model_name: str):
19
 
20
  model = HuggingFaceHub(
21
  repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
 
121
  # function for loading 4-bit quantized model
122
 
123
 
124
+ llm = load_model(model_name)
125
 
126
  retriever = vector_store.as_retriever()
127
 
 
137
 
138
  def get_conversational_rag_chain(retriever_chain):
139
 
140
+ llm = load_model(model_name)
141
 
142
  prompt = ChatPromptTemplate.from_messages([
143
  ("system", "Du bist ein freundlicher Mitarbeiter einens Call Center und beantwortest basierend auf dem Context. Benutze nur den Inhalt des Context. Antworte mit: Ich bin mir nicht sicher. Wenn die Antwort nicht aus dem Context hervorgeht. Antworte auf Deutsch, bitte? CONTEXT:\n\n{context}"),
 
198
 
199
  return response['answer']
200
 
201
+
202
+ def history_to_dialog_format(chat_history: list[str]):
203
+ dialog = []
204
+ if len(chat_history) > 0:
205
+ for idx, message in enumerate(chat_history[0]):
206
+ role = "user" if idx % 2 == 0 else "assistant"
207
+ dialog.append({
208
+ "role": role,
209
+ "content": message,
210
+ })
211
+ return dialog
212
+
213
+ def get_response(message, history):
214
+ dialog = history_to_dialog_format(history)
215
+ dialog.append({"role": "user", "content": message})
216
+
217
+ # Define the prompt as a ChatPromptValue object
218
+ #user_input = ChatPromptValue(user_input)
219
+
220
+ # Convert the prompt to a tensor
221
+ #input_ids = user_input.tensor
222
+
223
+
224
+ #vs = get_vectorstore_from_url(user_url, all_domain)
225
+ vs = get_vectorstore_from_url("https://www.bofrost.de/shop/fertige-gerichte_5507/auflaeufe_5509/hack-wirsing-auflauf.html?position=1&clicked=")
226
+ print("------ here 22 " )
227
+ chat_history =[]
228
+ retriever_chain = get_context_retriever_chain(vs)
229
+ conversation_rag_chain = get_conversational_rag_chain(retriever_chain)
230
+
231
+ response = conversation_rag_chain.invoke({
232
+ "chat_history": chat_history,
233
+ "input": user_input
234
+ })
235
+ print(response)
236
+ return response[-1]['generation']['content']
237
+
238
+
239
+
240
+
241
+
242
  def simple(text:str):
243
  return text +" hhhmmm "
244