Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

4.2 KB
Raw Blame
/**
 * Language-specific separators for recursive text splitting.
 * Each array is ordered from most to least specific separator.
 */

export type SupportedLanguage =
  | 'cpp'
  | 'go'
  | 'java'
  | 'js'
  | 'php'
  | 'proto'
  | 'python'
  | 'rst'
  | 'ruby'
  | 'rust'
  | 'scala'
  | 'swift'
  | 'markdown'
  | 'latex'
  | 'html'
  | 'sol';

export const SUPPORTED_LANGUAGES: SupportedLanguage[] = [
  'cpp',
  'go',
  'java',
  'js',
  'php',
  'proto',
  'python',
  'rst',
  'ruby',
  'rust',
  'scala',
  'swift',
  'markdown',
  'latex',
  'html',
  'sol',
];

export const DEFAULT_SEPARATORS = ['\n\n', '\n', ' ', ''];

export const MARKDOWN_SEPARATORS = [
  '\n## ',
  '\n### ',
  '\n#### ',
  '\n##### ',
  '\n###### ',
  '```\n\n',
  '\n\n***\n\n',
  '\n\n---\n\n',
  '\n\n___\n\n',
  '\n\n',
  '\n',
  ' ',
  '',
];

export const LATEX_SEPARATORS = [
  '\n\\chapter{',
  '\n\\section{',
  '\n\\subsection{',
  '\n\\subsubsection{',
  '\n\\begin{enumerate}',
  '\n\\begin{itemize}',
  '\n\\begin{description}',
  '\n\\begin{list}',
  '\n\\begin{quote}',
  '\n\\begin{quotation}',
  '\n\\begin{verse}',
  '\n\\begin{verbatim}',
  '\n\\begin{align}',
  '$$',
  '$',
  '\n\n',
  '\n',
  ' ',
  '',
];

const LANGUAGE_SEPARATORS: Record<SupportedLanguage, string[]> = {
  cpp: [
    '\nclass ',
    '\nvoid ',
    '\nint ',
    '\nfloat ',
    '\ndouble ',
    '\nif ',
    '\nfor ',
    '\nwhile ',
    '\nswitch ',
    '\ncase ',
    '\n\n',
    '\n',
    ' ',
    '',
  ],
  go: [
    '\nfunc ',
    '\nvar ',
    '\nconst ',
    '\ntype ',
    '\nif ',
    '\nfor ',
    '\nswitch ',
    '\ncase ',
    '\n\n',
    '\n',
    ' ',
    '',
  ],
  html: [
    '<body>',
    '<div>',
    '<p>',
    '<br>',
    '<li>',
    '<h1>',
    '<h2>',
    '<h3>',
    '<h4>',
    '<h5>',
    '<h6>',
    '<span>',
    '<table>',
    '<tr>',
    '<td>',
    '<th>',
    '<ul>',
    '<ol>',
    '<header>',
    '<footer>',
    '<nav>',
    '<head>',
    '<style>',
    '<script>',
    '<meta>',
    '<title>',
    ' ',
    '',
  ],
  java: [
    '\nclass ',
    '\npublic ',
    '\nprotected ',
    '\nprivate ',
    '\nstatic ',
    '\nif ',
    '\nfor ',
    '\nwhile ',
    '\nswitch ',
    '\ncase ',
    '\n\n',
    '\n',
    ' ',
    '',
  ],
  js: [
    '\nfunction ',
    '\nconst ',
    '\nlet ',
    '\nvar ',
    '\nclass ',
    '\nif ',
    '\nfor ',
    '\nwhile ',
    '\nswitch ',
    '\ncase ',
    '\ndefault ',
    '\n\n',
    '\n',
    ' ',
    '',
  ],
  latex: LATEX_SEPARATORS,
  markdown: MARKDOWN_SEPARATORS,
  php: [
    '\nfunction ',
    '\nclass ',
    '\nif ',
    '\nforeach ',
    '\nwhile ',
    '\ndo ',
    '\nswitch ',
    '\ncase ',
    '\n\n',
    '\n',
    ' ',
    '',
  ],
  proto: [
    '\nmessage ',
    '\nservice ',
    '\nenum ',
    '\noption ',
    '\nimport ',
    '\nsyntax ',
    '\n\n',
    '\n',
    ' ',
    '',
  ],
  python: ['\nclass ', '\ndef ', '\n\tdef ', '\n\n', '\n', ' ', ''],
  rst: ['\n===\n', '\n---\n', '\n***\n', '\n.. ', '\n\n', '\n', ' ', ''],
  ruby: [
    '\ndef ',
    '\nclass ',
    '\nif ',
    '\nunless ',
    '\nwhile ',
    '\nfor ',
    '\ndo ',
    '\nbegin ',
    '\nrescue ',
    '\n\n',
    '\n',
    ' ',
    '',
  ],
  rust: [
    '\nfn ',
    '\nconst ',
    '\nlet ',
    '\nif ',
    '\nwhile ',
    '\nfor ',
    '\nloop ',
    '\nmatch ',
    '\nconst ',
    '\n\n',
    '\n',
    ' ',
    '',
  ],
  scala: [
    '\nclass ',
    '\nobject ',
    '\ndef ',
    '\nval ',
    '\nvar ',
    '\nif ',
    '\nfor ',
    '\nwhile ',
    '\nmatch ',
    '\ncase ',
    '\n\n',
    '\n',
    ' ',
    '',
  ],
  sol: [
    '\npragma ',
    '\nusing ',
    '\ncontract ',
    '\ninterface ',
    '\nlibrary ',
    '\nconstructor ',
    '\ntype ',
    '\nfunction ',
    '\nevent ',
    '\nmodifier ',
    '\nerror ',
    '\nstruct ',
    '\nenum ',
    '\nif ',
    '\nfor ',
    '\nwhile ',
    '\ndo while ',
    '\nassembly ',
    '\n\n',
    '\n',
    ' ',
    '',
  ],
  swift: [
    '\nfunc ',
    '\nclass ',
    '\nstruct ',
    '\nenum ',
    '\nif ',
    '\nfor ',
    '\nwhile ',
    '\ndo ',
    '\nswitch ',
    '\ncase ',
    '\n\n',
    '\n',
    ' ',
    '',
  ],
};

export function getSeparatorsForLanguage(language: SupportedLanguage): string[] {
  return LANGUAGE_SEPARATORS[language];
}