mishig HF staff commited on
Commit
79d3242
1 Parent(s): 877b369

Improve CodeSnipetts.svelte (#16)

Browse files
src/lib/components/CodeSnippets.svelte CHANGED
@@ -1,17 +1,17 @@
1
  <script lang="ts">
2
- import { type ChatCompletionInputMessage } from '@huggingface/tasks';
3
  import hljs from 'highlight.js/lib/core';
4
  import javascript from 'highlight.js/lib/languages/javascript';
5
  import python from 'highlight.js/lib/languages/python';
6
  import bash from 'highlight.js/lib/languages/bash';
 
7
 
8
- export let model: string;
9
- export let streaming: Boolean;
10
- export let temperature: number;
11
- export let maxTokens: number;
12
- export let messages: ChatCompletionInputMessage[];
13
 
14
  type Langauge = 'javascript' | 'python' | 'bash';
 
 
 
 
15
 
16
  hljs.registerLanguage('javascript', javascript);
17
  hljs.registerLanguage('python', python);
@@ -21,35 +21,51 @@
21
  return hljs.highlight(code, { language }).value;
22
  }
23
 
24
- const npmSnippet = `import { HfInference } from '@huggingface/inference'
 
 
 
 
25
 
26
- const hf = new HfInference('your access token')`;
27
-
28
- $: nonStreamingSnippet = `await hf.chatCompletion({
29
- model: "${model}",
30
- messages: [
31
- { role: "user", content: "Complete the this sentence with words one plus one is equal " }
32
- ],
33
- max_tokens: ${maxTokens},
34
- temperature: ${temperature},
35
- seed: 0,
36
- });`;
37
-
38
- $: streamingSnippet = `let out = "";
39
 
40
  for await (const chunk of hf.chatCompletionStream({
41
- model: "${model}",
42
  messages: [
43
  { role: "user", content: "Complete the equation 1+1= ,just the answer" },
44
  ],
45
- max_tokens: ${maxTokens},
46
- temperature: ${temperature},
47
  seed: 0,
48
  })) {
49
  if (chunk.choices && chunk.choices.length > 0) {
50
  out += chunk.choices[0].delta.content;
51
  }
52
- }`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  </script>
54
 
55
  <div class="px-2 pt-2">
@@ -74,23 +90,14 @@ for await (const chunk of hf.chatCompletionStream({
74
  </ul>
75
  </div>
76
 
77
- <div class="px-4 pb-4 pt-6">
78
- <h2 class="font-semibold">Install and instantiate</h2>
79
- </div>
80
- <pre
81
- 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(
82
- npmSnippet,
83
- 'javascript'
84
- )}</pre>
85
-
86
- <div class="px-4 pb-4 pt-6">
87
- <h2 class="font-semibold">{streaming ? 'Streaming API' : 'Non-Streaming API'}</h2>
88
- </div>
89
-
90
- <pre
91
- 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(
92
- streaming ? streamingSnippet : nonStreamingSnippet,
93
- 'javascript'
94
- )}
95
- </pre>
96
  </div>
 
1
  <script lang="ts">
 
2
  import hljs from 'highlight.js/lib/core';
3
  import javascript from 'highlight.js/lib/languages/javascript';
4
  import python from 'highlight.js/lib/languages/python';
5
  import bash from 'highlight.js/lib/languages/bash';
6
+ import type { Conversation } from '$lib/types';
7
 
8
+ export let conversation: Conversation;
 
 
 
 
9
 
10
  type Langauge = 'javascript' | 'python' | 'bash';
11
+ interface Snippet {
12
+ label: string;
13
+ code: string;
14
+ }
15
 
16
  hljs.registerLanguage('javascript', javascript);
17
  hljs.registerLanguage('python', python);
 
21
  return hljs.highlight(code, { language }).value;
22
  }
23
 
24
+ function getJavascriptSnippets() {
25
+ const snippets: Snippet[] = [];
26
+ snippets.push({
27
+ label: 'Install',
28
+ code: `import { HfInference } from '@huggingface/inference'
29
 
30
+ const hf = new HfInference('your access token')`
31
+ });
32
+ if (conversation.config.streaming) {
33
+ snippets.push({
34
+ label: 'Streaming API',
35
+ code: `let out = "";
 
 
 
 
 
 
 
36
 
37
  for await (const chunk of hf.chatCompletionStream({
38
+ model: "${conversation.model}",
39
  messages: [
40
  { role: "user", content: "Complete the equation 1+1= ,just the answer" },
41
  ],
42
+ max_tokens: ${conversation.config.maxTokens},
43
+ temperature: ${conversation.config.temperature},
44
  seed: 0,
45
  })) {
46
  if (chunk.choices && chunk.choices.length > 0) {
47
  out += chunk.choices[0].delta.content;
48
  }
49
+ }`
50
+ });
51
+ } else {
52
+ // non-streaming
53
+ snippets.push({
54
+ label: 'Non-Streaming API',
55
+ code: `await hf.chatCompletion({
56
+ model: "${conversation.model}",
57
+ messages: [
58
+ { role: "user", content: "Complete the this sentence with words one plus one is equal " }
59
+ ],
60
+ max_tokens: ${conversation.config.maxTokens},
61
+ temperature: ${conversation.config.temperature},
62
+ seed: 0,
63
+ });`
64
+ });
65
+ }
66
+
67
+ return snippets;
68
+ }
69
  </script>
70
 
71
  <div class="px-2 pt-2">
 
90
  </ul>
91
  </div>
92
 
93
+ {#each getJavascriptSnippets() as { label, code }}
94
+ <div class="px-4 pb-4 pt-6">
95
+ <h2 class="font-semibold">{label}</h2>
96
+ </div>
97
+ <pre
98
+ 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(
99
+ code,
100
+ 'javascript'
101
+ )}</pre>
102
+ {/each}
 
 
 
 
 
 
 
 
 
103
  </div>
src/lib/components/Conversation.svelte CHANGED
@@ -103,6 +103,6 @@
103
  </div>
104
  </button>
105
  {:else}
106
- <CodeSnippets {...conversation} {...conversation.config} />
107
  {/if}
108
  </div>
 
103
  </div>
104
  </button>
105
  {:else}
106
+ <CodeSnippets {conversation} />
107
  {/if}
108
  </div>