Malaji71 commited on
Commit
585c7bd
·
verified ·
1 Parent(s): c62dd51

Update rag7.py

Browse files
Files changed (1) hide show
  1. rag7.py +8 -5
rag7.py CHANGED
@@ -6,19 +6,17 @@ import os
6
  from sentence_transformers import SentenceTransformer
7
  from huggingface_hub import InferenceClient
8
 
9
- # === CONFIGURACIÓN ===
10
  MODEL_NAME = "openai/gpt-oss-20b"
11
  HF_TOKEN = os.getenv("HF_TOKEN")
12
 
13
  if not HF_TOKEN:
14
  raise RuntimeError("❌ HF_TOKEN no encontrado en rag7.py")
15
 
16
- # === CARGAR FAISS RAG7 ===
17
  index_path = "nlp_index.faiss"
18
  docs_path = "nlp_docs.pkl"
19
 
20
  if not os.path.exists(index_path) or not os.path.exists(docs_path):
21
- raise FileNotFoundError("❌ Faltan archivos de RAG7: nlp_index.faiss o nlp_docs.pkl")
22
 
23
  index = faiss.read_index(index_path)
24
  with open(docs_path, "rb") as f:
@@ -28,7 +26,8 @@ with open(docs_path, "rb") as f:
28
 
29
  embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
30
 
31
- def retrieve_context(query: str, k: int = 2) -> str:
 
32
  try:
33
  emb = embedding_model.encode([query], convert_to_numpy=True).astype('float32')
34
  emb = emb / np.linalg.norm(emb)
@@ -39,6 +38,10 @@ def retrieve_context(query: str, k: int = 2) -> str:
39
 
40
  def generate_practical_guide(message: str, temperature=0.7, top_p=0.95, max_tokens=2048) -> str:
41
  context = retrieve_context(message)
 
 
 
 
42
  if context:
43
  full_prompt = (
44
  f"Responde usando únicamente la siguiente información de contexto. Si el contexto no responde la pregunta, usa tu conocimiento general pero sé honesto sobre sus límites.\n\n"
@@ -50,7 +53,7 @@ def generate_practical_guide(message: str, temperature=0.7, top_p=0.95, max_toke
50
 
51
  client = InferenceClient(token=HF_TOKEN, model=MODEL_NAME, timeout=60)
52
  messages = [
53
- {"role": "system", "content": "Eres un asistente experto en desarrollo humano. Responde con claridad, profundidad y empatía, citando conceptos de los libros si es relevante."},
54
  {"role": "user", "content": full_prompt}
55
  ]
56
 
 
6
  from sentence_transformers import SentenceTransformer
7
  from huggingface_hub import InferenceClient
8
 
 
9
  MODEL_NAME = "openai/gpt-oss-20b"
10
  HF_TOKEN = os.getenv("HF_TOKEN")
11
 
12
  if not HF_TOKEN:
13
  raise RuntimeError("❌ HF_TOKEN no encontrado en rag7.py")
14
 
 
15
  index_path = "nlp_index.faiss"
16
  docs_path = "nlp_docs.pkl"
17
 
18
  if not os.path.exists(index_path) or not os.path.exists(docs_path):
19
+ raise FileNotFoundError("❌ Faltan archivos de RAG7")
20
 
21
  index = faiss.read_index(index_path)
22
  with open(docs_path, "rb") as f:
 
26
 
27
  embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
28
 
29
+ def retrieve_context(query: str, k: int = 3) -> str:
30
+ """Recupera los 3 fragmentos más relevantes (aumentado de 2 a 3 para más contexto)"""
31
  try:
32
  emb = embedding_model.encode([query], convert_to_numpy=True).astype('float32')
33
  emb = emb / np.linalg.norm(emb)
 
38
 
39
  def generate_practical_guide(message: str, temperature=0.7, top_p=0.95, max_tokens=2048) -> str:
40
  context = retrieve_context(message)
41
+
42
+ # ✅ Prompt del sistema idéntico al de tu Space público
43
+ system_message = "Eres un asistente experto en desarrollo humano. Responde con claridad, profundidad y empatía, citando conceptos de los libros si es relevante."
44
+
45
  if context:
46
  full_prompt = (
47
  f"Responde usando únicamente la siguiente información de contexto. Si el contexto no responde la pregunta, usa tu conocimiento general pero sé honesto sobre sus límites.\n\n"
 
53
 
54
  client = InferenceClient(token=HF_TOKEN, model=MODEL_NAME, timeout=60)
55
  messages = [
56
+ {"role": "system", "content": system_message},
57
  {"role": "user", "content": full_prompt}
58
  ]
59