Saltar al contenido principal

Capítulo 3: Desafíos Específicos de los LLM

1. Gestión de Alucinaciones​

Los LLM pueden generar información plausible pero incorrecta:

class HallucinationDetector:
def __init__(self, fact_checker_model):
self.fact_checker = fact_checker_model

def check_response(self, query: str, response: str, sources: list) -> dict:
"""Check if response is grounded in sources."""

# Extract claims from response
claims = self.extract_claims(response)

# Verify each claim against sources
verified_claims = []
hallucinated_claims = []

for claim in claims:
if self.verify_against_sources(claim, sources):
verified_claims.append(claim)
else:
hallucinated_claims.append(claim)

return {
"hallucination_rate": len(hallucinated_claims) / len(claims),
"verified_claims": verified_claims,
"hallucinated_claims": hallucinated_claims
}

2. Seguridad y Filtrado de Contenido​

class SafetyGuard:
def __init__(self):
self.blocked_topics = ["illegal activities", "harmful content", ...]
self.pii_patterns = [r"\b\d{3}-\d{2}-\d{4}\b", ...] # SSN, etc.

def check_input(self, user_input: str) -> tuple[bool, str]:
"""Check user input for safety issues."""
# Topic filtering
for topic in self.blocked_topics:
if self.detects_topic(user_input, topic):
return False, f"Blocked topic: {topic}"

return True, "OK"

def check_output(self, llm_output: str) -> str:
"""Sanitize LLM output."""
# Remove PII
for pattern in self.pii_patterns:
llm_output = re.sub(pattern, "[REDACTED]", llm_output)

return llm_output

3. Optimización de Latencia​

TécnicaReducción de latenciaCompensación
StreamingLatencia percibidaComplejidad de implementación
Caching90%+ en aciertos de cachéAlmacenamiento, invalidación de caché
Modelos más pequeños50-80%Calidad
BatchingVariableTiempo de respuesta

4. Gestión de Costos​

class CostTracker:
def __init__(self, budget_limit: float):
self.budget_limit = budget_limit
self.total_cost = 0

def track_request(self, model: str, input_tokens: int, output_tokens: int):
cost = calculate_cost(input_tokens, output_tokens, model)
self.total_cost += cost

if self.total_cost > self.budget_limit * 0.8:
alert_team("Approaching budget limit")

if self.total_cost > self.budget_limit:
raise BudgetExceededError("Budget limit reached")

return cost

Siguiente: Cuestionario del Módulo 10