Skip to content

Commit 3e9d27c

Browse files
authored
🐛 fix: Handle content generation errors in GoogleClient (danny-avila#5575)
1 parent dc36e6d commit 3e9d27c

File tree

1 file changed

+78
-72
lines changed

1 file changed

+78
-72
lines changed

api/app/clients/GoogleClient.js

Lines changed: 78 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -605,91 +605,97 @@ class GoogleClient extends BaseClient {
605605

606606
let reply = '';
607607

608-
if (!EXCLUDED_GENAI_MODELS.test(modelName) && !this.project_id) {
609-
/** @type {GenAI} */
610-
const client = this.client;
611-
/** @type {GenerateContentRequest} */
612-
const requestOptions = {
613-
safetySettings,
614-
contents: _payload,
615-
generationConfig: googleGenConfigSchema.parse(this.modelOptions),
616-
};
617-
618-
const promptPrefix = (this.options.promptPrefix ?? '').trim();
619-
if (promptPrefix.length) {
620-
requestOptions.systemInstruction = {
621-
parts: [
622-
{
623-
text: promptPrefix,
624-
},
625-
],
608+
try {
609+
if (!EXCLUDED_GENAI_MODELS.test(modelName) && !this.project_id) {
610+
/** @type {GenAI} */
611+
const client = this.client;
612+
/** @type {GenerateContentRequest} */
613+
const requestOptions = {
614+
safetySettings,
615+
contents: _payload,
616+
generationConfig: googleGenConfigSchema.parse(this.modelOptions),
626617
};
618+
619+
const promptPrefix = (this.options.promptPrefix ?? '').trim();
620+
if (promptPrefix.length) {
621+
requestOptions.systemInstruction = {
622+
parts: [
623+
{
624+
text: promptPrefix,
625+
},
626+
],
627+
};
628+
}
629+
630+
const delay = modelName.includes('flash') ? 8 : 15;
631+
/** @type {GenAIUsageMetadata} */
632+
let usageMetadata;
633+
634+
const result = await client.generateContentStream(requestOptions);
635+
for await (const chunk of result.stream) {
636+
usageMetadata = !usageMetadata
637+
? chunk?.usageMetadata
638+
: Object.assign(usageMetadata, chunk?.usageMetadata);
639+
const chunkText = chunk.text();
640+
await this.generateTextStream(chunkText, onProgress, {
641+
delay,
642+
});
643+
reply += chunkText;
644+
await sleep(streamRate);
645+
}
646+
647+
if (usageMetadata) {
648+
this.usage = {
649+
input_tokens: usageMetadata.promptTokenCount,
650+
output_tokens: usageMetadata.candidatesTokenCount,
651+
};
652+
}
653+
654+
return reply;
655+
}
656+
657+
const { instances } = _payload;
658+
const { messages: messages, context } = instances?.[0] ?? {};
659+
660+
if (!this.isVisionModel && context && messages?.length > 0) {
661+
messages.unshift(new SystemMessage(context));
627662
}
628663

629-
const delay = modelName.includes('flash') ? 8 : 15;
630-
/** @type {GenAIUsageMetadata} */
664+
/** @type {import('@langchain/core/messages').AIMessageChunk['usage_metadata']} */
631665
let usageMetadata;
632-
const result = await client.generateContentStream(requestOptions);
633-
for await (const chunk of result.stream) {
666+
const stream = await this.client.stream(messages, {
667+
signal: abortController.signal,
668+
streamUsage: true,
669+
safetySettings,
670+
});
671+
672+
let delay = this.options.streamRate || 8;
673+
674+
if (!this.options.streamRate) {
675+
if (this.isGenerativeModel) {
676+
delay = 15;
677+
}
678+
if (modelName.includes('flash')) {
679+
delay = 5;
680+
}
681+
}
682+
683+
for await (const chunk of stream) {
634684
usageMetadata = !usageMetadata
635-
? chunk?.usageMetadata
636-
: Object.assign(usageMetadata, chunk?.usageMetadata);
637-
const chunkText = chunk.text();
685+
? chunk?.usage_metadata
686+
: concat(usageMetadata, chunk?.usage_metadata);
687+
const chunkText = chunk?.content ?? chunk;
638688
await this.generateTextStream(chunkText, onProgress, {
639689
delay,
640690
});
641691
reply += chunkText;
642-
await sleep(streamRate);
643692
}
644693

645694
if (usageMetadata) {
646-
this.usage = {
647-
input_tokens: usageMetadata.promptTokenCount,
648-
output_tokens: usageMetadata.candidatesTokenCount,
649-
};
650-
}
651-
return reply;
652-
}
653-
654-
const { instances } = _payload;
655-
const { messages: messages, context } = instances?.[0] ?? {};
656-
657-
if (!this.isVisionModel && context && messages?.length > 0) {
658-
messages.unshift(new SystemMessage(context));
659-
}
660-
661-
/** @type {import('@langchain/core/messages').AIMessageChunk['usage_metadata']} */
662-
let usageMetadata;
663-
const stream = await this.client.stream(messages, {
664-
signal: abortController.signal,
665-
streamUsage: true,
666-
safetySettings,
667-
});
668-
669-
let delay = this.options.streamRate || 8;
670-
671-
if (!this.options.streamRate) {
672-
if (this.isGenerativeModel) {
673-
delay = 15;
674-
}
675-
if (modelName.includes('flash')) {
676-
delay = 5;
695+
this.usage = usageMetadata;
677696
}
678-
}
679-
680-
for await (const chunk of stream) {
681-
usageMetadata = !usageMetadata
682-
? chunk?.usage_metadata
683-
: concat(usageMetadata, chunk?.usage_metadata);
684-
const chunkText = chunk?.content ?? chunk;
685-
await this.generateTextStream(chunkText, onProgress, {
686-
delay,
687-
});
688-
reply += chunkText;
689-
}
690-
691-
if (usageMetadata) {
692-
this.usage = usageMetadata;
697+
} catch (e) {
698+
logger.error('[GoogleClient] There was an issue generating the completion', e);
693699
}
694700
return reply;
695701
}

0 commit comments

Comments
 (0)