mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 21:05:19 +00:00
chore(server): try repair text in structured outputs (#11543)
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
||||
FilePart,
|
||||
generateObject,
|
||||
generateText,
|
||||
JSONParseError,
|
||||
streamText,
|
||||
TextPart,
|
||||
} from 'ai';
|
||||
@@ -234,6 +235,19 @@ export class GeminiProvider
|
||||
messages: msgs,
|
||||
schema,
|
||||
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) }))
|
||||
: await generateText({
|
||||
model: modelInstance,
|
||||
|
||||
Reference in New Issue
Block a user