Skip to content

Repository files navigation

ia-api

API en Bun y TypeScript que rota entre servicios de IA gratuitos (Groq, Cerebras...) para aprovechar sus capas gratuitas.

Uso

  1. Instala dependencias y ejecuta el servidor:

    bun install
    bun run start:dev
  2. Envía mensajes al endpoint /chat:

curl -X POST http://localhost:3000/chat -H "Content-Type: application/json" -d '{ "messages": [ { "role": "user", "content": "Cuentame un chiste de programadores" } ] }' ```

El servicio se alterna automáticamente entre los proveedores configurados.

Estructura

  • src/services/: Implementaciones de Groq y Cerebras
  • src/const/: Tipos e interfaces comunes
  • src/index.ts: Servidor principal

Para el frontend:

const response = await fetch("/chat", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    messages: [
      { role: "user", content: "Explícame recursión" },
    ],
  }),
});

if (!response.body) {
  throw new Error("La respuesta no soporta streaming");
}

const reader = response.body.getReader();
const decoder = new TextDecoder();
let accumulated = "";

while (true) {
  const { done, value } = await reader.read();
  if (done) break;

  accumulated += decoder.decode(value, { stream: true });
  console.log(accumulated);
}

About

No description or website provided.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages