Spaces:
Runtime error
Runtime error
Improve JS snippet (#18)
Browse files21 unmodified lines2223242526272811 unmodified lines4041424243444543444546474849505150525354555657525859605561626364656667626869707172731 unmodified line75767778798081707172737475767778828384858687888990919293949552 unmodified lines14814915013715115215315453 unmodified lines20820921019721121221321421521621720421821922022121 unmodified lines interface Snippet { label: string; code: string; language?: Language; }
const snippetsByLanguage: Record<Language, Snippet[]> = {11 unmodified lines function getJavascriptSnippets() { const snippets: Snippet[] = []; snippets.push({ label: 'Install', code: `import { HfInference } from '@huggingface/inference'
const hf = new HfInference('your access token')` label: 'Install @huggingface/inference', language: 'bash', code: `npm install --save @huggingface/inference# oryarn add @huggingface/inference` }); if (conversation.config.streaming) { snippets.push({ label: 'Streaming API', code: `let out = ""; code: `import { HfInference } from "@huggingface/inference"
const inference = new HfInference("your HF token")
let out = "";
for await (const chunk of hf.chatCompletionStream({for await (const chunk of inference.chatCompletionStream({ model: "${conversation.model}", messages: [ { role: "user", content: "Complete the equation 1+1= ,just the answer" }, { role: "user", content: "Tell me a story" }, ], max_tokens: ${conversation.config.maxTokens}, temperature: ${conversation.config.temperature}, seed: 0,})) { if (chunk.choices && chunk.choices.length > 0) { out += chunk.choices[0].delta.content; const newContent = chunk.choices[0].delta.content; out += newContent; process.stdout.write(newContent); } }` });1 unmodified line // non-streaming snippets.push({ label: 'Non-Streaming API', code: `import { HfInference } from '@huggingface/inference'
const inference = new HfInference("your access token")
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,});`const out = await inference.chatCompletion({ model: "${conversation.model}", messages: [ { role: "user", content: "Who are you?" } ], max_tokens: ${conversation.config.maxTokens}, temperature: ${conversation.config.temperature}, seed: 0,});
console.log(out.choices[0].message);` }); }
52 unmodified lines label: 'Install', code: `import { HfInference } from '@huggingface/inference'
const hf = new HfInference('your access token')`const hf = new HfInference("your access token")` }); if (conversation.config.streaming) { snippets.push({53 unmodified lines </ul> </div>
{#each snippetsByLanguage[selectedLanguage] as { label, code }} {#each snippetsByLanguage[selectedLanguage] as { label, code, language }} <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' language ?? selectedLanguage )}</pre> {/each}</div>src/lib/components/CodeSnippets.svelte+34-20