Spaces:
Sleeping
Sleeping
MatteoScript
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -122,27 +122,32 @@ def gen_augmented_prompt(prompt, top_k) :
|
|
122 |
links = ""
|
123 |
embedding = HuggingFaceEmbeddings(model_name="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
124 |
db = Chroma(persist_directory='./DB_Decreti', embedding_function=embedding)
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
130 |
generated_prompt = f"""
|
131 |
A PARTIRE DAL SEGUENTE CONTESTO: {docs},
|
132 |
|
133 |
----
|
134 |
RISPONDI ALLA SEGUENTE RICHIESTA: {prompt}
|
135 |
"""
|
136 |
-
return
|
137 |
|
138 |
def generate_chat_stream(prompt) :
|
139 |
links = []
|
140 |
prompt_originale = prompt
|
141 |
-
prompt = formattaPrompt(prompt, st.session_state.systemRole, st.session_state.systemStyle, st.session_state.instruction)
|
142 |
if st.session_state.rag_enabled :
|
143 |
with st.spinner("Ricerca nei documenti...."):
|
144 |
time.sleep(1)
|
145 |
-
|
|
|
146 |
print(prompt)
|
147 |
with st.spinner("Generazione in corso...") :
|
148 |
time.sleep(1)
|
@@ -162,8 +167,9 @@ def stream_handler(chat_stream, placeholder) :
|
|
162 |
|
163 |
def show_source(links) :
|
164 |
with st.expander("Mostra fonti") :
|
165 |
-
for
|
166 |
-
|
|
|
167 |
|
168 |
init_state()
|
169 |
sidebar()
|
|
|
122 |
links = ""
|
123 |
embedding = HuggingFaceEmbeddings(model_name="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
124 |
db = Chroma(persist_directory='./DB_Decreti', embedding_function=embedding)
|
125 |
+
docs = db.similarity_search(prompt, k=top_k)
|
126 |
+
|
127 |
+
links = []
|
128 |
+
context = ''
|
129 |
+
NomeCartellaOriginariaDB = 'Documenti_2\\'
|
130 |
+
for doc in docs:
|
131 |
+
testo = doc.page_content.replace('\n', ' ')
|
132 |
+
context += testo + '\n\n\n'
|
133 |
+
reference = doc.metadata["source"].replace(NomeCartellaOriginariaDB, '') + ' (Pag. ' + str(doc.metadata["page"]) + ')'
|
134 |
+
links.append((reference, testo))
|
135 |
generated_prompt = f"""
|
136 |
A PARTIRE DAL SEGUENTE CONTESTO: {docs},
|
137 |
|
138 |
----
|
139 |
RISPONDI ALLA SEGUENTE RICHIESTA: {prompt}
|
140 |
"""
|
141 |
+
return context, links
|
142 |
|
143 |
def generate_chat_stream(prompt) :
|
144 |
links = []
|
145 |
prompt_originale = prompt
|
|
|
146 |
if st.session_state.rag_enabled :
|
147 |
with st.spinner("Ricerca nei documenti...."):
|
148 |
time.sleep(1)
|
149 |
+
st.session_state.instruction, links = gen_augmented_prompt(prompt=prompt_originale, top_k=st.session_state.top_k)
|
150 |
+
prompt = formattaPrompt(prompt, st.session_state.systemRole, st.session_state.systemStyle, st.session_state.instruction)
|
151 |
print(prompt)
|
152 |
with st.spinner("Generazione in corso...") :
|
153 |
time.sleep(1)
|
|
|
167 |
|
168 |
def show_source(links) :
|
169 |
with st.expander("Mostra fonti") :
|
170 |
+
for link in links:
|
171 |
+
reference, testo = link
|
172 |
+
st.info(reference + '\n\n'+ testo)
|
173 |
|
174 |
init_state()
|
175 |
sidebar()
|