victor HF staff commited on
Commit
5640214
1 Parent(s): c077f8a
package.json CHANGED
@@ -30,7 +30,6 @@
30
  },
31
  "type": "module",
32
  "dependencies": {
33
- "@huggingface/inference": "^2.7.0",
34
- "sveltekit-search-params": "^2.1.2"
35
  }
36
  }
 
30
  },
31
  "type": "module",
32
  "dependencies": {
33
+ "@huggingface/inference": "^2.7.0"
 
34
  }
35
  }
src/lib/components/Playground/Playground.svelte CHANGED
@@ -1,6 +1,5 @@
1
  <script lang="ts">
2
  import { HfInference } from '@huggingface/inference';
3
- import { queryParam, ssp } from 'sveltekit-search-params';
4
 
5
  import PlaygroundCode from './PlaygroundCode.svelte';
6
  import PlaygroundMessage from '$lib/components/Playground/PlaygroundMessage.svelte';
@@ -39,23 +38,13 @@
39
 
40
  const startMessages: Message[] = [{ role: 'user', content: '' }];
41
 
42
- const messagesParam = queryParam('messages', {
43
- encode: (value: Message[]) => JSON.stringify(value),
44
- decode: (value: string | null) => (value ? JSON.parse(value) : startMessages)
45
- });
46
-
47
- const systemMessageParam = queryParam('system', {
48
- encode: (value: string) => value,
49
- decode: (value: string | null) => value || ''
50
- });
51
-
52
- const currentModel = queryParam('model', ssp.string(compatibleModels[0]));
53
- const temperature = queryParam('temperature', ssp.number(0.5));
54
- const maxTokens = queryParam('max_tokens', ssp.number(2048));
55
- const streaming = queryParam('streaming', ssp.boolean(true));
56
- const jsonMode = queryParam('json_mode', ssp.boolean(false));
57
- $: systemMessage = { role: 'system', content: $systemMessageParam };
58
- $: messages = $messagesParam;
59
 
60
  let hfToken: string | null = '';
61
  let viewCode = false;
@@ -66,19 +55,19 @@
66
  let messageContainer: HTMLDivElement | null = null;
67
 
68
  function addMessage() {
69
- $messagesParam = [
70
- ...$messagesParam,
71
- { role: $messagesParam.at(-1)?.role === 'user' ? 'assistant' : 'user', content: '' }
72
  ];
73
  }
74
 
75
  function deleteMessage(i: number) {
76
- $messagesParam = $messagesParam.filter((_, j) => j !== i);
77
  }
78
 
79
  function reset() {
80
- $messagesParam = [...startMessages];
81
- $systemMessageParam = '';
82
  }
83
 
84
  function onKeydown(event: KeyboardEvent) {
@@ -108,37 +97,37 @@
108
 
109
  if (streaming) {
110
  streamingMessage = { role: 'assistant', content: '' };
111
- $messagesParam = [...$messagesParam, streamingMessage];
112
  let out = '';
113
 
114
  for await (const chunk of hf.chatCompletionStream({
115
- model: $currentModel,
116
  messages: requestMessages,
117
- temperature: $temperature,
118
- max_tokens: $maxTokens,
119
- json_mode: $jsonMode
120
  })) {
121
  if (chunk.choices && chunk.choices.length > 0) {
122
  if (streamingMessage && chunk.choices[0]?.delta?.content) {
123
  out += chunk.choices[0].delta.content;
124
  streamingMessage.content = out;
125
- $messagesParam = [...$messagesParam];
126
  scrollToBottom();
127
  }
128
  }
129
  }
130
  } else {
131
  const response = await hf.chatCompletion({
132
- model: $currentModel,
133
  messages: requestMessages,
134
- temperature: $temperature,
135
- max_tokens: $maxTokens,
136
- json_mode: $jsonMode
137
  });
138
 
139
  if (response.choices && response.choices.length > 0) {
140
  const newMessage = { role: 'assistant', content: response.choices[0].message.content };
141
- $messagesParam = [...$messagesParam, newMessage];
142
  scrollToBottom();
143
  }
144
  }
@@ -175,7 +164,7 @@
175
  {/if}
176
 
177
  <div
178
- class="w-dvh maxdivide-gray-200 grid h-dvh overflow-hidden max-md:grid-cols-1 max-md:divide-y md:grid-cols-[260px,minmax(0,1fr),260px] md:divide-x dark:divide-gray-800 dark:bg-gray-900 dark:text-gray-300"
179
  >
180
  <div class="relative flex flex-col overflow-y-auto px-5 pb-24 pt-7">
181
  <div class="pb-2 text-sm font-semibold">SYSTEM</div>
@@ -183,7 +172,7 @@
183
  name=""
184
  id=""
185
  placeholder="Enter a custom prompt"
186
- bind:value={$systemMessageParam}
187
  class="absolute inset-x-0 bottom-0 h-full resize-none bg-transparent p-2 pl-5 pr-3 pt-16 text-sm outline-none"
188
  ></textarea>
189
  </div>
@@ -204,12 +193,7 @@
204
  <div class="!p-0 text-sm font-semibold">Add message</div>
205
  </button>
206
  {:else}
207
- <PlaygroundCode
208
- model={$currentModel}
209
- streaming={$streaming}
210
- temperature={$temperature}
211
- maxTokens={$maxTokens}
212
- />
213
  {/if}
214
  </div>
215
 
@@ -260,7 +244,7 @@
260
  submit();
261
  }}
262
  type="button"
263
- class="flex h-[42px] w-24 items-center justify-center rounded-lg bg-black px-5 py-2.5 text-sm font-medium text-white hover:bg-gray-900 focus:outline-none focus:ring-4 focus:ring-gray-300 disabled:opacity-50 dark:border-gray-700 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-gray-700"
264
  >
