chore(server): try repair text in structured outputs (#11543)
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
|||||||
FilePart,
|
FilePart,
|
||||||
generateObject,
|
generateObject,
|
||||||
generateText,
|
generateText,
|
||||||
|
JSONParseError,
|
||||||
streamText,
|
streamText,
|
||||||
TextPart,
|
TextPart,
|
||||||
} from 'ai';
|
} from 'ai';
|
||||||
@@ -234,6 +235,19 @@ export class GeminiProvider
|
|||||||
messages: msgs,
|
messages: msgs,
|
||||||
schema,
|
schema,
|
||||||
abortSignal: options.signal,
|
abortSignal: options.signal,
|
||||||
|
experimental_repairText: async ({ text, error }) => {
|
||||||
|
if (error instanceof JSONParseError) {
|
||||||
|
const ret = text.trim();
|
||||||
|
if (ret.startsWith('```') || ret.endsWith('```')) {
|
||||||
|
return ret
|
||||||
|
.replace(/```[\w\s]+\n/g, '')
|
||||||
|
.replace(/\n```/g, '')
|
||||||
|
.trim();
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
},
|
||||||
}).then(r => ({ text: JSON.stringify(r.object) }))
|
}).then(r => ({ text: JSON.stringify(r.object) }))
|
||||||
: await generateText({
|
: await generateText({
|
||||||
model: modelInstance,
|
model: modelInstance,
|
||||||
|
|||||||
Reference in New Issue
Block a user