Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- server/_core/llm.ts +11 -47
server/_core/llm.ts
CHANGED
|
@@ -171,50 +171,12 @@ const normalizeMessage = (message: Message) => {
|
|
| 171 |
};
|
| 172 |
};
|
| 173 |
|
| 174 |
-
const
|
| 175 |
-
toolChoice: ToolChoice | undefined,
|
| 176 |
-
tools: Tool[] | undefined
|
| 177 |
-
): "none" | "auto" | ToolChoiceExplicit | undefined => {
|
| 178 |
-
if (!toolChoice) return undefined;
|
| 179 |
-
|
| 180 |
-
if (toolChoice === "none" || toolChoice === "auto") {
|
| 181 |
-
return toolChoice;
|
| 182 |
-
}
|
| 183 |
-
|
| 184 |
-
if (toolChoice === "required") {
|
| 185 |
-
if (!tools || tools.length === 0) {
|
| 186 |
-
throw new Error(
|
| 187 |
-
"tool_choice 'required' was provided but no tools were configured"
|
| 188 |
-
);
|
| 189 |
-
}
|
| 190 |
-
|
| 191 |
-
if (tools.length > 1) {
|
| 192 |
-
throw new Error(
|
| 193 |
-
"tool_choice 'required' needs a single tool or specify the tool name explicitly"
|
| 194 |
-
);
|
| 195 |
-
}
|
| 196 |
-
|
| 197 |
-
return {
|
| 198 |
-
type: "function",
|
| 199 |
-
function: { name: tools[0].function.name },
|
| 200 |
-
};
|
| 201 |
-
}
|
| 202 |
-
|
| 203 |
-
if ("name" in toolChoice) {
|
| 204 |
-
return {
|
| 205 |
-
type: "function",
|
| 206 |
-
function: { name: toolChoice.name },
|
| 207 |
-
};
|
| 208 |
-
}
|
| 209 |
-
|
| 210 |
-
return toolChoice;
|
| 211 |
-
};
|
| 212 |
-
|
| 213 |
-
const resolveApiUrl = () => {
|
| 214 |
-
// Always prefer HF Inference API if we have a token and no explicit forge URL
|
| 215 |
const hfToken = process.env.HF_TOKEN || process.env.HF_ACCESS_TOKEN;
|
| 216 |
-
|
| 217 |
-
|
|
|
|
|
|
|
| 218 |
}
|
| 219 |
|
| 220 |
if (ENV.forgeApiUrl && ENV.forgeApiUrl.trim().length > 0) {
|
|
@@ -291,8 +253,10 @@ export async function invokeLLM(params: InvokeParams): Promise<InvokeResult> {
|
|
| 291 |
response_format,
|
| 292 |
} = params;
|
| 293 |
|
|
|
|
|
|
|
| 294 |
const payload: Record<string, unknown> = {
|
| 295 |
-
model:
|
| 296 |
messages: messages.map(normalizeMessage),
|
| 297 |
};
|
| 298 |
|
|
@@ -308,7 +272,7 @@ export async function invokeLLM(params: InvokeParams): Promise<InvokeResult> {
|
|
| 308 |
payload.tool_choice = normalizedToolChoice;
|
| 309 |
}
|
| 310 |
|
| 311 |
-
payload.max_tokens = 2048;
|
| 312 |
|
| 313 |
const normalizedResponseFormat = normalizeResponseFormat({
|
| 314 |
responseFormat,
|
|
@@ -321,8 +285,8 @@ export async function invokeLLM(params: InvokeParams): Promise<InvokeResult> {
|
|
| 321 |
payload.response_format = normalizedResponseFormat;
|
| 322 |
}
|
| 323 |
|
| 324 |
-
const apiUrl = resolveApiUrl();
|
| 325 |
-
console.log(`[LLM] Invoking ${
|
| 326 |
|
| 327 |
const response = await fetch(apiUrl, {
|
| 328 |
method: "POST",
|
|
|
|
| 171 |
};
|
| 172 |
};
|
| 173 |
|
| 174 |
+
const resolveApiUrl = (model: string) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
const hfToken = process.env.HF_TOKEN || process.env.HF_ACCESS_TOKEN;
|
| 176 |
+
|
| 177 |
+
// If we have a token and it's a Hugging Face model, use the direct model endpoint
|
| 178 |
+
if (hfToken && model.includes("/")) {
|
| 179 |
+
return `https://api-inference.huggingface.co/models/${model}/v1/chat/completions`;
|
| 180 |
}
|
| 181 |
|
| 182 |
if (ENV.forgeApiUrl && ENV.forgeApiUrl.trim().length > 0) {
|
|
|
|
| 253 |
response_format,
|
| 254 |
} = params;
|
| 255 |
|
| 256 |
+
const model = params.model || "huihui-ai/Qwen2.5-72B-Instruct-abliterated";
|
| 257 |
+
|
| 258 |
const payload: Record<string, unknown> = {
|
| 259 |
+
model: model,
|
| 260 |
messages: messages.map(normalizeMessage),
|
| 261 |
};
|
| 262 |
|
|
|
|
| 272 |
payload.tool_choice = normalizedToolChoice;
|
| 273 |
}
|
| 274 |
|
| 275 |
+
payload.max_tokens = 2048;
|
| 276 |
|
| 277 |
const normalizedResponseFormat = normalizeResponseFormat({
|
| 278 |
responseFormat,
|
|
|
|
| 285 |
payload.response_format = normalizedResponseFormat;
|
| 286 |
}
|
| 287 |
|
| 288 |
+
const apiUrl = resolveApiUrl(model);
|
| 289 |
+
console.log(`[LLM] Invoking ${model} at ${apiUrl}`);
|
| 290 |
|
| 291 |
const response = await fetch(apiUrl, {
|
| 292 |
method: "POST",
|