Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Improve JS snippet (#18)
Browse files
src/lib/components/CodeSnippets.svelte
CHANGED
|
@@ -22,6 +22,7 @@
|
|
| 22 |
interface Snippet {
|
| 23 |
label: string;
|
| 24 |
code: string;
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
const snippetsByLanguage: Record<Language, Snippet[]> = {
|
|
@@ -39,27 +40,34 @@
|
|
| 39 |
function getJavascriptSnippets() {
|
| 40 |
const snippets: Snippet[] = [];
|
| 41 |
snippets.push({
|
| 42 |
-
label: 'Install',
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
});
|
| 47 |
if (conversation.config.streaming) {
|
| 48 |
snippets.push({
|
| 49 |
label: 'Streaming API',
|
| 50 |
-
code: `
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
model: "${conversation.model}",
|
| 54 |
messages: [
|
| 55 |
-
{ role: "user", content: "
|
| 56 |
],
|
| 57 |
max_tokens: ${conversation.config.maxTokens},
|
| 58 |
temperature: ${conversation.config.temperature},
|
| 59 |
seed: 0,
|
| 60 |
})) {
|
| 61 |
if (chunk.choices && chunk.choices.length > 0) {
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
}
|
| 64 |
}`
|
| 65 |
});
|
|
@@ -67,15 +75,21 @@ for await (const chunk of hf.chatCompletionStream({
|
|
| 67 |
// non-streaming
|
| 68 |
snippets.push({
|
| 69 |
label: 'Non-Streaming API',
|
| 70 |
-
code: `
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
});
|
| 80 |
}
|
| 81 |
|
|
@@ -134,7 +148,7 @@ for await (const chunk of hf.chatCompletionStream({
|
|
| 134 |
label: 'Install',
|
| 135 |
code: `import { HfInference } from '@huggingface/inference'
|
| 136 |
|
| 137 |
-
const hf = new HfInference(
|
| 138 |
});
|
| 139 |
if (conversation.config.streaming) {
|
| 140 |
snippets.push({
|
|
@@ -194,14 +208,14 @@ for await (const chunk of hf.chatCompletionStream({
|
|
| 194 |
</ul>
|
| 195 |
</div>
|
| 196 |
|
| 197 |
-
{#each snippetsByLanguage[selectedLanguage] as { label, code }}
|
| 198 |
<div class="px-4 pb-4 pt-6">
|
| 199 |
<h2 class="font-semibold">{label}</h2>
|
| 200 |
</div>
|
| 201 |
<pre
|
| 202 |
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(
|
| 203 |
code,
|
| 204 |
-
|
| 205 |
)}</pre>
|
| 206 |
{/each}
|
| 207 |
</div>
|
|
|
|
| 22 |
interface Snippet {
|
| 23 |
label: string;
|
| 24 |
code: string;
|
| 25 |
+
language?: Language;
|
| 26 |
}
|
| 27 |
|
| 28 |
const snippetsByLanguage: Record<Language, Snippet[]> = {
|
|
|
|
| 40 |
function getJavascriptSnippets() {
|
| 41 |
const snippets: Snippet[] = [];
|
| 42 |
snippets.push({
|
| 43 |
+
label: 'Install @huggingface/inference',
|
| 44 |
+
language: 'bash',
|
| 45 |
+
code: `npm install --save @huggingface/inference
|
| 46 |
+
# or
|
| 47 |
+
yarn add @huggingface/inference`
|
| 48 |
});
|
| 49 |
if (conversation.config.streaming) {
|
| 50 |
snippets.push({
|
| 51 |
label: 'Streaming API',
|
| 52 |
+
code: `import { HfInference } from "@huggingface/inference"
|
| 53 |
|
| 54 |
+
const inference = new HfInference("your HF token")
|
| 55 |
+
|
| 56 |
+
let out = "";
|
| 57 |
+
|
| 58 |
+
for await (const chunk of inference.chatCompletionStream({
|
| 59 |
model: "${conversation.model}",
|
| 60 |
messages: [
|
| 61 |
+
{ role: "user", content: "Tell me a story" },
|
| 62 |
],
|
| 63 |
max_tokens: ${conversation.config.maxTokens},
|
| 64 |
temperature: ${conversation.config.temperature},
|
| 65 |
seed: 0,
|
| 66 |
})) {
|
| 67 |
if (chunk.choices && chunk.choices.length > 0) {
|
| 68 |
+
const newContent = chunk.choices[0].delta.content;
|
| 69 |
+
out += newContent;
|
| 70 |
+
process.stdout.write(newContent);
|
| 71 |
}
|
| 72 |
}`
|
| 73 |
});
|
|
|
|
| 75 |
// non-streaming
|
| 76 |
snippets.push({
|
| 77 |
label: 'Non-Streaming API',
|
| 78 |
+
code: `import { HfInference } from '@huggingface/inference'
|
| 79 |
+
|
| 80 |
+
const inference = new HfInference("your access token")
|
| 81 |
+
|
| 82 |
+
const out = await inference.chatCompletion({
|
| 83 |
+
model: "${conversation.model}",
|
| 84 |
+
messages: [
|
| 85 |
+
{ role: "user", content: "Who are you?" }
|
| 86 |
+
],
|
| 87 |
+
max_tokens: ${conversation.config.maxTokens},
|
| 88 |
+
temperature: ${conversation.config.temperature},
|
| 89 |
+
seed: 0,
|
| 90 |
+
});
|
| 91 |
+
|
| 92 |
+
console.log(out.choices[0].message);`
|
| 93 |
});
|
| 94 |
}
|
| 95 |
|
|
|
|
| 148 |
label: 'Install',
|
| 149 |
code: `import { HfInference } from '@huggingface/inference'
|
| 150 |
|
| 151 |
+
const hf = new HfInference("your access token")`
|
| 152 |
});
|
| 153 |
if (conversation.config.streaming) {
|
| 154 |
snippets.push({
|
|
|
|
| 208 |
</ul>
|
| 209 |
</div>
|
| 210 |
|
| 211 |
+
{#each snippetsByLanguage[selectedLanguage] as { label, code, language }}
|
| 212 |
<div class="px-4 pb-4 pt-6">
|
| 213 |
<h2 class="font-semibold">{label}</h2>
|
| 214 |
</div>
|
| 215 |
<pre
|
| 216 |
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(
|
| 217 |
code,
|
| 218 |
+
language ?? selectedLanguage
|
| 219 |
)}</pre>
|
| 220 |
{/each}
|
| 221 |
</div>
|