Skip to content

Instantly share code, notes, and snippets.

@benwoodward
Created April 13, 2023 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benwoodward/d460eac5c0e845cc97b2c74e306d2af6 to your computer and use it in GitHub Desktop.
Save benwoodward/d460eac5c0e845cc97b2c74e306d2af6 to your computer and use it in GitHub Desktop.
Extract JSON array from LLM text
function matchBalancedBrackets(inputText) {
const matches = []
let stack = 0
let startIndex
for (let i = 0; i < inputText.length; i++) {
const char = inputText[i]
if (char === '[') {
if (stack === 0) {
startIndex = i
}
stack++
} else if (char === ']') {
stack--
if (stack === 0) {
matches.push(inputText.slice(startIndex, i + 1))
}
}
}
return matches
}
export function extractJson(inputText: string): WordsArray {
// Find all JSON-like substrings
const candidates: string[] = matchBalancedBrackets(inputText)
// Iterate through the candidates and check if they conform to the WordsArray type
for (const candidate of candidates) {
try {
const jsonArray = JSON.parse(candidate)
const parsedArray = ExpectedSchema.safeParse(jsonArray)
console.log(parsedArray, null, 2)
if (parsedArray.success) {
return parsedArray.data
}
} catch (error) {
console.log('Error:', error)
}
}
return []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment