Spaces:
Running
Running
File size: 10,994 Bytes
fc69895 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
import { z } from "zod";
import { openAICompletionToTextGenerationStream } from "./openAICompletionToTextGenerationStream";
import {
openAIChatToTextGenerationSingle,
openAIChatToTextGenerationStream,
} from "./openAIChatToTextGenerationStream";
import type { CompletionCreateParamsStreaming } from "openai/resources/completions";
import type {
ChatCompletionCreateParamsNonStreaming,
ChatCompletionCreateParamsStreaming,
} from "openai/resources/chat/completions";
import { buildPrompt } from "$lib/buildPrompt";
import { config } from "$lib/server/config";
import type { Endpoint } from "../endpoints";
import type OpenAI from "openai";
import { createImageProcessorOptionsValidator, makeImageProcessor } from "../images";
import { TEXT_MIME_ALLOWLIST } from "$lib/constants/mime";
import type { MessageFile } from "$lib/types/Message";
import type { EndpointMessage } from "../endpoints";
// uuid import removed (no tool call ids)
export const endpointOAIParametersSchema = z.object({
weight: z.number().int().positive().default(1),
model: z.any(),
type: z.literal("openai"),
baseURL: z.string().url().default("https://api.openai.com/v1"),
// Canonical auth token is OPENAI_API_KEY; keep HF_TOKEN as legacy alias
apiKey: z.string().default(config.OPENAI_API_KEY || config.HF_TOKEN || "sk-"),
completion: z
.union([z.literal("completions"), z.literal("chat_completions")])
.default("chat_completions"),
defaultHeaders: z.record(z.string()).optional(),
defaultQuery: z.record(z.string()).optional(),
extraBody: z.record(z.any()).optional(),
multimodal: z
.object({
image: createImageProcessorOptionsValidator({
supportedMimeTypes: [
// Restrict to the most widely-supported formats
"image/png",
"image/jpeg",
],
preferredMimeType: "image/jpeg",
maxSizeInMB: 1,
maxWidth: 1024,
maxHeight: 1024,
}),
})
.default({}),
/* enable use of max_completion_tokens in place of max_tokens */
useCompletionTokens: z.boolean().default(false),
streamingSupported: z.boolean().default(true),
});
export async function endpointOai(
input: z.input<typeof endpointOAIParametersSchema>
): Promise<Endpoint> {
const {
baseURL,
apiKey,
completion,
model,
defaultHeaders,
defaultQuery,
multimodal,
extraBody,
useCompletionTokens,
streamingSupported,
} = endpointOAIParametersSchema.parse(input);
let OpenAI;
try {
OpenAI = (await import("openai")).OpenAI;
} catch (e) {
throw new Error("Failed to import OpenAI", { cause: e });
}
// Store router metadata if captured
let routerMetadata: { route?: string; model?: string; provider?: string } = {};
// Custom fetch wrapper to capture response headers for router metadata
const customFetch = async (url: RequestInfo, init?: RequestInit): Promise<Response> => {
const response = await fetch(url, init);
// Capture router headers if present (fallback for non-streaming)
const routeHeader = response.headers.get("X-Router-Route");
const modelHeader = response.headers.get("X-Router-Model");
const providerHeader = response.headers.get("x-inference-provider");
if (routeHeader && modelHeader) {
routerMetadata = {
route: routeHeader,
model: modelHeader,
provider: providerHeader || undefined,
};
} else if (providerHeader) {
// Even without router metadata, capture provider info
routerMetadata = {
provider: providerHeader,
};
}
return response;
};
const openai = new OpenAI({
apiKey: apiKey || "sk-",
baseURL,
defaultHeaders: {
...(config.PUBLIC_APP_NAME === "HuggingChat" && { "User-Agent": "huggingchat" }),
...defaultHeaders,
},
defaultQuery,
fetch: customFetch,
});
const imageProcessor = makeImageProcessor(multimodal.image);
if (completion === "completions") {
return async ({
messages,
preprompt,
generateSettings,
conversationId,
locals,
abortSignal,
}) => {
const prompt = await buildPrompt({
messages,
preprompt,
model,
});
const parameters = { ...model.parameters, ...generateSettings };
const body: CompletionCreateParamsStreaming = {
model: model.id ?? model.name,
prompt,
stream: true,
max_tokens: parameters?.max_tokens,
stop: parameters?.stop,
temperature: parameters?.temperature,
top_p: parameters?.top_p,
frequency_penalty: parameters?.frequency_penalty,
presence_penalty: parameters?.presence_penalty,
};
const openAICompletion = await openai.completions.create(body, {
body: { ...body, ...extraBody },
headers: {
"ChatUI-Conversation-ID": conversationId?.toString() ?? "",
"X-use-cache": "false",
...(locals?.token ? { Authorization: `Bearer ${locals.token}` } : {}),
},
signal: abortSignal,
});
return openAICompletionToTextGenerationStream(openAICompletion);
};
} else if (completion === "chat_completions") {
return async ({
messages,
preprompt,
generateSettings,
conversationId,
isMultimodal,
locals,
abortSignal,
}) => {
// Format messages for the chat API, handling multimodal content if supported
let messagesOpenAI: OpenAI.Chat.Completions.ChatCompletionMessageParam[] =
await prepareMessages(messages, imageProcessor, isMultimodal ?? model.multimodal);
// Normalize preprompt and handle empty values
const normalizedPreprompt =
typeof preprompt === "string" ? preprompt.trim() : "";
// Check if a system message already exists as the first message
const hasSystemMessage =
messagesOpenAI.length > 0 && messagesOpenAI[0]?.role === "system";
if (hasSystemMessage) {
// Prepend normalized preprompt to existing system content when non-empty
if (normalizedPreprompt) {
const userSystemPrompt =
(typeof messagesOpenAI[0].content === "string"
? (messagesOpenAI[0].content as string)
: "") || "";
messagesOpenAI[0].content =
normalizedPreprompt + (userSystemPrompt ? "\n\n" + userSystemPrompt : "");
}
} else {
// Insert a system message only if the preprompt is non-empty
if (normalizedPreprompt) {
messagesOpenAI = [
{ role: "system", content: normalizedPreprompt },
...messagesOpenAI,
];
}
}
// Combine model defaults with request-specific parameters
const parameters = { ...model.parameters, ...generateSettings };
const body = {
model: model.id ?? model.name,
messages: messagesOpenAI,
stream: streamingSupported,
// Support two different ways of specifying token limits depending on the model
...(useCompletionTokens
? { max_completion_tokens: parameters?.max_tokens }
: { max_tokens: parameters?.max_tokens }),
stop: parameters?.stop,
temperature: parameters?.temperature,
top_p: parameters?.top_p,
frequency_penalty: parameters?.frequency_penalty,
presence_penalty: parameters?.presence_penalty,
};
// Handle both streaming and non-streaming responses with appropriate processors
if (streamingSupported) {
const openChatAICompletion = await openai.chat.completions.create(
body as ChatCompletionCreateParamsStreaming,
{
body: { ...body, ...extraBody },
headers: {
"ChatUI-Conversation-ID": conversationId?.toString() ?? "",
"X-use-cache": "false",
...(locals?.token ? { Authorization: `Bearer ${locals.token}` } : {}),
},
signal: abortSignal,
}
);
return openAIChatToTextGenerationStream(openChatAICompletion, () => routerMetadata);
} else {
const openChatAICompletion = await openai.chat.completions.create(
body as ChatCompletionCreateParamsNonStreaming,
{
body: { ...body, ...extraBody },
headers: {
"ChatUI-Conversation-ID": conversationId?.toString() ?? "",
"X-use-cache": "false",
...(locals?.token ? { Authorization: `Bearer ${locals.token}` } : {}),
},
signal: abortSignal,
}
);
return openAIChatToTextGenerationSingle(openChatAICompletion, () => routerMetadata);
}
};
} else {
throw new Error("Invalid completion type");
}
}
async function prepareMessages(
messages: EndpointMessage[],
imageProcessor: ReturnType<typeof makeImageProcessor>,
isMultimodal: boolean
): Promise<OpenAI.Chat.Completions.ChatCompletionMessageParam[]> {
return Promise.all(
messages.map(async (message) => {
if (message.from === "user" && message.files && message.files.length > 0) {
const { imageParts, textContent } = await prepareFiles(
imageProcessor,
message.files,
isMultimodal
);
// If we have text files, prepend their content to the message
let messageText = message.content;
if (textContent.length > 0) {
messageText = textContent + "\n\n" + message.content;
}
// If we have images and multimodal is enabled, use structured content
if (imageParts.length > 0 && isMultimodal) {
const parts = [{ type: "text" as const, text: messageText }, ...imageParts];
return { role: message.from, content: parts };
}
// Otherwise just use the text (possibly with injected file content)
return { role: message.from, content: messageText };
}
return { role: message.from, content: message.content };
})
);
}
async function prepareFiles(
imageProcessor: ReturnType<typeof makeImageProcessor>,
files: MessageFile[],
isMultimodal: boolean
): Promise<{
imageParts: OpenAI.Chat.Completions.ChatCompletionContentPartImage[];
textContent: string;
}> {
// Separate image and text files
const imageFiles = files.filter((file) => file.mime.startsWith("image/"));
const textFiles = files.filter((file) => {
const mime = (file.mime || "").toLowerCase();
const [fileType, fileSubtype] = mime.split("/");
return TEXT_MIME_ALLOWLIST.some((allowed) => {
const [type, subtype] = allowed.toLowerCase().split("/");
const typeOk = type === "*" || type === fileType;
const subOk = subtype === "*" || subtype === fileSubtype;
return typeOk && subOk;
});
});
// Process images if multimodal is enabled
let imageParts: OpenAI.Chat.Completions.ChatCompletionContentPartImage[] = [];
if (isMultimodal && imageFiles.length > 0) {
const processedFiles = await Promise.all(imageFiles.map(imageProcessor));
imageParts = processedFiles.map((file) => ({
type: "image_url" as const,
image_url: {
url: `data:${file.mime};base64,${file.image.toString("base64")}`,
// Improves compatibility with some OpenAI-compatible servers
// that expect an explicit detail setting.
detail: "auto",
},
}));
}
// Process text files - inject their content
let textContent = "";
if (textFiles.length > 0) {
const textParts = await Promise.all(
textFiles.map(async (file) => {
const content = Buffer.from(file.value, "base64").toString("utf-8");
return `<document name="${file.name}" type="${file.mime}">\n${content}\n</document>`;
})
);
textContent = textParts.join("\n\n");
}
return { imageParts, textContent };
}
|