File size: 5,605 Bytes
8000347
877b369
 
 
 
79d3242
38434c2
c02e352
 
 
 
79d3242
6bcbb4b
c02e352
 
 
 
 
 
 
 
79d3242
 
 
 
877b369
c02e352
 
 
 
 
 
 
877b369
c02e352
877b369
 
 
79d3242
 
 
 
 
bdc9315
79d3242
 
 
 
 
 
e1cf22b
c02e352
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1cf22b
79d3242
e1cf22b
 
 
79d3242
 
e1cf22b
 
 
 
 
79d3242
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1cf22b
 
6fdab00
bdc9315
 
 
 
c02e352
 
 
 
 
 
 
 
 
 
 
bdc9315
 
e1cf22b
c02e352
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
<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';

	hljs.registerLanguage('javascript', javascript);
	hljs.registerLanguage('python', python);
	hljs.registerLanguage('bash', bash);

	export let conversation: Conversation;

	const lanuages = ['javascript', 'python', 'bash'];
	type Language = (typeof lanuages)[number];
	const labelsByLanguage: Record<Language, string> = {
		javascript: 'JavaScript',
		python: 'Python',
		bash: 'Curl'
	};

	interface Snippet {
		label: string;
		code: string;
	}

	const snippetsByLanguage: Record<Language, Snippet[]> = {
		javascript: getJavascriptSnippets(),
		python: getPythonSnippets(),
		bash: getBashSnippets()
	};

	let selectedLanguage: Language = 'javascript';

	function highlight(code: string, language: Language) {
		return 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-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;
	}

	function getPythonSnippets() {
		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-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;
	}

	function getBashSnippets() {
		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-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">
	<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 }}
		<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>