Skip to content

Commit

Permalink
Merge pull request #246 from meronogbai/fix/multiple-user-images-in-a…
Browse files Browse the repository at this point in the history
…-row-gemini-pro

fix: allow multiple user messages in a row for google gemini
  • Loading branch information
VisargD authored Mar 16, 2024
2 parents 92b1d5a + 1ff6e14 commit 5c70e12
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/providers/google/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ export const GoogleChatCompleteConfig: ProviderConfig = {
param: "contents",
default: "",
transform: (params: Params) => {
const messages = params.messages?.map((message: Message) => {
let lastRole: 'user' | 'model' | undefined;
const messages: { role: string; parts: { text: string; }[]; }[] = [];

params.messages?.forEach((message: Message) => {
const role = message.role === "assistant" ? "model" : "user";
let parts = [];
if (typeof message.content === "string") {
Expand All @@ -64,7 +67,17 @@ export const GoogleChatCompleteConfig: ProviderConfig = {
}
});
}
return { role, parts };

// @NOTE: This takes care of the "Please ensure that multiturn requests alternate between user and model."
// error that occurs when we have multiple user messages in a row.
const shouldAppendEmptyModeChat = lastRole === 'user' && role === 'user' && !params.model?.includes('vision');

if (shouldAppendEmptyModeChat) {
messages.push({ role: 'model', parts: [{ text: '' }] })
}

messages.push({ role, parts });
lastRole = role
});
return messages;
},
Expand Down

0 comments on commit 5c70e12

Please sign in to comment.