Spaces:
Running
Running
github-actions[bot] commited on
Commit ·
a3cd373
1
Parent(s): d0b546b
deploy: d10e28b — 更新 litellm.js
Browse files- backend/src/litellm.js +113 -211
backend/src/litellm.js
CHANGED
|
@@ -1,250 +1,152 @@
|
|
| 1 |
"use strict";
|
| 2 |
|
| 3 |
/**
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
*/
|
| 8 |
|
| 9 |
-
const
|
| 10 |
-
const
|
| 11 |
-
|
| 12 |
-
const LITELLM_BASE_URL = process.env.LITELLM_BASE_URL || “http://litellm:4000”;
|
| 13 |
-
const LITELLM_MASTER_KEY = process.env.LITELLM_MASTER_KEY || “sk-gateway-master-key”;
|
| 14 |
|
| 15 |
const client = axios.create({
|
| 16 |
-
baseURL: LITELLM_BASE_URL,
|
| 17 |
-
timeout: 15000,
|
| 18 |
-
headers: {
|
| 19 |
-
Authorization:
|
| 20 |
-
|
| 21 |
-
},
|
| 22 |
});
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
/**
|
| 27 |
-
|
| 28 |
-
- Register a new model in LiteLLM at runtime.
|
| 29 |
-
- @param {object} model - Our internal model record
|
| 30 |
-
- @returns {string} - LiteLLM internal model ID
|
| 31 |
-
*/
|
| 32 |
-
async function registerModel(model) {
|
| 33 |
const payload = {
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
};
|
| 42 |
|
| 43 |
-
logger.info(
|
| 44 |
-
|
| 45 |
-
const response = await client.post(
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
//
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
//
|
| 67 |
-
// LiteLLM /model/new documented response shapes:
|
| 68 |
-
// v1.x+ → { model_info: { id: “<uuid>”, … }, model_name: “…” }
|
| 69 |
-
// older → { id: “<uuid>”, … }
|
| 70 |
-
// ─────────────────────────────────────────────────────────────────────────
|
| 71 |
-
const litellmId =
|
| 72 |
-
response.data?.model_info?.id ||
|
| 73 |
-
response.data?.id ||
|
| 74 |
-
null;
|
| 75 |
-
|
| 76 |
-
if (!litellmId) {
|
| 77 |
-
logger.warn(
|
| 78 |
-
“[BUG#7] LiteLLM /model/new response contained no recognisable model ID. “ +
|
| 79 |
-
“Falling back to internal UUID as litellm_id. “ +
|
| 80 |
-
“Subsequent deregisterModel() calls for this model will likely fail silently, “ +
|
| 81 |
-
“leaving a ghost model active inside LiteLLM. “ +
|
| 82 |
-
“Inspect responseData below and verify your LiteLLM version.”,
|
| 83 |
-
{
|
| 84 |
-
modelName: model.name,
|
| 85 |
-
internalId: model.id,
|
| 86 |
-
responseTopLevelKeys: response.data ? Object.keys(response.data) : [],
|
| 87 |
-
responseData: response.data,
|
| 88 |
-
}
|
| 89 |
-
);
|
| 90 |
-
// Retain fallback so createModel() still returns a usable record.
|
| 91 |
-
return model.id;
|
| 92 |
-
}
|
| 93 |
|
| 94 |
-
logger.info(
|
| 95 |
-
return litellmId;
|
| 96 |
}
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
- Remove a model from LiteLLM.
|
| 101 |
-
-
|
| 102 |
-
- @param {string} litellmId - LiteLLM’s model ID (returned from registerModel)
|
| 103 |
-
-
|
| 104 |
-
- ─── BUG FIX #6: /model/delete field-name version incompatibility ──────────
|
| 105 |
-
-
|
| 106 |
-
- ORIGINAL CODE:
|
| 107 |
-
- await client.post(”/model/delete”, { id: litellmId });
|
| 108 |
-
-
|
| 109 |
-
- The request-body field name accepted by /model/delete has changed across
|
| 110 |
-
- LiteLLM releases:
|
| 111 |
-
-
|
| 112 |
-
- • Older versions (pre-v1.x): { model_id: “<id>” }
|
| 113 |
-
- • Current versions (v1.x+): { id: “<id>” }
|
| 114 |
-
-
|
| 115 |
-
- Sending only `id` against an older deployment produces a silent no-op:
|
| 116 |
-
- LiteLLM returns HTTP 200 but ignores the request because it only reads
|
| 117 |
-
- `model_id`. The same failure mode applies in reverse on newer versions.
|
| 118 |
-
-
|
| 119 |
-
- FIX: Send BOTH fields in every request. LiteLLM’s Pydantic models use
|
| 120 |
-
- `model_config = ConfigDict(extra="ignore")`, so unknown keys are silently
|
| 121 |
-
- discarded — the payload is safe for all known versions.
|
| 122 |
-
-
|
| 123 |
-
- If deletion silently fails after a future LiteLLM upgrade, verify the
|
| 124 |
-
- current accepted field name via the running instance’s Swagger UI:
|
| 125 |
-
- http://<litellm-host>:4000/docs → POST /model/delete
|
| 126 |
-
- ─────────────────────────────────────────────────────────────────────────
|
| 127 |
-
*/
|
| 128 |
-
async function deregisterModel(litellmId) {
|
| 129 |
if (!litellmId) return;
|
| 130 |
try {
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
} catch (err) {
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
httpStatus: err.response?.status,
|
| 143 |
-
error: err.message,
|
| 144 |
-
});
|
| 145 |
-
}
|
| 146 |
-
}
|
| 147 |
-
|
| 148 |
-
/**
|
| 149 |
-
|
| 150 |
-
- List all models currently registered in LiteLLM.
|
| 151 |
-
*/
|
| 152 |
-
async function listLitellmModels() {
|
| 153 |
-
const response = await client.get(”/model/info”);
|
| 154 |
-
return response.data?.data || response.data || [];
|
| 155 |
}
|
|
|
|
| 156 |
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
-
|
| 160 |
-
*/
|
| 161 |
-
async function updateModel(oldLitellmId, model) {
|
| 162 |
await deregisterModel(oldLitellmId);
|
| 163 |
return registerModel(model);
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
/**
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
- Uses /health/liveliness instead of /health:
|
| 171 |
-
- - /health validates all registered model upstreams. If any upstream is
|
| 172 |
-
- unreachable it returns an error even though LiteLLM itself is healthy,
|
| 173 |
-
- causing syncModelsToLitellmWithRetry() to retry endlessly and give up.
|
| 174 |
-
- - /health/liveliness only checks that the LiteLLM process is alive, which
|
| 175 |
-
- is the correct signal for “ready to accept /model/new requests”.
|
| 176 |
-
- This also matches the docker-compose.yml container healthcheck target.
|
| 177 |
-
*/
|
| 178 |
-
async function healthCheck() {
|
| 179 |
-
const response = await client.get(”/health/liveliness”);
|
| 180 |
return response.data;
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
/**
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
async function testModel(modelName, options = {}) {
|
| 188 |
const start = Date.now();
|
| 189 |
const messages =
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
try {
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
} catch (err) {
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
}
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
// ─── Helpers ──────────────────────────────────────────────────────────────
|
| 219 |
|
| 220 |
function buildLitellmParams(model) {
|
| 221 |
-
const params = {
|
| 222 |
-
model: model.litellmModel,
|
| 223 |
-
};
|
| 224 |
-
|
| 225 |
-
// Note: original code also checked `model._apiBase` which is never set
|
| 226 |
-
// anywhere in the codebase — that dead reference has been removed.
|
| 227 |
-
if (model.apiBase) {
|
| 228 |
-
params.api_base = model.apiBase;
|
| 229 |
-
}
|
| 230 |
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
params.api_key =
|
| 238 |
-
}
|
|
|
|
|
|
|
| 239 |
|
| 240 |
-
return params;
|
| 241 |
}
|
| 242 |
|
| 243 |
module.exports = {
|
| 244 |
-
registerModel,
|
| 245 |
-
deregisterModel,
|
| 246 |
-
listLitellmModels,
|
| 247 |
-
updateModel,
|
| 248 |
-
healthCheck,
|
| 249 |
-
testModel,
|
| 250 |
};
|
|
|
|
| 1 |
"use strict";
|
| 2 |
|
| 3 |
/**
|
| 4 |
+
* LiteLLM Proxy Management Client
|
| 5 |
+
* Wraps LiteLLM admin API for dynamic model registration.
|
| 6 |
+
*/
|
| 7 |
|
| 8 |
+
const axios = require("axios");
|
| 9 |
+
const { logger } = require("./logger");
|
|
|
|
| 10 |
|
| 11 |
+
const LITELLM_BASE_URL = process.env.LITELLM_BASE_URL || "http://litellm:4000";
|
| 12 |
+
const LITELLM_MASTER_KEY = process.env.LITELLM_MASTER_KEY || "sk-gateway-master-key";
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
const client = axios.create({
|
| 15 |
+
baseURL: LITELLM_BASE_URL,
|
| 16 |
+
timeout: 15000,
|
| 17 |
+
headers: {
|
| 18 |
+
Authorization: "Bearer " + LITELLM_MASTER_KEY,
|
| 19 |
+
"Content-Type": "application/json",
|
| 20 |
+
},
|
| 21 |
});
|
| 22 |
|
| 23 |
+
async function registerModel(model) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
const payload = {
|
| 25 |
+
model_name: model.name,
|
| 26 |
+
litellm_params: buildLitellmParams(model),
|
| 27 |
+
model_info: {
|
| 28 |
+
id: model.id,
|
| 29 |
+
description: model.description || "",
|
| 30 |
+
model_type: model.modelType || "chat",
|
| 31 |
+
},
|
| 32 |
};
|
| 33 |
|
| 34 |
+
logger.info("Registering model with LiteLLM", { modelName: model.name });
|
| 35 |
+
|
| 36 |
+
const response = await client.post("/model/new", payload);
|
| 37 |
+
|
| 38 |
+
const litellmId =
|
| 39 |
+
(response.data && response.data.model_info && response.data.model_info.id) ||
|
| 40 |
+
(response.data && response.data.id) ||
|
| 41 |
+
null;
|
| 42 |
+
|
| 43 |
+
if (!litellmId) {
|
| 44 |
+
logger.warn(
|
| 45 |
+
"LiteLLM /model/new response contained no recognisable model ID. " +
|
| 46 |
+
"Falling back to internal UUID. " +
|
| 47 |
+
"Deregistration may fail silently leaving a ghost model in LiteLLM.",
|
| 48 |
+
{
|
| 49 |
+
modelName: model.name,
|
| 50 |
+
internalId: model.id,
|
| 51 |
+
responseTopLevelKeys: response.data ? Object.keys(response.data) : [],
|
| 52 |
+
responseData: response.data,
|
| 53 |
+
}
|
| 54 |
+
);
|
| 55 |
+
return model.id;
|
| 56 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
logger.info("Model registered in LiteLLM", { modelName: model.name, litellmId: litellmId });
|
| 59 |
+
return litellmId;
|
| 60 |
}
|
| 61 |
|
| 62 |
+
async function deregisterModel(litellmId) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
if (!litellmId) return;
|
| 64 |
try {
|
| 65 |
+
await client.post("/model/delete", {
|
| 66 |
+
id: litellmId,
|
| 67 |
+
model_id: litellmId,
|
| 68 |
+
});
|
| 69 |
+
logger.info("Model deregistered from LiteLLM", { litellmId: litellmId });
|
| 70 |
} catch (err) {
|
| 71 |
+
logger.warn("Could not deregister model from LiteLLM", {
|
| 72 |
+
litellmId: litellmId,
|
| 73 |
+
httpStatus: err.response && err.response.status,
|
| 74 |
+
error: err.message,
|
| 75 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
}
|
| 77 |
+
}
|
| 78 |
|
| 79 |
+
async function listLitellmModels() {
|
| 80 |
+
const response = await client.get("/model/info");
|
| 81 |
+
return (response.data && response.data.data) || response.data || [];
|
| 82 |
+
}
|
| 83 |
|
| 84 |
+
async function updateModel(oldLitellmId, model) {
|
|
|
|
|
|
|
| 85 |
await deregisterModel(oldLitellmId);
|
| 86 |
return registerModel(model);
|
| 87 |
+
}
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
async function healthCheck() {
|
| 90 |
+
const response = await client.get("/health/liveliness");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
return response.data;
|
| 92 |
+
}
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
async function testModel(modelName, options) {
|
| 95 |
+
if (!options) { options = {}; }
|
|
|
|
| 96 |
const start = Date.now();
|
| 97 |
const messages =
|
| 98 |
+
options.messages && options.messages.length > 0
|
| 99 |
+
? options.messages
|
| 100 |
+
: [{ role: "user", content: options.prompt || "Say OK in one word." }];
|
| 101 |
try {
|
| 102 |
+
const response = await client.post(
|
| 103 |
+
"/v1/chat/completions",
|
| 104 |
+
{
|
| 105 |
+
model: modelName,
|
| 106 |
+
messages: messages,
|
| 107 |
+
max_tokens: 256,
|
| 108 |
+
stream: false,
|
| 109 |
+
},
|
| 110 |
+
{ timeout: 30000 }
|
| 111 |
+
);
|
| 112 |
+
return {
|
| 113 |
+
success: true,
|
| 114 |
+
latencyMs: Date.now() - start,
|
| 115 |
+
response: response.data,
|
| 116 |
+
};
|
| 117 |
} catch (err) {
|
| 118 |
+
return {
|
| 119 |
+
success: false,
|
| 120 |
+
latencyMs: Date.now() - start,
|
| 121 |
+
error: (err.response && err.response.data) || err.message,
|
| 122 |
+
};
|
| 123 |
}
|
| 124 |
+
}
|
|
|
|
|
|
|
| 125 |
|
| 126 |
function buildLitellmParams(model) {
|
| 127 |
+
const params = {
|
| 128 |
+
model: model.litellmModel,
|
| 129 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
if (model.apiBase) {
|
| 132 |
+
params.api_base = model.apiBase;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
const key = model._apiKey || model.apiKey;
|
| 136 |
+
if (key && key !== "\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022" && key.trim() !== "") {
|
| 137 |
+
params.api_key = key.trim();
|
| 138 |
+
} else {
|
| 139 |
+
params.api_key = "none";
|
| 140 |
+
}
|
| 141 |
|
| 142 |
+
return params;
|
| 143 |
}
|
| 144 |
|
| 145 |
module.exports = {
|
| 146 |
+
registerModel: registerModel,
|
| 147 |
+
deregisterModel: deregisterModel,
|
| 148 |
+
listLitellmModels: listLitellmModels,
|
| 149 |
+
updateModel: updateModel,
|
| 150 |
+
healthCheck: healthCheck,
|
| 151 |
+
testModel: testModel,
|
| 152 |
};
|