Spaces:
Sleeping
Sleeping
update chunck
Browse files- app/configs/prompts.py +1 -1
- app/llm_handling.py +48 -8
- ui/chatbot_tab.py +1 -0
app/configs/prompts.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
SYSTEM_PROMPTS = {
|
2 |
# Supporto allo studio
|
3 |
-
"tutor": "Sei un tutor didattico di nome Valter. Usa questo contesto per rispondere: {context}",
|
4 |
|
5 |
# Analisi dati scientifici
|
6 |
"scientist": "Sei uno scienziato esperto. Analizza il contesto: {context}",
|
|
|
1 |
SYSTEM_PROMPTS = {
|
2 |
# Supporto allo studio
|
3 |
+
"tutor": "Sei un tutor didattico di nome Valter. Usa questo contesto per rispondere: {context}. Cita sempre il titolo e l'autore dei documenti da cui prendi le informazioni. Inoltre, ricorda sempre di andare ad approfondire l'argomento",
|
4 |
|
5 |
# Analisi dati scientifici
|
6 |
"scientist": "Sei uno scienziato esperto. Analizza il contesto: {context}",
|
app/llm_handling.py
CHANGED
@@ -10,12 +10,16 @@ import gradio as gr
|
|
10 |
import asyncio
|
11 |
import edge_tts
|
12 |
from pathlib import Path
|
|
|
|
|
|
|
13 |
|
14 |
from app.config import OPENAI_API_KEY
|
15 |
from app.functions.database_handling import BASE_DB_PATH # Aggiungi questo import
|
16 |
from app.configs.prompts import SYSTEM_PROMPTS
|
17 |
|
18 |
logging.basicConfig(level=logging.INFO)
|
|
|
19 |
|
20 |
class LLMType(Enum):
|
21 |
OPENAI_GPT_4O_MINI = "openai - GPT-4o-mini"
|
@@ -30,14 +34,14 @@ LLM_CONFIGS = {
|
|
30 |
"base_url": None
|
31 |
},
|
32 |
LLMType.LOCAL_QWEN: {
|
33 |
-
"client": lambda: OpenAI(base_url="http://192.168.
|
34 |
"model": "qwen2.5-coder-7b-instruct",
|
35 |
-
"base_url": "http://192.168.
|
36 |
},
|
37 |
LLMType.LOCAL_PHI: {
|
38 |
-
"client": lambda: OpenAI(base_url="http://192.168.
|
39 |
"model": "phi-3.5-mini-ita",
|
40 |
-
"base_url": "http://192.168.
|
41 |
}
|
42 |
}
|
43 |
|
@@ -83,7 +87,7 @@ def clean_markdown(text):
|
|
83 |
text = re.sub(r'```[\s\S]*?```', '', text) # blocchi codice
|
84 |
text = re.sub(r'`.*?`', '', text) # codice inline
|
85 |
text = re.sub(r'\[([^\]]+)\]\([^\)]+\)', r'\1', text) # link
|
86 |
-
text = re.sub(r'\*\*(.*?)
|
87 |
text = re.sub(r'\*(.*?)\*', r'\1', text) # italic
|
88 |
return text.strip()
|
89 |
|
@@ -130,6 +134,15 @@ def get_system_prompt(prompt_type="tutor"):
|
|
130 |
"""Seleziona il prompt di sistema appropriato"""
|
131 |
return SYSTEM_PROMPTS.get(prompt_type, SYSTEM_PROMPTS["tutor"])
|
132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
def answer_question(question, db_name, prompt_type="tutor", chat_history=None, llm_type=LLMType.OPENAI_GPT_4O_MINI):
|
134 |
"""
|
135 |
Risponde alla domanda 'question' usando i documenti del database 'db_name'.
|
@@ -163,11 +176,33 @@ def answer_question(question, db_name, prompt_type="tutor", chat_history=None, l
|
|
163 |
vectorstore = FAISS.load_local(db_path, embeddings, allow_dangerous_deserialization=True)
|
164 |
|
165 |
# Cerca i documenti (chunk) più simili
|
166 |
-
relevant_docs = vectorstore.similarity_search(question, k=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
-
# Logga i chunk recuperati
|
169 |
for idx, doc in enumerate(relevant_docs):
|
170 |
logging.info(f"--- Chunk {idx+1} ---")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
logging.info(doc.page_content)
|
172 |
logging.info("---------------------")
|
173 |
|
@@ -190,6 +225,10 @@ def answer_question(question, db_name, prompt_type="tutor", chat_history=None, l
|
|
190 |
{"role": "user", "content": question}
|
191 |
]
|
192 |
|
|
|
|
|
|
|
|
|
193 |
client, model = get_llm_client(llm_type)
|
194 |
response = client.chat.completions.create(
|
195 |
model=model,
|
@@ -210,9 +249,10 @@ def answer_question(question, db_name, prompt_type="tutor", chat_history=None, l
|
|
210 |
|
211 |
except Exception as e:
|
212 |
logging.error(f"Errore durante la generazione della risposta: {e}")
|
|
|
213 |
return [
|
214 |
{"role": "user", "content": question},
|
215 |
-
{"role": "assistant", "content": f"
|
216 |
]
|
217 |
|
218 |
|
|
|
10 |
import asyncio
|
11 |
import edge_tts
|
12 |
from pathlib import Path
|
13 |
+
import requests
|
14 |
+
from tenacity import retry, stop_after_attempt, wait_exponential
|
15 |
+
import json
|
16 |
|
17 |
from app.config import OPENAI_API_KEY
|
18 |
from app.functions.database_handling import BASE_DB_PATH # Aggiungi questo import
|
19 |
from app.configs.prompts import SYSTEM_PROMPTS
|
20 |
|
21 |
logging.basicConfig(level=logging.INFO)
|
22 |
+
local_ip="192.168.82.5:1234"
|
23 |
|
24 |
class LLMType(Enum):
|
25 |
OPENAI_GPT_4O_MINI = "openai - GPT-4o-mini"
|
|
|
34 |
"base_url": None
|
35 |
},
|
36 |
LLMType.LOCAL_QWEN: {
|
37 |
+
"client": lambda: OpenAI(base_url="http://192.168.82.5:1234/v1", api_key="not-needed"),
|
38 |
"model": "qwen2.5-coder-7b-instruct",
|
39 |
+
"base_url": "http://192.168.82.5:1234/v1"
|
40 |
},
|
41 |
LLMType.LOCAL_PHI: {
|
42 |
+
"client": lambda: OpenAI(base_url="http://192.168.82.5:1234/v1", api_key="not-needed"),
|
43 |
"model": "phi-3.5-mini-ita",
|
44 |
+
"base_url": "http://192.168.82.5:1234/v1"
|
45 |
}
|
46 |
}
|
47 |
|
|
|
87 |
text = re.sub(r'```[\s\S]*?```', '', text) # blocchi codice
|
88 |
text = re.sub(r'`.*?`', '', text) # codice inline
|
89 |
text = re.sub(r'\[([^\]]+)\]\([^\)]+\)', r'\1', text) # link
|
90 |
+
text = re.sub(r'\*\*(.*?)\*\*\*', r'\1', text) # bold
|
91 |
text = re.sub(r'\*(.*?)\*', r'\1', text) # italic
|
92 |
return text.strip()
|
93 |
|
|
|
134 |
"""Seleziona il prompt di sistema appropriato"""
|
135 |
return SYSTEM_PROMPTS.get(prompt_type, SYSTEM_PROMPTS["tutor"])
|
136 |
|
137 |
+
def test_local_connection():
|
138 |
+
"""Verifica la connessione al server LLM locale"""
|
139 |
+
try:
|
140 |
+
response = requests.get(f"http://192.168.82.5:1234/v1/health", timeout=5)
|
141 |
+
return response.status_code == 200
|
142 |
+
except:
|
143 |
+
return False
|
144 |
+
|
145 |
+
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
|
146 |
def answer_question(question, db_name, prompt_type="tutor", chat_history=None, llm_type=LLMType.OPENAI_GPT_4O_MINI):
|
147 |
"""
|
148 |
Risponde alla domanda 'question' usando i documenti del database 'db_name'.
|
|
|
176 |
vectorstore = FAISS.load_local(db_path, embeddings, allow_dangerous_deserialization=True)
|
177 |
|
178 |
# Cerca i documenti (chunk) più simili
|
179 |
+
relevant_docs = vectorstore.similarity_search(question, k=5)
|
180 |
+
|
181 |
+
# Leggi il file metadata.json
|
182 |
+
metadata_file = os.path.join(db_path, "metadata.json")
|
183 |
+
metadata_dict = {}
|
184 |
+
if os.path.exists(metadata_file):
|
185 |
+
with open(metadata_file, 'r') as f:
|
186 |
+
metadata_list = json.load(f)
|
187 |
+
# Crea un dizionario per lookup veloce usando il filename come chiave
|
188 |
+
metadata_dict = {m["filename"]: m for m in metadata_list}
|
189 |
|
190 |
+
# Logga i chunk recuperati con i loro metadata
|
191 |
for idx, doc in enumerate(relevant_docs):
|
192 |
logging.info(f"--- Chunk {idx+1} ---")
|
193 |
+
# Recupera i metadata dal documento
|
194 |
+
source_file = doc.metadata.get("source", "Unknown")
|
195 |
+
chunk_info = f"File: {source_file}"
|
196 |
+
|
197 |
+
# Aggiungi informazioni dal metadata.json se disponibili
|
198 |
+
if source_file in metadata_dict:
|
199 |
+
file_metadata = metadata_dict[source_file]
|
200 |
+
chunk_info += f"\nTitolo: {file_metadata['title']}"
|
201 |
+
chunk_info += f"\nAutore: {file_metadata['author']}"
|
202 |
+
chunk_info += f"\nData caricamento: {file_metadata['upload_date']}"
|
203 |
+
|
204 |
+
logging.info(chunk_info)
|
205 |
+
logging.info("Contenuto:")
|
206 |
logging.info(doc.page_content)
|
207 |
logging.info("---------------------")
|
208 |
|
|
|
225 |
{"role": "user", "content": question}
|
226 |
]
|
227 |
|
228 |
+
if "local" in str(llm_type):
|
229 |
+
if not test_local_connection():
|
230 |
+
raise ConnectionError("LM Studio non raggiungibile")
|
231 |
+
|
232 |
client, model = get_llm_client(llm_type)
|
233 |
response = client.chat.completions.create(
|
234 |
model=model,
|
|
|
249 |
|
250 |
except Exception as e:
|
251 |
logging.error(f"Errore durante la generazione della risposta: {e}")
|
252 |
+
error_msg = "LLM locale non disponibile. Riprova più tardi o usa OpenAI." if "local" in str(llm_type) else str(e)
|
253 |
return [
|
254 |
{"role": "user", "content": question},
|
255 |
+
{"role": "assistant", "content": f"⚠️ {error_msg}"}
|
256 |
]
|
257 |
|
258 |
|
ui/chatbot_tab.py
CHANGED
@@ -251,3 +251,4 @@ def create_chatbot_tab():
|
|
251 |
|
252 |
# Ritorna il riferimento al dropdown corretto
|
253 |
return {"db_selector": db_name_chat}
|
|
|
|
251 |
|
252 |
# Ritorna il riferimento al dropdown corretto
|
253 |
return {"db_selector": db_name_chat}
|
254 |
+
|