Skip to content

Added support for all List type API calls #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions common/api-review/generative-ai-server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

```ts

/// <reference types="node" />

// Warning: (ae-incompatible-release-tags) The symbol "ArraySchema" is marked as @public, but its signature references "BaseSchema" which is marked as @internal
//
// @public
Expand Down Expand Up @@ -31,8 +33,6 @@ export interface BooleanSchema extends BaseSchema {
type: typeof SchemaType.BOOLEAN;
}

/// <reference types="node" />

// @public
export interface CachedContent extends CachedContentBase {
createTime?: string;
Expand Down
1 change: 1 addition & 0 deletions common/api-review/generative-ai.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ export class GoogleGenerativeAI {
apiKey: string;
getGenerativeModel(modelParams: ModelParams, requestOptions?: RequestOptions): GenerativeModel;
getGenerativeModelFromCachedContent(cachedContent: CachedContent, modelParams?: Partial<ModelParams>, requestOptions?: RequestOptions): GenerativeModel;
listModels(pageSize?: number, pageToken?: string, requestOptions?: RequestOptions): Promise<Response>;
}

// @public
Expand Down
110 changes: 110 additions & 0 deletions src/gen-ai.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,114 @@ describe("GoogleGenerativeAI", () => {
`Different value for "systemInstruction" specified in modelParams (yo) and cachedContent (hi)`,
);
});

it("listModels gets a Response without any params passed", () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listModels();
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});
it("listModels gets a Response when passed only pageSize", async () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listModels({ pageSize: 10 });
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});
it("listModels gets a Response when passed only pageToken", async () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listModels({ pageToken: "token" });
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});
it("listModels gets a Response when passed both pageSize and pageToken", async () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listModels({ pageSize: 10, pageToken: "token" });
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});

it("listTunedModels gets a Response without any params passed", () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listTunedModels();
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});
it("listTunedModels gets a Response when passed only pageSize", async () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listTunedModels({ pageSize: 10 });
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});
it("listTunedModels gets a Response when passed only pageToken", async () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listTunedModels({ pageToken: "token" });
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});
it("listTunedModels gets a Response when passed only filter", async () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listTunedModels({ filter: "filter" });
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});
it("listTunedModels gets a Response when passed all params pageSize, pageToken and filter", async () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listTunedModels({ pageSize: 10, pageToken: "token", filter: "filter" });
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});

it("listCachedContent gets a Response without any params passed", () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listCachedContent();
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});
it("listCachedContent gets a Response when passed only pageSize", async () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listCachedContent({ pageSize: 10 });
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});
it("listCachedContent gets a Response when passed only pageToken", async () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listCachedContent({ pageToken: "token" });
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});
it("listCachedContent gets a Response when passed both pageSize and pageToken", async () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listCachedContent({ pageSize: 10, pageToken: "token" });
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});

it("listCorpora gets a Response without any params passed", () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listCorpora();
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});
it("listCorpora gets a Response when passed only pageSize", async () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listCorpora({ pageSize: 10 });
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});
it("listCorpora gets a Response when passed only pageToken", async () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listCorpora({ pageToken: "token" });
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});
it("listCorpora gets a Response when passed both pageSize and pageToken", async () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listCorpora({ pageSize: 10, pageToken: "token" });
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});

it("listFiles gets a Response without any params passed", () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listFiles();
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});
it("listFiles gets a Response when passed only pageSize", async () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listFiles({ pageSize: 10 });
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});
it("listFiles gets a Response when passed only pageToken", async () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listFiles({ pageToken: "token" });
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});
it("listFiles gets a Response when passed both pageSize and pageToken", async () => {
const genAI = new GoogleGenerativeAI("apikey");
const genModel = genAI.listFiles({ pageSize: 10, pageToken: "token" });
expect(genModel).to.be.an.instanceOf(Promise<Response>);
});
});
118 changes: 115 additions & 3 deletions src/gen-ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from "./errors";
import { CachedContent, ModelParams, RequestOptions } from "../types";
import { GenerativeModel } from "./models/generative-model";
import { List, makeListRequest } from "./requests/request-list";

export { ChatSession } from "./methods/chat-session";
export { GenerativeModel };
Expand All @@ -30,7 +31,7 @@ export { GenerativeModel };
* @public
*/
export class GoogleGenerativeAI {
constructor(public apiKey: string) {}
constructor(public apiKey: string) { }

/**
* Gets a {@link GenerativeModel} instance for the provided model name.
Expand All @@ -42,7 +43,7 @@ export class GoogleGenerativeAI {
if (!modelParams.model) {
throw new GoogleGenerativeAIError(
`Must provide a model name. ` +
`Example: genai.getGenerativeModel({ model: 'my-model-name' })`,
`Example: genai.getGenerativeModel({ model: 'my-model-name' })`,
);
}
return new GenerativeModel(this.apiKey, modelParams, requestOptions);
Expand Down Expand Up @@ -93,7 +94,7 @@ export class GoogleGenerativeAI {
}
throw new GoogleGenerativeAIRequestInputError(
`Different value for "${key}" specified in modelParams` +
` (${modelParams[key]}) and cachedContent (${cachedContent[key]})`,
` (${modelParams[key]}) and cachedContent (${cachedContent[key]})`,
);
}
}
Expand All @@ -112,4 +113,115 @@ export class GoogleGenerativeAI {
requestOptions,
);
}

/**
* Gets a list of {@link GenerativeModel} available.
*/
async listModels(
params: { pageSize?: number; pageToken?: string } = {},
requestOptions?: RequestOptions,
): Promise<Response> {

const filteredParams = Object.fromEntries(
Object.entries({ params }).filter(([_, v]) => v != null)
);

const response = await makeListRequest(
List.MODELS,
this.apiKey,
filteredParams,
requestOptions,
);

return response.json();
}

/**
* Gets a list of tuned {@link GenerativeModel} available.
*/
async listTunedModels(
params: { pageSize?: number; pageToken?: string; filter?: string } = {},
requestOptions?: RequestOptions,
): Promise<Response> {

const filteredParams = Object.fromEntries(
Object.entries({ params }).filter(([_, v]) => v != null)
);

const response = await makeListRequest(
List.TUNED_MODELS,
this.apiKey,
filteredParams,
requestOptions,
);

return response.json();
}

/**
* Gets a list of CachedContent.
*/
async listCachedContent(
params: { pageSize?: number; pageToken?: string } = {},
requestOptions?: RequestOptions,
): Promise<Response> {

const filteredParams = Object.fromEntries(
Object.entries({ params }).filter(([_, v]) => v != null)
);

const response = await makeListRequest(
List.CASHED_CONTENTS,
this.apiKey,
filteredParams,
requestOptions,
);

return response.json();
}

/**
* Gets a list of all Corpora owned by the user.
*/
async listCorpora(
params: { pageSize?: number; pageToken?: string } = {},
requestOptions?: RequestOptions,
): Promise<Response> {

const filteredParams = Object.fromEntries(
Object.entries({ params }).filter(([_, v]) => v != null)
);

const response = await makeListRequest(
List.CORPORA,
this.apiKey,
filteredParams,
requestOptions,
);

return response.json();
}

/**
* Gets a list of the metadata for Files owned by the requesting project.
*/
async listFiles(
params: { pageSize?: number; pageToken?: string } = {},
requestOptions?: RequestOptions,
): Promise<Response> {

const filteredParams = Object.fromEntries(
Object.entries({ params }).filter(([_, v]) => v != null)
);

const response = await makeListRequest(
List.CORPORA,
this.apiKey,
filteredParams,
requestOptions,
);

return response.json();
}

}
Loading