Skip to content

Commit 5f461e5

Browse files
committed
support grounding mode in gemini
refactoring update request structure
1 parent bb797e3 commit 5f461e5

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

src/providers/google-vertex-ai/chatComplete.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
ContentType,
77
Message,
88
Params,
9+
Tool,
910
ToolCall,
1011
} from '../../types/requestBody';
1112
import {
@@ -36,9 +37,21 @@ import type {
3637
GoogleGenerateContentResponse,
3738
VertexLlamaChatCompleteStreamChunk,
3839
VertexLLamaChatCompleteResponse,
40+
GoogleSearchRetrievalTool,
3941
} from './types';
4042
import { getMimeType } from './utils';
4143

44+
const buildGoogleSearchRetrievalTool = (tool: Tool) => {
45+
const googleSearchRetrievalTool: GoogleSearchRetrievalTool = {
46+
googleSearchRetrieval: {},
47+
};
48+
if (tool.function.parameters?.dynamicRetrievalConfig) {
49+
googleSearchRetrievalTool.googleSearchRetrieval.dynamicRetrievalConfig =
50+
tool.function.parameters.dynamicRetrievalConfig;
51+
}
52+
return googleSearchRetrievalTool;
53+
};
54+
4255
export const VertexGoogleChatCompleteConfig: ProviderConfig = {
4356
// https://cloud.google.com/vertex-ai/generative-ai/docs/learn/model-versioning#gemini-model-versions
4457
model: {
@@ -253,12 +266,20 @@ export const VertexGoogleChatCompleteConfig: ProviderConfig = {
253266
default: '',
254267
transform: (params: Params) => {
255268
const functionDeclarations: any = [];
269+
const tools: any = [];
256270
params.tools?.forEach((tool) => {
257271
if (tool.type === 'function') {
258-
functionDeclarations.push(tool.function);
272+
if (tool.function.name === 'googleSearchRetrieval') {
273+
tools.push(buildGoogleSearchRetrievalTool(tool));
274+
} else {
275+
functionDeclarations.push(tool.function);
276+
}
259277
}
260278
});
261-
return { functionDeclarations };
279+
if (functionDeclarations.length) {
280+
tools.push({ functionDeclarations });
281+
}
282+
return tools;
262283
},
263284
},
264285
tool_choice: {
@@ -648,6 +669,9 @@ export const GoogleChatCompleteResponseTransform: (
648669
...(!strictOpenAiCompliance && {
649670
safetyRatings: generation.safetyRatings,
650671
}),
672+
...(!strictOpenAiCompliance && generation.groundingMetadata
673+
? { groundingMetadata: generation.groundingMetadata }
674+
: {}),
651675
};
652676
}) ?? [],
653677
usage: {

src/providers/google-vertex-ai/types.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ export interface GoogleGenerateContentResponse {
2828
category: string;
2929
probability: string;
3030
}[];
31+
groundingMetadata?: {
32+
webSearchQueries?: string[];
33+
searchEntryPoint?: {
34+
renderedContent: string;
35+
};
36+
groundingSupports?: Array<{
37+
segment: {
38+
startIndex: number;
39+
endIndex: number;
40+
text: string;
41+
};
42+
groundingChunkIndices: number[];
43+
confidenceScores: number[];
44+
}>;
45+
retrievalMetadata?: {
46+
webDynamicRetrievalScore: number;
47+
};
48+
};
3149
}[];
3250
promptFeedback: {
3351
safetyRatings: {
@@ -90,3 +108,12 @@ export interface GoogleEmbedResponse {
90108
billableCharacterCount: number;
91109
};
92110
}
111+
112+
export interface GoogleSearchRetrievalTool {
113+
googleSearchRetrieval: {
114+
dynamicRetrievalConfig?: {
115+
mode: string;
116+
dynamicThreshold?: string;
117+
};
118+
};
119+
}

src/types/requestBody.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ export interface Tool extends AnthropicPromptCache {
301301
/** The name of the function. */
302302
type: string;
303303
/** A description of the function. */
304-
function?: Function;
304+
function: Function;
305305
}
306306

307307
/**

0 commit comments

Comments
 (0)