Skip to content

Commit

Permalink
Initial create a imagen sdk.
Browse files Browse the repository at this point in the history
  • Loading branch information
junyanxu committed Oct 18, 2024
1 parent 89d6693 commit 708dd63
Show file tree
Hide file tree
Showing 11 changed files with 1,519 additions and 141 deletions.
36 changes: 26 additions & 10 deletions common/api-review/generative-ai.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
```ts

import * as jspb from 'google-protobuf';

// @public
export interface BaseParams {
// (undocumented)
Expand Down Expand Up @@ -620,29 +622,31 @@ export enum HarmProbability {
NEGLIGIBLE = "NEGLIGIBLE"
}

// @public
export class ImageGenerationModel {
constructor(apiKey: string, modelParams: ModelParams, _requestOptions?: RequestOptions);
// (undocumented)
apiKey: string;
generateImages(request: ImageGenerationRequest | string, requestOptions?: SingleRequestOptions): Promise<ImageGenerationResponse>;
// (undocumented)
model: string;
}

// @public
export interface ImageGenerationRequest {
// (undocumented)
aspectRatio?: "1:1" | "9:16" | "16:9" | "4:3" | "3:4";
// (undocumented)
compressionQuality?: number;
// (undocumented)
guidanceScale?: number;
// (undocumented)
height?: number;
// (undocumented)
language?: string;
// (undocumented)
negativePrompt?: string;
// (undocumented)
numberOfImages?: number;
// (undocumented)
outputMimeType?: "image/png" | "image/jpeg";
// (undocumented)
personGeneration?: "dont_allow" | "allow_adult";
// (undocumented)
prompt: string;
// (undocumented)
safetyFilterLevel?: "block_low_and_above" | "block_medium_and_above" | "block_only_high";
// (undocumented)
width?: number;
}

Expand Down Expand Up @@ -713,6 +717,18 @@ export type Part = TextPart | InlineDataPart | FunctionCallPart | FunctionRespon
// @public
export const POSSIBLE_ROLES: readonly ["user", "model", "function", "system"];

// @public
export interface PredictRequest {
instances?: any[];
model?: string;
parameters?: any;
}

// @public
export interface PredictResponse {
predictions?: jspb.FieldValue[];
}

// @public
export interface PromptFeedback {
// (undocumented)
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"devDependencies": {
"@changesets/cli": "^2.27.1",
"@esm-bundle/chai": "^4.3.4-fix.0",
"@google-cloud/asset": "^5.7.0",
"@microsoft/api-documenter": "^7.23.12",
"@microsoft/api-extractor": "^7.38.2",
"@rollup/plugin-json": "^6.0.1",
Expand Down Expand Up @@ -88,5 +89,9 @@
"bugs": {
"url": "https://github.com/google/generative-ai-js/issues"
},
"homepage": "https://github.com/google/generative-ai-js#readme"
"homepage": "https://github.com/google/generative-ai-js#readme",
"dependencies": {
"@types/google-protobuf": "^3.15.12",
"google-protobuf": "^3.21.4"
}
}
29 changes: 5 additions & 24 deletions src/methods/generate-images.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
/**
* @license
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License ats
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
ImageGenerationRequest,
ImageGenerationResponse,
PredictResponse,
SingleRequestOptions,
} from "../../types";
import { Task, makeModelRequest } from "../requests/request";
import { convertFromImageGenerationRequest } from "../requests/request-helpers";
import { ImageGenerationPredictResponse } from "../../types/predict";

import { convertToImageGenerationResponse } from "../requests/response-helpers";

export async function generateImages(
Expand All @@ -30,18 +15,14 @@ export async function generateImages(
params: ImageGenerationRequest,
requestOptions: SingleRequestOptions,
): Promise<ImageGenerationResponse> {
const predictRequest = convertFromImageGenerationRequest(model, params);
const response = await makeModelRequest(
model,
Task.PREDICT,
apiKey,
/* stream */ false,
JSON.stringify(predictRequest),
JSON.stringify(convertFromImageGenerationRequest(model, params)),
requestOptions,
);
const responseJson: ImageGenerationPredictResponse = await response.json();
return convertToImageGenerationResponse(
predictRequest.parameters,
responseJson,
);
const responseJson: PredictResponse = await response.json();
return convertToImageGenerationResponse(responseJson);
}
18 changes: 4 additions & 14 deletions src/models/vision-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import {
SingleRequestOptions,
} from "../../types";
import { generateImages } from "../methods/generate-images";
import { formatGenerateImageInput } from "../requests/request-helpers";

/**
* Class for generative model APIs.
* @public
Expand All @@ -45,19 +43,11 @@ export class ImageGenerationModel {
}
}
/**
* Makes a single non-streaming call to the model
* and returns an object containing a single {@link ImageGenerationResponse}.
*
* Inside the response there will be generated pictures specified by
* numberOfImages in {@link ImageGenerationRequest}
*
* Fields set in the optional {@link SingleRequestOptions} parameter will
* take precedence over the {@link RequestOptions} values provided to
* {@link GoogleGenerativeAI.getImageGenerationModel }.
* Generates image based on the request.
*/
async generateImages(
request: ImageGenerationRequest | string,
requestOptions: SingleRequestOptions = {},
request: ImageGenerationRequest,
requestOptions: SingleRequestOptions = {}
): Promise<ImageGenerationResponse> {
const generativeModelRequestOptions: SingleRequestOptions = {
...this._requestOptions,
Expand All @@ -66,7 +56,7 @@ export class ImageGenerationModel {
return generateImages(
this.apiKey,
this.model,
formatGenerateImageInput(request),
request,
generativeModelRequestOptions,
);
}
Expand Down
40 changes: 19 additions & 21 deletions src/requests/request-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import {
EmbedContentRequest,
GenerateContentRequest,
ImageGenerationRequest,
ImageGenerationRequest,
ModelParams,
Part,
PredictRequest,
_CountTokensRequestInternal,
_GenerateContentRequestInternal,
} from "../../types";
import { PredictRequest } from "../../types/predict";
import * as jspb from "google-protobuf";
import {
GoogleGenerativeAIError,
GoogleGenerativeAIRequestInputError,
Expand Down Expand Up @@ -179,32 +181,28 @@ export function formatEmbedContentInput(
return params;
}

export function formatGenerateImageInput(
params: ImageGenerationRequest | string,
): ImageGenerationRequest {
let formattedRequest: ImageGenerationRequest;
if (typeof params === "string") {
formattedRequest = { prompt: params };
} else {
formattedRequest = params as ImageGenerationRequest;
}
return formattedRequest;
}

export function convertFromImageGenerationRequest(
model: string,
modelName: string,
request: ImageGenerationRequest,
): PredictRequest {
const instances = [{ prompt: request.prompt }];
const sampleImageSize = Math.max(request.width || 0, request.height || 0);
const parameters = {
...request,
sampleCount: request.numberOfImages || 1,
sampleImageSize: sampleImageSize === 0 ? undefined : sampleImageSize,
negativePrompt: request.negativePrompt,
numberOfImages: request.numberOfImages,
width: request.width,
height: request.height,
aspectRatio: request.aspectRatio,
guidanceScale: request.guidanceScale,
outputMimeType: request.outputMimeType,
compressionQuality: request.compressionQuality,
language: request.language,
safetyFilterLevel: request.safetyFilterLevel,
personGeneration: request.personGeneration,
};
return {
model,
instances,
parameters,
model: modelName,
instances: instances,
parameters: parameters,
};
}

2 changes: 1 addition & 1 deletion src/requests/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export enum Task {
COUNT_TOKENS = "countTokens",
EMBED_CONTENT = "embedContent",
BATCH_EMBED_CONTENTS = "batchEmbedContents",
PREDICT = "predict",
PREDICT = "predict"
}

export class RequestUrl {
Expand Down
17 changes: 7 additions & 10 deletions src/requests/response-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
GenerateContentCandidate,
GenerateContentResponse,
ImageGenerationResponse,
PredictResponse
} from "../../types";
import { GeneratedImage } from "../../types/generated-media";
import {
Expand Down Expand Up @@ -214,15 +215,11 @@ export function formatBlockErrorMessage(
}

export function convertToImageGenerationResponse(
sharedParameters: PredictServiceValueType,
response: ImageGenerationPredictResponse,
response: PredictResponse
): ImageGenerationResponse {
const images: GeneratedImage[] = [];
for (const prediction of response!.predictions) {
images.push({
imageBytes: prediction.bytesBase64Encoded,
generationParameters: sharedParameters,
});
}
return { images };
console.log(response);
return {
images: []
};
}

24 changes: 20 additions & 4 deletions types/generated-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,29 @@
* limitations under the License.
*/

import { PredictServiceValueType } from "./predict";

/**
* Generated media.
* @public
*/
export interface GeneratedMedia {
video?: GeneratedVideo;
}

/**
* Generated images. It will be returned in response.
* Generated video.
* @public
*/
export interface GeneratedVideo {
/**
* Video bytes.
*/
video?: string;
/**
* Specifies the dynamic retrieval configuration for the given source.
*/
uri?: string;
}

export interface GeneratedImage {
/**
* Image bytes.
Expand All @@ -30,5 +46,5 @@ export interface GeneratedImage {
/**
* Generation parameters.
*/
generationParameters?: PredictServiceValueType;
generationParameters?: { [key: string]: any };
}
Loading

0 comments on commit 708dd63

Please sign in to comment.