victor HF staff commited on
Commit
a367e47
1 Parent(s): 65b948d

Implemented streaming mode and added option to toggle it in the Playground component.

Browse files
src/lib/components/Playground/Playground.svelte CHANGED
@@ -23,6 +23,7 @@
23
  let messages: Message[] = startMessages;
24
  let temperature = 0.5;
25
  let maxTokens = 32000;
 
26
 
27
  let loading = false;
28
  let streamingMessage: Message | null = null;
@@ -59,31 +60,47 @@
59
  }
60
  (document.activeElement as HTMLElement).blur();
61
  loading = true;
62
- streamingMessage = { role: 'assistant', content: '' };
63
- messages = [...messages, streamingMessage];
64
-
65
- let out = ''; // Declare the 'out' variable
66
-
67
  try {
68
  const hf = new HfInference(hfToken);
69
-
70
- for await (const chunk of hf.chatCompletionStream({
71
- model: currentModel,
72
- messages: [
73
- systemMessage,
74
- ...messages.map(({ role, content }) => ({ role, content }))
75
- ],
76
- temperature: 0.1,
77
- max_tokens: 500,
78
- seed: 0,
79
- })) {
80
- if (chunk.choices && chunk.choices.length > 0) {
81
- if (streamingMessage && chunk.choices[0]?.delta?.content) {
82
- out += chunk.choices[0].delta.content;
83
- streamingMessage.content = out;
84
- messages = [...messages];
 
 
 
 
 
 
 
85
  }
86
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  }
88
  } catch (error) {
89
  alert('error: ' + error.message);
@@ -181,6 +198,7 @@
181
  bind:currentModel
182
  bind:temperature
183
  bind:maxTokens
 
184
  />
185
  </div>
186
  </div>
 
23
  let messages: Message[] = startMessages;
24
  let temperature = 0.5;
25
  let maxTokens = 32000;
26
+ let streaming = true;
27
 
28
  let loading = false;
29
  let streamingMessage: Message | null = null;
 
60
  }
61
  (document.activeElement as HTMLElement).blur();
62
  loading = true;
63
+
 
 
 
 
64
  try {
65
  const hf = new HfInference(hfToken);
66
+ const requestMessages = [
67
+ systemMessage,
68
+ ...messages.map(({ role, content }) => ({ role, content }))
69
+ ];
70
+
71
+ if (streaming) {
72
+ streamingMessage = { role: 'assistant', content: '' };
73
+ messages = [...messages, streamingMessage];
74
+ let out = '';
75
+
76
+ for await (const chunk of hf.chatCompletionStream({
77
+ model: currentModel,
78
+ messages: requestMessages,
79
+ temperature,
80
+ max_tokens: maxTokens,
81
+ seed: 0,
82
+ })) {
83
+ if (chunk.choices && chunk.choices.length > 0) {
84
+ if (streamingMessage && chunk.choices[0]?.delta?.content) {
85
+ out += chunk.choices[0].delta.content;
86
+ streamingMessage.content = out;
87
+ messages = [...messages];
88
+ }
89
  }
90
  }
91
+ } else {
92
+ const response = await hf.chatCompletion({
93
+ model: currentModel,
94
+ messages: requestMessages,
95
+ temperature,
96
+ max_tokens: maxTokens,
97
+ seed: 0,
98
+ });
99
+
100
+ if (response.choices && response.choices.length > 0) {
101
+ const newMessage = { role: 'assistant', content: response.choices[0].message.content };
102
+ messages = [...messages, newMessage];
103
+ }
104
  }
105
  } catch (error) {
106
  alert('error: ' + error.message);
 
198
  bind:currentModel
199
  bind:temperature
200
  bind:maxTokens
201
+ bind:streaming
202
  />
203
  </div>
204
  </div>
src/lib/components/Playground/PlaygroundOptions.svelte CHANGED
@@ -72,7 +72,7 @@
72
  </div>
73
  <div class="mt-2">
74
  <label class="flex cursor-pointer items-center justify-between">
75
- <input type="checkbox" value="" class="peer sr-only" disabled />
76
  <span class="text-sm font-medium text-gray-900 dark:text-gray-300">Streaming</span>
77
  <div
78
  class="peer relative h-5 w-9 rounded-full bg-gray-200 after:absolute after:start-[2px] after:top-[2px] after:h-4 after:w-4 after:rounded-full after:border after:border-gray-300 after:bg-white after:transition-all after:content-[''] peer-checked:bg-black peer-checked:after:translate-x-full peer-checked:after:border-white peer-focus:outline-none dark:border-gray-600 dark:bg-gray-700"
 
72
  </div>
73
  <div class="mt-2">
74
  <label class="flex cursor-pointer items-center justify-between">
75
+ <input type="checkbox" bind:checked={streaming} class="peer sr-only" />
76
  <span class="text-sm font-medium text-gray-900 dark:text-gray-300">Streaming</span>
77
  <div
78
  class="peer relative h-5 w-9 rounded-full bg-gray-200 after:absolute after:start-[2px] after:top-[2px] after:h-4 after:w-4 after:rounded-full after:border after:border-gray-300 after:bg-white after:transition-all after:content-[''] peer-checked:bg-black peer-checked:after:translate-x-full peer-checked:after:border-white peer-focus:outline-none dark:border-gray-600 dark:bg-gray-700"