265
  {#if loading}
266
  <div class="flex flex-none items-center gap-[3px]">
@@ -278,7 +262,10 @@
278
  />
279
  </div>
280
  {:else}
281
- Submit
 
 
 
282
  {/if}
283
  </button>
284
  </div>
@@ -286,11 +273,11 @@
286
  <div class="flex flex-col gap-6 overflow-hidden p-5">
287
  <PlaygroundOptions
288
  {compatibleModels}
289
- bind:currentModel={$currentModel}
290
- bind:temperature={$temperature}
291
- bind:maxTokens={$maxTokens}
292
- bind:jsonMode={$jsonMode}
293
- bind:streaming={$streaming}
294
  />
295
  </div>
296
  </div>
 
1
  <script lang="ts">
2
  import { HfInference } from '@huggingface/inference';
 
3
 
4
  import PlaygroundCode from './PlaygroundCode.svelte';
5
  import PlaygroundMessage from '$lib/components/Playground/PlaygroundMessage.svelte';
 
38
 
39
  const startMessages: Message[] = [{ role: 'user', content: '' }];
40
 
41
+ let messages = startMessages;
42
+ let systemMessage = { role: 'system', content: '' };
43
+ let currentModel = compatibleModels[0];
44
+ let temperature = 0.5;
45
+ let maxTokens = 2048;
46
+ let streaming = true;
47
+ let jsonMode = false;
 
 
 
 
 
 
 
 
 
 
48
 
49
  let hfToken: string | null = '';
50
  let viewCode = false;
 
55
  let messageContainer: HTMLDivElement | null = null;
56
 
57
  function addMessage() {
58
+ messages = [
59
+ ...messages,
60
+ { role: messages.at(-1)?.role === 'user' ? 'assistant' : 'user', content: '' }
61
  ];
62
  }
63
 
64
  function deleteMessage(i: number) {
65
+ messages = messages.filter((_, j) => j !== i);
66
  }
67
 
68
  function reset() {
69
+ messages = [...startMessages];
70
+ systemMessage.content = '';
71
  }
72
 
73
  function onKeydown(event: KeyboardEvent) {
 
97
 
98
  if (streaming) {
99
  streamingMessage = { role: 'assistant', content: '' };
100
+ messages = [...messages, streamingMessage];
101
  let out = '';
102
 
103
  for await (const chunk of hf.chatCompletionStream({
104
+ model: currentModel,
105
  messages: requestMessages,
106
+ temperature: temperature,
107
+ max_tokens: maxTokens,
108
+ json_mode: jsonMode
109
  })) {
110
  if (chunk.choices && chunk.choices.length > 0) {
111
  if (streamingMessage && chunk.choices[0]?.delta?.content) {
112
  out += chunk.choices[0].delta.content;
113
  streamingMessage.content = out;
114
+ messages = [...messages];
115
  scrollToBottom();
116
  }
117
  }
118
  }
119
  } else {
120
  const response = await hf.chatCompletion({
121
+ model: currentModel,
122
  messages: requestMessages,
123
+ temperature: temperature,
124
+ max_tokens: maxTokens,
125
+ json_mode: jsonMode
126
  });
127
 
128
  if (response.choices && response.choices.length > 0) {
129
  const newMessage = { role: 'assistant', content: response.choices[0].message.content };
130
+ messages = [...messages, newMessage];
131
  scrollToBottom();
132
  }
133
  }
 
164
  {/if}
165
 
166
  <div
167
+ class="w-dvh maxdivide-gray-200 grid overflow-hidden max-md:grid-cols-1 max-md:divide-y md:h-dvh md:grid-cols-[260px,minmax(0,1fr),260px] md:divide-x dark:divide-gray-800 dark:bg-gray-900 dark:text-gray-300"
168
  >
169
  <div class="relative flex flex-col overflow-y-auto px-5 pb-24 pt-7">
170
  <div class="pb-2 text-sm font-semibold">SYSTEM</div>
 
172
  name=""
173
  id=""
174
  placeholder="Enter a custom prompt"
175
+ bind:value={systemMessage.content}
176
  class="absolute inset-x-0 bottom-0 h-full resize-none bg-transparent p-2 pl-5 pr-3 pt-16 text-sm outline-none"
177
  ></textarea>
178
  </div>
 
193
  <div class="!p-0 text-sm font-semibold">Add message</div>
194
  </button>
195
  {:else}
196
+ <PlaygroundCode model={currentModel} {streaming} {temperature} {maxTokens} />
 
 
 
 
 
197
  {/if}
198
  </div>
199
 
 
244
  submit();
245
  }}
246
  type="button"
247
+ class="flex h-[42px] w-24 items-center justify-center gap-2 rounded-lg bg-black px-5 py-2.5 text-sm font-medium text-white hover:bg-gray-900 focus:outline-none focus:ring-4 focus:ring-gray-300 disabled:opacity-50 dark:border-gray-700 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-gray-700"
248
  >
249
  {#if loading}
250
  <div class="flex flex-none items-center gap-[3px]">
 
262
  />
263
  </div>
264
  {:else}
265
+ Run <span
266
+ class="inline-flex gap-0.5 rounded border border-white/20 bg-white/10 px-0.5 text-xs text-white/70"
267
+ >⌘<span class="translate-y-px">↵</span></span
268
+ >
269
  {/if}
270
  </button>
271
  </div>
 
273
  <div class="flex flex-col gap-6 overflow-hidden p-5">
274
  <PlaygroundOptions
275
  {compatibleModels}
276
+ bind:currentModel
277
+ bind:temperature
278
+ bind:maxTokens
279
+ bind:jsonMode
280
+ bind:streaming
281
  />
282
  </div>
283
  </div>