Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#837 delete unsupported fields in gemini models tool definitions #840

Merged
6 changes: 5 additions & 1 deletion src/providers/google-vertex-ai/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import type {
VertexLLamaChatCompleteResponse,
GoogleSearchRetrievalTool,
} from './types';
import { getMimeType } from './utils';
import { getMimeType, recursivelyDeleteUnsupportedParameters } from './utils';

export const buildGoogleSearchRetrievalTool = (tool: Tool) => {
const googleSearchRetrievalTool: GoogleSearchRetrievalTool = {
Expand Down Expand Up @@ -270,6 +270,10 @@ export const VertexGoogleChatCompleteConfig: ProviderConfig = {
const tools: any = [];
params.tools?.forEach((tool) => {
if (tool.type === 'function') {
// these are not supported by google
recursivelyDeleteUnsupportedParameters(tool.function?.parameters);
delete tool.function?.strict;

if (tool.function.name === 'googleSearchRetrieval') {
tools.push(buildGoogleSearchRetrievalTool(tool));
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/providers/google-vertex-ai/transformGenerationConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Params } from '../../types/requestBody';
import { derefer } from './utils';
import { derefer, recursivelyDeleteUnsupportedParameters } from './utils';
/**
* @see https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/gemini#request_body
*/
Expand Down Expand Up @@ -28,6 +28,9 @@ export function transformGenerationConfig(params: Params) {
}
if (params?.response_format?.type === 'json_schema') {
generationConfig['responseMimeType'] = 'application/json';
recursivelyDeleteUnsupportedParameters(
params?.response_format?.json_schema?.schema
);
let schema =
params?.response_format?.json_schema?.schema ??
params?.response_format?.json_schema;
Expand Down
18 changes: 18 additions & 0 deletions src/providers/google-vertex-ai/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,21 @@ export const derefer = (spec: Record<string, any>, defs = null) => {
}
return original;
};

// Vertex AI does not support additionalProperties in JSON Schema
// https://cloud.google.com/vertex-ai/docs/reference/rest/v1/Schema
export const recursivelyDeleteUnsupportedParameters = (obj: any) => {
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) return;
delete obj.additional_properties;
delete obj.additionalProperties;
for (const key in obj) {
if (obj[key] !== null && typeof obj[key] === 'object') {
recursivelyDeleteUnsupportedParameters(obj[key]);
}
if (key == 'anyOf' && Array.isArray(obj[key])) {
obj[key].forEach((item: any) => {
recursivelyDeleteUnsupportedParameters(item);
});
}
}
};
13 changes: 12 additions & 1 deletion src/providers/google/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {
SYSTEM_MESSAGE_ROLES,
} from '../../types/requestBody';
import { buildGoogleSearchRetrievalTool } from '../google-vertex-ai/chatComplete';
import { derefer, getMimeType } from '../google-vertex-ai/utils';
import {
derefer,
getMimeType,
recursivelyDeleteUnsupportedParameters,
} from '../google-vertex-ai/utils';
import {
ChatCompletionResponse,
ErrorResponse,
Expand Down Expand Up @@ -45,6 +49,9 @@ const transformGenerationConfig = (params: Params) => {
}
if (params?.response_format?.type === 'json_schema') {
generationConfig['responseMimeType'] = 'application/json';
recursivelyDeleteUnsupportedParameters(
params?.response_format?.json_schema?.schema
);
let schema =
params?.response_format?.json_schema?.schema ??
params?.response_format?.json_schema;
Expand Down Expand Up @@ -332,6 +339,10 @@ export const GoogleChatCompleteConfig: ProviderConfig = {
const tools: any = [];
params.tools?.forEach((tool) => {
if (tool.type === 'function') {
// these are not supported by google
recursivelyDeleteUnsupportedParameters(tool.function?.parameters);
delete tool.function?.strict;

if (tool.function.name === 'googleSearchRetrieval') {
tools.push(buildGoogleSearchRetrievalTool(tool));
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/types/requestBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ export interface Function {
description?: string;
/** The parameters for the function. */
parameters?: JsonSchema;
/** Whether to enable strict schema adherence when generating the function call. If set to true, the model will follow the exact schema defined in the parameters field. Only a subset of JSON Schema is supported when strict is true */
strict?: boolean;
}

export interface ToolChoiceObject {
Expand Down
Loading