javiervz commited on
Commit
9d9f444
·
verified ·
1 Parent(s): c683bff

Update rag_hf.py

Browse files
Files changed (1) hide show
  1. rag_hf.py +59 -19
rag_hf.py CHANGED
@@ -165,33 +165,73 @@ def query_llm(prompt):
165
  except Exception as e:
166
  return f"Error al consultar el modelo: {str(e)}"
167
 
168
- def generate_response(matrix, id_map, G, rdf, user_question, k, embedder):
169
- ids = get_top_k(matrix, id_map, user_question, k, embedder)
 
170
  context = [get_context(G, i) for i in ids]
171
  rdf_facts = []
172
  for i in ids:
173
  rdf_facts.extend([f"{p}: {v}" for p, v in query_rdf(rdf, i)])
174
 
175
- prompt_es = (
176
- "Contexto: " + " ".join(context) + "\n" +
177
- "Hechos RDF: " + ", ".join(rdf_facts) + "\n" +
178
- f"Pregunta: {user_question} Responde en español:"
179
- )
180
 
181
- prompt_en = (
182
- "Context: " + " ".join(context) + "\n" +
183
- "RDF facts: " + ", ".join(rdf_facts) + "\n" +
184
- f"Question: {user_question} Answer in English:"
185
- )
186
 
187
- response_es = query_llm(prompt_es)
188
- response_en = query_llm(prompt_en)
189
 
190
- full_response = (
191
- f"<b>Respuesta en español:</b><br>{response_es}<br><br>"
192
- f"<b>Answer in English:</b><br>{response_en}"
193
- )
194
- return full_response, ids, context, rdf_facts
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
 
196
  # === UI MAIN ===
197
  def main():
 
165
  except Exception as e:
166
  return f"Error al consultar el modelo: {str(e)}"
167
 
168
+ # === PROMPT PARA MODELO MISTRAL ===
169
+ def generate_response(matrix, id_map, G, rdf, user_question, k=3):
170
+ ids = get_top_k(matrix, id_map, user_question, k)
171
  context = [get_context(G, i) for i in ids]
172
  rdf_facts = []
173
  for i in ids:
174
  rdf_facts.extend([f"{p}: {v}" for p, v in query_rdf(rdf, i)])
175
 
176
+ prompt_es = f"""<s>[INST]
177
+ Eres un experto en lenguas indígenas sudamericanas. Utiliza estricta y únicamente la información a continuación para responder la pregunta del usuario en **español**.
178
+ - No infieras ni asumas hechos que no estén explícitamente establecidos.
179
+ - Si la respuesta es desconocida o insuficiente, di \"No puedo responder con los datos disponibles.\"
180
+ - Limita tu respuesta a 100 palabras.
181
 
182
+ ### CONTEXTO:
183
+ {chr(10).join(context)}
 
 
 
184
 
185
+ ### RELACIONES RDF:
186
+ {chr(10).join(rdf_facts)}
187
 
188
+ ### PREGUNTA:
189
+ {user_question}
190
+
191
+ Respuesta:
192
+ [/INST]"""
193
+
194
+ prompt_en = f"""<s>[INST]
195
+ You are an expert in South American indigenous languages.
196
+ Use strictly and only the information below to answer the user question in **English**.
197
+ - Do not infer or assume facts that are not explicitly stated.
198
+ - If the answer is unknown or insufficient, say \"I cannot answer with the available data.\"
199
+ - Limit your answer to 100 words.
200
+
201
+ ### CONTEXT:
202
+ {chr(10).join(context)}
203
+
204
+ ### RDF RELATIONS:
205
+ {chr(10).join(rdf_facts)}
206
+
207
+ ### QUESTION:
208
+ {user_question}
209
+
210
+ Answer:
211
+ [/INST]"""
212
+
213
+ try:
214
+ res_es = requests.post(
215
+ ENDPOINT_URL,
216
+ headers={"Authorization": f"Bearer {HF_API_TOKEN}", "Content-Type": "application/json"},
217
+ json={"inputs": prompt_es}, timeout=60
218
+ )
219
+ res_en = requests.post(
220
+ ENDPOINT_URL,
221
+ headers={"Authorization": f"Bearer {HF_API_TOKEN}", "Content-Type": "application/json"},
222
+ json={"inputs": prompt_en}, timeout=60
223
+ )
224
+ out_es = res_es.json()
225
+ out_en = res_en.json()
226
+
227
+ response_es = out_es[0]["generated_text"].replace(prompt_es.strip(), "").strip() if isinstance(out_es, list) and "generated_text" in out_es[0] else "Error en respuesta en español."
228
+ response_en = out_en[0]["generated_text"].replace(prompt_en.strip(), "").strip() if isinstance(out_en, list) and "generated_text" in out_en[0] else "Error in English response."
229
+
230
+ combined = f"<b>Respuesta en español:</b><br>{response_es}<br><br><b>Answer in English:</b><br>{response_en}"
231
+ return combined, ids, context, rdf_facts
232
+
233
+ except Exception as e:
234
+ return f"Error al consultar el modelo: {str(e)}", ids, context, rdf_facts
235
 
236
  # === UI MAIN ===
237
  def main():