Spaces:
Sleeping
Sleeping
Improve CodeSnipetts.svelte (#16)
Browse filesFiles changed (2)
12345678910111213141516173 unmodified lines212223242526272829303132333435363738394041424344454647484950515253545518 unmodified lines7475767778798081828384858687888990919293949596<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';export let model: string;export let streaming: Boolean;export let temperature: number;export let maxTokens: number;export let messages: ChatCompletionInputMessage[];type Langauge = 'javascript' | 'python' | 'bash';hljs.registerLanguage('javascript', javascript);hljs.registerLanguage('python', python);3 unmodified linesreturn hljs.highlight(code, { language }).value;}const npmSnippet = `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 = "";for await (const chunk of hf.chatCompletionStream({model: "${model}",messages: [{ role: "user", content: "Complete the equation 1+1= ,just the answer" },],max_tokens: ${maxTokens},temperature: ${temperature},seed: 0,})) {if (chunk.choices && chunk.choices.length > 0) {out += chunk.choices[0].delta.content;}}`;</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><preclass="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><preclass="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></div>12345678910111213141516173 unmodified lines21222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707118 unmodified lines90919293949596979899100101102103<script lang="ts">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 conversation: Conversation;type Langauge = 'javascript' | 'python' | 'bash';interface Snippet {label: string;code: string;}hljs.registerLanguage('javascript', javascript);hljs.registerLanguage('python', python);3 unmodified linesreturn hljs.highlight(code, { language }).value;}function getJavascriptSnippets() {const snippets: Snippet[] = [];snippets.push({label: 'Install',code: `import { HfInference } from '@huggingface/inference'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: "${conversation.model}",messages: [{ role: "user", content: "Complete the equation 1+1= ,just the answer" },],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-streamingsnippets.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>{#each getJavascriptSnippets() as { label, code }}<div class="px-4 pb-4 pt-6"><h2 class="font-semibold">{label}</h2></div><preclass="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 lines103104105106107108102 unmodified lines</div></button>{:else}<CodeSnippets {...conversation} {...conversation.config} />{/if}</div>102 unmodified lines103104105106107108102 unmodified lines</div></button>{:else}<CodeSnippets {conversation} />{/if}</div>
src/lib/components/CodeSnippets.svelte+50-43
src/lib/components/Conversation.svelte+1-1