File size: 872 Bytes
3d4392e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export function dirtyLLMResponseCleaner(input: string): string {
  let str = (
    `${input || ""}`
    // a summary of all the weird hallucinations I saw it make..
    .replaceAll(`"\n`, `",\n`) // fix missing commas at the end of a line
    .replaceAll(`"]`, `"}]`)
    .replaceAll(/"\S*,?\S*\]/gi, `"}]`)
    .replaceAll(/"\S*,?\S*\}\S*]/gi, `"}]`)

    // this removes the trailing commas (which are valid in JS but not JSON)
    .replace(/,(?=\s*?[\}\]])/g, '')
    
    .replaceAll("}}", "}")
    .replaceAll("]]", "]")
    .replaceAll("[[", "[")
    .replaceAll("{{", "{")
    .replaceAll(",,", ",")
  )

  // repair missing end of JSON array
  if (str.at(-1) === '}') {
    str = str + "]"
  }

  if (str.at(-1) === '"') {
    str = str + "}]"
  }

  if (str[0] === '{') {
    str = "[" + str
  }
  
  if (str[0] === '"') {
    str = "[{" + str
  }

  return str
}