mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-16 16:30:15 +01:00
* refactor(extractBaseURL): add handling for all possible Cloudflare AI Gateway endpoints * chore: added endpointoption todo for updating type and optimizing handling app-wide * feat(azureUtils): - `genAzureChatCompletion`: allow optional client pass to update azure property - `constructAzureURL`: optionally replace placeholders for instance and deployment names of an azure baseURL - add tests for module * refactor(extractBaseURL): return entire input when cloudflare `azure-openai` suffix detected - also add more tests for both construct and extract URL * refactor(genAzureChatCompletion): only allow omitting instance name if baseURL is not set * refactor(initializeClient): determine `reverseProxyUrl` based on endpoint (azure or openai) * refactor: utitlize `constructAzureURL` when `AZURE_OPENAI_BASEURL` is set * docs: update docs on `AZURE_OPENAI_BASEURL` * fix(ci): update expected error message for `azureUtils` tests
79 lines
2.6 KiB
JavaScript
79 lines
2.6 KiB
JavaScript
const { ChatOpenAI } = require('langchain/chat_models/openai');
|
|
const { sanitizeModelName, constructAzureURL } = require('~/utils');
|
|
const { isEnabled } = require('~/server/utils');
|
|
|
|
/**
|
|
* Creates a new instance of a language model (LLM) for chat interactions.
|
|
*
|
|
* @param {Object} options - The options for creating the LLM.
|
|
* @param {ModelOptions} options.modelOptions - The options specific to the model, including modelName, temperature, presence_penalty, frequency_penalty, and other model-related settings.
|
|
* @param {ConfigOptions} options.configOptions - Configuration options for the API requests, including proxy settings and custom headers.
|
|
* @param {Callbacks} options.callbacks - Callback functions for managing the lifecycle of the LLM, including token buffers, context, and initial message count.
|
|
* @param {boolean} [options.streaming=false] - Determines if the LLM should operate in streaming mode.
|
|
* @param {string} options.openAIApiKey - The API key for OpenAI, used for authentication.
|
|
* @param {AzureOptions} [options.azure={}] - Optional Azure-specific configurations. If provided, Azure configurations take precedence over OpenAI configurations.
|
|
*
|
|
* @returns {ChatOpenAI} An instance of the ChatOpenAI class, configured with the provided options.
|
|
*
|
|
* @example
|
|
* const llm = createLLM({
|
|
* modelOptions: { modelName: 'gpt-3.5-turbo', temperature: 0.2 },
|
|
* configOptions: { basePath: 'https://example.api/path' },
|
|
* callbacks: { onMessage: handleMessage },
|
|
* openAIApiKey: 'your-api-key'
|
|
* });
|
|
*/
|
|
function createLLM({
|
|
modelOptions,
|
|
configOptions,
|
|
callbacks,
|
|
streaming = false,
|
|
openAIApiKey,
|
|
azure = {},
|
|
}) {
|
|
let credentials = { openAIApiKey };
|
|
let configuration = {
|
|
apiKey: openAIApiKey,
|
|
};
|
|
|
|
/** @type {AzureOptions} */
|
|
let azureOptions = {};
|
|
if (azure) {
|
|
const useModelName = isEnabled(process.env.AZURE_USE_MODEL_AS_DEPLOYMENT_NAME);
|
|
|
|
credentials = {};
|
|
configuration = {};
|
|
azureOptions = azure;
|
|
|
|
azureOptions.azureOpenAIApiDeploymentName = useModelName
|
|
? sanitizeModelName(modelOptions.modelName)
|
|
: azureOptions.azureOpenAIApiDeploymentName;
|
|
}
|
|
|
|
if (azure && process.env.AZURE_OPENAI_DEFAULT_MODEL) {
|
|
modelOptions.modelName = process.env.AZURE_OPENAI_DEFAULT_MODEL;
|
|
}
|
|
|
|
if (azure && configOptions.basePath) {
|
|
configOptions.basePath = constructAzureURL({
|
|
baseURL: configOptions.basePath,
|
|
azure: azureOptions,
|
|
});
|
|
}
|
|
|
|
return new ChatOpenAI(
|
|
{
|
|
streaming,
|
|
verbose: true,
|
|
credentials,
|
|
configuration,
|
|
...azureOptions,
|
|
...modelOptions,
|
|
...credentials,
|
|
callbacks,
|
|
},
|
|
configOptions,
|
|
);
|
|
}
|
|
|
|
module.exports = createLLM;
|