NoeMartinezSanchez commited on
Commit
e5f13e0
·
1 Parent(s): 12c3994

Implementacion de groq

Browse files
.gitignore CHANGED
@@ -48,9 +48,7 @@ data/*.xlsx
48
  # menu.json SÍ se sube (es ligero, texto plano)
49
  # data/menu.json
50
 
51
- # Pickle files - excluir data/ pero permitir vector_store/
52
- data/*.pkl
53
- !data/vector_store/
54
  data/vector_store/*.pkl
55
 
56
  # ignora las imagenes
 
48
  # menu.json SÍ se sube (es ligero, texto plano)
49
  # data/menu.json
50
 
51
+ # Pickle files - permitir vector_store/
 
 
52
  data/vector_store/*.pkl
53
 
54
  # ignora las imagenes
data/vector_store/documents.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3704f6da862beab2ffdc52e9f168c4587e09edd7bb82c13f192feee847169550
3
+ size 35144
data/vector_store/metadata.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:66a5d0cd8e69724eaa5043371310a84c8c304ce8db5b92f42c6edef5da22ed30
3
+ size 57847
models/groq_wrapper.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # models/groq_wrapper.py
2
+ import os
3
+ from groq import Groq
4
+ import time
5
+
6
+ class GroqWrapper:
7
+ def __init__(self, api_key=None):
8
+ # Leer API key desde variable de entorno o parámetro
9
+ self.api_key = api_key or os.environ.get("GROQ_API_KEY")
10
+ if not self.api_key:
11
+ raise ValueError("GROQ_API_KEY no encontrada. Configúrala en el archivo .env")
12
+
13
+ self.client = Groq(api_key=self.api_key)
14
+ self.model = "llama-3.3-70b-versatile"
15
+ self.max_retries = 3
16
+ print("✅ Groq API inicializada correctamente")
17
+
18
+ def generate_with_context(self, context, question, **kwargs):
19
+ """Genera respuesta basada en el contexto recuperado por RAG"""
20
+
21
+ # Construir el prompt según si hay contexto o no
22
+ if context:
23
+ prompt = f"""Basado en la siguiente información oficial de Prepa en Línea SEP:
24
+
25
+ CONTEXTO:
26
+ {context}
27
+
28
+ PREGUNTA: {question}
29
+
30
+ RESPUESTA (usa SOLO la información del contexto. Si no está en el contexto, responde: "No encontré información específica en los materiales oficiales"):"""
31
+ else:
32
+ prompt = f"""Eres un asistente académico de Prepa en Línea SEP.
33
+ Responde de manera clara y amigable: {question}"""
34
+
35
+ # Mensajes para Groq
36
+ messages = [
37
+ {
38
+ "role": "system",
39
+ "content": "Eres un asistente académico de Prepa en Línea SEP. Hablas en español. Das respuestas claras, precisas y útiles para estudiantes mexicanos de bachillerato."
40
+ },
41
+ {
42
+ "role": "user",
43
+ "content": prompt
44
+ }
45
+ ]
46
+
47
+ # Reintentos automáticos
48
+ for intento in range(self.max_retries):
49
+ try:
50
+ response = self.client.chat.completions.create(
51
+ messages=messages,
52
+ model=self.model,
53
+ temperature=0.3,
54
+ max_tokens=1024,
55
+ top_p=1,
56
+ )
57
+ return response.choices[0].message.content
58
+
59
+ except Exception as e:
60
+ print(f"❌ Error en intento {intento+1}: {e}")
61
+ if intento < self.max_retries - 1:
62
+ time.sleep(2 ** intento) # Espera: 1, 2, 4 segundos
63
+ else:
64
+ return "Lo siento, tuve un problema procesando tu pregunta. Por favor intenta de nuevo."
65
+
66
+ def generate(self, prompt, **kwargs):
67
+ """Método de compatibilidad con la interfaz web"""
68
+ return self.generate_with_context("", prompt)
rag/gemma_generator.py CHANGED
@@ -10,19 +10,19 @@ from typing import Optional, Callable
10
 
11
  from loguru import logger
12
 
13
- from models.gemini_wrapper import GeminiWrapper
14
 
15
 
16
  class GemmaGenerator:
17
  def __init__(self, cache_dir: str = "models/cache", model: str = "gemini-2.5-flash"):
18
- logger.info("Initializing GemmaGenerator with Gemini API...")
19
  start_time = time.time()
20
 
21
- self.wrapper = GeminiWrapper()
22
  self.model = model
23
 
24
  load_time = time.time() - start_time
25
- logger.success(f"✅ GemmaGenerator initialized in {load_time:.1f}s")
26
 
27
  def generate(self, query: str, context: str = "", **kwargs) -> str:
28
  """Generate a response for the given query.
 
10
 
11
  from loguru import logger
12
 
13
+ from models.groq_wrapper import GroqWrapper
14
 
15
 
16
  class GemmaGenerator:
17
  def __init__(self, cache_dir: str = "models/cache", model: str = "gemini-2.5-flash"):
18
+ logger.info("Initializing GemmaGenerator with Groq API...")
19
  start_time = time.time()
20
 
21
+ self.wrapper = GroqWrapper()
22
  self.model = model
23
 
24
  load_time = time.time() - start_time
25
+ logger.success(f"✅ GemmaGenerator initialized with Groq API in {load_time:.1f}s")
26
 
27
  def generate(self, query: str, context: str = "", **kwargs) -> str:
28
  """Generate a response for the given query.
requirements.txt CHANGED
@@ -31,3 +31,5 @@ pandas==2.0.3
31
 
32
  # Google Gemini API
33
  google-generativeai>=0.7.0
 
 
 
31
 
32
  # Google Gemini API
33
  google-generativeai>=0.7.0
34
+
35
+ groq>=0.12.0
test_groq.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # test_groq.py
2
+ import os
3
+ from dotenv import load_dotenv
4
+ from models.groq_wrapper import GroqWrapper
5
+
6
+ # Cargar variables del archivo .env
7
+ load_dotenv()
8
+
9
+ print("=" * 50)
10
+ print("Probando Groq API...")
11
+ print("=" * 50)
12
+
13
+ # Crear instancia del wrapper
14
+ wrapper = GroqWrapper()
15
+
16
+ # Prueba 1: Pregunta simple (sin contexto)
17
+ print("\n📝 Prueba 1: Pregunta simple")
18
+ print("-" * 30)
19
+ respuesta1 = wrapper.generate("Hola, ¿cómo estás?")
20
+ print(f"Respuesta: {respuesta1}")
21
+
22
+ # Prueba 2: Pregunta con contexto (simulando RAG)
23
+ print("\n📝 Prueba 2: Pregunta con contexto")
24
+ print("-" * 30)
25
+ contexto = "El módulo propedéutico de Prepa en Línea SEP dura 10 días naturales. La calificación mínima aprobatoria es 60 puntos."
26
+ pregunta = "¿Cuánto dura el propedéutico y cuál es la calificación mínima?"
27
+ respuesta2 = wrapper.generate_with_context(contexto, pregunta)
28
+ print(f"Respuesta: {respuesta2}")
29
+
30
+ print("\n" + "=" * 50)
31
+ print("✅ Pruebas completadas")
32
+ print("=" * 50)