mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2024-11-12 17:50:11 +01:00
4bb99ab4bf
* feature: add LocalAI as llm provider * update Onboarding/mgmt settings Grab models from models endpoint for localai merge with master * update streaming for complete chunk streaming update localAI LLM to be able to stream * force schema on URL --------- Co-authored-by: timothycarambat <rambat1010@gmail.com> Co-authored-by: tlandenberger <tobiaslandenberger@gmail.com>
58 lines
1.5 KiB
JavaScript
58 lines
1.5 KiB
JavaScript
const SUPPORT_CUSTOM_MODELS = ["openai", "localai"];
|
|
|
|
async function getCustomModels(provider = "", apiKey = null, basePath = null) {
|
|
if (!SUPPORT_CUSTOM_MODELS.includes(provider))
|
|
return { models: [], error: "Invalid provider for custom models" };
|
|
|
|
switch (provider) {
|
|
case "openai":
|
|
return await openAiModels(apiKey);
|
|
case "localai":
|
|
return await localAIModels(basePath);
|
|
default:
|
|
return { models: [], error: "Invalid provider for custom models" };
|
|
}
|
|
}
|
|
|
|
async function openAiModels(apiKey = null) {
|
|
const { Configuration, OpenAIApi } = require("openai");
|
|
const config = new Configuration({
|
|
apiKey: apiKey || process.env.OPEN_AI_KEY,
|
|
});
|
|
const openai = new OpenAIApi(config);
|
|
const models = (
|
|
await openai
|
|
.listModels()
|
|
.then((res) => res.data.data)
|
|
.catch((e) => {
|
|
console.error(`OpenAI:listModels`, e.message);
|
|
return [];
|
|
})
|
|
).filter(
|
|
(model) => !model.owned_by.includes("openai") && model.owned_by !== "system"
|
|
);
|
|
|
|
return { models, error: null };
|
|
}
|
|
|
|
async function localAIModels(basePath = null) {
|
|
const { Configuration, OpenAIApi } = require("openai");
|
|
const config = new Configuration({
|
|
basePath,
|
|
});
|
|
const openai = new OpenAIApi(config);
|
|
const models = await openai
|
|
.listModels()
|
|
.then((res) => res.data.data)
|
|
.catch((e) => {
|
|
console.error(`LocalAI:listModels`, e.message);
|
|
return [];
|
|
});
|
|
|
|
return { models, error: null };
|
|
}
|
|
|
|
module.exports = {
|
|
getCustomModels,
|
|
};
|