Spaces:
Runtime error
Runtime error
Andrew commited on
Commit ·
da494bc
1
Parent(s): 27b2e17
(feat) Use hugging face token when user authenticated
Browse files
src/routes/conversation/[id]/+server.ts
CHANGED
|
@@ -22,6 +22,7 @@ import { usageLimits } from "$lib/server/usageLimits";
|
|
| 22 |
import { textGeneration } from "$lib/server/textGeneration";
|
| 23 |
import type { TextGenerationContext } from "$lib/server/textGeneration/types";
|
| 24 |
import { logger } from "$lib/server/logger.js";
|
|
|
|
| 25 |
|
| 26 |
export async function POST({ request, locals, params, getClientAddress }) {
|
| 27 |
const id = z.string().parse(params.id);
|
|
@@ -134,6 +135,13 @@ export async function POST({ request, locals, params, getClientAddress }) {
|
|
| 134 |
error(410, "Model not available anymore");
|
| 135 |
}
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
// finally parse the content of the request
|
| 138 |
const form = await request.formData();
|
| 139 |
|
|
@@ -488,7 +496,7 @@ export async function POST({ request, locals, params, getClientAddress }) {
|
|
| 488 |
try {
|
| 489 |
const ctx: TextGenerationContext = {
|
| 490 |
model,
|
| 491 |
-
endpoint: await model.getEndpoint(),
|
| 492 |
conv,
|
| 493 |
messages: messagesForPrompt,
|
| 494 |
assistant: undefined,
|
|
@@ -496,6 +504,7 @@ export async function POST({ request, locals, params, getClientAddress }) {
|
|
| 496 |
promptedAt,
|
| 497 |
ip: getClientAddress(),
|
| 498 |
username: locals.user?.username,
|
|
|
|
| 499 |
// Force-enable multimodal if user settings say so for this model
|
| 500 |
forceMultimodal: Boolean(userSettings?.multimodalOverrides?.[model.id]),
|
| 501 |
};
|
|
@@ -577,7 +586,9 @@ export async function POST({ request, locals, params, getClientAddress }) {
|
|
| 577 |
try {
|
| 578 |
// Generate title if needed (do this once, not per-persona)
|
| 579 |
if (conv.title === "New Chat") {
|
| 580 |
-
for await (const titleUpdate of generateTitleForConversation(conv
|
|
|
|
|
|
|
| 581 |
await update(titleUpdate);
|
| 582 |
}
|
| 583 |
}
|
|
@@ -588,7 +599,7 @@ export async function POST({ request, locals, params, getClientAddress }) {
|
|
| 588 |
|
| 589 |
const ctx: TextGenerationContext = {
|
| 590 |
model,
|
| 591 |
-
endpoint: await model.getEndpoint(),
|
| 592 |
conv,
|
| 593 |
messages: messagesWithoutSystem,
|
| 594 |
assistant: undefined,
|
|
@@ -596,6 +607,7 @@ export async function POST({ request, locals, params, getClientAddress }) {
|
|
| 596 |
promptedAt,
|
| 597 |
ip: getClientAddress(),
|
| 598 |
username: locals.user?.username,
|
|
|
|
| 599 |
forceMultimodal: Boolean(userSettings?.multimodalOverrides?.[model.id]),
|
| 600 |
};
|
| 601 |
|
|
|
|
| 22 |
import { textGeneration } from "$lib/server/textGeneration";
|
| 23 |
import type { TextGenerationContext } from "$lib/server/textGeneration/types";
|
| 24 |
import { logger } from "$lib/server/logger.js";
|
| 25 |
+
import { getUserHFToken } from "$lib/server/userTokens";
|
| 26 |
|
| 27 |
export async function POST({ request, locals, params, getClientAddress }) {
|
| 28 |
const id = z.string().parse(params.id);
|
|
|
|
| 135 |
error(410, "Model not available anymore");
|
| 136 |
}
|
| 137 |
|
| 138 |
+
const userApiKeyOverride =
|
| 139 |
+
locals.user?._id && locals.user?.authProvider === "huggingface"
|
| 140 |
+
? await getUserHFToken(locals.user._id)
|
| 141 |
+
: null;
|
| 142 |
+
|
| 143 |
+
const endpointOptions = userApiKeyOverride ? { apiKey: userApiKeyOverride } : undefined;
|
| 144 |
+
|
| 145 |
// finally parse the content of the request
|
| 146 |
const form = await request.formData();
|
| 147 |
|
|
|
|
| 496 |
try {
|
| 497 |
const ctx: TextGenerationContext = {
|
| 498 |
model,
|
| 499 |
+
endpoint: await model.getEndpoint(endpointOptions),
|
| 500 |
conv,
|
| 501 |
messages: messagesForPrompt,
|
| 502 |
assistant: undefined,
|
|
|
|
| 504 |
promptedAt,
|
| 505 |
ip: getClientAddress(),
|
| 506 |
username: locals.user?.username,
|
| 507 |
+
authToken: endpointOptions?.apiKey,
|
| 508 |
// Force-enable multimodal if user settings say so for this model
|
| 509 |
forceMultimodal: Boolean(userSettings?.multimodalOverrides?.[model.id]),
|
| 510 |
};
|
|
|
|
| 586 |
try {
|
| 587 |
// Generate title if needed (do this once, not per-persona)
|
| 588 |
if (conv.title === "New Chat") {
|
| 589 |
+
for await (const titleUpdate of generateTitleForConversation(conv, {
|
| 590 |
+
apiKey: endpointOptions?.apiKey,
|
| 591 |
+
})) {
|
| 592 |
await update(titleUpdate);
|
| 593 |
}
|
| 594 |
}
|
|
|
|
| 599 |
|
| 600 |
const ctx: TextGenerationContext = {
|
| 601 |
model,
|
| 602 |
+
endpoint: await model.getEndpoint(endpointOptions),
|
| 603 |
conv,
|
| 604 |
messages: messagesWithoutSystem,
|
| 605 |
assistant: undefined,
|
|
|
|
| 607 |
promptedAt,
|
| 608 |
ip: getClientAddress(),
|
| 609 |
username: locals.user?.username,
|
| 610 |
+
authToken: endpointOptions?.apiKey,
|
| 611 |
forceMultimodal: Boolean(userSettings?.multimodalOverrides?.[model.id]),
|
| 612 |
};
|
| 613 |
|