Skip to content

Commit

Permalink
delete additional_parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
narengogi committed Dec 31, 2024
1 parent e3d45cd commit 70524ae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
7 changes: 2 additions & 5 deletions src/providers/google-vertex-ai/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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') {
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);
});
}
}
};

0 comments on commit 70524ae

Please sign in to comment.