alexkueck commited on
Commit
2d98523
1 Parent(s): 04ceb17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -19,6 +19,7 @@ from langchain.embeddings import HuggingFaceInstructEmbeddings, HuggingFaceEmbed
19
  from langchain.prompts import PromptTemplate
20
  from langchain.embeddings.openai import OpenAIEmbeddings
21
  from langchain.text_splitter import RecursiveCharacterTextSplitter
 
22
  from langchain.vectorstores import Chroma
23
  from chromadb.errors import InvalidDimensionException
24
 
@@ -198,7 +199,10 @@ def document_retrieval_chroma():
198
  #ChromaDb um die embedings zu speichern
199
  db = Chroma(embedding_function = embeddings, persist_directory = PATH_WORK + CHROMA_DIR)
200
  print ("Chroma DB bereit ...................")
201
- return db
 
 
 
202
 
203
 
204
 
@@ -213,10 +217,10 @@ def llm_chain(prompt):
213
 
214
  #langchain nutzen, um prompt an llm zu leiten, aber vorher in der VektorDB suchen, um passende splits zum Prompt hinzuzufügen
215
  #prompt mit RAG!!!
216
- def rag_chain(prompt, db):
217
  rag_template = "Nutze die folgenden Kontext Teile am Ende, um die Frage zu beantworten . " + template + "Frage: " + prompt + "Kontext Teile: "
218
- retriever = db.as_retriever()
219
- retrieved_chunks = retriever.search(prompt, k=3)
220
  neu_prompt = rag_template
221
  for i, chunk in enumerate(retrieved_chunks):
222
  neu_prompt += f"{i+1}. {chunk}\n"
@@ -276,9 +280,9 @@ def generate(text, history, rag_option, model_option, temperature=0.5, max_new_
276
  if not splittet:
277
  splits = document_loading_splitting()
278
  document_storage_chroma(splits)
279
- db = document_retrieval_chroma()
280
  #mit RAG:
281
- neu_text_mit_chunks = rag_chain(text, db)
282
  #für Chat LLM:
283
  #prompt = generate_prompt_with_history_openai(neu_text_mit_chunks, history)
284
  #als reiner prompt:
 
19
  from langchain.prompts import PromptTemplate
20
  from langchain.embeddings.openai import OpenAIEmbeddings
21
  from langchain.text_splitter import RecursiveCharacterTextSplitter
22
+ from langchain.retrievers.self_query.base import SelfQueryRetriever
23
  from langchain.vectorstores import Chroma
24
  from chromadb.errors import InvalidDimensionException
25
 
 
199
  #ChromaDb um die embedings zu speichern
200
  db = Chroma(embedding_function = embeddings, persist_directory = PATH_WORK + CHROMA_DIR)
201
  print ("Chroma DB bereit ...................")
202
+ llm = OpenAI(temperature=0.5)
203
+ retriever = SelfQueryRetriever.from_llm(llm,vectorstore,document_content_description,metadata_field_info,enable_limit=True,verbose=True,)
204
+
205
+ return db, retriever
206
 
207
 
208
 
 
217
 
218
  #langchain nutzen, um prompt an llm zu leiten, aber vorher in der VektorDB suchen, um passende splits zum Prompt hinzuzufügen
219
  #prompt mit RAG!!!
220
+ def rag_chain(prompt, db, retriever):
221
  rag_template = "Nutze die folgenden Kontext Teile am Ende, um die Frage zu beantworten . " + template + "Frage: " + prompt + "Kontext Teile: "
222
+ retrieved_chunks = retriever.get_relevant_documents(prompt)
223
+ print
224
  neu_prompt = rag_template
225
  for i, chunk in enumerate(retrieved_chunks):
226
  neu_prompt += f"{i+1}. {chunk}\n"
 
280
  if not splittet:
281
  splits = document_loading_splitting()
282
  document_storage_chroma(splits)
283
+ db, retriever = document_retrieval_chroma()
284
  #mit RAG:
285
+ neu_text_mit_chunks = rag_chain(text, db, retriever)
286
  #für Chat LLM:
287
  #prompt = generate_prompt_with_history_openai(neu_text_mit_chunks, history)
288
  #als reiner prompt: