Update app.py
Browse files
app.py
CHANGED
|
@@ -23,13 +23,95 @@ collection = client.get_collection(name="chromatsc", embedding_function=sentence
|
|
| 23 |
|
| 24 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
def
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
def
|
|
|
|
|
|
|
| 33 |
temperature = float(temperature)
|
| 34 |
if temperature < 1e-2: temperature = 1e-2
|
| 35 |
top_p = float(top_p)
|
|
@@ -41,25 +123,42 @@ def response(prompt, history, selected_option, temperature=0.9, max_new_tokens=5
|
|
| 41 |
do_sample=True,
|
| 42 |
seed=42,
|
| 43 |
)
|
| 44 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
selected_option = "Die Frage bezieht sich auf keine These speziell und ist somit allgemeiner zu behandeln."
|
| 46 |
else:
|
| 47 |
-
selected_option = "Die Nutzer Frage bezieht sich auf folgende Wahl-O-Mat These: " +
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
)
|
| 54 |
-
|
| 55 |
-
results
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
combination = [' '.join(triplets) for triplets in combination]
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
formatted_prompt = format_prompt(system
|
| 63 |
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 64 |
output = ""
|
| 65 |
for response in stream:
|
|
|
|
| 23 |
|
| 24 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 25 |
|
| 26 |
+
def format_prompt(message):
|
| 27 |
+
prompt = "" #"<s>"
|
| 28 |
+
#for user_prompt, bot_response in history:
|
| 29 |
+
# prompt += f"[INST] {user_prompt} [/INST]"
|
| 30 |
+
# prompt += f" {bot_response}</s> "
|
| 31 |
+
prompt += f"[INST] {message} [/INST]"
|
| 32 |
+
return prompt
|
| 33 |
+
|
| 34 |
+
def responsevote(
|
| 35 |
+
prompt, temperature=0.9, max_new_tokens=5, top_p=0.95, repetition_penalty=1.0,
|
| 36 |
+
):
|
| 37 |
+
temperature = float(temperature)
|
| 38 |
+
if temperature < 1e-2: temperature = 1e-2
|
| 39 |
+
top_p = float(top_p)
|
| 40 |
+
generate_kwargs = dict(
|
| 41 |
+
temperature=temperature,
|
| 42 |
+
max_new_tokens=max_new_tokens,
|
| 43 |
+
top_p=top_p,
|
| 44 |
+
repetition_penalty=repetition_penalty,
|
| 45 |
+
do_sample=True,
|
| 46 |
+
seed=42,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
system="Evaluiere, ob die Nutzer Frage, darauf abziehlt, dass du eine Wahlempfehlung wen man wählen sollte gibt. Sollte das der Fall sein antworte mit `Ja` \n\nUser-Anliegen:"
|
| 50 |
+
formatted_prompt = format_prompt(system+"\n"+prompt)
|
| 51 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 52 |
+
output = ""
|
| 53 |
+
for response in stream:
|
| 54 |
+
output += response.token.text
|
| 55 |
+
#print(output)
|
| 56 |
+
sentence_lower = output.lower()
|
| 57 |
+
# Check if the word 'nein' is in the sentence
|
| 58 |
+
if 'ja' in sentence_lower:
|
| 59 |
+
return True
|
| 60 |
+
else:
|
| 61 |
+
return False
|
| 62 |
+
|
| 63 |
+
def responsecritical(
|
| 64 |
+
prompt, temperature=0.9, max_new_tokens=5, top_p=0.95, repetition_penalty=1.0,
|
| 65 |
+
):
|
| 66 |
+
temperature = float(temperature)
|
| 67 |
+
if temperature < 1e-2: temperature = 1e-2
|
| 68 |
+
top_p = float(top_p)
|
| 69 |
+
generate_kwargs = dict(
|
| 70 |
+
temperature=temperature,
|
| 71 |
+
max_new_tokens=max_new_tokens,
|
| 72 |
+
top_p=top_p,
|
| 73 |
+
repetition_penalty=repetition_penalty,
|
| 74 |
+
do_sample=True,
|
| 75 |
+
seed=42,
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
system="Bitte evaluiere ob die Frage beleidigend oder auch sensibel ist. Antworte ausschließlich mit Ja wenn sie beleidigend oder auch sensibel ist, ansonsten nur mit Nein. Erkläre deine Entscheidung nicht.\n\nUser-Anliegen:"
|
| 79 |
+
formatted_prompt = format_prompt(system+"\n"+prompt)
|
| 80 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 81 |
+
output = ""
|
| 82 |
+
for response in stream:
|
| 83 |
+
output += response.token.text
|
| 84 |
+
#print(output)
|
| 85 |
+
sentence_lower = output.lower()
|
| 86 |
+
# Check if the word 'nein' is in the sentence
|
| 87 |
+
if 'ja' in sentence_lower:
|
| 88 |
+
return False
|
| 89 |
+
else:
|
| 90 |
+
return True
|
| 91 |
|
| 92 |
+
def contains_op(sentence):
|
| 93 |
+
# Liste der Pronomen der 1. Person im Deutschen
|
| 94 |
+
first_person_pronouns = ["du", "dich", "dir", "dein", "deine", "deinen", "deinem", "deiner"]
|
| 95 |
+
|
| 96 |
+
# Satz in Kleinbuchstaben umwandeln, um die Suche case-insensitive zu machen
|
| 97 |
+
sentence = sentence.lower()
|
| 98 |
+
|
| 99 |
+
# Überprüfen, ob eines der Pronomen der 1. Person im Satz vorkommt
|
| 100 |
+
for pronoun in first_person_pronouns:
|
| 101 |
+
if pronoun in sentence.split():
|
| 102 |
+
return True
|
| 103 |
+
|
| 104 |
+
return False
|
| 105 |
|
| 106 |
+
def is_single_word_question(question):
|
| 107 |
+
# Frage in Wörter aufteilen
|
| 108 |
+
words = question.split()
|
| 109 |
+
# Überprüfen, ob nur ein Wort vorhanden ist
|
| 110 |
+
return len(words) == 1
|
| 111 |
|
| 112 |
+
def responsefull(
|
| 113 |
+
prompt, statemementval1, these1,temperature=0.9, max_new_tokens=500, top_p=0.95, repetition_penalty=1.0,
|
| 114 |
+
):
|
| 115 |
temperature = float(temperature)
|
| 116 |
if temperature < 1e-2: temperature = 1e-2
|
| 117 |
top_p = float(top_p)
|
|
|
|
| 123 |
do_sample=True,
|
| 124 |
seed=42,
|
| 125 |
)
|
| 126 |
+
if is_single_word_question(prompt)==1:
|
| 127 |
+
return "Bitte spezifiziere deine Frage."
|
| 128 |
+
if responsecritical(prompt)==False:
|
| 129 |
+
return "Es scheint so, als sei dies keine Frage, die sich auf Bundestagswahl oder den Wahl-O-Maten bezieht"
|
| 130 |
+
|
| 131 |
+
if statemementval1 == 2:
|
| 132 |
selected_option = "Die Frage bezieht sich auf keine These speziell und ist somit allgemeiner zu behandeln."
|
| 133 |
else:
|
| 134 |
+
selected_option = "Die Nutzer Frage bezieht sich auf folgende Wahl-O-Mat These: " + these1
|
| 135 |
+
if contains_op(prompt)==True:
|
| 136 |
+
return "Als KI Assitenz kann ich dich nur beraten, jedoch bleibe ich neutral und werde keine Position einehmen. Frage mich aber geren etwas anders zum Wahl-O-Maten."
|
| 137 |
+
#time.sleep(10)
|
| 138 |
+
if responsevote(prompt)==True:
|
| 139 |
+
return "Als KI Assitenz bin ich politisch neutral. Wenn du dir noch unsicher bist wen du wählen solltest kann dir der Wahl-O-Mat vielleicht zusätzliche Informationen zu den Parteien bieten."
|
| 140 |
+
prompt= prompt + selected_option
|
| 141 |
+
addon=""
|
| 142 |
+
results=collection.query(
|
| 143 |
+
query_texts=[prompt],
|
| 144 |
+
n_results=60,
|
| 145 |
+
#where={"source": "google-docs"}
|
| 146 |
+
#where_document={"$contains":"search_string"}
|
| 147 |
)
|
| 148 |
+
#print("REsults")
|
| 149 |
+
#print(results)
|
| 150 |
+
#print("_____")
|
| 151 |
+
dists=["<br><small>(relevance: "+str(round((1-d)*100)/100)+";" for d in results['distances'][0]]
|
| 152 |
+
|
| 153 |
+
#sources=["source: "+s["source"]+")</small>" for s in results['metadatas'][0]]
|
| 154 |
+
results=results['documents'][0]
|
| 155 |
+
combination = zip(results,dists)
|
| 156 |
combination = [' '.join(triplets) for triplets in combination]
|
| 157 |
+
#print(str(prompt)+"\n\n"+str(combination))
|
| 158 |
+
if(len(results)>1):
|
| 159 |
+
addon="Bitte berücksichtige bei deiner Antwort ausschießlich folgende Auszüge aus unserer Wahlhilfe Datenbank, sofern sie für die Antwort erforderlich sind. Beantworte die Frage knapp und präzise. Ignoriere unpassende Datenbank-Auszüge OHNE sie zu kommentieren, zu erwähnen oder aufzulisten:\n"+"\n".join(results)
|
| 160 |
+
system="Du heißt Avah und bist ein deutschsprachiges KI-basiertes Wahlhilfe Beratungssystem zum Wahl-O-Maten für die Bundestagswahl 2021."+addon+"\n\nUser-Anliegen:"
|
| 161 |
+
formatted_prompt = format_prompt(system+"\n"+prompt)
|
| 162 |
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 163 |
output = ""
|
| 164 |
for response in stream:
|