Skip to content
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
20 changes: 11 additions & 9 deletions js/plugins/compat-oai/src/deepseek/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,21 @@ const listActions = async (client: OpenAI): Promise<ActionMetadata[]> => {
};

export function deepSeekPlugin(options?: DeepSeekPluginOptions): GenkitPlugin {
const apiKey = options?.apiKey ?? process.env.DEEPSEEK_API_KEY;
if (!apiKey) {
throw new GenkitError({
status: 'FAILED_PRECONDITION',
message:
'Please pass in the API key or set the DEEPSEEK_API_KEY environment variable.',
});
}

return openAICompatible({
name: 'deepseek',
baseURL: 'https://api.deepseek.com',
apiKey,
...options,
precondition: async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one way I see this working is if this is not just a precondition but aresolveOptions function. It should return the resolved options which can be used to create the OpenAI client.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that should be good

const apiKey = options?.apiKey ?? process.env.DEEPSEEK_API_KEY;
if (!apiKey) {
throw new GenkitError({
status: 'FAILED_PRECONDITION',
message:
'Please pass in the API key or set the DEEPSEEK_API_KEY environment variable.',
});
}
},
initializer: async (ai, client) => {
Object.values(SUPPORTED_DEEPSEEK_MODELS).forEach((modelRef) =>
defineCompatOpenAIModel({
Expand Down
15 changes: 15 additions & 0 deletions js/plugins/compat-oai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export {

export interface PluginOptions extends Partial<ClientOptions> {
name: string;
precondition?: () => Promise<void>;
initializer?: (ai: Genkit, client: OpenAI) => Promise<void>;
resolver?: (
ai: Genkit,
Expand Down Expand Up @@ -113,12 +114,21 @@ export const openAICompatible = (options: PluginOptions) => {
return genkitPlugin(
options.name,
async (ai: Genkit) => {
if (options.precondition) {
await options.precondition();
}

if (options.initializer) {
const client = new OpenAI(options);
await options.initializer(ai, client);
}
},
async (ai: Genkit, actionType: ActionType, actionName: string) => {

if (options.precondition) {
await options.precondition();
}

if (options.resolver) {
const client = new OpenAI(options);
await options.resolver(ai, client, actionType, actionName);
Expand All @@ -127,6 +137,11 @@ export const openAICompatible = (options: PluginOptions) => {
options.listActions
? async () => {
if (listActionsCache) return listActionsCache;

if (options.precondition) {
await options.precondition();
}

const client = new OpenAI(options);
listActionsCache = await options.listActions!(client);
return listActionsCache;
Expand Down
20 changes: 11 additions & 9 deletions js/plugins/compat-oai/src/xai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,21 @@ const listActions = async (client: OpenAI): Promise<ActionMetadata[]> => {
};

export function xAIPlugin(options?: XAIPluginOptions): GenkitPlugin {
const apiKey = options?.apiKey ?? process.env.XAI_API_KEY;
if (!apiKey) {
throw new GenkitError({
status: 'FAILED_PRECONDITION',
message:
'Please pass in the API key or set the XAI_API_KEY environment variable.',
});
}

return openAICompatible({
name: 'xai',
baseURL: 'https://api.x.ai/v1',
apiKey,
...options,
precondition: async () => {
const apiKey = options?.apiKey ?? process.env.XAI_API_KEY;
if (!apiKey) {
throw new GenkitError({
status: 'FAILED_PRECONDITION',
message:
'Please pass in the API key or set the XAI_API_KEY environment variable.',
});
}
},
initializer: async (ai, client) => {
Object.values(SUPPORTED_LANGUAGE_MODELS).forEach((modelRef) =>
defineCompatOpenAIModel({
Expand Down
Loading