File size: 815 Bytes
9705b6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Escaping curly braces is necessary for LangChain to correctly process the prompt
function escapeBraces(str) {
  return str
    .replace(/({{2,})|(}{2,})/g, (match) => `${match[0]}`)
    .replace(/{|}/g, (match) => `${match}${match}`);
}

function getSnippet(text) {
  let limit = 50;
  let splitText = escapeBraces(text).split(' ');

  if (splitText.length === 1 && splitText[0].length > limit) {
    return splitText[0].substring(0, limit);
  }

  let result = '';
  let spaceCount = 0;

  for (let i = 0; i < splitText.length; i++) {
    if (result.length + splitText[i].length <= limit) {
      result += splitText[i] + ' ';
      spaceCount++;
    } else {
      break;
    }

    if (spaceCount == 10) {
      break;
    }
  }

  return result.trim();
}

module.exports = {
  escapeBraces,
  getSnippet,
};