mishig HF staff commited on
Commit
b61328c
1 Parent(s): af961f3

System message as part of Conversation

Browse files
src/lib/components/InferencePlayground/InferencePlayground.svelte CHANGED
@@ -20,16 +20,17 @@
20
 
21
  export let models: ModelEntryWithTokenizer[];
22
 
23
- const startMessage: ChatCompletionInputMessage = { role: "user", content: "" };
 
24
 
25
  let conversation: Conversation = {
26
  model: models[0],
27
  config: defaultGenerationConfig,
28
- messages: [{ ...startMessage }],
 
29
  streaming: true,
30
  };
31
 
32
- let systemMessage: ChatCompletionInputMessage = { role: "system", content: "" };
33
  let hfToken: string | undefined = import.meta.env.VITE_HF_TOKEN;
34
  let viewCode = false;
35
  let showTokenModal = false;
@@ -41,7 +42,7 @@
41
  $: systemPromptSupported = isSystemPromptSupported(conversation.model);
42
  $: {
43
  if (!systemPromptSupported) {
44
- systemMessage = { role: "system", content: "" };
45
  }
46
  }
47
 
@@ -61,8 +62,8 @@
61
  }
62
 
63
  function reset() {
64
- systemMessage.content = "";
65
- conversation.messages = [{ ...startMessage }];
66
  }
67
 
68
  function abort() {
@@ -98,12 +99,11 @@
98
  conversation.messages = [...conversation.messages];
99
  }
100
  },
101
- abortController,
102
- systemMessage
103
  );
104
  } else {
105
  waitForNonStreaming = true;
106
- const newMessage = await handleNonStreamingResponse(hf, conversation, systemMessage);
107
  // check if the user did not abort the request
108
  if (waitForNonStreaming) {
109
  conversation.messages = [...conversation.messages, newMessage];
@@ -162,7 +162,7 @@
162
  placeholder={systemPromptSupported
163
  ? "Enter a custom prompt"
164
  : "System prompt is not supported with the chosen model."}
165
- bind:value={systemMessage.content}
166
  class="absolute inset-x-0 bottom-0 h-full resize-none bg-transparent px-3 pt-10 text-sm outline-none"
167
  ></textarea>
168
  </div>
 
20
 
21
  export let models: ModelEntryWithTokenizer[];
22
 
23
+ const startMessageUser: ChatCompletionInputMessage = { role: "user", content: "" };
24
+ const startMessageSystem: ChatCompletionInputMessage = { role: "system", content: "" };
25
 
26
  let conversation: Conversation = {
27
  model: models[0],
28
  config: defaultGenerationConfig,
29
+ messages: [{ ...startMessageUser }],
30
+ systemMessage: startMessageSystem,
31
  streaming: true,
32
  };
33
 
 
34
  let hfToken: string | undefined = import.meta.env.VITE_HF_TOKEN;
35
  let viewCode = false;
36
  let showTokenModal = false;
 
42
  $: systemPromptSupported = isSystemPromptSupported(conversation.model);
43
  $: {
44
  if (!systemPromptSupported) {
45
+ conversation.systemMessage = { role: "system", content: "" };
46
  }
47
  }
48
 
 
62
  }
63
 
64
  function reset() {
65
+ conversation.systemMessage.content = "";
66
+ conversation.messages = [{ ...startMessageUser }];
67
  }
68
 
69
  function abort() {
 
99
  conversation.messages = [...conversation.messages];
100
  }
101
  },
102
+ abortController
 
103
  );
104
  } else {
105
  waitForNonStreaming = true;
106
+ const newMessage = await handleNonStreamingResponse(hf, conversation);
107
  // check if the user did not abort the request
108
  if (waitForNonStreaming) {
109
  conversation.messages = [...conversation.messages, newMessage];
 
162
  placeholder={systemPromptSupported
163
  ? "Enter a custom prompt"
164
  : "System prompt is not supported with the chosen model."}
165
+ bind:value={conversation.systemMessage.content}
166
  class="absolute inset-x-0 bottom-0 h-full resize-none bg-transparent px-3 pt-10 text-sm outline-none"
167
  ></textarea>
168
  </div>
src/lib/components/InferencePlayground/inferencePlaygroundUtils.ts CHANGED
@@ -11,17 +11,17 @@ export async function handleStreamingResponse(
11
  hf: HfInference,
12
  conversation: Conversation,
13
  onChunk: (content: string) => void,
14
- abortController: AbortController,
15
- systemMessage?: ChatCompletionInputMessage
16
  ): Promise<void> {
 
17
  const messages = [
18
- ...(isSystemPromptSupported(conversation.model) && systemMessage?.content?.length ? [systemMessage] : []),
19
  ...conversation.messages,
20
  ];
21
  let out = "";
22
  for await (const chunk of hf.chatCompletionStream(
23
  {
24
- model: conversation.model.id,
25
  messages,
26
  temperature: conversation.config.temperature,
27
  max_tokens: conversation.config.maxTokens,
@@ -37,16 +37,16 @@ export async function handleStreamingResponse(
37
 
38
  export async function handleNonStreamingResponse(
39
  hf: HfInference,
40
- conversation: Conversation,
41
- systemMessage?: ChatCompletionInputMessage
42
  ): Promise<ChatCompletionInputMessage> {
 
43
  const messages = [
44
- ...(isSystemPromptSupported(conversation.model) && systemMessage?.content?.length ? [systemMessage] : []),
45
  ...conversation.messages,
46
  ];
47
 
48
  const response = await hf.chatCompletion({
49
- model: conversation.model,
50
  messages,
51
  temperature: conversation.config.temperature,
52
  max_tokens: conversation.config.maxTokens,
 
11
  hf: HfInference,
12
  conversation: Conversation,
13
  onChunk: (content: string) => void,
14
+ abortController: AbortController
 
15
  ): Promise<void> {
16
+ const { model, systemMessage } = conversation;
17
  const messages = [
18
+ ...(isSystemPromptSupported(model) && systemMessage.content?.length ? [systemMessage] : []),
19
  ...conversation.messages,
20
  ];
21
  let out = "";
22
  for await (const chunk of hf.chatCompletionStream(
23
  {
24
+ model: model.id,
25
  messages,
26
  temperature: conversation.config.temperature,
27
  max_tokens: conversation.config.maxTokens,
 
37
 
38
  export async function handleNonStreamingResponse(
39
  hf: HfInference,
40
+ conversation: Conversation
 
41
  ): Promise<ChatCompletionInputMessage> {
42
+ const { model, systemMessage } = conversation;
43
  const messages = [
44
+ ...(isSystemPromptSupported(model) && systemMessage.content?.length ? [systemMessage] : []),
45
  ...conversation.messages,
46
  ];
47
 
48
  const response = await hf.chatCompletion({
49
+ model: model.id,
50
  messages,
51
  temperature: conversation.config.temperature,
52
  max_tokens: conversation.config.maxTokens,
src/lib/components/InferencePlayground/types.ts CHANGED
@@ -6,6 +6,7 @@ export type Conversation = {
6
  model: ModelEntryWithTokenizer;
7
  config: GenerationConfig;
8
  messages: ChatCompletionInputMessage[];
 
9
  streaming: boolean;
10
  };
11
 
 
6
  model: ModelEntryWithTokenizer;
7
  config: GenerationConfig;
8
  messages: ChatCompletionInputMessage[];
9
+ systemMessage: ChatCompletionInputMessage;
10
  streaming: boolean;
11
  };
12