Spaces:
Sleeping
Sleeping
Check if model support system prompt (#26)
Browse files
src/lib/components/InferencePlayground/InferencePlayground.svelte
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
<script lang="ts">
|
2 |
import {
|
3 |
createHfInference,
|
4 |
-
prepareRequestMessages,
|
5 |
handleStreamingResponse,
|
6 |
-
handleNonStreamingResponse
|
|
|
7 |
} from './inferencePlaygroundUtils';
|
8 |
import PlaygroundOptions from './InferencePlaygroundGenerationConfig.svelte';
|
9 |
import PlaygroundTokenModal from './InferencePlaygroundHFTokenModal.svelte';
|
@@ -20,7 +20,7 @@
|
|
20 |
let conversations: Conversation[] = [
|
21 |
{
|
22 |
id: String(Math.random()),
|
23 |
-
model:
|
24 |
config: { temperature: 0.5, maxTokens: 2048, streaming: true },
|
25 |
messages: startMessages
|
26 |
}
|
@@ -40,6 +40,8 @@
|
|
40 |
let abortControllers: AbortController[] = [];
|
41 |
let waitForNonStreaming = true;
|
42 |
|
|
|
|
|
43 |
onDestroy(() => {
|
44 |
for (const abortController of abortControllers) {
|
45 |
abortController.abort();
|
@@ -114,7 +116,10 @@
|
|
114 |
async function runInference(conversation: Conversation) {
|
115 |
const startTime = performance.now();
|
116 |
const hf = createHfInference(hfToken);
|
117 |
-
const requestMessages =
|
|
|
|
|
|
|
118 |
|
119 |
if (conversation.config.streaming) {
|
120 |
const streamingMessage = { role: 'assistant', content: '' };
|
@@ -216,12 +221,16 @@
|
|
216 |
<div class=" flex flex-col overflow-y-auto py-3 pr-3">
|
217 |
<div
|
218 |
class="relative flex flex-1 flex-col gap-6 overflow-y-hidden rounded-r-xl border-x border-y border-gray-200/80 bg-gradient-to-b from-white via-white p-3 shadow-sm dark:border-white/5 dark:from-gray-800/40 dark:via-gray-800/40"
|
|
|
|
|
219 |
>
|
220 |
-
<div class="pb-2 text-sm font-semibold">
|
221 |
<textarea
|
222 |
name=""
|
223 |
id=""
|
224 |
-
placeholder=
|
|
|
|
|
225 |
bind:value={systemMessage.content}
|
226 |
class="absolute inset-x-0 bottom-0 h-full resize-none bg-transparent px-3 pt-10 text-sm outline-none"
|
227 |
></textarea>
|
|
|
1 |
<script lang="ts">
|
2 |
import {
|
3 |
createHfInference,
|
|
|
4 |
handleStreamingResponse,
|
5 |
+
handleNonStreamingResponse,
|
6 |
+
isSystemPromptSupported
|
7 |
} from './inferencePlaygroundUtils';
|
8 |
import PlaygroundOptions from './InferencePlaygroundGenerationConfig.svelte';
|
9 |
import PlaygroundTokenModal from './InferencePlaygroundHFTokenModal.svelte';
|
|
|
20 |
let conversations: Conversation[] = [
|
21 |
{
|
22 |
id: String(Math.random()),
|
23 |
+
model: models[0],
|
24 |
config: { temperature: 0.5, maxTokens: 2048, streaming: true },
|
25 |
messages: startMessages
|
26 |
}
|
|
|
40 |
let abortControllers: AbortController[] = [];
|
41 |
let waitForNonStreaming = true;
|
42 |
|
43 |
+
$: systemPromptSupported = isSystemPromptSupported(conversations[0].model);
|
44 |
+
|
45 |
onDestroy(() => {
|
46 |
for (const abortController of abortControllers) {
|
47 |
abortController.abort();
|
|
|
116 |
async function runInference(conversation: Conversation) {
|
117 |
const startTime = performance.now();
|
118 |
const hf = createHfInference(hfToken);
|
119 |
+
const requestMessages = [
|
120 |
+
...(systemPromptSupported && systemMessage?.content?.length ? [systemMessage] : []),
|
121 |
+
...conversation.messages
|
122 |
+
];
|
123 |
|
124 |
if (conversation.config.streaming) {
|
125 |
const streamingMessage = { role: 'assistant', content: '' };
|
|
|
221 |
<div class=" flex flex-col overflow-y-auto py-3 pr-3">
|
222 |
<div
|
223 |
class="relative flex flex-1 flex-col gap-6 overflow-y-hidden rounded-r-xl border-x border-y border-gray-200/80 bg-gradient-to-b from-white via-white p-3 shadow-sm dark:border-white/5 dark:from-gray-800/40 dark:via-gray-800/40"
|
224 |
+
class:pointer-events-none={!systemPromptSupported}
|
225 |
+
class:opacity-70={!systemPromptSupported}
|
226 |
>
|
227 |
+
<div class="pb-2 text-sm font-semibold uppercase">system</div>
|
228 |
<textarea
|
229 |
name=""
|
230 |
id=""
|
231 |
+
placeholder={systemPromptSupported
|
232 |
+
? 'Enter a custom prompt'
|
233 |
+
: 'System prompt is not supported with the chosen model.'}
|
234 |
bind:value={systemMessage.content}
|
235 |
class="absolute inset-x-0 bottom-0 h-full resize-none bg-transparent px-3 pt-10 text-sm outline-none"
|
236 |
></textarea>
|
src/lib/components/InferencePlayground/inferencePlaygroundUtils.ts
CHANGED
@@ -1,17 +1,11 @@
|
|
1 |
import { type ChatCompletionInputMessage } from '@huggingface/tasks';
|
2 |
import { HfInference } from '@huggingface/inference';
|
|
|
3 |
|
4 |
export function createHfInference(token: string): HfInference {
|
5 |
return new HfInference(token);
|
6 |
}
|
7 |
|
8 |
-
export function prepareRequestMessages(
|
9 |
-
systemMessage: ChatCompletionInputMessage,
|
10 |
-
messages: ChatCompletionInputMessage[]
|
11 |
-
): ChatCompletionInputMessage[] {
|
12 |
-
return [...(systemMessage.content.length ? [systemMessage] : []), ...messages];
|
13 |
-
}
|
14 |
-
|
15 |
export async function handleStreamingResponse(
|
16 |
hf: HfInference,
|
17 |
model: string,
|
@@ -65,3 +59,7 @@ export async function handleNonStreamingResponse(
|
|
65 |
}
|
66 |
throw new Error('No response from the model');
|
67 |
}
|
|
|
|
|
|
|
|
|
|
1 |
import { type ChatCompletionInputMessage } from '@huggingface/tasks';
|
2 |
import { HfInference } from '@huggingface/inference';
|
3 |
+
import type { ModelEntryWithTokenizer } from '$lib/types';
|
4 |
|
5 |
export function createHfInference(token: string): HfInference {
|
6 |
return new HfInference(token);
|
7 |
}
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
export async function handleStreamingResponse(
|
10 |
hf: HfInference,
|
11 |
model: string,
|
|
|
59 |
}
|
60 |
throw new Error('No response from the model');
|
61 |
}
|
62 |
+
|
63 |
+
export function isSystemPromptSupported(model: ModelEntryWithTokenizer) {
|
64 |
+
return model.tokenizerConfig.chat_template?.includes('system');
|
65 |
+
}
|