victor HF staff commited on
Commit
2ac97e2
·
1 Parent(s): 6dfa4f5
src/lib/components/Playground/Playground.svelte CHANGED
@@ -10,6 +10,7 @@
10
  import PlaygroundOptions from '$lib/components/Playground/PlaygroundOptions.svelte';
11
  import PlaygroundTokenModal from './PlaygroundTokenModal.svelte';
12
  import PlaygroundModelSelector from './PlaygroundModelSelector.svelte';
 
13
 
14
  const compatibleModels: string[] = [
15
  '01-ai/Yi-1.5-34B-Chat',
@@ -65,6 +66,13 @@
65
  let tokens = 0;
66
  let latency = 0;
67
  let messageContainer: HTMLDivElement | null = null;
 
 
 
 
 
 
 
68
 
69
  function addMessage() {
70
  currentConversation.messages = [
@@ -78,6 +86,14 @@
78
  }
79
 
80
  function deleteMessage(i: number) {
 
 
 
 
 
 
 
 
81
  currentConversation.messages = currentConversation.messages.filter((_, j) => j !== i);
82
  conversations = conversations;
83
  }
@@ -104,6 +120,7 @@
104
  if (currentConversation.config.streaming) {
105
  streamingMessage = { role: 'assistant', content: '' };
106
  currentConversation.messages = [...currentConversation.messages, streamingMessage];
 
107
 
108
  await handleStreamingResponse(
109
  hf,
@@ -119,7 +136,8 @@
119
  conversations = conversations;
120
  scrollToBottom();
121
  }
122
- }
 
123
  );
124
  } else {
125
  const newMessage = await handleNonStreamingResponse(
@@ -135,12 +153,15 @@
135
  scrollToBottom();
136
  }
137
  } catch (error) {
138
- alert('error: ' + (error as Error).message);
 
 
139
  } finally {
140
  const endTime = performance.now();
141
  latency = Math.round(endTime - startTime);
142
  loading = false;
143
  streamingMessage = null;
 
144
  scrollToBottom();
145
  }
146
  }
@@ -217,6 +238,7 @@
217
  <button
218
  class="flex px-6 py-6 hover:bg-gray-50 dark:hover:bg-gray-800/50"
219
  on:click={addMessage}
 
220
  >
221
  <div class="flex items-center gap-2 !p-0 text-sm font-semibold">
222
  <svg
 
10
  import PlaygroundOptions from '$lib/components/Playground/PlaygroundOptions.svelte';
11
  import PlaygroundTokenModal from './PlaygroundTokenModal.svelte';
12
  import PlaygroundModelSelector from './PlaygroundModelSelector.svelte';
13
+ import { onDestroy } from 'svelte';
14
 
15
  const compatibleModels: string[] = [
16
  '01-ai/Yi-1.5-34B-Chat',
 
66
  let tokens = 0;
67
  let latency = 0;
68
  let messageContainer: HTMLDivElement | null = null;
69
+ let abortController: AbortController | null = null;
70
+
71
+ onDestroy(() => {
72
+ if (abortController) {
73
+ abortController.abort();
74
+ }
75
+ });
76
 
77
  function addMessage() {
78
  currentConversation.messages = [
 
86
  }
87
 
88
  function deleteMessage(i: number) {
89
+ if (i === currentConversation.messages.length - 1 && streamingMessage) {
90
+ if (abortController) {
91
+ abortController.abort();
92
+ abortController = null;
93
+ }
94
+ loading = false;
95
+ streamingMessage = null;
96
+ }
97
  currentConversation.messages = currentConversation.messages.filter((_, j) => j !== i);
98
  conversations = conversations;
99
  }
 
120
  if (currentConversation.config.streaming) {
121
  streamingMessage = { role: 'assistant', content: '' };
122
  currentConversation.messages = [...currentConversation.messages, streamingMessage];
123
+ abortController = new AbortController();
124
 
125
  await handleStreamingResponse(
126
  hf,
 
136
  conversations = conversations;
137
  scrollToBottom();
138
  }
139
+ },
140
+ abortController
141
  );
142
  } else {
143
  const newMessage = await handleNonStreamingResponse(
 
153
  scrollToBottom();
154
  }
155
  } catch (error) {
156
+ if (error.name !== 'AbortError') {
157
+ alert('error: ' + (error as Error).message);
158
+ }
159
  } finally {
160
  const endTime = performance.now();
161
  latency = Math.round(endTime - startTime);
162
  loading = false;
163
  streamingMessage = null;
164
+ abortController = null;
165
  scrollToBottom();
166
  }
167
  }
 
238
  <button
239
  class="flex px-6 py-6 hover:bg-gray-50 dark:hover:bg-gray-800/50"
240
  on:click={addMessage}
241
+ disabled={loading}
242
  >
243
  <div class="flex items-center gap-2 !p-0 text-sm font-semibold">
244
  <svg
src/lib/components/Playground/playgroundUtils.ts CHANGED
@@ -20,19 +20,28 @@ export async function handleStreamingResponse(
20
  temperature: number,
21
  maxTokens: number,
22
  jsonMode: boolean,
23
- onChunk: (content: string) => void
 
24
  ): Promise<void> {
25
  let out = '';
26
- for await (const chunk of hf.chatCompletionStream({
27
- model: model,
28
- messages: messages,
29
- temperature: temperature,
30
- max_tokens: maxTokens,
31
- json_mode: jsonMode
32
- })) {
33
- if (chunk.choices && chunk.choices.length > 0 && chunk.choices[0]?.delta?.content) {
34
- out += chunk.choices[0].delta.content;
35
- onChunk(out);
 
 
 
 
 
 
 
 
36
  }
37
  }
38
  }
 
20
  temperature: number,
21
  maxTokens: number,
22
  jsonMode: boolean,
23
+ onChunk: (content: string) => void,
24
+ abortController: AbortController
25
  ): Promise<void> {
26
  let out = '';
27
+ try {
28
+ for await (const chunk of hf.chatCompletionStream({
29
+ model: model,
30
+ messages: messages,
31
+ temperature: temperature,
32
+ max_tokens: maxTokens,
33
+ json_mode: jsonMode
34
+ }, { signal: abortController.signal })) {
35
+ if (chunk.choices && chunk.choices.length > 0 && chunk.choices[0]?.delta?.content) {
36
+ out += chunk.choices[0].delta.content;
37
+ onChunk(out);
38
+ }
39
+ }
40
+ } catch (error) {
41
+ if (error.name === 'AbortError') {
42
+ console.log('Stream aborted');
43
+ } else {
44
+ throw error;
45
  }
46
  }
47
  }