coyotte508 HF staff commited on
Commit
0abf663
1 Parent(s): 1bba900

✨ Remove endoftext (#70)

Browse files
src/lib/components/chat/ChatMessage.svelte CHANGED
@@ -2,7 +2,7 @@
2
  import { marked } from "marked";
3
  import type { Message } from "$lib/types/Message";
4
  import { afterUpdate } from "svelte";
5
- import { deepestChild } from "$lib/utils/dom";
6
 
7
  import CodeBlock from "../CodeBlock.svelte";
8
  import IconLoading from "../icons/IconLoading.svelte";
 
2
  import { marked } from "marked";
3
  import type { Message } from "$lib/types/Message";
4
  import { afterUpdate } from "svelte";
5
+ import { deepestChild } from "$lib/utils/deepestChild";
6
 
7
  import CodeBlock from "../CodeBlock.svelte";
8
  import IconLoading from "../icons/IconLoading.svelte";
src/lib/utils/{dom.ts → deepestChild.ts} RENAMED
File without changes
src/lib/utils/trimPrefix.ts ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ export function trimPrefix(input: string, prefix: string) {
2
+ if (input.startsWith(prefix)) {
3
+ return input.slice(prefix.length);
4
+ }
5
+ return input;
6
+ }
src/lib/utils/trimSuffix.ts ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ export function trimSuffix(input: string, end: string): string {
2
+ if (input.endsWith(end)) {
3
+ return input.slice(0, input.length - end.length);
4
+ }
5
+ return input;
6
+ }
src/routes/conversation/[id]/+page.svelte CHANGED
@@ -7,6 +7,9 @@
7
  import { HfInference } from "@huggingface/inference";
8
  import { invalidate } from "$app/navigation";
9
  import { base } from "$app/paths";
 
 
 
10
 
11
  export let data: PageData;
12
 
@@ -47,6 +50,19 @@
47
 
48
  if (!data || conversationId !== $page.params.id) break;
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  if (!data.token.special) {
51
  const lastMessage = messages.at(-1);
52
 
 
7
  import { HfInference } from "@huggingface/inference";
8
  import { invalidate } from "$app/navigation";
9
  import { base } from "$app/paths";
10
+ import { trimSuffix } from "$lib/utils/trimSuffix";
11
+ import { PUBLIC_SEP_TOKEN } from "$env/static/public";
12
+ import { trimPrefix } from "$lib/utils/trimPrefix";
13
 
14
  export let data: PageData;
15
 
 
50
 
51
  if (!data || conversationId !== $page.params.id) break;
52
 
53
+ // final message
54
+ if (data.generated_text) {
55
+ const lastMessage = messages.at(-1);
56
+ if (lastMessage) {
57
+ lastMessage.content = trimPrefix(
58
+ trimSuffix(data.generated_text, PUBLIC_SEP_TOKEN),
59
+ "<|startoftext|>"
60
+ );
61
+ messages = [...messages];
62
+ }
63
+ break;
64
+ }
65
+
66
  if (!data.token.special) {
67
  const lastMessage = messages.at(-1);
68
 
src/routes/conversation/[id]/+server.ts CHANGED
@@ -1,10 +1,12 @@
1
  import { HF_TOKEN } from "$env/static/private";
2
- import { PUBLIC_MODEL_ENDPOINT } from "$env/static/public";
3
  import { buildPrompt } from "$lib/buildPrompt.js";
4
  import { collections } from "$lib/server/database.js";
5
  import type { Message } from "$lib/types/Message.js";
6
  import { streamToAsyncIterable } from "$lib/utils/streamToAsyncIterable";
7
  import { sum } from "$lib/utils/sum";
 
 
8
  import { error } from "@sveltejs/kit";
9
  import { ObjectId } from "mongodb";
10
 
@@ -49,6 +51,8 @@ export async function POST({ request, fetch, locals, params }) {
49
  generated_text = generated_text.slice(prompt.length);
50
  }
51
 
 
 
52
  messages.push({ from: "assistant", content: generated_text });
53
 
54
  await collections.conversations.updateOne(
 
1
  import { HF_TOKEN } from "$env/static/private";
2
+ import { PUBLIC_MODEL_ENDPOINT, PUBLIC_SEP_TOKEN } from "$env/static/public";
3
  import { buildPrompt } from "$lib/buildPrompt.js";
4
  import { collections } from "$lib/server/database.js";
5
  import type { Message } from "$lib/types/Message.js";
6
  import { streamToAsyncIterable } from "$lib/utils/streamToAsyncIterable";
7
  import { sum } from "$lib/utils/sum";
8
+ import { trimPrefix } from "$lib/utils/trimPrefix.js";
9
+ import { trimSuffix } from "$lib/utils/trimSuffix.js";
10
  import { error } from "@sveltejs/kit";
11
  import { ObjectId } from "mongodb";
12
 
 
51
  generated_text = generated_text.slice(prompt.length);
52
  }
53
 
54
+ generated_text = trimSuffix(trimPrefix(generated_text, "<|startoftext|>"), PUBLIC_SEP_TOKEN);
55
+
56
  messages.push({ from: "assistant", content: generated_text });
57
 
58
  await collections.conversations.updateOne(