mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2025-04-23 13:08:11 +00:00
Patch Gemini/Google AI errors (#977)
This commit is contained in:
parent
efe9dfa5e3
commit
0dd6001fa6
1 changed files with 32 additions and 2 deletions
|
@ -87,7 +87,7 @@ class GeminiLLM {
|
||||||
formatMessages(messages = []) {
|
formatMessages(messages = []) {
|
||||||
// Gemini roles are either user || model.
|
// Gemini roles are either user || model.
|
||||||
// and all "content" is relabeled to "parts"
|
// and all "content" is relabeled to "parts"
|
||||||
return messages
|
const allMessages = messages
|
||||||
.map((message) => {
|
.map((message) => {
|
||||||
if (message.role === "system")
|
if (message.role === "system")
|
||||||
return { role: "user", parts: message.content };
|
return { role: "user", parts: message.content };
|
||||||
|
@ -98,6 +98,16 @@ class GeminiLLM {
|
||||||
return null;
|
return null;
|
||||||
})
|
})
|
||||||
.filter((msg) => !!msg);
|
.filter((msg) => !!msg);
|
||||||
|
|
||||||
|
// Specifically, Google cannot have the last sent message be from a user with no assistant reply
|
||||||
|
// otherwise it will crash. So if the last item is from the user, it was not completed so pop it off
|
||||||
|
// the history.
|
||||||
|
if (
|
||||||
|
allMessages.length > 0 &&
|
||||||
|
allMessages[allMessages.length - 1].role === "user"
|
||||||
|
)
|
||||||
|
allMessages.pop();
|
||||||
|
return allMessages;
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendChat(chatHistory = [], prompt, workspace = {}, rawHistory = []) {
|
async sendChat(chatHistory = [], prompt, workspace = {}, rawHistory = []) {
|
||||||
|
@ -210,7 +220,27 @@ class GeminiLLM {
|
||||||
response.on("close", handleAbort);
|
response.on("close", handleAbort);
|
||||||
|
|
||||||
for await (const chunk of stream) {
|
for await (const chunk of stream) {
|
||||||
fullText += chunk.text();
|
let chunkText;
|
||||||
|
try {
|
||||||
|
// Due to content sensitivity we cannot always get the function .text();
|
||||||
|
// https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/configure-safety-attributes#gemini-TASK-samples-nodejs
|
||||||
|
// and it is not possible to unblock or disable this safety protocol without being allowlisted by Google.
|
||||||
|
chunkText = chunk.text();
|
||||||
|
} catch (e) {
|
||||||
|
chunkText = e.message;
|
||||||
|
writeResponseChunk(response, {
|
||||||
|
uuid,
|
||||||
|
sources: [],
|
||||||
|
type: "abort",
|
||||||
|
textResponse: null,
|
||||||
|
close: true,
|
||||||
|
error: e.message,
|
||||||
|
});
|
||||||
|
resolve(e.message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fullText += chunkText;
|
||||||
writeResponseChunk(response, {
|
writeResponseChunk(response, {
|
||||||
uuid,
|
uuid,
|
||||||
sources: [],
|
sources: [],
|
||||||
|
|
Loading…
Add table
Reference in a new issue