Spaces:
Sleeping
Sleeping
Commit
·
5ca0261
1
Parent(s):
a4dec41
get answers with hf space
Browse files
app.py
CHANGED
|
@@ -154,6 +154,54 @@ def search_qdrant_with_context(query_text, collection_name, top_k=3):
|
|
| 154 |
print("Retrieved Text ", retrieved_texts)
|
| 155 |
|
| 156 |
return retrieved_texts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
@spaces.GPU
|
| 158 |
def respond(
|
| 159 |
query,
|
|
@@ -162,32 +210,33 @@ def respond(
|
|
| 162 |
temperature,
|
| 163 |
top_p,
|
| 164 |
):
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
| 175 |
# Retrieve relevant context from Qdrant
|
| 176 |
-
collection_name = "products"
|
| 177 |
-
if "rezept" in query.lower() or "gericht" in query.lower():
|
| 178 |
-
|
| 179 |
-
elif "bestellung" in query.lower() or "order" in query.lower():
|
| 180 |
-
|
| 181 |
|
| 182 |
|
| 183 |
-
context = search_qdrant_with_context(query + " " + refined_context[0]["generated_text"].split("assistant\n").pop(), collection_name)
|
| 184 |
-
answer = generate_response(query, context, last_messages, max_tokens, temperature, top_p, generator[0])
|
| 185 |
-
full_conv = f"<|im_start|>user {query}<|im_end|><|im_start|>assistent {answer}<|im_end|>"
|
| 186 |
-
if len(last_messages) > 5:
|
| 187 |
-
|
| 188 |
-
last_messages.append(full_conv)
|
| 189 |
-
print(last_messages)
|
| 190 |
-
return answer
|
| 191 |
|
| 192 |
"""
|
| 193 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
|
|
|
| 154 |
print("Retrieved Text ", retrieved_texts)
|
| 155 |
|
| 156 |
return retrieved_texts
|
| 157 |
+
|
| 158 |
+
@spaces.GPU
|
| 159 |
+
def interactive_chat(query):
|
| 160 |
+
|
| 161 |
+
collection_name = "products"
|
| 162 |
+
if "rezept" in query.lower() or "gericht" in query.lower():
|
| 163 |
+
collection_name = "recipes"
|
| 164 |
+
elif "bestellung" in query.lower() or "order" in query.lower():
|
| 165 |
+
collection_name = "products"
|
| 166 |
+
print(collection_name)
|
| 167 |
+
print(query)
|
| 168 |
+
if len(query.split()) < 3:
|
| 169 |
+
return generate_response(query, "Der Kunde muss womöglich detailliertere Angaben machen, entscheide, was du sagst.", last_messages, 512, 0.2, 0.95, generator)
|
| 170 |
+
context = [document["document"] for document in search_qdrant_with_context(query, collection_name)]
|
| 171 |
+
|
| 172 |
+
system_message = f"""<|im_start|>system Rolle: Du bist ein KI-Assistent der die Informationen in Relation zum Kontext bewertet.
|
| 173 |
+
Oberstes Ziel: Bewerte die die Ergebnisse und stufe sie nach Relevanz in Bezug auf die Konversation ein.
|
| 174 |
+
Meta-Anweisung: Analysiere die Konversation und mache Vorschläge für Suchbegriffe in Stichpunkten.
|
| 175 |
+
Suchergebnisse: {context}
|
| 176 |
+
<|im_end|>
|
| 177 |
+
{"".join(last_messages)}
|
| 178 |
+
<|im_start|>user
|
| 179 |
+
{query}
|
| 180 |
+
<|im_end|>
|
| 181 |
+
<|im_start|>assistant"""
|
| 182 |
+
refined_context = generator_mini(system_message, do_sample=True, padding=True, truncation=True, top_p=0.95, max_new_tokens=100)
|
| 183 |
+
# Retrieve relevant context from Qdrant
|
| 184 |
+
print(f"""Refined context: {refined_context[0]["generated_text"].split("assistant").pop()}""")
|
| 185 |
+
|
| 186 |
+
context = [document["document"] for document in search_qdrant_with_context(query + " " + refined_context[0]["generated_text"].split("assistant\n").pop(), collection_name)]
|
| 187 |
+
answer = generate_response(query, context, last_messages, 512, 0.2, 0.95, generator)
|
| 188 |
+
full_conv = f"<|im_start|>user {query}<|im_end|><|im_start|>assistent {answer}<|im_end|>"
|
| 189 |
+
# if len(last_messages) > 5:
|
| 190 |
+
# last_messages.pop(0)
|
| 191 |
+
# last_messages.append(full_conv)
|
| 192 |
+
print(f"last messages: {last_messages}")
|
| 193 |
+
print()
|
| 194 |
+
return answer
|
| 195 |
+
@spaces.GPU
|
| 196 |
+
def get_answers():
|
| 197 |
+
answers = []
|
| 198 |
+
last_messages = []
|
| 199 |
+
with open("../Data/questions.json", "r", encoding="utf-8")as f:
|
| 200 |
+
json_data = json.load(f)["questions"]
|
| 201 |
+
for question in json_data:
|
| 202 |
+
answer = interactive_chat(question)
|
| 203 |
+
answers.append(answer)
|
| 204 |
+
|
| 205 |
@spaces.GPU
|
| 206 |
def respond(
|
| 207 |
query,
|
|
|
|
| 210 |
temperature,
|
| 211 |
top_p,
|
| 212 |
):
|
| 213 |
+
get_answers()
|
| 214 |
+
#generator = get_model()
|
| 215 |
+
#system_message = f"""<|im_start|>system Rolle: Du bist ein KI-Assistent der die vom Kunden formuliert Frage in Stichworte verwandelt die für eine Vektorsuche verwendet werden.
|
| 216 |
+
# Oberstes Ziel: Suche Schlüsselbegriffe aus der Frage heraus und gebe diese als Hauptbegriff aus. Suche zusätzlich ähnliche Begriffe aus.
|
| 217 |
+
# Meta-Anweisung: Wenn nach Produkten beziehungsweise Gewürzen gefragt wird, suche ähnliche Eigenschaften. Wenn nach einem Rezept gefragt ist, versuche die Küche beziehungsweise regionale Abstammung herauszufinden und als Schlüsselbegriff ausgeben. Gebe die vermutete Abstammung wie folgt aus: "Küche: ''". Du bekommst maximal 5 vorherige Fragen und Antworten aus dem Gespräch als Kontext. Wenn du keine exakten antworten geben kannst, geb nur Schlüsselbegriffe aus der Frage und den vorherigen wieder. Antworte in maximal 3 Stichpunkten und gebe keine Beschreibung.
|
| 218 |
+
# <|im_end|>
|
| 219 |
+
# <|im_start|>user
|
| 220 |
+
# Frage: {query}
|
| 221 |
+
# <|im_end|>
|
| 222 |
+
# <|im_start|>assistant"""
|
| 223 |
+
#refined_context = generator[1](system_message, do_sample=True, padding=True, truncation=True, top_p=0.95, max_new_tokens=150)
|
| 224 |
# Retrieve relevant context from Qdrant
|
| 225 |
+
#collection_name = "products"
|
| 226 |
+
#if "rezept" in query.lower() or "gericht" in query.lower():
|
| 227 |
+
# collection_name = "recipes"
|
| 228 |
+
#elif "bestellung" in query.lower() or "order" in query.lower():
|
| 229 |
+
# collection_name = "products"
|
| 230 |
|
| 231 |
|
| 232 |
+
#context = search_qdrant_with_context(query + " " + refined_context[0]["generated_text"].split("assistant\n").pop(), collection_name)
|
| 233 |
+
#answer = generate_response(query, context, last_messages, max_tokens, temperature, top_p, generator[0])
|
| 234 |
+
#full_conv = f"<|im_start|>user {query}<|im_end|><|im_start|>assistent {answer}<|im_end|>"
|
| 235 |
+
#if len(last_messages) > 5:
|
| 236 |
+
# last_messages.pop(0)
|
| 237 |
+
#last_messages.append(full_conv)
|
| 238 |
+
#print(last_messages)
|
| 239 |
+
#return answer
|
| 240 |
|
| 241 |
"""
|
| 242 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|