Skip to content
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

feat(js/plugins/googleai): adds gemini() function for unspecified model support #1372

Open
wants to merge 4 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
56 changes: 49 additions & 7 deletions js/plugins/googleai/src/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const GeminiConfigSchema = GenerationCommonConfigSchema.extend({
})
.optional(),
});
export type GeminiConfig = z.infer<typeof GeminiConfigSchema>;

export const gemini10Pro = modelRef({
name: 'googleai/gemini-1.0-pro',
Expand Down Expand Up @@ -167,13 +168,54 @@ export const SUPPORTED_V15_MODELS = {
'gemini-1.5-flash-8b': gemini15Flash8b,
};

export const SUPPORTED_GEMINI_MODELS: Record<
string,
ModelReference<typeof GeminiConfigSchema>
> = {
export const GENERIC_GEMINI_MODEL = modelRef({
name: 'googleai/gemini',
configSchema: GeminiConfigSchema,
info: {
label: 'Google Gemini',
supports: {
multiturn: true,
media: true,
tools: true,
systemRole: true,
},
},
});

export const SUPPORTED_GEMINI_MODELS = {
...SUPPORTED_V1_MODELS,
...SUPPORTED_V15_MODELS,
};
} as const;

function longestMatchingPrefix(version: string, potentialMatches: string[]) {
return potentialMatches
.filter((p) => version.startsWith(p))
.reduce(
(longest, current) =>
current.length > longest.length ? current : longest,
''
);
}
export type GeminiVersionString =
| keyof typeof SUPPORTED_GEMINI_MODELS
| (string & {});

export function gemini(
version: GeminiVersionString,
options: GeminiConfig = {}
): ModelReference<typeof GeminiConfigSchema> {
const matchingKey = longestMatchingPrefix(
version,
Object.keys(SUPPORTED_GEMINI_MODELS)
);
if (matchingKey) {
return SUPPORTED_GEMINI_MODELS[matchingKey].withConfig({
...options,
version,
});
}
return GENERIC_GEMINI_MODEL.withConfig({ ...options, version });
}

function toGeminiRole(
role: MessageData['role'],
Expand Down Expand Up @@ -473,7 +515,7 @@ export function defineGoogleAIModel(
apiVersion?: string,
baseUrl?: string,
info?: ModelInfo,
defaultConfig?: z.infer<typeof GeminiConfigSchema>
defaultConfig?: GeminiConfig
): ModelAction {
if (!apiKey) {
apiKey = process.env.GOOGLE_GENAI_API_KEY || process.env.GOOGLE_API_KEY;
Expand Down Expand Up @@ -624,7 +666,7 @@ export function defineGoogleAIModel(
const chatRequest = {
systemInstruction,
generationConfig,
tools,
tools: tools.length ? tools : undefined,
toolConfig,
history: messages
.slice(0, -1)
Expand Down
20 changes: 19 additions & 1 deletion js/plugins/googleai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@ import {
textEmbeddingGecko001,
} from './embedder.js';
import {
GENERIC_GEMINI_MODEL,
SUPPORTED_V15_MODELS,
SUPPORTED_V1_MODELS,
defineGoogleAIModel,
gemini,
gemini10Pro,
gemini15Flash,
gemini15Pro,
} from './gemini.js';
export { gemini10Pro, gemini15Flash, gemini15Pro, textEmbeddingGecko001 };
export {
gemini,
gemini10Pro,
gemini15Flash,
gemini15Pro,
textEmbeddingGecko001,
};

export interface PluginOptions {
apiKey?: string;
Expand All @@ -48,6 +56,16 @@ export function googleAI(options?: PluginOptions): GenkitPlugin {
apiVersions = [options?.apiVersion];
}
}

defineGoogleAIModel(
ai,
GENERIC_GEMINI_MODEL.name,
options?.apiKey,
undefined,
options?.baseUrl,
GENERIC_GEMINI_MODEL.info
);

if (apiVersions.includes('v1beta')) {
Object.keys(SUPPORTED_V15_MODELS).forEach((name) =>
defineGoogleAIModel(
Expand Down
9 changes: 9 additions & 0 deletions js/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions js/testapps/flow-simple-ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "lib/index.js",
"scripts": {
"start": "node lib/index.js",
"start": "pnpm exec genkit start -- pnpm exec tsx --watch src/index.ts",
"compile": "tsc",
"build": "pnpm build:clean && pnpm compile",
"build:clean": "rimraf ./lib",
Expand All @@ -15,18 +15,21 @@
"author": "",
"license": "ISC",
"dependencies": {
"genkit": "workspace:*",
"@genkit-ai/firebase": "workspace:*",
"@genkit-ai/google-cloud": "workspace:*",
"@genkit-ai/googleai": "workspace:*",
"@genkit-ai/vertexai": "workspace:*",
"@google/generative-ai": "^0.15.0",
"@opentelemetry/sdk-trace-base": "^1.25.0",
"body-parser": "^1.20.3",
"express": "^4.21.0",
"firebase-admin": ">=12.2",
"genkit": "workspace:*",
"partial-json": "^0.1.7"
},
"devDependencies": {
"rimraf": "^6.0.1",
"tsx": "^4.19.2",
"typescript": "^5.3.3"
}
}
Loading