Spaces:
Sleeping
Sleeping
Improve CodeSnipetts.svelte (#16)
Browse filesFiles changed (2)
12234567891011128910111213141516173 unmodified lines21222324252627242829262728293031323334353637383031323334353637413839404145464243444546474852495051525354555657585960616263646566676869707118 unmodified lines9091927778798081828384858687888990919293949593949596979899100101102103<script lang="ts"> import { type ChatCompletionInputMessage } from '@huggingface/tasks'; import hljs from 'highlight.js/lib/core'; import javascript from 'highlight.js/lib/languages/javascript'; import python from 'highlight.js/lib/languages/python'; import bash from 'highlight.js/lib/languages/bash'; import type { Conversation } from '$lib/types';
export let model: string; export let streaming: Boolean; export let temperature: number; export let maxTokens: number; export let messages: ChatCompletionInputMessage[]; export let conversation: Conversation;
type Langauge = 'javascript' | 'python' | 'bash'; interface Snippet { label: string; code: string; }
hljs.registerLanguage('javascript', javascript); hljs.registerLanguage('python', python);3 unmodified lines return hljs.highlight(code, { language }).value; }
function getJavascriptSnippets() { const snippets: Snippet[] = []; snippets.push({ label: 'Install', const npmSnippet = `import { HfInference } from '@huggingface/inference' code: `import { HfInference } from '@huggingface/inference'
const hf = new HfInference('your access token')`;
$: nonStreamingSnippet = `await hf.chatCompletion({ model: "${model}", messages: [ { role: "user", content: "Complete the this sentence with words one plus one is equal " } ], max_tokens: ${maxTokens}, temperature: ${temperature}, seed: 0,});`;
$: streamingSnippet = `let out = "";const hf = new HfInference('your access token')` }); if (conversation.config.streaming) { snippets.push({ label: 'Streaming API', code: `let out = "";
for await (const chunk of hf.chatCompletionStream({ model: "${model}", model: "${conversation.model}", messages: [ { role: "user", content: "Complete the equation 1+1= ,just the answer" }, ], max_tokens: ${maxTokens}, temperature: ${temperature}, max_tokens: ${conversation.config.maxTokens}, temperature: ${conversation.config.temperature}, seed: 0,})) { if (chunk.choices && chunk.choices.length > 0) { out += chunk.choices[0].delta.content; } }`;}` }); } else { // non-streaming snippets.push({ label: 'Non-Streaming API', code: `await hf.chatCompletion({ model: "${conversation.model}", messages: [ { role: "user", content: "Complete the this sentence with words one plus one is equal " } ], max_tokens: ${conversation.config.maxTokens}, temperature: ${conversation.config.temperature}, seed: 0,});` }); }
return snippets; }</script>
<div class="px-2 pt-2">18 unmodified lines </ul> </div>
<div class="px-4 pb-4 pt-6"> <h2 class="font-semibold">Install and instantiate</h2> </div> <pre class="overflow-x-auto rounded-lg border border-gray-200/80 bg-white px-4 py-6 text-sm shadow-sm dark:border-gray-800 dark:bg-gray-800/50">{@html highlight( npmSnippet, 'javascript' )}</pre>
<div class="px-4 pb-4 pt-6"> <h2 class="font-semibold">{streaming ? 'Streaming API' : 'Non-Streaming API'}</h2> </div>
<pre class="overflow-x-auto rounded-lg border border-gray-200/80 bg-white px-4 py-6 text-sm shadow-sm dark:border-gray-800 dark:bg-gray-800/50">{@html highlight( streaming ? streamingSnippet : nonStreamingSnippet, 'javascript' )} </pre> {#each getJavascriptSnippets() as { label, code }} <div class="px-4 pb-4 pt-6"> <h2 class="font-semibold">{label}</h2> </div> <pre class="overflow-x-auto rounded-lg border border-gray-200/80 bg-white px-4 py-6 text-sm shadow-sm dark:border-gray-800 dark:bg-gray-800/50">{@html highlight( code, 'javascript' )}</pre> {/each}</div>102 unmodified lines103104105106106107108102 unmodified lines </div> </button> {:else} <CodeSnippets {...conversation} {...conversation.config} /> <CodeSnippets {conversation} /> {/if}</div>src/lib/components/CodeSnippets.svelte+50-43
src/lib/components/Conversation.svelte+1-1