@genkit-ai/compat-oai
Version:
Genkit AI framework plugin for OpenAI APIs.
125 lines • 4.14 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");
const resolver = async (ai, client, actionType, actionName) => {
if (actionType === "model") {
const modelRef = (0, import_grok.xaiModelRef)({ name: `xai/${actionName}` });
(0, import_model.defineCompatOpenAIModel)({
ai,
name: modelRef.name,
client,
modelRef,
requestBuilder: import_grok.grokRequestBuilder
});
} else {
import_logging.logger.warn("Only model actions are supported by the XAI plugin");
}
};
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: `xai/${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: `xai/${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."
});
}
return (0, import__.openAICompatible)({
name: "xai",
baseURL: "https://api.x.ai/v1",
apiKey,
...options,
initializer: async (ai, client) => {
Object.values(import_grok.SUPPORTED_LANGUAGE_MODELS).forEach(
(modelRef) => (0, import_model.defineCompatOpenAIModel)({
ai,
name: modelRef.name,
client,
modelRef,
requestBuilder: import_grok.grokRequestBuilder
})
);
Object.values(import_grok_image.SUPPORTED_IMAGE_MODELS).forEach(
(modelRef) => (0, import_image.defineCompatOpenAIImageModel)({
ai,
name: modelRef.name,
client,
modelRef
})
);
},
resolver,
listActions
});
}
const model = (name, config) => {
if (name.includes("image")) {
return (0, import_grok_image.xaiImageModelRef)({
name: `xai/${name}`,
config
});
}
return (0, import_grok.xaiModelRef)({
name: `xai/${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