Skip to content

Commit ef5752c

Browse files
authored
Merge pull request #994 from narengogi/feat/enhanced-grounding-support
changes to support cached streaming in grounding requests
2 parents 1de329f + 955c31c commit ef5752c

File tree

4 files changed

+33
-38
lines changed

4 files changed

+33
-38
lines changed

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

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChatCompletionResponse } from '../types';
1+
import { ChatCompletionResponse, GroundingMetadata } from '../types';
22

33
export interface GoogleErrorResponse {
44
error: {
@@ -46,24 +46,7 @@ export interface GoogleResponseCandidate {
4646
category: string;
4747
probability: string;
4848
}[];
49-
groundingMetadata?: {
50-
webSearchQueries?: string[];
51-
searchEntryPoint?: {
52-
renderedContent: string;
53-
};
54-
groundingSupports?: Array<{
55-
segment: {
56-
startIndex: number;
57-
endIndex: number;
58-
text: string;
59-
};
60-
groundingChunkIndices: number[];
61-
confidenceScores: number[];
62-
}>;
63-
retrievalMetadata?: {
64-
webDynamicRetrievalScore: number;
65-
};
66-
};
49+
groundingMetadata?: GroundingMetadata;
6750
}
6851

6952
export interface GoogleGenerateContentResponse {

src/providers/google/chatComplete.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
import {
2020
ChatCompletionResponse,
2121
ErrorResponse,
22+
GroundingMetadata,
2223
Logprobs,
2324
ProviderConfig,
2425
} from '../types';
@@ -452,24 +453,7 @@ interface GoogleResponseCandidate {
452453
category: string;
453454
probability: string;
454455
}[];
455-
groundingMetadata?: {
456-
webSearchQueries?: string[];
457-
searchEntryPoint?: {
458-
renderedContent: string;
459-
};
460-
groundingSupports?: Array<{
461-
segment: {
462-
startIndex: number;
463-
endIndex: number;
464-
text: string;
465-
};
466-
groundingChunkIndices: number[];
467-
confidenceScores: number[];
468-
}>;
469-
retrievalMetadata?: {
470-
webDynamicRetrievalScore: number;
471-
};
472-
};
456+
groundingMetadata?: GroundingMetadata;
473457
}
474458

475459
interface GoogleGenerateContentResponse {

src/providers/openai/chatComplete.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,14 @@ export const OpenAIChatCompleteJSONToStreamResponseTransform: (
146146
provider: string
147147
) => Array<string> = (response, provider) => {
148148
const streamChunkArray: Array<string> = [];
149-
const { id, model, system_fingerprint, choices } = response;
149+
const { id, model, system_fingerprint, choices, citations } = response;
150150

151151
const {
152152
prompt_tokens,
153153
completion_tokens,
154154
cache_read_input_tokens,
155155
cache_creation_input_tokens,
156+
num_search_queries,
156157
} = response.usage || {};
157158

158159
let total_tokens;
@@ -179,7 +180,9 @@ export const OpenAIChatCompleteJSONToStreamResponseTransform: (
179180
cache_read_input_tokens,
180181
cache_creation_input_tokens,
181182
}),
183+
...(num_search_queries && { num_search_queries }),
182184
},
185+
...(citations && { citations }),
183186
};
184187

185188
for (const [index, choice] of choices.entries()) {
@@ -262,6 +265,9 @@ export const OpenAIChatCompleteJSONToStreamResponseTransform: (
262265
role: 'assistant',
263266
content: word,
264267
},
268+
...(choice.groundingMetadata && {
269+
groundingMetadata: choice.groundingMetadata,
270+
}),
265271
},
266272
],
267273
})}\n\n`

src/providers/types.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ export interface CResponse extends BaseResponse {
152152
*/
153153
cache_read_input_tokens?: number;
154154
cache_creation_input_tokens?: number;
155+
num_search_queries?: number;
155156
};
156157
}
157158

@@ -168,6 +169,25 @@ export interface CompletionResponse extends CResponse {
168169
}[];
169170
}
170171

172+
export interface GroundingMetadata {
173+
webSearchQueries?: string[];
174+
searchEntryPoint?: {
175+
renderedContent: string;
176+
};
177+
groundingSupports?: Array<{
178+
segment: {
179+
startIndex: number;
180+
endIndex: number;
181+
text: string;
182+
};
183+
groundingChunkIndices: number[];
184+
confidenceScores: number[];
185+
}>;
186+
retrievalMetadata?: {
187+
webDynamicRetrievalScore: number;
188+
};
189+
}
190+
171191
/**
172192
* The structure of a choice in a chat completion response.
173193
* @interface
@@ -177,6 +197,7 @@ export interface ChatChoice {
177197
message: Message;
178198
finish_reason: string;
179199
logprobs?: object | null;
200+
groundingMetadata?: GroundingMetadata;
180201
}
181202

182203
export interface Logprobs {
@@ -197,6 +218,7 @@ export interface Logprobs {
197218
export interface ChatCompletionResponse extends CResponse {
198219
choices: ChatChoice[];
199220
provider?: string;
221+
citations?: string[];
200222
}
201223

202224
/**

0 commit comments

Comments
 (0)