From b0167d6480dc740385a6474716c58a58e67bd728 Mon Sep 17 00:00:00 2001 From: Narendranath Gogineni Date: Tue, 24 Dec 2024 15:17:27 +0530 Subject: [PATCH 1/5] #837 delete unsupported fields in gemini models tool definitions --- src/providers/google-vertex-ai/chatComplete.ts | 4 ++++ src/providers/google/chatComplete.ts | 4 ++++ src/types/requestBody.ts | 2 ++ 3 files changed, 10 insertions(+) diff --git a/src/providers/google-vertex-ai/chatComplete.ts b/src/providers/google-vertex-ai/chatComplete.ts index c428e4bd6..49d6da853 100644 --- a/src/providers/google-vertex-ai/chatComplete.ts +++ b/src/providers/google-vertex-ai/chatComplete.ts @@ -269,6 +269,10 @@ export const VertexGoogleChatCompleteConfig: ProviderConfig = { const tools: any = []; params.tools?.forEach((tool) => { if (tool.type === 'function') { + // these are not supported by google + delete tool.function?.parameters?.properties?.additional_properties; + delete tool.function?.strict; + if (tool.function.name === 'googleSearchRetrieval') { tools.push(buildGoogleSearchRetrievalTool(tool)); } else { diff --git a/src/providers/google/chatComplete.ts b/src/providers/google/chatComplete.ts index 44937db3f..1b50311dd 100644 --- a/src/providers/google/chatComplete.ts +++ b/src/providers/google/chatComplete.ts @@ -329,6 +329,10 @@ export const GoogleChatCompleteConfig: ProviderConfig = { const tools: any = []; params.tools?.forEach((tool) => { if (tool.type === 'function') { + // these are not supported by google + delete tool.function?.parameters?.properties?.additional_properties; + delete tool.function?.strict; + if (tool.function.name === 'googleSearchRetrieval') { tools.push(buildGoogleSearchRetrievalTool(tool)); } else { diff --git a/src/types/requestBody.ts b/src/types/requestBody.ts index 272a7b3aa..17fca4df0 100644 --- a/src/types/requestBody.ts +++ b/src/types/requestBody.ts @@ -279,6 +279,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 { From fc19cf7e99a22e73a10213bf306b96c02a20723f Mon Sep 17 00:00:00 2001 From: Narendranath Gogineni Date: Thu, 26 Dec 2024 14:38:48 +0530 Subject: [PATCH 2/5] delete nested additionalProperties and alos handle both snake_case and camelCase --- src/providers/google-vertex-ai/chatComplete.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/providers/google-vertex-ai/chatComplete.ts b/src/providers/google-vertex-ai/chatComplete.ts index 49d6da853..6e446daad 100644 --- a/src/providers/google-vertex-ai/chatComplete.ts +++ b/src/providers/google-vertex-ai/chatComplete.ts @@ -270,7 +270,10 @@ export const VertexGoogleChatCompleteConfig: ProviderConfig = { params.tools?.forEach((tool) => { if (tool.type === 'function') { // these are not supported by google + delete tool.function?.parameters?.additional_properties; + delete tool.function?.parameters?.additionalProperties; delete tool.function?.parameters?.properties?.additional_properties; + delete tool.function?.parameters?.properties?.additionalProperties; delete tool.function?.strict; if (tool.function.name === 'googleSearchRetrieval') { From e3d45cdd7dd991e05e886805001e977ad2d7a166 Mon Sep 17 00:00:00 2001 From: Narendranath Gogineni Date: Thu, 26 Dec 2024 14:39:45 +0530 Subject: [PATCH 3/5] delete nested additionalProperties and alos handle both snake_case and camelCase --- src/providers/google/chatComplete.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/providers/google/chatComplete.ts b/src/providers/google/chatComplete.ts index 1b50311dd..695bc3b92 100644 --- a/src/providers/google/chatComplete.ts +++ b/src/providers/google/chatComplete.ts @@ -330,7 +330,10 @@ export const GoogleChatCompleteConfig: ProviderConfig = { params.tools?.forEach((tool) => { if (tool.type === 'function') { // these are not supported by google + delete tool.function?.parameters?.additional_properties; + delete tool.function?.parameters?.additionalProperties; delete tool.function?.parameters?.properties?.additional_properties; + delete tool.function?.parameters?.properties?.additionalProperties; delete tool.function?.strict; if (tool.function.name === 'googleSearchRetrieval') { From 70524aebc173f2ae9f20c46175f255154ebd33db Mon Sep 17 00:00:00 2001 From: Narendranath Gogineni Date: Tue, 31 Dec 2024 07:58:46 +0530 Subject: [PATCH 4/5] delete additional_parameters --- src/providers/google-vertex-ai/chatComplete.ts | 7 ++----- .../transformGenerationConfig.ts | 5 ++++- src/providers/google-vertex-ai/utils.ts | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/providers/google-vertex-ai/chatComplete.ts b/src/providers/google-vertex-ai/chatComplete.ts index 6e446daad..11dc81de8 100644 --- a/src/providers/google-vertex-ai/chatComplete.ts +++ b/src/providers/google-vertex-ai/chatComplete.ts @@ -39,7 +39,7 @@ import type { VertexLLamaChatCompleteResponse, GoogleSearchRetrievalTool, } from './types'; -import { getMimeType } from './utils'; +import { getMimeType, recursivelyDeleteUnsupportedParameters } from './utils'; export const buildGoogleSearchRetrievalTool = (tool: Tool) => { const googleSearchRetrievalTool: GoogleSearchRetrievalTool = { @@ -270,10 +270,7 @@ export const VertexGoogleChatCompleteConfig: ProviderConfig = { params.tools?.forEach((tool) => { if (tool.type === 'function') { // these are not supported by google - delete tool.function?.parameters?.additional_properties; - delete tool.function?.parameters?.additionalProperties; - delete tool.function?.parameters?.properties?.additional_properties; - delete tool.function?.parameters?.properties?.additionalProperties; + recursivelyDeleteUnsupportedParameters(tool.function?.parameters); delete tool.function?.strict; if (tool.function.name === 'googleSearchRetrieval') { diff --git a/src/providers/google-vertex-ai/transformGenerationConfig.ts b/src/providers/google-vertex-ai/transformGenerationConfig.ts index 8d11b948b..654f66e65 100644 --- a/src/providers/google-vertex-ai/transformGenerationConfig.ts +++ b/src/providers/google-vertex-ai/transformGenerationConfig.ts @@ -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 */ @@ -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; diff --git a/src/providers/google-vertex-ai/utils.ts b/src/providers/google-vertex-ai/utils.ts index adc897228..b722aa501 100644 --- a/src/providers/google-vertex-ai/utils.ts +++ b/src/providers/google-vertex-ai/utils.ts @@ -202,3 +202,21 @@ export const derefer = (spec: Record, 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); + }); + } + } +}; From e1354dce869f6044958b1c18354f435fd9f703c3 Mon Sep 17 00:00:00 2001 From: Narendranath Gogineni Date: Tue, 31 Dec 2024 08:08:56 +0530 Subject: [PATCH 5/5] delete additional_parameters in google tools and response format --- src/providers/google/chatComplete.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/providers/google/chatComplete.ts b/src/providers/google/chatComplete.ts index 695bc3b92..2e53d52a7 100644 --- a/src/providers/google/chatComplete.ts +++ b/src/providers/google/chatComplete.ts @@ -8,7 +8,11 @@ import { ToolChoice, } 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, @@ -44,6 +48,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; @@ -330,10 +337,7 @@ export const GoogleChatCompleteConfig: ProviderConfig = { params.tools?.forEach((tool) => { if (tool.type === 'function') { // these are not supported by google - delete tool.function?.parameters?.additional_properties; - delete tool.function?.parameters?.additionalProperties; - delete tool.function?.parameters?.properties?.additional_properties; - delete tool.function?.parameters?.properties?.additionalProperties; + recursivelyDeleteUnsupportedParameters(tool.function?.parameters); delete tool.function?.strict; if (tool.function.name === 'googleSearchRetrieval') {