Nugh75 commited on
Commit
767bdf9
·
1 Parent(s): a647c2c
Files changed (2) hide show
  1. app/llm_handling.py +38 -28
  2. ui/chatbot_tab.py +36 -32
app/llm_handling.py CHANGED
@@ -18,17 +18,35 @@ from app.configs.prompts import SYSTEM_PROMPTS
18
  logging.basicConfig(level=logging.INFO)
19
 
20
  class LLMType(Enum):
21
- OPENAI = "openai"
22
- LOCAL = "local"
23
-
24
- # Client OpenAI standard
25
- openai_client = OpenAI(api_key=OPENAI_API_KEY)
26
-
27
- # Client LM Studio locale
28
- local_client = OpenAI(
29
- base_url="http://192.168.140.5:1234/v1",
30
- api_key="not-needed"
31
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  # Voci italiane edge-tts
34
  VOICE_USER = "it-IT-DiegoNeural" # Voce maschile utente
@@ -112,7 +130,7 @@ def get_system_prompt(prompt_type="tutor"):
112
  """Seleziona il prompt di sistema appropriato"""
113
  return SYSTEM_PROMPTS.get(prompt_type, SYSTEM_PROMPTS["tutor"])
114
 
115
- def answer_question(question, db_name, prompt_type="tutor", chat_history=None, llm_type=LLMType.OPENAI):
116
  """
117
  Risponde alla domanda 'question' usando i documenti del database 'db_name'.
118
  Restituisce una lista di 2 messaggi in formato:
@@ -172,22 +190,14 @@ def answer_question(question, db_name, prompt_type="tutor", chat_history=None, l
172
  {"role": "user", "content": question}
173
  ]
174
 
175
- if llm_type == LLMType.OPENAI:
176
- response = openai_client.chat.completions.create(
177
- model="gpt-4o-mini",
178
- messages=messages,
179
- temperature=0.7,
180
- max_tokens=2048 # Aumenta token per gestire conversazioni lunghe
181
- )
182
- answer = response.choices[0].message.content
183
-
184
- else: # LOCAL
185
- response = local_client.chat.completions.create(
186
- model="qwen2.5-coder-7b-instruct",
187
- messages=messages,
188
- temperature=0.7
189
- )
190
- answer = response.choices[0].message.content
191
 
192
  # Genera audio per domanda e risposta
193
  user_audio = generate_speech(question, is_user=True)
 
18
  logging.basicConfig(level=logging.INFO)
19
 
20
  class LLMType(Enum):
21
+ OPENAI_GPT_4O_MINI = "openai - GPT-4o-mini"
22
+ LOCAL_QWEN = "local - Qwen 7B"
23
+ LOCAL_PHI = "local - Phi-3 Mini"
24
+
25
+ # Configurazione modelli
26
+ LLM_CONFIGS = {
27
+ LLMType.OPENAI_GPT_4O_MINI: {
28
+ "client": lambda: OpenAI(api_key=OPENAI_API_KEY),
29
+ "model": "gpt-4-mini",
30
+ "base_url": None
31
+ },
32
+ LLMType.LOCAL_QWEN: {
33
+ "client": lambda: OpenAI(base_url="http://192.168.140.5:1234/v1", api_key="not-needed"),
34
+ "model": "qwen2.5-coder-7b-instruct",
35
+ "base_url": "http://192.168.140.5:1234/v1"
36
+ },
37
+ LLMType.LOCAL_PHI: {
38
+ "client": lambda: OpenAI(base_url="http://192.168.140.5:1234/v1", api_key="not-needed"),
39
+ "model": "phi-3.5-mini-ita",
40
+ "base_url": "http://192.168.140.5:1234/v1"
41
+ }
42
+ }
43
+
44
+ def get_llm_client(llm_type: LLMType):
45
+ """Ottiene il client appropriato per il modello selezionato"""
46
+ config = LLM_CONFIGS.get(llm_type)
47
+ if not config:
48
+ raise ValueError(f"Modello {llm_type} non supportato")
49
+ return config["client"](), config["model"]
50
 
51
  # Voci italiane edge-tts
52
  VOICE_USER = "it-IT-DiegoNeural" # Voce maschile utente
 
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'.
136
  Restituisce una lista di 2 messaggi in formato:
 
190
  {"role": "user", "content": question}
191
  ]
192
 
193
+ client, model = get_llm_client(llm_type)
194
+ response = client.chat.completions.create(
195
+ model=model,
196
+ messages=messages,
197
+ temperature=0.7,
198
+ max_tokens=2048 # Aumenta token per gestire conversazioni lunghe
199
+ )
200
+ answer = response.choices[0].message.content
 
 
 
 
 
 
 
 
201
 
202
  # Genera audio per domanda e risposta
203
  user_audio = generate_speech(question, is_user=True)
ui/chatbot_tab.py CHANGED
@@ -9,6 +9,9 @@ from utils.helpers import extract_text_from_files
9
 
10
  logging.basicConfig(level=logging.INFO)
11
 
 
 
 
12
  def create_chatbot_tab():
13
  """Crea il tab 'Chatbot' dell'interfaccia Gradio."""
14
 
@@ -27,24 +30,26 @@ def create_chatbot_tab():
27
  return chat_history
28
 
29
  def respond(message, chat_history, db_name, prompt_type, llm_type):
30
- """Genera una risposta alla domanda dell'utente e aggiorna la chat."""
31
  if chat_history is None:
32
  chat_history = []
33
 
34
- # Converti stringa in enum
35
- selected_llm = LLMType.LOCAL if llm_type == "local" else LLMType.OPENAI
 
 
 
 
 
 
36
 
37
- # Ottieni risposta con audio
38
  messages = answer_question(
39
  message,
40
  db_name,
41
- prompt_type,
42
- chat_history=chat_history, # Passa la cronologia
43
  llm_type=selected_llm
44
  )
45
 
46
  chat_history.extend(messages)
47
-
48
  return "", chat_history
49
 
50
  def clear_chat():
@@ -151,23 +156,27 @@ def create_chatbot_tab():
151
  with gr.Row():
152
  with gr.Column(scale=1):
153
  db_name_chat = gr.Dropdown(
154
- choices=databases,
155
- label="Seleziona conoscenze",
156
- value="default_db"
157
  )
158
 
159
  with gr.Column(scale=1):
160
  prompt_selector = gr.Dropdown(
161
- choices=list(SYSTEM_PROMPTS.keys()),
162
- label="Seleziona istruzioni LLM",
163
  value="tutor"
164
  )
165
 
166
  with gr.Column(scale=1):
167
  llm_selector = gr.Dropdown(
168
- choices=["openai", "local"],
 
 
 
 
169
  label="Seleziona Modello",
170
- value="openai"
171
  )
172
 
173
  # Chatbot e input
@@ -181,33 +190,28 @@ def create_chatbot_tab():
181
  # Bottoni per azioni
182
  with gr.Row():
183
  ask_button = gr.Button("Invia")
184
- clear_button = gr.Button("Pulisci Chat")
185
-
186
- # Upload file con dimensioni ridotte
 
 
187
  with gr.Row():
188
  file_input = gr.File(
189
  label="Carica PDF/Docx/TXT per la conversazione",
190
  file_types=[".pdf", ".docx", ".txt"],
191
  file_count="multiple",
192
- height="100px",
193
- scale=3
194
- )
195
- upload_button = gr.Button("Carica Documenti", scale=1)
 
 
 
196
 
197
  # Stato della chat
198
  chat_state = gr.State([])
199
 
200
-
201
- # Download e Audio in due righe separate
202
- with gr.Row():
203
- with gr.Column(scale=1):
204
- download_button = gr.Button("💾 Scarica Conversazione")
205
- download_file = gr.File(
206
- label="Download Conversazione",
207
- visible=True,
208
- interactive=False
209
- )
210
-
211
  with gr.Row():
212
  with gr.Column(scale=1):
213
  audio_button = gr.Button("🎤 Genera Audio Chat")
 
9
 
10
  logging.basicConfig(level=logging.INFO)
11
 
12
+
13
+
14
+
15
  def create_chatbot_tab():
16
  """Crea il tab 'Chatbot' dell'interfaccia Gradio."""
17
 
 
30
  return chat_history
31
 
32
  def respond(message, chat_history, db_name, prompt_type, llm_type):
 
33
  if chat_history is None:
34
  chat_history = []
35
 
36
+ # Mappatura dei modelli
37
+ llm_mapping = {
38
+ "openai - GPT-4o-Mini": LLMType.OPENAI_GPT_4O_MINI,
39
+ "local - Qwen 7B": LLMType.LOCAL_QWEN,
40
+ "local - Phi-3 Mini": LLMType.LOCAL_PHI
41
+ }
42
+
43
+ selected_llm = llm_mapping.get(llm_type, LLMType.OPENAI_GPT_4O_MINI)
44
 
 
45
  messages = answer_question(
46
  message,
47
  db_name,
48
+ prompt_type=prompt_type.split(" - ")[0],
 
49
  llm_type=selected_llm
50
  )
51
 
52
  chat_history.extend(messages)
 
53
  return "", chat_history
54
 
55
  def clear_chat():
 
156
  with gr.Row():
157
  with gr.Column(scale=1):
158
  db_name_chat = gr.Dropdown(
159
+ choices=list_databases(), # Lista dinamica dei database
160
+ label="Seleziona Database",
161
+ value=list_databases()[0] if list_databases() else None
162
  )
163
 
164
  with gr.Column(scale=1):
165
  prompt_selector = gr.Dropdown(
166
+ choices=list(SYSTEM_PROMPTS.keys()), # Usa le chiavi da SYSTEM_PROMPTS
167
+ label="Seleziona Stile Risposta",
168
  value="tutor"
169
  )
170
 
171
  with gr.Column(scale=1):
172
  llm_selector = gr.Dropdown(
173
+ choices=[
174
+ "openai - GPT-4o-Mini",
175
+ "local - Qwen 7B",
176
+ "local - Phi-3 Mini"
177
+ ],
178
  label="Seleziona Modello",
179
+ value="openai - GPT-4o-Mini"
180
  )
181
 
182
  # Chatbot e input
 
190
  # Bottoni per azioni
191
  with gr.Row():
192
  ask_button = gr.Button("Invia")
193
+ upload_button = gr.Button("Carica Documenti")
194
+ download_button = gr.Button("💾 Scarica Conversazione")
195
+ clear_button = gr.Button("Pulisci Chat")
196
+
197
+ # box
198
  with gr.Row():
199
  file_input = gr.File(
200
  label="Carica PDF/Docx/TXT per la conversazione",
201
  file_types=[".pdf", ".docx", ".txt"],
202
  file_count="multiple",
203
+ height="10px"
204
+ )
205
+ download_file = gr.File(
206
+ label="Download Conversazione",
207
+ visible=True,
208
+ interactive=False
209
+ )
210
 
211
  # Stato della chat
212
  chat_state = gr.State([])
213
 
214
+
 
 
 
 
 
 
 
 
 
 
215
  with gr.Row():
216
  with gr.Column(scale=1):
217
  audio_button = gr.Button("🎤 Genera Audio Chat")