Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Qwen FIM multi file template #3589

Open
2 tasks done
tomasz-stefaniak opened this issue Jan 2, 2025 · 0 comments
Open
2 tasks done

New Qwen FIM multi file template #3589

tomasz-stefaniak opened this issue Jan 2, 2025 · 0 comments
Assignees
Labels
area:autocomplete Relates to the auto complete feature good-first-issue Suggested issue for new contributors kind:enhancement Indicates a new feature request, imrovement, or extension "needs-triage"

Comments

@tomasz-stefaniak
Copy link
Collaborator

tomasz-stefaniak commented Jan 2, 2025

Validations

  • I believe this is a way to improve. I'll try to join the Continue Discord for questions
  • I'm not able to find an open issue that requests the same enhancement

Problem

Pasted from Discord:

Hi, a few days ago I implemented a template in my local Continue clone which correctly uses repository-level autocompletion for Qwen templates, a bit like the Codestral template.
Snippet: https://gist.github.com/DanielusG/ff4c2d008673e75bdd31bba6a81c6f07
Insert the snippet in AutocompleteTemplate.ts and replace the template in the funciton getTemplateForModel

I have used it daily for several days and it seems to work quite well. I think it's not the best way to implement it since I used a special token created by me and then did a split, but I did it in a short time, and I honestly wouldn't want to create a pull request to implement a ‘’janky‘’ code

However, I wanted to share it in case anyone wanted to improve or implement it as it is.
Source to FIM repository level template for Qwen: QwenLM/Qwen2.5-Coder#25 (comment)

Solution

const qwenMultifileFimTemplate: AutocompleteTemplate = {
  compilePrefixSuffix: (
    prefix: string,
    suffix: string,
    filepath: string,
    reponame: string,
    snippets: AutocompleteSnippet[],
  ): [string, string] => {
    if (snippets.length === 0) {
      return [prefix, suffix];
    }

    const relativePaths = shortestRelativePaths([
      ...snippets.map((snippet) =>
        "filepath" in snippet ? snippet.filepath : "Untitled.txt",
      ),
      filepath,
    ]);

    const fileContents = snippets
      .map((snippet, i) => {
        if (snippet.type === AutocompleteSnippetType.Diff) {
          return `<|file_sep|>${relativePaths[i]}\n${snippet.content}`;
        }
        return `<|file_sep|>${relativePaths[i]}\n${snippet.content}`;
      })
      .join("\n");

    const fullPrefix = `<|repo_name|>${reponame}\n${fileContents}\n<|file_sep|>${
      relativePaths[relativePaths.length - 1]
    }<|system_separator_istruction_repository_level|>${prefix}`;

    return [fullPrefix, suffix];
  },
  template: (prefix: string, suffix: string): string => {
    if(prefix.includes("<|system_separator_istruction_repository_level|>")){
      const [beforeSeparator, ...afterSeparator] = prefix.split(
        "<|system_separator_istruction_repository_level|>"
      );
      const combinedAfterSeparator = afterSeparator.join(
        ""
      );
      return `${beforeSeparator}\n<|fim_prefix|>${combinedAfterSeparator}<|fim_suffix|>${suffix}<|fim_middle|>`;
    }
    return `<|fim_prefix|>${prefix}<|fim_suffix|>${suffix}<|fim_middle|>`;
  },
  completionOptions: {
    stop: [
      "<|endoftext|>",
      "<|fim_prefix|>",
      "<|fim_middle|>",
      "<|fim_suffix|>",
      "<|fim_pad|>",
      "<|repo_name|>",
      "<|file_sep|>",
      "<|im_start|>",
      "<|im_end|>",
    ],
  },
};
@tomasz-stefaniak tomasz-stefaniak added the good-first-issue Suggested issue for new contributors label Jan 2, 2025
@dosubot dosubot bot added area:autocomplete Relates to the auto complete feature kind:enhancement Indicates a new feature request, imrovement, or extension labels Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:autocomplete Relates to the auto complete feature good-first-issue Suggested issue for new contributors kind:enhancement Indicates a new feature request, imrovement, or extension "needs-triage"
Projects
None yet
Development

No branches or pull requests

2 participants