Download api/server/services/Endpoints/openAI/initializeClient.js from Zhitomir/chat: direct link, hf CLI and curl.
- Browser
- Download file 3.49 kB
-
https://huggingface.co/spaces/Zhitomir/chat/resolve/main/api/server/services/Endpoints/openAI/initializeClient.js
- Command line
-
hf download hf://spaces/Zhitomir/chat/api/server/services/Endpoints/openAI/initializeClient.js
-
curl -L -o initializeClient.js https://huggingface.co/spaces/Zhitomir/chat/resolve/main/api/server/services/Endpoints/openAI/initializeClient.js
3.49 kB
| const { | |
| ErrorTypes, | |
| EModelEndpoint, | |
| resolveHeaders, | |
| mapModelToAzureConfig, | |
| } = require('librechat-data-provider'); | |
| const { getUserKeyValues, checkUserKeyExpiry } = require('~/server/services/UserService'); | |
| const { isEnabled, isUserProvided } = require('~/server/utils'); | |
| const { getAzureCredentials } = require('~/utils'); | |
| const { OpenAIClient } = require('~/app'); | |
| const initializeClient = async ({ req, res, endpointOption }) => { | |
| const { | |
| PROXY, | |
| OPENAI_API_KEY, | |
| AZURE_API_KEY, | |
| OPENAI_REVERSE_PROXY, | |
| AZURE_OPENAI_BASEURL, | |
| OPENAI_SUMMARIZE, | |
| DEBUG_OPENAI, | |
| } = process.env; | |
| const { key: expiresAt, endpoint, model: modelName } = req.body; | |
| const contextStrategy = isEnabled(OPENAI_SUMMARIZE) ? 'summarize' : null; | |
| const credentials = { | |
| [EModelEndpoint.openAI]: OPENAI_API_KEY, | |
| [EModelEndpoint.azureOpenAI]: AZURE_API_KEY, | |
| }; | |
| const baseURLOptions = { | |
| [EModelEndpoint.openAI]: OPENAI_REVERSE_PROXY, | |
| [EModelEndpoint.azureOpenAI]: AZURE_OPENAI_BASEURL, | |
| }; | |
| const userProvidesKey = isUserProvided(credentials[endpoint]); | |
| const userProvidesURL = isUserProvided(baseURLOptions[endpoint]); | |
| let userValues = null; | |
| if (expiresAt && (userProvidesKey || userProvidesURL)) { | |
| checkUserKeyExpiry(expiresAt, endpoint); | |
| userValues = await getUserKeyValues({ userId: req.user.id, name: endpoint }); | |
| } | |
| let apiKey = userProvidesKey ? userValues?.apiKey : credentials[endpoint]; | |
| let baseURL = userProvidesURL ? userValues?.baseURL : baseURLOptions[endpoint]; | |
| const clientOptions = { | |
| debug: isEnabled(DEBUG_OPENAI), | |
| contextStrategy, | |
| reverseProxyUrl: baseURL ? baseURL : null, | |
| proxy: PROXY ?? null, | |
| req, | |
| res, | |
| ...endpointOption, | |
| }; | |
| const isAzureOpenAI = endpoint === EModelEndpoint.azureOpenAI; | |
| /** @type {false | TAzureConfig} */ | |
| const azureConfig = isAzureOpenAI && req.app.locals[EModelEndpoint.azureOpenAI]; | |
| if (isAzureOpenAI && azureConfig) { | |
| const { modelGroupMap, groupMap } = azureConfig; | |
| const { | |
| azureOptions, | |
| baseURL, | |
| headers = {}, | |
| serverless, | |
| } = mapModelToAzureConfig({ | |
| modelName, | |
| modelGroupMap, | |
| groupMap, | |
| }); | |
| clientOptions.reverseProxyUrl = baseURL ?? clientOptions.reverseProxyUrl; | |
| clientOptions.headers = resolveHeaders({ ...headers, ...(clientOptions.headers ?? {}) }); | |
| clientOptions.titleConvo = azureConfig.titleConvo; | |
| clientOptions.titleModel = azureConfig.titleModel; | |
| clientOptions.titleMethod = azureConfig.titleMethod ?? 'completion'; | |
| const groupName = modelGroupMap[modelName].group; | |
| clientOptions.addParams = azureConfig.groupMap[groupName].addParams; | |
| clientOptions.dropParams = azureConfig.groupMap[groupName].dropParams; | |
| clientOptions.forcePrompt = azureConfig.groupMap[groupName].forcePrompt; | |
| apiKey = azureOptions.azureOpenAIApiKey; | |
| clientOptions.azure = !serverless && azureOptions; | |
| } else if (isAzureOpenAI) { | |
| clientOptions.azure = userProvidesKey ? JSON.parse(userValues.apiKey) : getAzureCredentials(); | |
| apiKey = clientOptions.azure.azureOpenAIApiKey; | |
| } | |
| if (userProvidesKey & !apiKey) { | |
| throw new Error( | |
| JSON.stringify({ | |
| type: ErrorTypes.NO_USER_KEY, | |
| }), | |
| ); | |
| } | |
| if (!apiKey) { | |
| throw new Error(`${endpoint} API Key not provided.`); | |
| } | |
| const client = new OpenAIClient(apiKey, clientOptions); | |
| return { | |
| client, | |
| openAIApiKey: apiKey, | |
| }; | |
| }; | |
| module.exports = initializeClient; | |