Spaces:
Sleeping
Sleeping
conv ui
Browse filesFiles changed (2)
186 unmodified lines187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223186 unmodified lines</div><div class="relative divide-y divide-gray-200 dark:divide-gray-800"><divclass="@container flex max-h-[calc(100dvh-5rem)] flex-col divide-y divide-gray-200 overflow-y-auto overflow-x-hidden dark:divide-gray-800"bind:this={messageContainer}>{#if !viewCode}{#each messages as message, i}<PlaygroundMessage {message} on:delete={() => deleteMessage(i)} />{/each}<buttonclass="flex px-6 py-6 hover:bg-gray-50 dark:hover:bg-gray-800/50"on:click={addMessage}><div class="flex items-center gap-2 !p-0 text-sm font-semibold"><svgxmlns="http://www.w3.org/2000/svg"width="1em"height="1em"viewBox="0 0 32 32"class="text-lg"><pathfill="currentColor"d="M16 2A14.172 14.172 0 0 0 2 16a14.172 14.172 0 0 0 14 14a14.172 14.172 0 0 0 14-14A14.172 14.172 0 0 0 16 2Zm8 15h-7v7h-2v-7H8v-2h7V8h2v7h7Z"/><path fill="none" d="M24 17h-7v7h-2v-7H8v-2h7V8h2v7h7v2z" /></svg>Add message</div></button>{:else}<PlaygroundCode {...currentConversation} {...currentConversation.config} />{/if}</div><divclass="inset-x-0 bottom-0 flex h-20 items-center gap-2 overflow-hidden whitespace-nowrap px-5 md:absolute">186 unmodified lines187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239186 unmodified lines</div><div class="relative divide-y divide-gray-200 dark:divide-gray-800"><divclass="flex h-[calc(100dvh-5rem)] divide-x divide-gray-200 {conversations.length === 2? '*:w-1/2': conversations.length == 3? '*:w-1/3': ''} dark:divide-gray-800">{#each conversations as conversation}<divclass="@container flex max-h-[calc(100dvh-5rem)] flex-col divide-y divide-gray-200 overflow-y-auto overflow-x-hidden dark:divide-gray-800"bind:this={messageContainer}>{#if conversations.length > 1}<divclass="flex h-10 items-center bg-gradient-to-r from-gray-50 px-6 text-gray-500 dark:from-gray-400/20">{conversation.model}</div>{/if}{#if !viewCode}{#each messages as message, i}<PlaygroundMessage {message} on:delete={() => deleteMessage(i)} />{/each}<buttonclass="flex px-6 py-6 hover:bg-gray-50 dark:hover:bg-gray-800/50"on:click={addMessage}><div class="flex items-center gap-2 !p-0 text-sm font-semibold"><svgxmlns="http://www.w3.org/2000/svg"width="1em"height="1em"viewBox="0 0 32 32"class="text-lg"><pathfill="currentColor"d="M16 2A14.172 14.172 0 0 0 2 16a14.172 14.172 0 0 0 14 14a14.172 14.172 0 0 0 14-14A14.172 14.172 0 0 0 16 2Zm8 15h-7v7h-2v-7H8v-2h7V8h2v7h7Z"/><path fill="none" d="M24 17h-7v7h-2v-7H8v-2h7V8h2v7h7v2z" /></svg>Add message</div></button>{:else}<PlaygroundCode {...currentConversation} {...currentConversation.config} />{/if}</div>{/each}</div><divclass="inset-x-0 bottom-0 flex h-20 items-center gap-2 overflow-hidden whitespace-nowrap px-5 md:absolute">
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364import { HfInference } from '@huggingface/inference';import type { ChatCompletionStreamOutput, ChatCompletionOutput } from '@huggingface/inference';export interface Message {role: string;content: string;}export function createHfInference(token: string): HfInference {return new HfInference(token);}export function prepareRequestMessages(systemMessage: Message, messages: Message[]): Message[] {return [...(systemMessage.content.length ? [systemMessage] : []),...messages];}export async function handleStreamingResponse(hf: HfInference,model: string,messages: Message[],temperature: number,maxTokens: number,jsonMode: boolean,onChunk: (content: string) => void): Promise<void> {let out = '';for await (const chunk of hf.chatCompletionStream({model: model,messages: messages,temperature: temperature,max_tokens: maxTokens,json_mode: jsonMode})) {if (chunk.choices && chunk.choices.length > 0 && chunk.choices[0]?.delta?.content) {out += chunk.choices[0].delta.content;onChunk(out);}}}export async function handleNonStreamingResponse(hf: HfInference,model: string,messages: Message[],temperature: number,maxTokens: number,jsonMode: boolean): Promise<Message> {const response = await hf.chatCompletion({model: model,messages: messages,temperature: temperature,max_tokens: maxTokens,json_mode: jsonMode});if (response.choices && response.choices.length > 0) {return response.choices[0].message;}throw new Error('No response from the model');}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import { HfInference } from '@huggingface/inference';export interface Message {role: string;content: string;}export function createHfInference(token: string): HfInference {return new HfInference(token);}export function prepareRequestMessages(systemMessage: Message, messages: Message[]): Message[] {return [...(systemMessage.content.length ? [systemMessage] : []), ...messages];}export async function handleStreamingResponse(hf: HfInference,model: string,messages: Message[],temperature: number,maxTokens: number,jsonMode: boolean,onChunk: (content: string) => void): Promise<void> {let out = '';for await (const chunk of hf.chatCompletionStream({model: model,messages: messages,temperature: temperature,max_tokens: maxTokens,json_mode: jsonMode})) {if (chunk.choices && chunk.choices.length > 0 && chunk.choices[0]?.delta?.content) {out += chunk.choices[0].delta.content;onChunk(out);}}}export async function handleNonStreamingResponse(hf: HfInference,model: string,messages: Message[],temperature: number,maxTokens: number,jsonMode: boolean): Promise<Message> {const response = await hf.chatCompletion({model: model,messages: messages,temperature: temperature,max_tokens: maxTokens,json_mode: jsonMode});if (response.choices && response.choices.length > 0) {return response.choices[0].message;}throw new Error('No response from the model');}
src/lib/components/Playground/Playground.svelte+44-28
src/lib/components/Playground/playgroundUtils.ts+41-45