Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,8 @@ import io
|
|
| 9 |
from PIL import Image, ImageDraw, ImageOps, ImageFont
|
| 10 |
import base64
|
| 11 |
|
|
|
|
|
|
|
| 12 |
from langchain.chains import LLMChain, RetrievalQA
|
| 13 |
from langchain.chat_models import ChatOpenAI
|
| 14 |
from langchain.document_loaders import PyPDFLoader, WebBaseLoader, UnstructuredWordDocumentLoader, DirectoryLoader
|
|
@@ -208,11 +210,14 @@ def process_image(image_path, prompt):
|
|
| 208 |
|
| 209 |
##################################################
|
| 210 |
#openassistant um uploaded Files zu analysieren
|
| 211 |
-
def
|
| 212 |
global client, general_assistant
|
| 213 |
#neues File dem Assistant hinzufügen
|
| 214 |
file_neu = client.files.create(file=open(file,"rb",),purpose="assistants",)
|
| 215 |
# Update Assistant
|
|
|
|
|
|
|
|
|
|
| 216 |
updated_assistant = client.beta.assistants.update(general_assistant.id,tools=[{"type": "code_interpreter"}, {"type": "retrieval"}],file_ids=[file_neu.id],)
|
| 217 |
thread, run = create_thread_and_run(prompt, client, updated_assistant.id)
|
| 218 |
run = wait_on_run(run, thread, client)
|
|
@@ -220,6 +225,14 @@ def create_assistant(prompt, file):
|
|
| 220 |
result = response.data[1].content[0].text.value
|
| 221 |
return result
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
###################################################
|
| 224 |
#Funktion von Gradio aus, die den dort eingegebenen Prompt annimmt und weiterverarbeitet
|
| 225 |
###################################################
|
|
@@ -333,7 +346,7 @@ def generate_text_zu_doc(file, prompt, k, rag_option, chatbot, db):
|
|
| 333 |
#als reiner prompt:
|
| 334 |
prompt_neu = generate_prompt_with_history(neu_text_mit_chunks, chatbot)
|
| 335 |
|
| 336 |
-
result =
|
| 337 |
return result
|
| 338 |
|
| 339 |
|
|
@@ -396,7 +409,11 @@ def generate_text (prompt, chatbot, history, rag_option, model_option, openai_ap
|
|
| 396 |
splittet = False
|
| 397 |
print("LLM aufrufen ohne RAG: ...........")
|
| 398 |
result = llm_chain(llm, history_text_und_prompt)
|
| 399 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
except Exception as e:
|
| 401 |
raise gr.Error(e)
|
| 402 |
|
|
|
|
| 9 |
from PIL import Image, ImageDraw, ImageOps, ImageFont
|
| 10 |
import base64
|
| 11 |
|
| 12 |
+
from tavily import TavilyClient
|
| 13 |
+
|
| 14 |
from langchain.chains import LLMChain, RetrievalQA
|
| 15 |
from langchain.chat_models import ChatOpenAI
|
| 16 |
from langchain.document_loaders import PyPDFLoader, WebBaseLoader, UnstructuredWordDocumentLoader, DirectoryLoader
|
|
|
|
| 210 |
|
| 211 |
##################################################
|
| 212 |
#openassistant um uploaded Files zu analysieren
|
| 213 |
+
def create_assistant_file(prompt, file):
|
| 214 |
global client, general_assistant
|
| 215 |
#neues File dem Assistant hinzufügen
|
| 216 |
file_neu = client.files.create(file=open(file,"rb",),purpose="assistants",)
|
| 217 |
# Update Assistant
|
| 218 |
+
#wenn type: code_interpreter, wird das file mit angehängt an den Prpmt, aber vorher nicht bearbeitet
|
| 219 |
+
#wenn type: retrieval, wird das Dokument vorher embedded in einem vektorstore und nur entsprechende chunks mitgegeben.
|
| 220 |
+
#pro Assistant 20 cent pro Tag als Nutzung - egal wie viele Fragen dazu.
|
| 221 |
updated_assistant = client.beta.assistants.update(general_assistant.id,tools=[{"type": "code_interpreter"}, {"type": "retrieval"}],file_ids=[file_neu.id],)
|
| 222 |
thread, run = create_thread_and_run(prompt, client, updated_assistant.id)
|
| 223 |
run = wait_on_run(run, thread, client)
|
|
|
|
| 225 |
result = response.data[1].content[0].text.value
|
| 226 |
return result
|
| 227 |
|
| 228 |
+
##################################################
|
| 229 |
+
#openassistant um im Netz zu suchen
|
| 230 |
+
def create_assistant_suche(prompt):
|
| 231 |
+
global client, general_assistant
|
| 232 |
+
#neues File dem Assistant hinzufügen
|
| 233 |
+
|
| 234 |
+
return result
|
| 235 |
+
|
| 236 |
###################################################
|
| 237 |
#Funktion von Gradio aus, die den dort eingegebenen Prompt annimmt und weiterverarbeitet
|
| 238 |
###################################################
|
|
|
|
| 346 |
#als reiner prompt:
|
| 347 |
prompt_neu = generate_prompt_with_history(neu_text_mit_chunks, chatbot)
|
| 348 |
|
| 349 |
+
result = create_assistant_file(prompt_neu, file)
|
| 350 |
return result
|
| 351 |
|
| 352 |
|
|
|
|
| 409 |
splittet = False
|
| 410 |
print("LLM aufrufen ohne RAG: ...........")
|
| 411 |
result = llm_chain(llm, history_text_und_prompt)
|
| 412 |
+
|
| 413 |
+
#Wenn keine Antwort möglich "Ich weiß es nicht", dann versuchen mit Suche im Internet.
|
| 414 |
+
if (result == "Ich weiß es nicht."):
|
| 415 |
+
result = create_assistant_suche(history_text_und_prompt)
|
| 416 |
+
|
| 417 |
except Exception as e:
|
| 418 |
raise gr.Error(e)
|
| 419 |
|