@llumiverse/drivers
Version:
LLM driver implementations. Currently supported are: openai, huggingface, bedrock, replicate.
71 lines • 2.84 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.xAIDriver = void 0;
const openai_format_js_1 = require("../openai/openai_format.js");
const api_fetch_client_1 = require("@vertesia/api-fetch-client");
const openai_1 = __importDefault(require("openai"));
const index_js_1 = require("../openai/index.js");
class xAIDriver extends index_js_1.BaseOpenAIDriver {
service;
provider;
xai_service;
DEFAULT_ENDPOINT = "https://api.x.ai/v1";
constructor(opts) {
super(opts);
if (!opts.apiKey) {
throw new Error("apiKey is required");
}
this.service = new openai_1.default({
apiKey: opts.apiKey,
baseURL: opts.endpoint ?? this.DEFAULT_ENDPOINT,
});
this.xai_service = new api_fetch_client_1.FetchClient(opts.endpoint ?? this.DEFAULT_ENDPOINT).withAuthCallback(async () => `Bearer ${opts.apiKey}`);
this.provider = "xai";
//this.formatPrompt = this._formatPrompt; //TODO: fix xai prompt formatting
}
async _formatPrompt(segments, opts) {
const options = {
multimodal: opts.model.includes("vision"),
schema: opts.result_schema,
useToolForFormatting: false,
};
const p = await (0, openai_format_js_1.formatOpenAILikeMultimodalPrompt)(segments, { ...options, ...opts });
return p;
}
extractDataFromResponse(_options, result) {
return {
result: result.choices[0].message.content ? [{ type: "text", value: result.choices[0].message.content }] : [],
finish_reason: result.choices[0].finish_reason,
token_usage: {
prompt: result.usage?.prompt_tokens,
result: result.usage?.completion_tokens,
total: result.usage?.total_tokens,
}
};
}
async listModels() {
const [lm, em] = await Promise.all([
this.xai_service.get("/language-models"),
this.xai_service.get("/embedding-models")
]);
em.models.forEach(m => {
m.output_modalities.push("vectors");
});
const models = [...lm.models, ...em.models].map(model => {
return {
id: model.id,
provider: this.provider,
name: model.object,
description: model.object,
is_multimodal: model.input_modalities.length > 1,
tags: [...model.input_modalities.map(m => `ì:${m}`), ...model.output_modalities.map(m => `ì:${m}`)],
};
});
return models;
}
}
exports.xAIDriver = xAIDriver;
//# sourceMappingURL=index.js.map