Spaces:
Runtime error
Runtime error
Improve config: add top_p, top_k, repetition_penalty and their correct ranges (#29)
Browse filesFiles changed (6)
- src/lib/components/InferencePlayground/InferencePlayground.svelte+9-4
- src/lib/components/InferencePlayground/InferencePlaygroundCodeSnippets.svelte+3-3
- src/lib/components/InferencePlayground/InferencePlaygroundConversation.svelte+2-1
- src/lib/components/InferencePlayground/InferencePlaygroundGenerationConfig.svelte+32-49
- src/lib/components/InferencePlayground/generationConfigSettings.tsAdded+61
- src/lib/types/index.d.ts+2-8
11 unmodified lines121314151617183 unmodified lines222324242525262728293092 unmodified lines123124125124126127128129272 unmodified lines40240340440340540640740840941041111 unmodified lines import { onDestroy } from 'svelte'; import { type ChatCompletionInputMessage } from '@huggingface/tasks'; import type { ModelEntryWithTokenizer } from '$lib/types'; import { defaultGenerationConfig } from './generationConfigSettings';
export let models: ModelEntryWithTokenizer[];
3 unmodified lines { id: String(Math.random()), model: models[0], config: { temperature: 0.5, maxTokens: 2048, streaming: true }, messages: startMessages config: defaultGenerationConfig, messages: startMessages, streaming: true } ];
92 unmodified lines ...conversation.messages ];
if (conversation.config.streaming) { if (conversation.streaming) { const streamingMessage = { role: 'assistant', content: '' }; conversation.messages = [...conversation.messages, streamingMessage]; const abortController = new AbortController();272 unmodified lines </select> </div>
<PlaygroundOptions bind:config={conversations[0].config} /> <PlaygroundOptions bind:config={conversations[0].config} bind:streaming={conversations[0].streaming} /> <div class="mt-auto"> <div class="mb-3 flex items-center justify-between gap-2"> <label55 unmodified lines565758595960616248 unmodified lines11111211311411411511611736 unmodified lines15415515615715715815916055 unmodified lines# oryarn add @huggingface/inference` }); if (conversation.config.streaming) { if (conversation.streaming) { snippets.push({ label: 'Streaming API', code: `import { HfInference } from "@huggingface/inference"48 unmodified lines language: 'bash', code: `pip install huggingface_hub` }); if (conversation.config.streaming) { if (conversation.streaming) { snippets.push({ label: 'Streaming API', code: `from huggingface_hub import InferenceClient36 unmodified lines const messagesStr = getMessages(); const snippets: Snippet[] = [];
if (conversation.config.streaming) { if (conversation.streaming) { snippets.push({ label: 'Streaming API', code: `curl 'https://api-inference.huggingface.co/models/${conversation.model.id}/v1/chat/completions' \33 unmodified lines343536373738394024 unmodified lines6566676869707133 unmodified lines<div class="flex max-h-[calc(100dvh-5.8rem)] flex-col overflow-y-auto overflow-x-hidden @container" class:pointer-events-none={loading} class:animate-pulse={loading && !conversation.config.streaming} class:animate-pulse={loading && !conversation.streaming} bind:this={messageContainer}> {#if sideBySide}24 unmodified lines > <PlaygroundOptions bind:config={conversation.config} bind:streaming={conversation.streaming} classNames="absolute top-8 right-0 w-56 invisible group-focus:visible hover:visible border border-gray-200/80 bg-white z-10 px-4 py-6 text-sm shadow-sm dark:border-gray-800 dark:bg-gray-800 rounded-xl" /> </button>1234567891067891011121112131415161718192021222324252627281415161718192930313233343536372223242526272829303132333435363738394041424344454647484950515253545556383940415942434445<script lang="ts"> import { GENERATION_CONFIG_KEYS, GENERATION_CONFIG_SETTINGS } from './generationConfigSettings';
export let config; export let streaming; export let classNames = '';</script>
<div class="flex flex-col gap-y-5 {classNames}"> {#each GENERATION_CONFIG_KEYS as key}<div class={classNames}> <div> <div class="flex items-center justify-between"> <label for="temperature-range" class="mb-2 block text-sm font-medium text-gray-900 dark:text-white">Temperature</label > {@const settings = GENERATION_CONFIG_SETTINGS[key]} <div> <div class="flex items-center justify-between"> <label for="temperature-range" class="mb-2 block text-sm font-medium text-gray-900 dark:text-white" >{settings.label}</label > <input type="number" class="w-16 rounded border bg-transparent px-1 py-0.5 text-right text-sm dark:border-gray-700" min={settings.min} max={settings.max} step={settings.step} bind:value={config[key]} /> </div> <input type="number" class="w-16 rounded border bg-transparent px-1 py-0.5 text-right text-sm dark:border-gray-700" bind:value={config.temperature} min="0" max="1" step="0.1" id="temperature-range" type="range" min={settings.min} max={settings.max} step={settings.step} bind:value={config[key]} class="h-2 w-full cursor-pointer appearance-none rounded-lg bg-gray-200 accent-black dark:bg-gray-700 dark:accent-blue-500" /> </div> <input id="temperature-range" type="range" bind:value={config.temperature} min="0" max="1" step="0.1" class="h-2 w-full cursor-pointer appearance-none rounded-lg bg-gray-200 accent-black dark:bg-gray-700 dark:accent-blue-500" /> </div> <div> <div class="flex items-center justify-between"> <label for="max-tokens-range" class="mb-2 block text-sm font-medium text-gray-900 dark:text-white">Max tokens</label > <input type="number" class="w-20 rounded border bg-transparent px-1 py-0.5 text-right text-sm dark:border-gray-700" bind:value={config.maxTokens} min="0" max="4096" step="512" /> </div> <input id="max-tokens-range" type="range" bind:value={config.maxTokens} min="0" max="4096" step="512" class="h-2 w-full cursor-pointer appearance-none rounded-lg bg-gray-200 accent-black dark:bg-gray-700 dark:accent-blue-500" /> </div> {/each}
<div class="mt-2"> <label class="flex cursor-pointer items-center justify-between"> <input type="checkbox" bind:checked={config.streaming} class="peer sr-only" /> <input type="checkbox" bind:checked={streaming} class="peer sr-only" /> <span class="text-sm font-medium text-gray-900 dark:text-gray-300">Streaming</span> <div class="peer relative h-5 w-9 rounded-full bg-gray-200 after:absolute after:start-[2px] after:top-[2px] after:h-4 after:w-4 after:rounded-full after:border after:border-gray-300 after:bg-white after:transition-all after:content-[''] peer-checked:bg-black peer-checked:after:translate-x-full peer-checked:after:border-white peer-focus:outline-none dark:border-gray-600 dark:bg-gray-700 dark:peer-checked:bg-blue-600"12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061interface GenerationKeySettings { default: number; step: number; min: number; max: number; label: string;}
export const GENERATION_CONFIG_SETTINGS: Record<string, GenerationKeySettings> = { temperature: { default: 0.7, step: 0.01, min: 0, max: 2, label: 'Temperature' }, max_tokens: { default: 0.7, step: 512, min: 1, max: 8192, // changed dynamically based on model label: 'Output Length' }, top_p: { default: 0.7, step: 0.01, min: 0, max: 1, label: 'Top-P' }, top_k: { default: 50, step: 1, min: 1, max: 100, label: 'Top-K' }, repetition_penalty: { default: 1, step: 0.01, min: 1, max: 2, label: 'Repetition Penalty' }};
export type GenerationConfigKey = keyof typeof GENERATION_CONFIG_SETTINGS;
export const GENERATION_CONFIG_KEYS: GenerationConfigKey[] = Object.keys( GENERATION_CONFIG_SETTINGS);
export type GenerationConfig = Record<GenerationConfigKey, number>;
export const defaultGenerationConfig = Object.keys(GENERATION_CONFIG_SETTINGS).reduce( (acc, key) => { acc[key] = GENERATION_CONFIG_SETTINGS[key].default; return acc; }, {} as GenerationConfig);123445678910115678910111213import type { GenerationConfig } from '$lib/components/InferencePlayground/generationConfigSettings';import type { ModelEntry } from '@huggingface/hub';import type { ChatCompletionInputMessage } from '@huggingface/tasks';
type Model = string;
type GenerationConfig = { temperature: number; maxTokens: number; streaming: boolean;};
type Conversation = { id: string; model: ModelEntryWithTokenizer; config: GenerationConfig; messages: ChatCompletionInputMessage[]; streaming: boolean;};
interface TokenizerConfig {src/lib/components/InferencePlayground/InferencePlayground.svelte+9-4
src/lib/components/InferencePlayground/InferencePlaygroundCodeSnippets.svelte+3-3
src/lib/components/InferencePlayground/InferencePlaygroundConversation.svelte+2-1
src/lib/components/InferencePlayground/InferencePlaygroundGenerationConfig.svelte+32-49
src/lib/components/InferencePlayground/generationConfigSettings.tsAdded+61
src/lib/types/index.d.ts+2-8