1717import { ActionMetadata } from 'genkit' ;
1818import { ResolvableAction , genkitPluginV2 } from 'genkit/plugin' ;
1919import { ActionType } from 'genkit/registry' ;
20- import { OpenAI , type ClientOptions } from 'openai' ;
20+ import OpenAI , { type ClientOptions } from 'openai' ;
2121import { compatOaiModelRef , defineCompatOpenAIModel } from './model.js' ;
2222
2323export {
@@ -45,7 +45,8 @@ export {
4545 type ModelRequestBuilder ,
4646} from './model.js' ;
4747
48- export interface PluginOptions extends Partial < ClientOptions > {
48+ export interface PluginOptions extends Partial < Omit < ClientOptions , 'apiKey' > > {
49+ apiKey ?: ClientOptions [ 'apiKey' ] | false ;
4950 name : string ;
5051 initializer ?: ( client : OpenAI ) => Promise < ResolvableAction [ ] > ;
5152 resolver ?: (
@@ -110,24 +111,33 @@ export interface PluginOptions extends Partial<ClientOptions> {
110111 */
111112export const openAICompatible = ( options : PluginOptions ) => {
112113 let listActionsCache ;
114+ var client : OpenAI ;
115+ function createClient ( ) {
116+ if ( client ) return client ;
117+ const { apiKey, ...restofOptions } = options ;
118+ client = new OpenAI ( {
119+ ...restofOptions ,
120+ apiKey : apiKey === false ? 'placeholder' : apiKey ,
121+ } ) ;
122+ return client ;
123+ }
113124 return genkitPluginV2 ( {
114125 name : options . name ,
115126 async init ( ) {
116127 if ( ! options . initializer ) {
117128 return [ ] ;
118129 }
119- const client = new OpenAI ( options ) ;
120- return await options . initializer ( client ) ;
130+ return await options . initializer ( createClient ( ) ) ;
121131 } ,
122132 async resolve ( actionType : ActionType , actionName : string ) {
123- const client = new OpenAI ( options ) ;
124133 if ( options . resolver ) {
125- return await options . resolver ( client , actionType , actionName ) ;
134+ return await options . resolver ( createClient ( ) , actionType , actionName ) ;
126135 } else {
127136 if ( actionType === 'model' ) {
128137 return defineCompatOpenAIModel ( {
129138 name : actionName ,
130- client,
139+ client : createClient ( ) ,
140+ pluginOptions : options ,
131141 modelRef : compatOaiModelRef ( {
132142 name : actionName ,
133143 } ) ,
@@ -136,14 +146,15 @@ export const openAICompatible = (options: PluginOptions) => {
136146 return undefined ;
137147 }
138148 } ,
139- list : options . listActions
140- ? async ( ) => {
141- if ( listActionsCache ) return listActionsCache ;
142- const client = new OpenAI ( options ) ;
143- listActionsCache = await options . listActions ! ( client ) ;
144- return listActionsCache ;
145- }
146- : undefined ,
149+ list :
150+ // Don't attempt to list models if apiKey set to false
151+ options . listActions && options . apiKey !== false
152+ ? async ( ) => {
153+ if ( listActionsCache ) return listActionsCache ;
154+ listActionsCache = await options . listActions ! ( createClient ( ) ) ;
155+ return listActionsCache ;
156+ }
157+ : undefined ,
147158 } ) ;
148159} ;
149160
0 commit comments