retagUpdate = async (codeWithSnippetDelimited, updatedCodeWithoutDelimiters, delimiter) => {
console.log(codeWithSnippetDelimited, updatedCodeWithoutDelimiters, delimiter)
let gptOut;
try {
const gptOutCompletion = await openai.chat.completions.create({
messages: [
{
role: "system",
content: "You are a helpful assistant designed to output JSON.",
},
{ role: "user", content: prompt_breakdown11({codeWithSnippetDelimited,
updatedCodeWithSnippetDelimited: updatedCodeWithoutDelimiters,
delimiter })}
],
model: "gpt-4-0125-preview",
response_format: { type: "json_object" },
});
console.log(gptOutCompletion)
gptOut = gptOutCompletion.choices[0].message.content
console.log(gptOut)
} catch (e) {
return {error: e, errorType: 'model'}
}
let gptRetaggingJSON;
try {
gptRetaggingJSON = JSON.parse(gptOut)
console.log(gptRetaggingJSON)
} catch (e) {
// (This should never happen based on the OpenAI documentation.)
return {error: e, errorType: 'JSON parse', gptOut}
}
// console.log(completion.choices[0].message.content);
/* Unhandled issues:
* the response may be incorrect; could check across several tries to mitigate
* the response may be correct but there may be multiple correct responses; disambiguation needed
* the response may have an unreadable format, leading to failure in the next part
*/
// const gptRetaggingJSONString = (await askMX([
// { role: 'system',
// content: 'You are a text parser designed to output JSON.'},
// { role: 'user',
// content: retagPromptGPT(gptOut)}
// ])).trim() // sometimes Mixtral puts a space in front of the response...
// console.log(gptRetaggingJSONString)
// const gptRetaggingJSON = JSON.parse(gptRetaggingJSONString)
/* Unhandled issues:
* the response may be incorrect given the input
* failure to parse
*/
const computeUpdatedCodeWithSnippetRetagged = ({code, snippet, lineStart, lineEnd, nthOccurrence, delimiterStart, delimiterEnd}) => {
// Note lineStart and lineEnd are 1-indexed.
/* We expand the search by one line if it fails on the identified segment to handle off-by-one issues. */
/* NOTE expanded search was introduced after the initial evaluation.
/* Unhandled issues:
* any non-whitespace typos in the output (even e.g. missing comments) will cause a failure to match
* potentially allowing the model to place the delimiter interactively
would guarantee placement in the "intended" location,
but this is slow
*/
let sectionString = code.split('\n').slice(lineStart - 1, lineEnd).join('\n')
let lenUpToSection = code.split('\n').slice(0, lineStart - 1).map(s=>s + '\n').join('').length
let snippetIdxInSection = findStartAndEndNormalized(sectionString, snippet, nthOccurrence)
if (snippetIdxInSection.start === -1) {
lineStart = Math.max(0, lineStart - 1)
lineEnd = Math.min(lineEnd + 1, code.split('\n').length)
sectionString = code.split('\n').slice(lineStart - 1, lineEnd).join('\n')
lenUpToSection = code.split('\n').slice(0, lineStart - 1).map(s=>s + '\n').join('').length
snippetIdxInSection = findStartAndEndNormalized(sectionString, snippet, nthOccurrence)
}
// const sectionString = code.split('\n').slice(lineStart - 1, lineEnd).join('\n')
// const lenUpToSection = code.split('\n').slice(0, lineStart - 1).map(s=>s + '\n').join('').length
// const snippetIdxInSection = findStartAndEndNormalized(sectionString, snippet, nthOccurrence)
const leftIdx = lenUpToSection + snippetIdxInSection.start
const rightIdx = leftIdx + snippetIdxInSection.end - snippetIdxInSection.start
return code.slice(0, leftIdx) + delimiterStart + code.slice(leftIdx, rightIdx) + delimiterEnd + code.slice(rightIdx, code.length)
}
try {
const out = computeUpdatedCodeWithSnippetRetagged({
code:updatedCodeWithoutDelimiters,
snippet:gptRetaggingJSON[1],
lineStart:gptRetaggingJSON[2],
lineEnd:gptRetaggingJSON[3],
nthOccurrence:gptRetaggingJSON[4],
delimiterStart:delimiter,
delimiterEnd:delimiter})
console.log(out)
return {gptRetaggingJSON, out}
} catch (e) {
return {error: e, errorType: 'snippet matching', gptOut, gptRetaggingJSON}
}
}