Skip to content

Commit

Permalink
Merge pull request Portkey-AI#461 from narengogi/fix/vertex-ai-gemini…
Browse files Browse the repository at this point in the history
…-role-alternation-fix

vertex-ai/gemini Combine messages with the same role in succession to handle multi turn messages
  • Loading branch information
VisargD authored Jul 19, 2024
2 parents 491d265 + ae11fb9 commit d68542c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
24 changes: 14 additions & 10 deletions src/providers/google-vertex-ai/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import {
GoogleMessage,
GoogleMessageRole,
GoogleToolConfig,
SYSTEM_INSTRUCTION_DISABLED_MODELS,
transformOpenAIRoleToGoogleRole,
transformToolChoiceForGemini,
Expand Down Expand Up @@ -135,16 +136,15 @@ export const VertexGoogleChatCompleteConfig: ProviderConfig = {

// @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');
const shouldCombineMessages =
lastRole === role && !params.model?.includes('vision');

if (shouldAppendEmptyModeChat) {
messages.push({ role: 'model', parts: [{ text: '' }] });
if (shouldCombineMessages) {
messages[messages.length - 1].parts.push(...parts);
} else {
messages.push({ role, parts });
}

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

Expand Down Expand Up @@ -258,12 +258,16 @@ export const VertexGoogleChatCompleteConfig: ProviderConfig = {
) {
allowedFunctionNames.push(params.tool_choice.function.name);
}
return {
functionCallingConfig: {
const toolConfig: GoogleToolConfig = {
function_calling_config: {
mode: transformToolChoiceForGemini(params.tool_choice),
allowedFunctionNames,
},
};
if (allowedFunctionNames.length > 0) {
toolConfig.function_calling_config.allowed_function_names =
allowedFunctionNames;
}
return toolConfig;
}
},
},
Expand Down
17 changes: 14 additions & 3 deletions src/providers/google/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ export interface GoogleMessage {
parts: GoogleMessagePart[];
}

export interface GoogleToolConfig {
function_calling_config: {
mode: GoogleToolChoiceType | undefined;
allowed_function_names?: string[];
};
}

export const transformOpenAIRoleToGoogleRole = (
role: OpenAIMessageRole
): GoogleMessageRole => {
Expand Down Expand Up @@ -285,12 +292,16 @@ export const GoogleChatCompleteConfig: ProviderConfig = {
) {
allowedFunctionNames.push(params.tool_choice.function.name);
}
return {
functionCallingConfig: {
const toolConfig: GoogleToolConfig = {
function_calling_config: {
mode: transformToolChoiceForGemini(params.tool_choice),
allowedFunctionNames,
},
};
if (allowedFunctionNames.length > 0) {
toolConfig.function_calling_config.allowed_function_names =
allowedFunctionNames;
}
return toolConfig;
}
},
},
Expand Down

0 comments on commit d68542c

Please sign in to comment.