@genkit-ai/compat-oai
Version:
Genkit AI framework plugin for OpenAI APIs.
135 lines • 4.41 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var xai_exports = {};
__export(xai_exports, {
default: () => xai_default,
xAI: () => xAI,
xAIPlugin: () => xAIPlugin
});
module.exports = __toCommonJS(xai_exports);
var import_genkit = require("genkit");
var import_logging = require("genkit/logging");
var import_image = require("../image.js");
var import__ = require("../index.js");
var import_model = require("../model.js");
var import_grok_image = require("./grok-image.js");
var import_grok = require("./grok.js");
function createResolver(pluginOptions) {
return async (client, actionType, actionName) => {
if (actionType === "model") {
const modelRef = (0, import_grok.xaiModelRef)({ name: actionName });
return (0, import_model.defineCompatOpenAIModel)({
name: modelRef.name,
client,
pluginOptions,
modelRef,
requestBuilder: import_grok.grokRequestBuilder
});
} else {
import_logging.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 = import_grok_image.SUPPORTED_IMAGE_MODELS[model2.id] ?? (0, import_grok_image.xaiImageModelRef)({ name: model2.id });
return (0, import_genkit.modelActionMetadata)({
name: modelRef.name,
info: modelRef.info,
configSchema: modelRef.configSchema
});
} else {
const modelRef = import_grok.SUPPORTED_LANGUAGE_MODELS[model2.id] ?? (0, import_grok.xaiModelRef)({ name: model2.id });
return (0, import_genkit.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 import_genkit.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 (0, import__.openAICompatible)({
name: "xai",
baseURL: "https://api.x.ai/v1",
apiKey,
...options,
initializer: async (client) => {
const models = [];
models.push(
...Object.values(import_grok.SUPPORTED_LANGUAGE_MODELS).map(
(modelRef) => (0, import_model.defineCompatOpenAIModel)({
name: modelRef.name,
client,
pluginOptions,
modelRef,
requestBuilder: import_grok.grokRequestBuilder
})
)
);
models.push(
...Object.values(import_grok_image.SUPPORTED_IMAGE_MODELS).map(
(modelRef) => (0, import_image.defineCompatOpenAIImageModel)({
name: modelRef.name,
client,
pluginOptions,
modelRef
})
)
);
return models;
},
resolver: createResolver(pluginOptions),
listActions
});
}
const model = ((name, config) => {
if (name.includes("image")) {
return (0, import_grok_image.xaiImageModelRef)({
name,
config
});
}
return (0, import_grok.xaiModelRef)({
name,
config
});
});
const xAI = Object.assign(xAIPlugin, {
model
});
var xai_default = xAI;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
xAI,
xAIPlugin
});
//# sourceMappingURL=index.js.map