Spaces:
Sleeping
Sleeping
Andrew
commited on
Commit
·
27b2e17
1
Parent(s):
f407536
(feat) Pass auth token to auxiliary generation helpers
Browse files
src/lib/server/textGeneration/index.ts
CHANGED
|
@@ -23,7 +23,7 @@ async function* keepAlive(done: AbortSignal): AsyncGenerator<MessageUpdate, unde
|
|
| 23 |
export async function* textGeneration(ctx: TextGenerationContext) {
|
| 24 |
const done = new AbortController();
|
| 25 |
|
| 26 |
-
const titleGen = generateTitleForConversation(ctx.conv);
|
| 27 |
const textGen = textGenerationWithoutTitle(ctx, done);
|
| 28 |
const keepAliveGen = keepAlive(done.signal);
|
| 29 |
|
|
|
|
| 23 |
export async function* textGeneration(ctx: TextGenerationContext) {
|
| 24 |
const done = new AbortController();
|
| 25 |
|
| 26 |
+
const titleGen = generateTitleForConversation(ctx.conv, { apiKey: ctx.authToken });
|
| 27 |
const textGen = textGenerationWithoutTitle(ctx, done);
|
| 28 |
const keepAliveGen = keepAlive(done.signal);
|
| 29 |
|
src/lib/server/textGeneration/reasoning.ts
CHANGED
|
@@ -3,7 +3,8 @@ import { getReturnFromGenerator } from "$lib/utils/getReturnFromGenerator";
|
|
| 3 |
|
| 4 |
export async function generateSummaryOfReasoning(
|
| 5 |
buffer: string,
|
| 6 |
-
modelId?: string
|
|
|
|
| 7 |
): Promise<string> {
|
| 8 |
let summary: string | undefined;
|
| 9 |
|
|
@@ -25,6 +26,7 @@ export async function generateSummaryOfReasoning(
|
|
| 25 |
max_tokens: 50,
|
| 26 |
},
|
| 27 |
modelId,
|
|
|
|
| 28 |
})
|
| 29 |
);
|
| 30 |
}
|
|
|
|
| 3 |
|
| 4 |
export async function generateSummaryOfReasoning(
|
| 5 |
buffer: string,
|
| 6 |
+
modelId?: string,
|
| 7 |
+
opts?: { apiKey?: string }
|
| 8 |
): Promise<string> {
|
| 9 |
let summary: string | undefined;
|
| 10 |
|
|
|
|
| 26 |
max_tokens: 50,
|
| 27 |
},
|
| 28 |
modelId,
|
| 29 |
+
apiKey: opts?.apiKey,
|
| 30 |
})
|
| 31 |
);
|
| 32 |
}
|
src/lib/server/textGeneration/title.ts
CHANGED
|
@@ -7,7 +7,8 @@ import { getReturnFromGenerator } from "$lib/utils/getReturnFromGenerator";
|
|
| 7 |
import { stripThinkBlocks } from "$lib/utils/stripThinkBlocks";
|
| 8 |
|
| 9 |
export async function* generateTitleForConversation(
|
| 10 |
-
conv: Conversation
|
|
|
|
| 11 |
): AsyncGenerator<MessageUpdate, undefined, undefined> {
|
| 12 |
try {
|
| 13 |
const userMessage = conv.messages.find((m) => m.from === "user");
|
|
@@ -16,7 +17,8 @@ export async function* generateTitleForConversation(
|
|
| 16 |
|
| 17 |
const prompt = userMessage.content;
|
| 18 |
const modelForTitle = config.TASK_MODEL?.trim() ? config.TASK_MODEL : conv.model;
|
| 19 |
-
const title =
|
|
|
|
| 20 |
|
| 21 |
yield {
|
| 22 |
type: MessageUpdateType.Title,
|
|
@@ -27,7 +29,7 @@ export async function* generateTitleForConversation(
|
|
| 27 |
}
|
| 28 |
}
|
| 29 |
|
| 30 |
-
export async function generateTitle(prompt: string, modelId?: string) {
|
| 31 |
if (config.LLM_SUMMARIZATION !== "true") {
|
| 32 |
// When summarization is disabled, use the first five words without adding emojis
|
| 33 |
const firstFive = prompt.split(/\s+/g).slice(0, 5).join(" ");
|
|
@@ -51,6 +53,7 @@ Return ONLY the title text.`,
|
|
| 51 |
max_tokens: 30,
|
| 52 |
},
|
| 53 |
modelId,
|
|
|
|
| 54 |
})
|
| 55 |
)
|
| 56 |
.then((summary) => {
|
|
|
|
| 7 |
import { stripThinkBlocks } from "$lib/utils/stripThinkBlocks";
|
| 8 |
|
| 9 |
export async function* generateTitleForConversation(
|
| 10 |
+
conv: Conversation,
|
| 11 |
+
opts?: { apiKey?: string }
|
| 12 |
): AsyncGenerator<MessageUpdate, undefined, undefined> {
|
| 13 |
try {
|
| 14 |
const userMessage = conv.messages.find((m) => m.from === "user");
|
|
|
|
| 17 |
|
| 18 |
const prompt = userMessage.content;
|
| 19 |
const modelForTitle = config.TASK_MODEL?.trim() ? config.TASK_MODEL : conv.model;
|
| 20 |
+
const title =
|
| 21 |
+
(await generateTitle(prompt, modelForTitle, { apiKey: opts?.apiKey })) ?? "New Chat";
|
| 22 |
|
| 23 |
yield {
|
| 24 |
type: MessageUpdateType.Title,
|
|
|
|
| 29 |
}
|
| 30 |
}
|
| 31 |
|
| 32 |
+
export async function generateTitle(prompt: string, modelId?: string, opts?: { apiKey?: string }) {
|
| 33 |
if (config.LLM_SUMMARIZATION !== "true") {
|
| 34 |
// When summarization is disabled, use the first five words without adding emojis
|
| 35 |
const firstFive = prompt.split(/\s+/g).slice(0, 5).join(" ");
|
|
|
|
| 53 |
max_tokens: 30,
|
| 54 |
},
|
| 55 |
modelId,
|
| 56 |
+
apiKey: opts?.apiKey,
|
| 57 |
})
|
| 58 |
)
|
| 59 |
.then((summary) => {
|
src/lib/server/textGeneration/types.ts
CHANGED
|
@@ -14,6 +14,8 @@ export interface TextGenerationContext {
|
|
| 14 |
promptedAt: Date;
|
| 15 |
ip: string;
|
| 16 |
username?: string;
|
|
|
|
|
|
|
| 17 |
/** Force-enable multimodal handling for endpoints that support it */
|
| 18 |
forceMultimodal?: boolean;
|
| 19 |
}
|
|
|
|
| 14 |
promptedAt: Date;
|
| 15 |
ip: string;
|
| 16 |
username?: string;
|
| 17 |
+
/** Optional per-user API key for downstream inference calls */
|
| 18 |
+
authToken?: string;
|
| 19 |
/** Force-enable multimodal handling for endpoints that support it */
|
| 20 |
forceMultimodal?: boolean;
|
| 21 |
}
|