Spaces:
Running
Running
some types + some API tweaks
Browse filesCo-Authored-By: OlivierDehaene <23298448+OlivierDehaene@users.noreply.github.com>
- src/lib/Types.ts +20 -0
- src/routes/+page.svelte +8 -5
src/lib/Types.ts
CHANGED
@@ -8,3 +8,23 @@ export type Message =
|
|
8 |
content: string;
|
9 |
};
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
content: string;
|
9 |
};
|
10 |
|
11 |
+
|
12 |
+
export interface Token {
|
13 |
+
id: number;
|
14 |
+
text: string;
|
15 |
+
logprob: number;
|
16 |
+
special: boolean;
|
17 |
+
}
|
18 |
+
|
19 |
+
|
20 |
+
export interface StreamResponse {
|
21 |
+
/**
|
22 |
+
* Generated token
|
23 |
+
*/
|
24 |
+
token: Token;
|
25 |
+
/**
|
26 |
+
* Complete generated text
|
27 |
+
* Only available when the generation is finished
|
28 |
+
*/
|
29 |
+
generated_text?: string;
|
30 |
+
}
|
src/routes/+page.svelte
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
import { fetchEventSource } from '@microsoft/fetch-event-source';
|
3 |
import ChatBox from '$lib/chat/ChatBox.svelte';
|
4 |
import ChatIntroduction from '$lib/chat/ChatIntroduction.svelte';
|
5 |
-
import type { Message } from '$lib/Types';
|
6 |
import { PUBLIC_ASSISTANT_MESSAGE_TOKEN, PUBLIC_ENDPOINT, PUBLIC_HF_TOKEN, PUBLIC_SEP_TOKEN, PUBLIC_USER_MESSAGE_TOKEN } from '$env/static/public';
|
7 |
|
8 |
const userToken = PUBLIC_USER_MESSAGE_TOKEN || "<|prompter|>";
|
@@ -22,12 +22,13 @@
|
|
22 |
+ (m.content.endsWith(sepToken) ? "" : sepToken))
|
23 |
.join('') + assistantToken;
|
24 |
|
|
|
25 |
fetchEventSource(PUBLIC_ENDPOINT, {
|
26 |
method: 'POST',
|
27 |
headers: {
|
28 |
Accept: 'text/event-stream',
|
29 |
'Content-Type': 'application/json',
|
30 |
-
"user-agent": "
|
31 |
"authorization": `Bearer ${PUBLIC_HF_TOKEN}`,
|
32 |
},
|
33 |
body: JSON.stringify({
|
@@ -52,10 +53,12 @@
|
|
52 |
}
|
53 |
},
|
54 |
onmessage(msg) {
|
55 |
-
const data = JSON.parse(msg.data);
|
56 |
// console.log(data);
|
57 |
-
|
58 |
-
|
|
|
|
|
59 |
}
|
60 |
});
|
61 |
}
|
|
|
2 |
import { fetchEventSource } from '@microsoft/fetch-event-source';
|
3 |
import ChatBox from '$lib/chat/ChatBox.svelte';
|
4 |
import ChatIntroduction from '$lib/chat/ChatIntroduction.svelte';
|
5 |
+
import type { Message, StreamResponse } from '$lib/Types';
|
6 |
import { PUBLIC_ASSISTANT_MESSAGE_TOKEN, PUBLIC_ENDPOINT, PUBLIC_HF_TOKEN, PUBLIC_SEP_TOKEN, PUBLIC_USER_MESSAGE_TOKEN } from '$env/static/public';
|
7 |
|
8 |
const userToken = PUBLIC_USER_MESSAGE_TOKEN || "<|prompter|>";
|
|
|
22 |
+ (m.content.endsWith(sepToken) ? "" : sepToken))
|
23 |
.join('') + assistantToken;
|
24 |
|
25 |
+
console.log(inputs);
|
26 |
fetchEventSource(PUBLIC_ENDPOINT, {
|
27 |
method: 'POST',
|
28 |
headers: {
|
29 |
Accept: 'text/event-stream',
|
30 |
'Content-Type': 'application/json',
|
31 |
+
"user-agent": "chat-ui/0.0.1",
|
32 |
"authorization": `Bearer ${PUBLIC_HF_TOKEN}`,
|
33 |
},
|
34 |
body: JSON.stringify({
|
|
|
53 |
}
|
54 |
},
|
55 |
onmessage(msg) {
|
56 |
+
const data = JSON.parse(msg.data) as StreamResponse;
|
57 |
// console.log(data);
|
58 |
+
if (!data.token.special) {
|
59 |
+
messages.at(-1)!.content += data.token.text;
|
60 |
+
messages = messages;
|
61 |
+
}
|
62 |
}
|
63 |
});
|
64 |
}
|