Saltar al contenido principal

Servir Modelos

Sirve tus modelos entrenados para inferencia en producción.

Interfaz de Chat

La forma más simple de probar e interactuar con modelos:
aitraining chat
Abre una interfaz web en http://localhost:7860/inference. El Chat UI te permite cargar cualquier modelo local o del Hub para pruebas interactivas.

Puerto Personalizado

aitraining chat --port 3000

Host Personalizado

aitraining chat --host 0.0.0.0

Servidor de API

El servidor de API es un ejecutor de entrenamiento, no un servidor de inferencia. Expone endpoints mínimos para verificaciones de salud mientras ejecuta trabajos de entrenamiento.

Iniciar Servidor de API

aitraining api
Inicia la API de entrenamiento en http://127.0.0.1:7860 por defecto.

Parámetros

ParameterDescriptionDefault
--portPort to run the API on7860
--hostHost to bind to127.0.0.1
--taskTask to run (optional)None

Puerto/Host Personalizados

aitraining api --port 8000 --host 0.0.0.0

Variables de Entorno

El servidor de API lee la configuración de variables de entorno:
VariableDescription
HF_TOKENHugging Face token for authentication
AUTOTRAIN_USERNAMEUsername for training
PROJECT_NAMEName of the project
TASK_IDTask identifier
PARAMSTraining parameters (JSON)
DATA_PATHPath to training data
MODELModel to use

Endpoints

EndpointDescription
GET /Returns training status message
GET /healthHealth check (returns “OK”)
El servidor de API se apaga automáticamente cuando no hay trabajos de entrenamiento activos. Para inferencia en producción, usa vLLM o TGI en su lugar.

Despliegue en Producción

Usando vLLM

Para servir de nivel de producción con alto rendimiento:
pip install vllm

python -m vllm.entrypoints.openai.api_server \
  --model ./my-trained-model \
  --port 8000

Usando Text Generation Inference (TGI)

docker run --gpus all -p 8080:80 \
  -v ./my-model:/model \
  ghcr.io/huggingface/text-generation-inference:latest \
  --model-id /model

API Compatible con OpenAI

Tanto vLLM como TGI proporcionan endpoints compatibles con OpenAI:
from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:8000/v1",
    api_key="dummy"  # Not needed for local
)

response = client.chat.completions.create(
    model="my-model",
    messages=[
        {"role": "user", "content": "Hello!"}
    ]
)

Despliegue Docker

Ejemplo de Dockerfile

FROM python:3.10-slim

WORKDIR /app

# Install dependencies
RUN pip install aitraining torch

# Expose port
EXPOSE 7860

# Run chat server
CMD ["aitraining", "chat", "--host", "0.0.0.0", "--port", "7860"]
Construir y ejecutar:
docker build -t my-model-server .
docker run -p 7860:7860 my-model-server

Con GPU

docker run --gpus all -p 7860:7860 my-model-server

Prueba de Carga

Usando hey

hey -n 100 -c 10 \
  -m POST \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Hello", "max_tokens": 50}' \
  http://localhost:8000/generate

Usando locust

# locustfile.py
from locust import HttpUser, task

class ModelUser(HttpUser):
    @task
    def generate(self):
        self.client.post("/generate", json={
            "prompt": "Hello, how are you?",
            "max_tokens": 50
        })
locust -f locustfile.py --host http://localhost:8000

Monitorización

Métricas Prometheus

Si usas vLLM o TGI, las métricas están disponibles en /metrics.

Logging

aitraining api --port 8000 2>&1 | tee server.log

Próximos Pasos