API en Bun y TypeScript que rota entre servicios de IA gratuitos (Groq, Cerebras...) para aprovechar sus capas gratuitas.
-
Instala dependencias y ejecuta el servidor:
bun install bun run start:dev
-
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.
src/services/: Implementaciones de Groq y Cerebrassrc/const/: Tipos e interfaces comunessrc/index.ts: Servidor principal
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);
}