mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-16 16:30:15 +01:00
Some checks failed
Docker Dev Branch Images Build / build (Dockerfile, lc-dev, node) (push) Waiting to run
Docker Dev Branch Images Build / build (Dockerfile.multi, lc-dev-api, api-build) (push) Waiting to run
Docker Dev Images Build / build (Dockerfile, librechat-dev, node) (push) Has been cancelled
Docker Dev Images Build / build (Dockerfile.multi, librechat-dev-api, api-build) (push) Has been cancelled
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Has been cancelled
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Has been cancelled
* 🔧 fix: Correct URL Construction in fetchModels Function Updated the URL construction in the fetchModels function to ensure proper formatting by removing trailing slashes from the base URL. This change prevents potential issues with API endpoint calls. * 🔧 fix: Remove OLLAMA from Known Custom Providers Updated the isKnownCustomProvider function and providerConfigMap to exclude OLLAMA as a known custom provider, streamlining the provider checks and configurations. * 🔧 test: Enhance fetchModels Tests for URL Construction Added new test cases to validate the URL construction in the fetchModels function, ensuring it handles trailing slashes correctly and appends query parameters as expected. This improves the robustness of the API endpoint calls. * chore: remove ollama provider-specific handling * chore: Refactor imports to use isUserProvided from @librechat/api
96 lines
2.3 KiB
JavaScript
96 lines
2.3 KiB
JavaScript
const OpenAI = require('openai');
|
|
const { ProxyAgent } = require('undici');
|
|
const { isUserProvided } = require('@librechat/api');
|
|
const { ErrorTypes, EModelEndpoint } = require('librechat-data-provider');
|
|
const {
|
|
getUserKeyValues,
|
|
getUserKeyExpiry,
|
|
checkUserKeyExpiry,
|
|
} = require('~/server/services/UserService');
|
|
const OAIClient = require('~/app/clients/OpenAIClient');
|
|
|
|
const initializeClient = async ({ req, res, endpointOption, version, initAppClient = false }) => {
|
|
const { PROXY, OPENAI_ORGANIZATION, ASSISTANTS_API_KEY, ASSISTANTS_BASE_URL } = process.env;
|
|
|
|
const userProvidesKey = isUserProvided(ASSISTANTS_API_KEY);
|
|
const userProvidesURL = isUserProvided(ASSISTANTS_BASE_URL);
|
|
|
|
let userValues = null;
|
|
if (userProvidesKey || userProvidesURL) {
|
|
const expiresAt = await getUserKeyExpiry({
|
|
userId: req.user.id,
|
|
name: EModelEndpoint.assistants,
|
|
});
|
|
checkUserKeyExpiry(expiresAt, EModelEndpoint.assistants);
|
|
userValues = await getUserKeyValues({ userId: req.user.id, name: EModelEndpoint.assistants });
|
|
}
|
|
|
|
let apiKey = userProvidesKey ? userValues.apiKey : ASSISTANTS_API_KEY;
|
|
let baseURL = userProvidesURL ? userValues.baseURL : ASSISTANTS_BASE_URL;
|
|
|
|
const opts = {
|
|
defaultHeaders: {
|
|
'OpenAI-Beta': `assistants=${version}`,
|
|
},
|
|
};
|
|
|
|
const clientOptions = {
|
|
reverseProxyUrl: baseURL ?? null,
|
|
proxy: PROXY ?? null,
|
|
req,
|
|
res,
|
|
...endpointOption,
|
|
};
|
|
|
|
if (userProvidesKey & !apiKey) {
|
|
throw new Error(
|
|
JSON.stringify({
|
|
type: ErrorTypes.NO_USER_KEY,
|
|
}),
|
|
);
|
|
}
|
|
|
|
if (!apiKey) {
|
|
throw new Error('Assistants API key not provided. Please provide it again.');
|
|
}
|
|
|
|
if (baseURL) {
|
|
opts.baseURL = baseURL;
|
|
}
|
|
|
|
if (PROXY) {
|
|
const proxyAgent = new ProxyAgent(PROXY);
|
|
opts.fetchOptions = {
|
|
dispatcher: proxyAgent,
|
|
};
|
|
}
|
|
|
|
if (OPENAI_ORGANIZATION) {
|
|
opts.organization = OPENAI_ORGANIZATION;
|
|
}
|
|
|
|
/** @type {OpenAIClient} */
|
|
const openai = new OpenAI({
|
|
apiKey,
|
|
...opts,
|
|
});
|
|
|
|
openai.req = req;
|
|
openai.res = res;
|
|
|
|
if (endpointOption && initAppClient) {
|
|
const client = new OAIClient(apiKey, clientOptions);
|
|
return {
|
|
client,
|
|
openai,
|
|
openAIApiKey: apiKey,
|
|
};
|
|
}
|
|
|
|
return {
|
|
openai,
|
|
openAIApiKey: apiKey,
|
|
};
|
|
};
|
|
|
|
module.exports = initializeClient;
|