UNPKG

@genkit-ai/compat-oai

Version:

Genkit AI framework plugin for OpenAI APIs.

119 lines 3.21 kB
import { GenkitError, modelActionMetadata } from "genkit"; import { logger } from "genkit/logging"; import { defineCompatOpenAIImageModel } from "../image.mjs"; import { openAICompatible } from "../index.mjs"; import { defineCompatOpenAIModel } from "../model.mjs"; import { SUPPORTED_IMAGE_MODELS, xaiImageModelRef } from "./grok-image.mjs"; import { grokRequestBuilder, SUPPORTED_LANGUAGE_MODELS, xaiModelRef } from "./grok.mjs"; function createResolver(pluginOptions) { return async (client, actionType, actionName) => { if (actionType === "model") { const modelRef = xaiModelRef({ name: actionName }); return defineCompatOpenAIModel({ name: modelRef.name, client, pluginOptions, modelRef, requestBuilder: grokRequestBuilder }); } else { logger.warn("Only model actions are supported by the XAI plugin"); } return void 0; }; } const listActions = async (client) => { return await client.models.list().then( (response) => response.data.filter((model2) => model2.object === "model").map((model2) => { if (model2.id.includes("image")) { const modelRef = SUPPORTED_IMAGE_MODELS[model2.id] ?? xaiImageModelRef({ name: model2.id }); return modelActionMetadata({ name: modelRef.name, info: modelRef.info, configSchema: modelRef.configSchema }); } else { const modelRef = SUPPORTED_LANGUAGE_MODELS[model2.id] ?? xaiModelRef({ name: model2.id }); return modelActionMetadata({ name: modelRef.name, info: modelRef.info, configSchema: modelRef.configSchema }); } }) ); }; function xAIPlugin(options) { 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." }); } const pluginOptions = { name: "xai", ...options }; return openAICompatible({ name: "xai", baseURL: "https://api.x.ai/v1", apiKey, ...options, initializer: async (client) => { const models = []; models.push( ...Object.values(SUPPORTED_LANGUAGE_MODELS).map( (modelRef) => defineCompatOpenAIModel({ name: modelRef.name, client, pluginOptions, modelRef, requestBuilder: grokRequestBuilder }) ) ); models.push( ...Object.values(SUPPORTED_IMAGE_MODELS).map( (modelRef) => defineCompatOpenAIImageModel({ name: modelRef.name, client, pluginOptions, modelRef }) ) ); return models; }, resolver: createResolver(pluginOptions), listActions }); } const model = ((name, config) => { if (name.includes("image")) { return xaiImageModelRef({ name, config }); } return xaiModelRef({ name, config }); }); const xAI = Object.assign(xAIPlugin, { model }); var xai_default = xAI; export { xai_default as default, xAI, xAIPlugin }; //# sourceMappingURL=index.mjs.map