File size: 8,140 Bytes
8000347 877b369 54f6626 79d3242 b37c7b7 38434c2 c02e352 54f6626 c02e352 79d3242 6bcbb4b 54f6626 c02e352 54f6626 c02e352 79d3242 05476c1 79d3242 877b369 86574c0 54f6626 c02e352 b37c7b7 877b369 635c529 78b198f bd9c03c e08d702 c02e352 877b369 86574c0 d48b731 79d3242 05476c1 54f6626 6e0fceb 79d3242 d5e14b5 79d3242 05476c1 e1cf22b 05476c1 86574c0 ff5e863 d48b731 c02e352 05476c1 ce34835 c02e352 05476c1 86574c0 ff5e863 d48b731 05476c1 c02e352 86574c0 d48b731 c02e352 635c529 54f6626 635c529 c02e352 d5e14b5 c02e352 635c529 c02e352 86574c0 635c529 d48b731 635c529 d48b731 635c529 c02e352 635c529 86574c0 635c529 d48b731 635c529 d48b731 635c529 c02e352 54f6626 d48b731 15a83db d48b731 c02e352 d5e14b5 c02e352 54f6626 f1b84d3 d48b731 f1b84d3 79d3242 54f6626 f1b84d3 d48b731 f1b84d3 79d3242 b37c7b7 e1cf22b 6fdab00 bdc9315 c02e352 bdc9315 e1cf22b 05476c1 993f27b 79d3242 b37c7b7 09931c7 b37c7b7 993f27b b37c7b7 993f27b 79d3242 e1cf22b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
<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 http from 'highlight.js/lib/languages/http';
import type { Conversation } from '$lib/types';
import IconCopyCode from '../Icons/IconCopyCode.svelte';
import { onDestroy } from 'svelte';
hljs.registerLanguage('javascript', javascript);
hljs.registerLanguage('python', python);
hljs.registerLanguage('http', http);
export let conversation: Conversation;
const lanuages = ['javascript', 'python', 'http'];
type Language = (typeof lanuages)[number];
const labelsByLanguage: Record<Language, string> = {
javascript: 'JavaScript',
python: 'Python',
http: 'Curl'
};
interface Snippet {
label: string;
code: string;
language?: Language;
}
$: snippetsByLanguage = {
javascript: getJavascriptSnippets(conversation),
python: getPythonSnippets(conversation),
http: getHttpSnippets(conversation)
};
let selectedLanguage: Language = 'javascript';
let timeout: ReturnType<typeof setTimeout>;
function getMessages() {
const placeholder = [{ role: 'user', content: 'Tell me a story' }];
let messages = conversation.messages;
if (messages.length === 1 && messages[0].role === 'user' && !messages[0].content) {
messages = placeholder;
}
return messages;
}
function highlight(code: string, language: Language) {
return hljs.highlight(code, { language }).value;
}
function getJavascriptSnippets(conversation: Conversation) {
const formattedMessages = ({ sep, start, end }) =>
start +
getMessages()
.map(({ role, content }) => `{ role: "${role}", content: "${content}" }`)
.join(sep) +
end;
const formattedConfig = ({ sep, start, end }) =>
start +
Object.entries(conversation.config)
.map(([key, val]) => `${key}: ${val}`)
.join(sep) +
end;
const snippets: Snippet[] = [];
snippets.push({
label: 'Install @huggingface/inference',
language: 'http',
code: `npm install --save @huggingface/inference`
});
if (conversation.streaming) {
snippets.push({
label: 'Streaming API',
code: `import { HfInference } from "@huggingface/inference"
const inference = new HfInference("your HF token")
let out = "";
for await (const chunk of inference.chatCompletionStream({
model: "${conversation.model.id}",
messages: ${formattedMessages({ sep: ',\n ', start: '[\n ', end: '\n ]' })},
${formattedConfig({ sep: ',\n ', start: '', end: '' })},
seed: 0,
})) {
if (chunk.choices && chunk.choices.length > 0) {
const newContent = chunk.choices[0].delta.content;
out += newContent;
console.clear();
console.log(out);
}
}`
});
} else {
// non-streaming
snippets.push({
label: 'Non-Streaming API',
code: `import { HfInference } from '@huggingface/inference'
const inference = new HfInference("your access token")
const out = await inference.chatCompletion({
model: "${conversation.model.id}",
messages: ${formattedMessages({ sep: ',\n ', start: '[\n ', end: '\n ]' })},
${formattedConfig({ sep: ',\n ', start: '', end: '' })},
seed: 0,
});
console.log(out.choices[0].message);`
});
}
return snippets;
}
function getPythonSnippets(conversation: Conversation) {
const formattedMessages = ({ sep, start, end }) =>
start +
getMessages()
.map(({ role, content }) => `{ "role": "${role}", "content": "${content}" }`)
.join(sep) +
end;
const formattedConfig = ({ sep, start, end }) =>
start +
Object.entries(conversation.config)
.map(([key, val]) => `${key}: ${val}`)
.join(sep) +
end;
const snippets: Snippet[] = [];
snippets.push({
label: 'Install huggingface_hub',
language: 'http',
code: `pip install huggingface_hub`
});
if (conversation.streaming) {
snippets.push({
label: 'Streaming API',
code: `from huggingface_hub import InferenceClient
model_id="${conversation.model.id}"
hf_token = "your HF token"
inference_client = InferenceClient(model_id, token=hf_token)
output = ""
messages = ${formattedMessages({ sep: ',\n ', start: `[\n `, end: `\n]` })}
for token in client.chat_completion(messages, stream=True, ${formattedConfig({ sep: ', ', start: '', end: '' })}):
new_content = token.choices[0].delta.content
print(new_content, end="")
output += new_content`
});
} else {
// non-streaming
snippets.push({
label: 'Non-Streaming API',
code: `from huggingface_hub import InferenceClient
model_id="${conversation.model.id}"
hf_token = "your HF token"
inference_client = InferenceClient(model_id, token=hf_token)
messages = ${formattedMessages({ sep: ',\n ', start: `[\n `, end: `\n]` })}
output = inference_client.chat_completion(messages, ${formattedConfig({ sep: ', ', start: '', end: '' })})
print(output.choices[0].message)`
});
}
return snippets;
}
function getHttpSnippets(conversation: Conversation) {
const formattedMessages = ({ sep, start, end }) =>
start +
getMessages()
.map(({ role, content }) => `{ "role": "${role}", "content": "${content}" }`)
.join(sep) +
end;
const formattedConfig = ({ sep, start, end }) =>
start +
Object.entries(conversation.config)
.map(([key, val]) => `"${key}": ${val}`)
.join(sep) +
end;
const snippets: Snippet[] = [];
if (conversation.streaming) {
snippets.push({
label: 'Streaming API',
code: `curl 'https://api-inference.huggingface.co/models/${conversation.model.id}/v1/chat/completions' \\
--header "Authorization: Bearer {YOUR_HF_TOKEN}" \\
--header 'Content-Type: application/json' \\
--data '{
"model": "meta-llama/Meta-Llama-3-8B-Instruct",
"messages": ${formattedMessages({ sep: ',\n ', start: `[\n `, end: `\n]` })},
${formattedConfig({ sep: ',\n ', start: '', end: '' })},
"stream": true
}'`
});
} else {
// non-streaming
snippets.push({
label: 'Non-Streaming API',
code: `curl 'https://api-inference.huggingface.co/models/${conversation.model.id}/v1/chat/completions' \\
--header "Authorization: Bearer {YOUR_HF_TOKEN}" \\
--header 'Content-Type: application/json' \\
--data '{
"model": "meta-llama/Meta-Llama-3-8B-Instruct",
"messages": ${formattedMessages({ sep: ',\n ', start: `[\n `, end: `\n]` })},
${formattedConfig({ sep: ',\n ', start: '', end: '' })}
}'`
});
}
return snippets;
}
onDestroy(() => {
if (timeout) {
clearTimeout(timeout);
}
});
</script>
<div class="px-2 pt-2">
<div
class="border-b border-gray-200 text-center text-sm font-medium text-gray-500 dark:border-gray-700 dark:text-gray-400"
>
<ul class="-mb-px flex flex-wrap">
{#each Object.entries(labelsByLanguage) as [language, label]}
<li>
<button
on:click={() => (selectedLanguage = language)}
class="inline-block rounded-t-lg border-b-2 p-4 {language === selectedLanguage
? 'border-black text-black dark:border-blue-500 dark:text-blue-500'
: 'border-transparent hover:border-gray-300 hover:text-gray-600 dark:hover:text-gray-300'}"
aria-current="page">{label}</button
>
</li>
{/each}
</ul>
</div>
{#each snippetsByLanguage[selectedLanguage] as { label, code, language }}
<div class="flex items-center justify-between px-4 pb-4 pt-6">
<h2 class="font-semibold">{label}</h2>
<button
class="flex items-center gap-x-1.5 rounded-md bg-gray-200 px-1.5 py-0.5 text-sm transition dark:bg-gray-950"
on:click={(e) => {
const el = e.currentTarget;
el.classList.add('text-green-500');
navigator.clipboard.writeText(code);
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
el.classList.remove('text-green-500');
}, 1000);
}}
>
<IconCopyCode /> Copy code
</button>
</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,
language ?? selectedLanguage
)}</pre>
{/each}
</div>
|