nsarrazin HF Staff commited on
Commit
c03be55
·
unverified ·
1 Parent(s): 5ca011a

fix(continue): fix continue feature (#1459)

Browse files

* fix(continue): more reliable end token stripping

before we would try to strip end tokens once in order from the stop array, now we strip all end tokens from the array until we can't anymore

* fix(tools): remove generation prompt in continue mode

src/lib/buildPrompt.ts CHANGED
@@ -28,6 +28,7 @@ export async function buildPrompt({
28
  preprompt,
29
  tools,
30
  toolResults,
 
31
  })
32
  // Not super precise, but it's truncated in the model's backend anyway
33
  .split(" ")
@@ -35,12 +36,20 @@ export async function buildPrompt({
35
  .join(" ");
36
 
37
  if (continueMessage && model.parameters?.stop) {
38
- prompt = model.parameters.stop.reduce((acc: string, curr: string) => {
39
- if (acc.endsWith(curr)) {
40
- return acc.slice(0, acc.length - curr.length);
 
 
 
 
 
 
 
41
  }
42
- return acc;
43
- }, prompt.trimEnd());
 
44
  }
45
 
46
  return prompt;
 
28
  preprompt,
29
  tools,
30
  toolResults,
31
+ continueMessage,
32
  })
33
  // Not super precise, but it's truncated in the model's backend anyway
34
  .split(" ")
 
36
  .join(" ");
37
 
38
  if (continueMessage && model.parameters?.stop) {
39
+ let trimmedPrompt = prompt.trimEnd();
40
+ let hasRemovedStop = true;
41
+ while (hasRemovedStop) {
42
+ hasRemovedStop = false;
43
+ for (const stopToken of model.parameters.stop) {
44
+ if (trimmedPrompt.endsWith(stopToken)) {
45
+ trimmedPrompt = trimmedPrompt.slice(0, -stopToken.length);
46
+ hasRemovedStop = true;
47
+ break;
48
+ }
49
  }
50
+ trimmedPrompt = trimmedPrompt.trimEnd();
51
+ }
52
+ prompt = trimmedPrompt;
53
  }
54
 
55
  return prompt;
src/lib/server/models.ts CHANGED
@@ -94,7 +94,13 @@ async function getChatPromptRender(
94
  process.exit();
95
  }
96
 
97
- const renderTemplate = ({ messages, preprompt, tools, toolResults }: ChatTemplateInput) => {
 
 
 
 
 
 
98
  let formattedMessages: { role: string; content: string }[] = messages.map((message) => ({
99
  content: message.content,
100
  role: message.from,
@@ -222,10 +228,8 @@ async function getChatPromptRender(
222
 
223
  const output = tokenizer.apply_chat_template(formattedMessages, {
224
  tokenize: false,
225
- add_generation_prompt: true,
226
  chat_template: chatTemplate,
227
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
228
- // @ts-ignore
229
  tools: mappedTools,
230
  documents,
231
  });
 
94
  process.exit();
95
  }
96
 
97
+ const renderTemplate = ({
98
+ messages,
99
+ preprompt,
100
+ tools,
101
+ toolResults,
102
+ continueMessage,
103
+ }: ChatTemplateInput) => {
104
  let formattedMessages: { role: string; content: string }[] = messages.map((message) => ({
105
  content: message.content,
106
  role: message.from,
 
228
 
229
  const output = tokenizer.apply_chat_template(formattedMessages, {
230
  tokenize: false,
231
+ add_generation_prompt: !continueMessage,
232
  chat_template: chatTemplate,
 
 
233
  tools: mappedTools,
234
  documents,
235
  });
src/lib/types/Template.ts CHANGED
@@ -6,4 +6,5 @@ export type ChatTemplateInput = {
6
  preprompt?: string;
7
  tools?: Tool[];
8
  toolResults?: ToolResult[];
 
9
  };
 
6
  preprompt?: string;
7
  tools?: Tool[];
8
  toolResults?: ToolResult[];
9
+ continueMessage?: boolean;
10
  };