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 lines1213141516173 unmodified lines212223242526272892 unmodified lines121122123124125126127272 unmodified lines40040140240340440540611 unmodified linesimport { onDestroy } from 'svelte';import { type ChatCompletionInputMessage } from '@huggingface/tasks';import type { ModelEntryWithTokenizer } from '$lib/types';export let models: ModelEntryWithTokenizer[];3 unmodified lines{id: String(Math.random()),model: models[0],config: { temperature: 0.5, maxTokens: 2048, streaming: true },messages: startMessages}];92 unmodified lines...conversation.messages];if (conversation.config.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} /><div class="mt-auto"><div class="mb-3 flex items-center justify-between gap-2"><label11 unmodified lines121314151617183 unmodified lines22232425262728293092 unmodified lines123124125126127128129272 unmodified lines40240340440540640740840941041111 unmodified linesimport { 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: defaultGenerationConfig,messages: startMessages,streaming: true}];92 unmodified lines...conversation.messages];if (conversation.streaming) {const streamingMessage = { role: 'assistant', content: '' };conversation.messages = [...conversation.messages, streamingMessage];const abortController = new AbortController();272 unmodified lines</select></div><PlaygroundOptionsbind:config={conversations[0].config}bind:streaming={conversations[0].streaming}/><div class="mt-auto"><div class="mb-3 flex items-center justify-between gap-2"><label
55 unmodified lines5657585960616248 unmodified lines11111211311411511611736 unmodified lines15415515615715815916055 unmodified lines# oryarn add @huggingface/inference`});if (conversation.config.streaming) {snippets.push({label: 'Streaming API',code: `import { HfInference } from "@huggingface/inference"48 unmodified lineslanguage: 'bash',code: `pip install huggingface_hub`});if (conversation.config.streaming) {snippets.push({label: 'Streaming API',code: `from huggingface_hub import InferenceClient36 unmodified linesconst messagesStr = getMessages();const snippets: Snippet[] = [];if (conversation.config.streaming) {snippets.push({label: 'Streaming API',code: `curl 'https://api-inference.huggingface.co/models/${conversation.model.id}/v1/chat/completions' \55 unmodified lines5657585960616248 unmodified lines11111211311411511611736 unmodified lines15415515615715815916055 unmodified lines# oryarn add @huggingface/inference`});if (conversation.streaming) {snippets.push({label: 'Streaming API',code: `import { HfInference } from "@huggingface/inference"48 unmodified lineslanguage: 'bash',code: `pip install huggingface_hub`});if (conversation.streaming) {snippets.push({label: 'Streaming API',code: `from huggingface_hub import InferenceClient36 unmodified linesconst messagesStr = getMessages();const snippets: Snippet[] = [];if (conversation.streaming) {snippets.push({label: 'Streaming API',code: `curl 'https://api-inference.huggingface.co/models/${conversation.model.id}/v1/chat/completions' \
33 unmodified lines3435363738394024 unmodified lines65666768697033 unmodified lines<divclass="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}bind:this={messageContainer}>{#if sideBySide}24 unmodified lines><PlaygroundOptionsbind:config={conversation.config}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>33 unmodified lines3435363738394024 unmodified lines6566676869707133 unmodified lines<divclass="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.streaming}bind:this={messageContainer}>{#if sideBySide}24 unmodified lines><PlaygroundOptionsbind: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>
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162<script lang="ts">export let config;export let classNames = '';</script><div class={classNames}><div><div class="flex items-center justify-between"><labelfor="temperature-range"class="mb-2 block text-sm font-medium text-gray-900 dark:text-white">Temperature</label><inputtype="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"/></div><inputid="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"><labelfor="max-tokens-range"class="mb-2 block text-sm font-medium text-gray-900 dark:text-white">Max tokens</label><inputtype="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><inputid="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><div class="mt-2"><label class="flex cursor-pointer items-center justify-between"><input type="checkbox" bind:checked={config.streaming} class="peer sr-only" /><span class="text-sm font-medium text-gray-900 dark:text-gray-300">Streaming</span><divclass="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"123456789101112131415161718192021222324252627282930313233343536373839404142434445<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}{@const settings = GENERATION_CONFIG_SETTINGS[key]}<div><div class="flex items-center justify-between"><labelfor="temperature-range"class="mb-2 block text-sm font-medium text-gray-900 dark:text-white">{settings.label}</label><inputtype="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><inputid="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>{/each}<div class="mt-2"><label class="flex cursor-pointer items-center justify-between"><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><divclass="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);12345678910111213141516171819import 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[];};interface TokenizerConfig {12345678910111213import type { GenerationConfig } from '$lib/components/InferencePlayground/generationConfigSettings';import type { ModelEntry } from '@huggingface/hub';import type { ChatCompletionInputMessage } from '@huggingface/tasks';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