Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -159,15 +159,35 @@ def rag_chain(llm, prompt, db):
|
|
159 |
result = rag_chain({"query": prompt})
|
160 |
return result["result"]
|
161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
###################################################
|
163 |
#Funktion von Gradio aus, die den dort eingegebenen Prompt annimmt und weiterverarbeitet
|
164 |
def invoke (prompt, history, openai_api_key, rag_option, temperature=0.9, max_new_tokens=512, top_p=0.6, repetition_penalty=1.3,):
|
165 |
global splittet
|
166 |
|
167 |
-
#Prompt an history anhängen
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
if (openai_api_key == "" or openai_api_key == "sk-"):
|
172 |
#raise gr.Error("OpenAI API Key is required.")
|
173 |
#eigenen OpenAI key nutzen
|
@@ -187,57 +207,32 @@ def invoke (prompt, history, openai_api_key, rag_option, temperature=0.9, max_ne
|
|
187 |
if not splittet:
|
188 |
splits = document_loading_splitting()
|
189 |
document_storage_chroma(splits)
|
190 |
-
db = document_retrieval_chroma(llm,
|
191 |
-
result = rag_chain(llm,
|
192 |
elif (rag_option == "MongoDB"):
|
193 |
#splits = document_loading_splitting()
|
194 |
#document_storage_mongodb(splits)
|
195 |
-
db = document_retrieval_mongodb(llm,
|
196 |
-
result = rag_chain(llm,
|
197 |
else:
|
198 |
-
result = llm_chain(llm,
|
|
|
199 |
except Exception as e:
|
200 |
raise gr.Error(e)
|
201 |
|
202 |
-
#Antwort als Stream ausgeben...
|
203 |
-
|
204 |
-
for character in result:
|
205 |
-
history[-1][1] += character
|
206 |
time.sleep(0.05)
|
207 |
-
yield
|
208 |
|
209 |
-
return result
|
210 |
-
|
211 |
################################################
|
212 |
#GUI
|
213 |
###############################################
|
214 |
#Beschreibung oben in GUI
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
<a href='""" + YOUTUBE_URL_1 + """'>YouTube</a>, <a href='""" + PDF_URL + """'>PDF</a>, and <a href='""" + WEB_URL + """'>Web.</a> <br>
|
220 |
-
Alle neueren Datums!.
|
221 |
-
<ul style="list-style-type:square;">
|
222 |
-
<li>Setze "Retrieval Augmented Generation" auf "<strong>Off</strong>" und gib einen Prompt ein." Das entspricht <strong> ein LLM nutzen ohne RAG</strong></li>
|
223 |
-
<li>Setze "Retrieval Augmented Generation" to "<strong>Chroma</strong>" und gib einen Prompt ein. Das <strong>LLM mit RAG</strong> weiß auch Antworten zu aktuellen Themen aus den angefügten Datenquellen</li>
|
224 |
-
<li>Experimentiere mit Prompts, z.B. Antworte in deutsch, englisch, ..." oder "schreibe ein Python Programm, dass die GPT-4 API aufruft."</li>
|
225 |
-
</ul>\n\n
|
226 |
-
"""
|
227 |
-
"""
|
228 |
-
#Gradio......
|
229 |
-
gr.close_all()
|
230 |
-
demo = gr.Interface(fn=invoke,
|
231 |
-
inputs = [gr.Textbox(label = "OpenAI API Key", value = "sk-", lines = 1),
|
232 |
-
#gr.Radio(["Off", "Chroma", "MongoDB"], label="Retrieval Augmented Generation", value = "Off"),
|
233 |
-
gr.Radio(["Off", "Chroma"], label="Retrieval Augmented Generation", value = "Off"),
|
234 |
-
gr.Textbox(label = "Prompt", value = "What is GPT-4?", lines = 1)],
|
235 |
-
outputs = [gr.Textbox(label = "Completion", lines = 1)],
|
236 |
-
title = "Generative AI - LLM & RAG",
|
237 |
-
description = description)
|
238 |
-
demo.launch()
|
239 |
-
"""
|
240 |
-
|
241 |
|
242 |
###########################################
|
243 |
title = "LLM mit RAG"
|
@@ -270,55 +265,9 @@ additional_inputs = [
|
|
270 |
]
|
271 |
|
272 |
|
273 |
-
gr.ChatInterface(fn=invoke,
|
274 |
#additional_inputs = additional_inputs,
|
275 |
title = "Generative AI - LLM & RAG",
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
"""
|
295 |
-
|
296 |
-
chatbot_stream = gr.Chatbot(avatar_images=(
|
297 |
-
"https://drive.google.com/uc?id=18xKoNOHN15H_qmGhK__VKnGjKjirrquW",
|
298 |
-
"https://drive.google.com/uc?id=1tfELAQW_VbPCy6QTRbexRlwAEYo8rSSv"
|
299 |
-
), bubble_full_width = False)
|
300 |
-
|
301 |
-
chat_interface_stream = gr.Interface(fn=invoke,
|
302 |
-
inputs = [gr.Textbox(label = "OpenAI API Key", value = "sk-", lines = 1),
|
303 |
-
#gr.Radio(["Off", "Chroma", "MongoDB"], label="Retrieval Augmented Generation", value = "Off"),
|
304 |
-
gr.Radio(["Off", "Chroma"], label="Retrieval Augmented Generation", value = "Off"),
|
305 |
-
gr.Textbox(label = "Prompt", value = "What is GPT-4?", lines = 1),
|
306 |
-
gr.Slider(label="Temperature", value=0.9, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Höhere Werte erzeugen diversere Antworten"),
|
307 |
-
gr.Slider(label="Max new tokens", value=256, minimum=0, maximum=4096, step=64, interactive=True, info="Maximale Anzahl neuer Tokens"),
|
308 |
-
gr.Slider(label="Top-p (nucleus sampling)", value=0.6, minimum=0.0, maximum=1, step=0.05, interactive=True, info="Höhere Werte verwenden auch Tokens mit niedrigerer Wahrscheinlichkeit."),
|
309 |
-
gr.Slider(label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Strafe für wiederholte Tokens")
|
310 |
-
],
|
311 |
-
outputs = [chatbot_stream],
|
312 |
-
title = "Generative AI - LLM & RAG",
|
313 |
-
description = description)
|
314 |
-
|
315 |
-
with gr.Blocks() as demo:
|
316 |
-
with gr.Tab("General LLM"):
|
317 |
-
chatbot_stream.like(vote, None, None)
|
318 |
-
chat_interface_stream.render()
|
319 |
-
with gr.Row():
|
320 |
-
gr.Radio(["Off", "Chroma"], label="Retrieval Augmented Generation", value = "Off")
|
321 |
-
gr.Textbox(label = "OpenAI API Key", value = "sk-", lines = 1)
|
322 |
-
|
323 |
-
demo.queue( max_size=100).launch(debug=True)
|
324 |
-
"""
|
|
|
159 |
result = rag_chain({"query": prompt})
|
160 |
return result["result"]
|
161 |
|
162 |
+
###################################################
|
163 |
+
#Funktion, die einen Prompt mit der history zusammen erzeugt
|
164 |
+
def generate_prompt_with_history(text, history, max_length=2048):
|
165 |
+
#prompt = "The following is a conversation between a human and an AI assistant named Baize (named after a mythical creature in Chinese folklore). Baize is an open-source AI assistant developed by UCSD and Sun Yat-Sen University. The human and the AI assistant take turns chatting. Human statements start with [|Human|] and AI assistant statements start with [|AI|]. The AI assistant always provides responses in as much detail as possible, and in Markdown format. The AI assistant always declines to engage with topics, questions and instructions related to unethical, controversial, or sensitive issues. Complete the transcript in exactly that format.\n[|Human|]Hello!\n[|AI|]Hi!"
|
166 |
+
#prompt = "Das folgende ist eine Unterhaltung in deutsch zwischen einem Menschen und einem KI-Assistenten, der Baize genannt wird. Baize ist ein open-source KI-Assistent, der von UCSD entwickelt wurde. Der Mensch und der KI-Assistent chatten abwechselnd miteinander in deutsch. Die Antworten des KI Assistenten sind immer so ausführlich wie möglich und in Markdown Schreibweise und in deutscher Sprache. Wenn nötig übersetzt er sie ins Deutsche. Die Antworten des KI-Assistenten vermeiden Themen und Antworten zu unethischen, kontroversen oder sensiblen Themen. Die Antworten sind immer sehr höflich formuliert..\n[|Human|]Hallo!\n[|AI|]Hi!"
|
167 |
+
prompt=""
|
168 |
+
history = ["\n{}\n{}".format(x[0],x[1]) for x in history]
|
169 |
+
history.append("\n{}\n".format(text))
|
170 |
+
history_text = ""
|
171 |
+
flag = False
|
172 |
+
for x in history[::-1]:
|
173 |
+
history_text = x + history_text
|
174 |
+
flag = True
|
175 |
+
|
176 |
+
if flag:
|
177 |
+
return prompt+history_text
|
178 |
+
else:
|
179 |
+
return None
|
180 |
+
|
181 |
+
|
182 |
+
|
183 |
###################################################
|
184 |
#Funktion von Gradio aus, die den dort eingegebenen Prompt annimmt und weiterverarbeitet
|
185 |
def invoke (prompt, history, openai_api_key, rag_option, temperature=0.9, max_new_tokens=512, top_p=0.6, repetition_penalty=1.3,):
|
186 |
global splittet
|
187 |
|
188 |
+
#Prompt an history anhängen und einen Text daraus machen
|
189 |
+
history_text_und_prompt = generate_prompt_with_history(prompt, history)
|
190 |
+
|
|
|
191 |
if (openai_api_key == "" or openai_api_key == "sk-"):
|
192 |
#raise gr.Error("OpenAI API Key is required.")
|
193 |
#eigenen OpenAI key nutzen
|
|
|
207 |
if not splittet:
|
208 |
splits = document_loading_splitting()
|
209 |
document_storage_chroma(splits)
|
210 |
+
db = document_retrieval_chroma(llm, history_text_und_prompt)
|
211 |
+
result = rag_chain(llm, history_text_und_prompt, db)
|
212 |
elif (rag_option == "MongoDB"):
|
213 |
#splits = document_loading_splitting()
|
214 |
#document_storage_mongodb(splits)
|
215 |
+
db = document_retrieval_mongodb(llm, history_text_und_prompt)
|
216 |
+
result = rag_chain(llm, history_text_und_prompt, db)
|
217 |
else:
|
218 |
+
result = llm_chain(llm, history_text_und_prompt)
|
219 |
+
|
220 |
except Exception as e:
|
221 |
raise gr.Error(e)
|
222 |
|
223 |
+
#Antwort als Stream ausgeben...
|
224 |
+
for i in range(len(result)):
|
|
|
|
|
225 |
time.sleep(0.05)
|
226 |
+
yield result[: i+1]
|
227 |
|
|
|
|
|
228 |
################################################
|
229 |
#GUI
|
230 |
###############################################
|
231 |
#Beschreibung oben in GUI
|
232 |
+
################################################
|
233 |
+
#GUI
|
234 |
+
###############################################
|
235 |
+
#Beschreibung oben in GUI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
236 |
|
237 |
###########################################
|
238 |
title = "LLM mit RAG"
|
|
|
265 |
]
|
266 |
|
267 |
|
268 |
+
demo1 = gr.ChatInterface(fn=invoke,
|
269 |
#additional_inputs = additional_inputs,
|
270 |
title = "Generative AI - LLM & RAG",
|
271 |
+
theme="soft",
|
272 |
+
additional_inputs=additional_inputs,
|
273 |
+
description = description).queue().launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|