Skip to content

Commit 974fcc9

Browse files
russellwheatleymikehardy
authored andcommitted
generate-content.ts
1 parent 6c9623c commit 974fcc9

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

packages/ai/lib/methods/generate-content.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@ import { Task, makeRequest } from '../requests/request';
2626
import { createEnhancedContentResponse } from '../requests/response-helpers';
2727
import { processStream } from '../requests/stream-reader';
2828
import { ApiSettings } from '../types/internal';
29+
import { BackendType } from '../public-types';
30+
import * as GoogleAIMapper from '../googleai-mappers';
2931

3032
export async function generateContentStream(
3133
apiSettings: ApiSettings,
3234
model: string,
3335
params: GenerateContentRequest,
3436
requestOptions?: RequestOptions,
3537
): Promise<GenerateContentStreamResult> {
38+
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
39+
params = GoogleAIMapper.mapGenerateContentRequest(params);
40+
}
3641
const response = await makeRequest(
3742
model,
3843
Task.STREAM_GENERATE_CONTENT,
@@ -41,7 +46,7 @@ export async function generateContentStream(
4146
JSON.stringify(params),
4247
requestOptions,
4348
);
44-
return processStream(response);
49+
return processStream(response, apiSettings);
4550
}
4651

4752
export async function generateContent(
@@ -50,6 +55,9 @@ export async function generateContent(
5055
params: GenerateContentRequest,
5156
requestOptions?: RequestOptions,
5257
): Promise<GenerateContentResult> {
58+
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
59+
params = GoogleAIMapper.mapGenerateContentRequest(params);
60+
}
5361
const response = await makeRequest(
5462
model,
5563
Task.GENERATE_CONTENT,
@@ -58,9 +66,21 @@ export async function generateContent(
5866
JSON.stringify(params),
5967
requestOptions,
6068
);
61-
const responseJson: GenerateContentResponse = await response.json();
62-
const enhancedResponse = createEnhancedContentResponse(responseJson);
69+
const generateContentResponse = await processGenerateContentResponse(response, apiSettings);
70+
const enhancedResponse = createEnhancedContentResponse(generateContentResponse);
6371
return {
6472
response: enhancedResponse,
6573
};
6674
}
75+
76+
async function processGenerateContentResponse(
77+
response: Response,
78+
apiSettings: ApiSettings,
79+
): Promise<GenerateContentResponse> {
80+
const responseJson = await response.json();
81+
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
82+
return GoogleAIMapper.mapGenerateContentResponse(responseJson);
83+
} else {
84+
return responseJson;
85+
}
86+
}

0 commit comments

Comments
 (0)