feat: add utility to stringify JSON for prompt

old-agentic-v1^2
Philipp Burckhardt 2023-06-15 20:58:37 -04:00
rodzic 5b3f08b396
commit f7cc081c18
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: A2C3BCA4F31D1DDD
2 zmienionych plików z 89 dodań i 2 usunięć

Wyświetl plik

@ -50,7 +50,7 @@ export function isValidTaskIdentifier(id: string): boolean {
* @param maxLength - maximum length of each chunk
* @returns array of chunks
*/
export const chunkString = (text: string, maxLength: number) => {
export function chunkString(text: string, maxLength: number): string[] {
const words = text.split(' ')
const chunks: string[] = []
let chunk = ''
@ -74,3 +74,41 @@ export const chunkString = (text: string, maxLength: number) => {
return chunks
}
/**
* Stringifies a JSON value for use in an LLM prompt.
*
* @param json - JSON value to stringify
* @returns stringified value with all double quotes around object keys removed
*/
export function stringifyForModel(json: types.JsonValue): string {
const UNIQUE_PREFIX = defaultIDGeneratorFn()
return (
JSON.stringify(json, replacer)
// Remove all double quotes around keys:
.replace(new RegExp('"' + UNIQUE_PREFIX + '(.*?)"', 'g'), '$1')
)
/**
* Replacer function prefixing all keys with a unique identifier.
*/
function replacer(_: string, value: any) {
if (value && typeof value === 'object') {
if (Array.isArray(value)) {
return value
}
const replacement = {}
for (const k in value) {
if (Object.hasOwnProperty.call(value, k)) {
replacement[UNIQUE_PREFIX + k] = value[k]
}
}
return replacement
}
return value
}
}

51
test/utils.test.ts vendored
Wyświetl plik

@ -6,7 +6,8 @@ import {
extractJSONArrayFromString,
extractJSONObjectFromString,
isValidTaskIdentifier,
sleep
sleep,
stringifyForModel
} from '@/utils'
test('isValidTaskIdentifier - valid', async (t) => {
@ -75,3 +76,51 @@ test('chunkString should split string into chunks', (t) => {
'function.'
])
})
test('stringifyForModel should stringify JSON values correctly', (t) => {
const input = { a: 1, b: 2 }
const expectedOutput = '{a:1,b:2}'
const actualOutput = stringifyForModel(input)
t.is(actualOutput, expectedOutput)
})
test('stringifyForModel should stringify primitive values correctly', (t) => {
const input = true
const expectedOutput = 'true'
const actualOutput = stringifyForModel(input)
t.is(actualOutput, expectedOutput)
const input2 = 1
const expectedOutput2 = '1'
const actualOutput2 = stringifyForModel(input2)
t.is(actualOutput2, expectedOutput2)
const input3 = 'foo'
const expectedOutput3 = '"foo"'
const actualOutput3 = stringifyForModel(input3)
t.is(actualOutput3, expectedOutput3)
})
test('stringifyForModel should stringify nested objects correctly', (t) => {
const input = { a: 1, b: { c: 3, d: 4 } }
const expectedOutput = '{a:1,b:{c:3,d:4}}'
const actualOutput = stringifyForModel(input)
t.is(actualOutput, expectedOutput)
})
test('stringifyForModel should stringify arrays correctly', (t) => {
const input = { a: 'Hello World!', b: [2, 3] }
const expectedOutput = '{a:"Hello World!",b:[2,3]}'
const actualOutput = stringifyForModel(input)
t.is(actualOutput, expectedOutput)
})
test('stringifyForModel should stringify objects with null values correctly', (t) => {
const input = { a: 'baz', b: null }
const expectedOutput = '{a:"baz",b:null}'
const actualOutput = stringifyForModel(input)
t.is(actualOutput, expectedOutput)
})