|
16 | 16 |
|
17 | 17 | import { Genkit, ModelReference } from 'genkit';
|
18 | 18 | import { GenkitPlugin, genkitPlugin } from 'genkit/plugin';
|
| 19 | +import { ActionType } from 'genkit/registry'; |
19 | 20 | import {
|
20 | 21 | SUPPORTED_MODELS as EMBEDDER_MODELS,
|
21 | 22 | defineGoogleAIEmbedder,
|
@@ -81,82 +82,127 @@ export interface PluginOptions {
|
81 | 82 | experimental_debugTraces?: boolean;
|
82 | 83 | }
|
83 | 84 |
|
84 |
| -/** |
85 |
| - * Google Gemini Developer API plugin. |
86 |
| - */ |
87 |
| -export function googleAI(options?: PluginOptions): GenkitPlugin { |
88 |
| - return genkitPlugin('googleai', async (ai: Genkit) => { |
89 |
| - let apiVersions = ['v1']; |
| 85 | +async function initializer(ai: Genkit, options?: PluginOptions) { |
| 86 | + let apiVersions = ['v1']; |
90 | 87 |
|
91 |
| - if (options?.apiVersion) { |
92 |
| - if (Array.isArray(options?.apiVersion)) { |
93 |
| - apiVersions = options?.apiVersion; |
94 |
| - } else { |
95 |
| - apiVersions = [options?.apiVersion]; |
96 |
| - } |
| 88 | + if (options?.apiVersion) { |
| 89 | + if (Array.isArray(options?.apiVersion)) { |
| 90 | + apiVersions = options?.apiVersion; |
| 91 | + } else { |
| 92 | + apiVersions = [options?.apiVersion]; |
97 | 93 | }
|
| 94 | + } |
98 | 95 |
|
99 |
| - if (apiVersions.includes('v1beta')) { |
100 |
| - Object.keys(SUPPORTED_V15_MODELS).forEach((name) => |
101 |
| - defineGoogleAIModel({ |
102 |
| - ai, |
103 |
| - name, |
104 |
| - apiKey: options?.apiKey, |
105 |
| - apiVersion: 'v1beta', |
106 |
| - baseUrl: options?.baseUrl, |
107 |
| - debugTraces: options?.experimental_debugTraces, |
108 |
| - }) |
109 |
| - ); |
110 |
| - } |
111 |
| - if (apiVersions.includes('v1')) { |
112 |
| - Object.keys(SUPPORTED_V1_MODELS).forEach((name) => |
113 |
| - defineGoogleAIModel({ |
114 |
| - ai, |
115 |
| - name, |
116 |
| - apiKey: options?.apiKey, |
117 |
| - apiVersion: undefined, |
118 |
| - baseUrl: options?.baseUrl, |
119 |
| - debugTraces: options?.experimental_debugTraces, |
120 |
| - }) |
121 |
| - ); |
122 |
| - Object.keys(SUPPORTED_V15_MODELS).forEach((name) => |
123 |
| - defineGoogleAIModel({ |
124 |
| - ai, |
125 |
| - name, |
126 |
| - apiKey: options?.apiKey, |
127 |
| - apiVersion: undefined, |
128 |
| - baseUrl: options?.baseUrl, |
129 |
| - debugTraces: options?.experimental_debugTraces, |
130 |
| - }) |
131 |
| - ); |
132 |
| - Object.keys(EMBEDDER_MODELS).forEach((name) => |
133 |
| - defineGoogleAIEmbedder(ai, name, { apiKey: options?.apiKey }) |
134 |
| - ); |
135 |
| - } |
| 96 | + if (apiVersions.includes('v1beta')) { |
| 97 | + Object.keys(SUPPORTED_V15_MODELS).forEach((name) => |
| 98 | + defineGoogleAIModel({ |
| 99 | + ai, |
| 100 | + name, |
| 101 | + apiKey: options?.apiKey, |
| 102 | + apiVersion: 'v1beta', |
| 103 | + baseUrl: options?.baseUrl, |
| 104 | + debugTraces: options?.experimental_debugTraces, |
| 105 | + }) |
| 106 | + ); |
| 107 | + } |
| 108 | + if (apiVersions.includes('v1')) { |
| 109 | + Object.keys(SUPPORTED_V1_MODELS).forEach((name) => |
| 110 | + defineGoogleAIModel({ |
| 111 | + ai, |
| 112 | + name, |
| 113 | + apiKey: options?.apiKey, |
| 114 | + apiVersion: undefined, |
| 115 | + baseUrl: options?.baseUrl, |
| 116 | + debugTraces: options?.experimental_debugTraces, |
| 117 | + }) |
| 118 | + ); |
| 119 | + Object.keys(SUPPORTED_V15_MODELS).forEach((name) => |
| 120 | + defineGoogleAIModel({ |
| 121 | + ai, |
| 122 | + name, |
| 123 | + apiKey: options?.apiKey, |
| 124 | + apiVersion: undefined, |
| 125 | + baseUrl: options?.baseUrl, |
| 126 | + debugTraces: options?.experimental_debugTraces, |
| 127 | + }) |
| 128 | + ); |
| 129 | + Object.keys(EMBEDDER_MODELS).forEach((name) => |
| 130 | + defineGoogleAIEmbedder(ai, name, { apiKey: options?.apiKey }) |
| 131 | + ); |
| 132 | + } |
136 | 133 |
|
137 |
| - if (options?.models) { |
138 |
| - for (const modelOrRef of options?.models) { |
139 |
| - const modelName = |
140 |
| - typeof modelOrRef === 'string' |
141 |
| - ? modelOrRef |
142 |
| - : // strip out the `googleai/` prefix |
143 |
| - modelOrRef.name.split('/')[1]; |
144 |
| - const modelRef = |
145 |
| - typeof modelOrRef === 'string' ? gemini(modelOrRef) : modelOrRef; |
146 |
| - defineGoogleAIModel({ |
147 |
| - ai, |
148 |
| - name: modelName, |
149 |
| - apiKey: options?.apiKey, |
150 |
| - baseUrl: options?.baseUrl, |
151 |
| - info: { |
152 |
| - ...modelRef.info, |
153 |
| - label: `Google AI - ${modelName}`, |
154 |
| - }, |
155 |
| - debugTraces: options?.experimental_debugTraces, |
156 |
| - }); |
157 |
| - } |
| 134 | + if (options?.models) { |
| 135 | + for (const modelOrRef of options?.models) { |
| 136 | + const modelName = |
| 137 | + typeof modelOrRef === 'string' |
| 138 | + ? modelOrRef |
| 139 | + : // strip out the `googleai/` prefix |
| 140 | + modelOrRef.name.split('/')[1]; |
| 141 | + const modelRef = |
| 142 | + typeof modelOrRef === 'string' ? gemini(modelOrRef) : modelOrRef; |
| 143 | + defineGoogleAIModel({ |
| 144 | + ai, |
| 145 | + name: modelName, |
| 146 | + apiKey: options?.apiKey, |
| 147 | + baseUrl: options?.baseUrl, |
| 148 | + info: { |
| 149 | + ...modelRef.info, |
| 150 | + label: `Google AI - ${modelName}`, |
| 151 | + }, |
| 152 | + debugTraces: options?.experimental_debugTraces, |
| 153 | + }); |
158 | 154 | }
|
159 |
| - }); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +async function resolver( |
| 159 | + ai: Genkit, |
| 160 | + actionType: ActionType, |
| 161 | + actionName: string, |
| 162 | + options?: PluginOptions |
| 163 | +) { |
| 164 | + // TODO: also support other actions like 'embedder' |
| 165 | + switch (actionType) { |
| 166 | + case 'model': |
| 167 | + await resolveModel(ai, actionName, options); |
| 168 | + break; |
| 169 | + default: |
| 170 | + // no-op |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +async function resolveModel( |
| 175 | + ai: Genkit, |
| 176 | + actionName: string, |
| 177 | + options?: PluginOptions |
| 178 | +) { |
| 179 | + if (actionName.includes('gemini')) { |
| 180 | + const modelRef = gemini(actionName); |
| 181 | + defineGoogleAIModel({ |
| 182 | + ai, |
| 183 | + name: modelRef.name, |
| 184 | + apiKey: options?.apiKey, |
| 185 | + baseUrl: options?.baseUrl, |
| 186 | + info: { |
| 187 | + ...modelRef.info, |
| 188 | + label: `Google AI - ${actionName}`, |
| 189 | + }, |
| 190 | + debugTraces: options?.experimental_debugTraces, |
| 191 | + }); |
| 192 | + } |
| 193 | + // TODO: Support other models |
| 194 | +} |
| 195 | + |
| 196 | +/** |
| 197 | + * Google Gemini Developer API plugin. |
| 198 | + */ |
| 199 | +export function googleAI(options?: PluginOptions): GenkitPlugin { |
| 200 | + return genkitPlugin( |
| 201 | + 'googleai', |
| 202 | + async (ai: Genkit) => await initializer(ai, options), |
| 203 | + async (ai: Genkit, actionType: ActionType, actionName: string) => |
| 204 | + await resolver(ai, actionType, actionName, options) |
| 205 | + ); |
160 | 206 | }
|
161 | 207 |
|
162 | 208 | export default googleAI;
|
0 commit comments