Moonshot AI LLM & agent provider (#4178)
* add moonshot ai LLM & agent provider * fix moonshot agent calling * handle attachments/fix moonshot llm provider * update docs/example env * add moonshot to onboarding privacy * add moonshot to onboarding llm preference * update privacy for moonshot ai * update logo higher res * remove caching and use modelmap
This commit is contained in:
parent
1112ba75c9
commit
6d6bd14622
@ -101,6 +101,7 @@ AnythingLLM divides your documents into objects called `workspaces`. A Workspace
|
|||||||
- [xAI](https://x.ai/)
|
- [xAI](https://x.ai/)
|
||||||
- [Novita AI (chat models)](https://novita.ai/model-api/product/llm-api?utm_source=github_anything-llm&utm_medium=github_readme&utm_campaign=link)
|
- [Novita AI (chat models)](https://novita.ai/model-api/product/llm-api?utm_source=github_anything-llm&utm_medium=github_readme&utm_campaign=link)
|
||||||
- [PPIO](https://ppinfra.com?utm_source=github_anything-llm)
|
- [PPIO](https://ppinfra.com?utm_source=github_anything-llm)
|
||||||
|
- [Moonshot AI](https://www.moonshot.ai/)
|
||||||
|
|
||||||
**Embedder models:**
|
**Embedder models:**
|
||||||
|
|
||||||
|
|||||||
@ -133,6 +133,10 @@ GID='1000'
|
|||||||
# PPIO_API_KEY='your-ppio-api-key-here'
|
# PPIO_API_KEY='your-ppio-api-key-here'
|
||||||
# PPIO_MODEL_PREF=deepseek/deepseek-v3/community
|
# PPIO_MODEL_PREF=deepseek/deepseek-v3/community
|
||||||
|
|
||||||
|
# LLM_PROVIDER='moonshotai'
|
||||||
|
# MOONSHOT_AI_API_KEY='your-moonshot-api-key-here'
|
||||||
|
# MOONSHOT_AI_MODEL_PREF='moonshot-v1-32k'
|
||||||
|
|
||||||
###########################################
|
###########################################
|
||||||
######## Embedding API SElECTION ##########
|
######## Embedding API SElECTION ##########
|
||||||
###########################################
|
###########################################
|
||||||
|
|||||||
117
frontend/src/components/LLMSelection/MoonshotAiOptions/index.jsx
Normal file
117
frontend/src/components/LLMSelection/MoonshotAiOptions/index.jsx
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
import { useState, useEffect } from "react";
|
||||||
|
import System from "@/models/system";
|
||||||
|
|
||||||
|
export default function MoonshotAiOptions({ settings }) {
|
||||||
|
const [inputValue, setInputValue] = useState(settings?.MoonshotAiApiKey);
|
||||||
|
const [moonshotAiKey, setMoonshotAiKey] = useState(
|
||||||
|
settings?.MoonshotAiApiKey
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex gap-[36px] mt-1.5">
|
||||||
|
<div className="flex flex-col w-60">
|
||||||
|
<label className="text-white text-sm font-semibold block mb-3">
|
||||||
|
API Key
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="password"
|
||||||
|
name="MoonshotAiApiKey"
|
||||||
|
className="border-none bg-theme-settings-input-bg text-white placeholder:text-theme-settings-input-placeholder text-sm rounded-lg focus:outline-primary-button active:outline-primary-button outline-none block w-full p-2.5"
|
||||||
|
placeholder="Moonshot AI API Key"
|
||||||
|
defaultValue={settings?.MoonshotAiApiKey ? "*".repeat(20) : ""}
|
||||||
|
required={true}
|
||||||
|
autoComplete="off"
|
||||||
|
spellCheck={false}
|
||||||
|
onChange={(e) => setInputValue(e.target.value)}
|
||||||
|
onBlur={() => setMoonshotAiKey(inputValue)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{!settings?.credentialsOnly && (
|
||||||
|
<MoonshotAiModelSelection settings={settings} apiKey={moonshotAiKey} />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function MoonshotAiModelSelection({ apiKey, settings }) {
|
||||||
|
const [models, setModels] = useState([]);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
async function findCustomModels() {
|
||||||
|
setLoading(true);
|
||||||
|
const { models: availableModels } = await System.customModels(
|
||||||
|
"moonshotai",
|
||||||
|
typeof apiKey === "boolean" ? null : apiKey
|
||||||
|
);
|
||||||
|
|
||||||
|
if (availableModels?.length > 0) {
|
||||||
|
setModels(availableModels);
|
||||||
|
}
|
||||||
|
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
findCustomModels();
|
||||||
|
}, [apiKey]);
|
||||||
|
|
||||||
|
if (!apiKey) {
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col w-60">
|
||||||
|
<label className="text-white text-sm font-semibold block mb-3">
|
||||||
|
Chat Model Selection
|
||||||
|
</label>
|
||||||
|
<select
|
||||||
|
name="MoonshotAiModelPref"
|
||||||
|
disabled={true}
|
||||||
|
className="border-none bg-theme-settings-input-bg border-gray-500 text-white text-sm rounded-lg block w-full p-2.5"
|
||||||
|
>
|
||||||
|
<option disabled={true} selected={true}>
|
||||||
|
-- Enter API key --
|
||||||
|
</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loading) {
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col w-60">
|
||||||
|
<label className="text-white text-sm font-semibold block mb-3">
|
||||||
|
Chat Model Selection
|
||||||
|
</label>
|
||||||
|
<select
|
||||||
|
name="MoonshotAiModelPref"
|
||||||
|
disabled={true}
|
||||||
|
className="border-none bg-theme-settings-input-bg border-gray-500 text-white text-sm rounded-lg block w-full p-2.5"
|
||||||
|
>
|
||||||
|
<option disabled={true} selected={true}>
|
||||||
|
-- loading available models --
|
||||||
|
</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col w-60">
|
||||||
|
<label className="text-white text-sm font-semibold block mb-3">
|
||||||
|
Chat Model Selection
|
||||||
|
</label>
|
||||||
|
<select
|
||||||
|
name="MoonshotAiModelPref"
|
||||||
|
required={true}
|
||||||
|
className="border-none bg-theme-settings-input-bg border-gray-500 text-white text-sm rounded-lg block w-full p-2.5"
|
||||||
|
>
|
||||||
|
{models.map((model) => (
|
||||||
|
<option
|
||||||
|
key={model.id}
|
||||||
|
value={model.id}
|
||||||
|
selected={settings?.MoonshotAiModelPref === model.id}
|
||||||
|
>
|
||||||
|
{model.id}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
BIN
frontend/src/media/llmprovider/moonshotai.png
Normal file
BIN
frontend/src/media/llmprovider/moonshotai.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 51 KiB |
@ -32,6 +32,7 @@ import XAILogo from "@/media/llmprovider/xai.png";
|
|||||||
import NvidiaNimLogo from "@/media/llmprovider/nvidia-nim.png";
|
import NvidiaNimLogo from "@/media/llmprovider/nvidia-nim.png";
|
||||||
import PPIOLogo from "@/media/llmprovider/ppio.png";
|
import PPIOLogo from "@/media/llmprovider/ppio.png";
|
||||||
import DellProAiStudioLogo from "@/media/llmprovider/dpais.png";
|
import DellProAiStudioLogo from "@/media/llmprovider/dpais.png";
|
||||||
|
import MoonshotAiLogo from "@/media/llmprovider/moonshotai.png";
|
||||||
|
|
||||||
import PreLoader from "@/components/Preloader";
|
import PreLoader from "@/components/Preloader";
|
||||||
import OpenAiOptions from "@/components/LLMSelection/OpenAiOptions";
|
import OpenAiOptions from "@/components/LLMSelection/OpenAiOptions";
|
||||||
@ -61,6 +62,7 @@ import XAILLMOptions from "@/components/LLMSelection/XAiLLMOptions";
|
|||||||
import NvidiaNimOptions from "@/components/LLMSelection/NvidiaNimOptions";
|
import NvidiaNimOptions from "@/components/LLMSelection/NvidiaNimOptions";
|
||||||
import PPIOLLMOptions from "@/components/LLMSelection/PPIOLLMOptions";
|
import PPIOLLMOptions from "@/components/LLMSelection/PPIOLLMOptions";
|
||||||
import DellProAiStudioOptions from "@/components/LLMSelection/DPAISOptions";
|
import DellProAiStudioOptions from "@/components/LLMSelection/DPAISOptions";
|
||||||
|
import MoonshotAiOptions from "@/components/LLMSelection/MoonshotAiOptions";
|
||||||
|
|
||||||
import LLMItem from "@/components/LLMSelection/LLMItem";
|
import LLMItem from "@/components/LLMSelection/LLMItem";
|
||||||
import { CaretUpDown, MagnifyingGlass, X } from "@phosphor-icons/react";
|
import { CaretUpDown, MagnifyingGlass, X } from "@phosphor-icons/react";
|
||||||
@ -293,6 +295,14 @@ export const AVAILABLE_LLM_PROVIDERS = [
|
|||||||
description: "A unified API of AI services from leading providers",
|
description: "A unified API of AI services from leading providers",
|
||||||
requiredConfig: ["ApipieLLMApiKey", "ApipieLLMModelPref"],
|
requiredConfig: ["ApipieLLMApiKey", "ApipieLLMModelPref"],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Moonshot AI",
|
||||||
|
value: "moonshotai",
|
||||||
|
logo: MoonshotAiLogo,
|
||||||
|
options: (settings) => <MoonshotAiOptions settings={settings} />,
|
||||||
|
description: "Run Moonshot AI's powerful LLMs.",
|
||||||
|
requiredConfig: ["MoonshotAiApiKey"],
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "Generic OpenAI",
|
name: "Generic OpenAI",
|
||||||
value: "generic-openai",
|
value: "generic-openai",
|
||||||
|
|||||||
@ -38,6 +38,8 @@ import VoyageAiLogo from "@/media/embeddingprovider/voyageai.png";
|
|||||||
import PPIOLogo from "@/media/llmprovider/ppio.png";
|
import PPIOLogo from "@/media/llmprovider/ppio.png";
|
||||||
import PGVectorLogo from "@/media/vectordbs/pgvector.png";
|
import PGVectorLogo from "@/media/vectordbs/pgvector.png";
|
||||||
import DPAISLogo from "@/media/llmprovider/dpais.png";
|
import DPAISLogo from "@/media/llmprovider/dpais.png";
|
||||||
|
import MoonshotAiLogo from "@/media/llmprovider/moonshotai.png";
|
||||||
|
|
||||||
import React, { useState, useEffect } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
import paths from "@/utils/paths";
|
import paths from "@/utils/paths";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
@ -242,6 +244,14 @@ export const LLM_SELECTION_PRIVACY = {
|
|||||||
],
|
],
|
||||||
logo: DPAISLogo,
|
logo: DPAISLogo,
|
||||||
},
|
},
|
||||||
|
moonshotai: {
|
||||||
|
name: "Moonshot AI",
|
||||||
|
description: [
|
||||||
|
"Your chats may be used by Moonshot AI for training and model refinement",
|
||||||
|
"Your prompts and document text used in response creation are visible to Moonshot AI",
|
||||||
|
],
|
||||||
|
logo: MoonshotAiLogo,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export const VECTOR_DB_PRIVACY = {
|
export const VECTOR_DB_PRIVACY = {
|
||||||
|
|||||||
@ -27,6 +27,7 @@ import NvidiaNimLogo from "@/media/llmprovider/nvidia-nim.png";
|
|||||||
import CohereLogo from "@/media/llmprovider/cohere.png";
|
import CohereLogo from "@/media/llmprovider/cohere.png";
|
||||||
import PPIOLogo from "@/media/llmprovider/ppio.png";
|
import PPIOLogo from "@/media/llmprovider/ppio.png";
|
||||||
import DellProAiStudioLogo from "@/media/llmprovider/dpais.png";
|
import DellProAiStudioLogo from "@/media/llmprovider/dpais.png";
|
||||||
|
import MoonshotAiLogo from "@/media/llmprovider/moonshotai.png";
|
||||||
|
|
||||||
import OpenAiOptions from "@/components/LLMSelection/OpenAiOptions";
|
import OpenAiOptions from "@/components/LLMSelection/OpenAiOptions";
|
||||||
import GenericOpenAiOptions from "@/components/LLMSelection/GenericOpenAiOptions";
|
import GenericOpenAiOptions from "@/components/LLMSelection/GenericOpenAiOptions";
|
||||||
@ -55,6 +56,7 @@ import XAILLMOptions from "@/components/LLMSelection/XAiLLMOptions";
|
|||||||
import NvidiaNimOptions from "@/components/LLMSelection/NvidiaNimOptions";
|
import NvidiaNimOptions from "@/components/LLMSelection/NvidiaNimOptions";
|
||||||
import PPIOLLMOptions from "@/components/LLMSelection/PPIOLLMOptions";
|
import PPIOLLMOptions from "@/components/LLMSelection/PPIOLLMOptions";
|
||||||
import DellProAiStudioOptions from "@/components/LLMSelection/DPAISOptions";
|
import DellProAiStudioOptions from "@/components/LLMSelection/DPAISOptions";
|
||||||
|
import MoonshotAiOptions from "@/components/LLMSelection/MoonshotAiOptions";
|
||||||
|
|
||||||
import LLMItem from "@/components/LLMSelection/LLMItem";
|
import LLMItem from "@/components/LLMSelection/LLMItem";
|
||||||
import System from "@/models/system";
|
import System from "@/models/system";
|
||||||
@ -263,6 +265,13 @@ const LLMS = [
|
|||||||
options: (settings) => <XAILLMOptions settings={settings} />,
|
options: (settings) => <XAILLMOptions settings={settings} />,
|
||||||
description: "Run xAI's powerful LLMs like Grok-2 and more.",
|
description: "Run xAI's powerful LLMs like Grok-2 and more.",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Moonshot AI",
|
||||||
|
value: "moonshotai",
|
||||||
|
logo: MoonshotAiLogo,
|
||||||
|
options: (settings) => <MoonshotAiOptions settings={settings} />,
|
||||||
|
description: "Run Moonshot AI's powerful LLMs.",
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
export default function LLMPreference({
|
export default function LLMPreference({
|
||||||
|
|||||||
@ -31,6 +31,7 @@ const ENABLED_PROVIDERS = [
|
|||||||
"xai",
|
"xai",
|
||||||
"nvidia-nim",
|
"nvidia-nim",
|
||||||
"gemini",
|
"gemini",
|
||||||
|
"moonshotai",
|
||||||
// TODO: More agent support.
|
// TODO: More agent support.
|
||||||
// "cohere", // Has tool calling and will need to build explicit support
|
// "cohere", // Has tool calling and will need to build explicit support
|
||||||
// "huggingface" // Can be done but already has issues with no-chat templated. Needs to be tested.
|
// "huggingface" // Can be done but already has issues with no-chat templated. Needs to be tested.
|
||||||
|
|||||||
@ -131,6 +131,10 @@ SIG_SALT='salt' # Please generate random string at least 32 chars long.
|
|||||||
# PPIO_API_KEY='your-ppio-api-key-here'
|
# PPIO_API_KEY='your-ppio-api-key-here'
|
||||||
# PPIO_MODEL_PREF='deepseek/deepseek-v3/community'
|
# PPIO_MODEL_PREF='deepseek/deepseek-v3/community'
|
||||||
|
|
||||||
|
# LLM_PROVIDER='moonshotai'
|
||||||
|
# MOONSHOT_AI_API_KEY='your-moonshot-api-key-here'
|
||||||
|
# MOONSHOT_AI_MODEL_PREF='moonshot-v1-32k'
|
||||||
|
|
||||||
###########################################
|
###########################################
|
||||||
######## Embedding API SElECTION ##########
|
######## Embedding API SElECTION ##########
|
||||||
###########################################
|
###########################################
|
||||||
|
|||||||
@ -142,6 +142,9 @@ function getModelTag() {
|
|||||||
case "gemini":
|
case "gemini":
|
||||||
model = process.env.GEMINI_LLM_MODEL_PREF;
|
model = process.env.GEMINI_LLM_MODEL_PREF;
|
||||||
break;
|
break;
|
||||||
|
case "moonshotai":
|
||||||
|
model = process.env.MOONSHOT_AI_MODEL_PREF;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
model = "--";
|
model = "--";
|
||||||
break;
|
break;
|
||||||
|
|||||||
@ -544,6 +544,11 @@ const SystemSettings = {
|
|||||||
LiteLLMBasePath: process.env.LITE_LLM_BASE_PATH,
|
LiteLLMBasePath: process.env.LITE_LLM_BASE_PATH,
|
||||||
LiteLLMApiKey: !!process.env.LITE_LLM_API_KEY,
|
LiteLLMApiKey: !!process.env.LITE_LLM_API_KEY,
|
||||||
|
|
||||||
|
// Moonshot AI Keys
|
||||||
|
MoonshotAiApiKey: !!process.env.MOONSHOT_AI_API_KEY,
|
||||||
|
MoonshotAiModelPref:
|
||||||
|
process.env.MOONSHOT_AI_MODEL_PREF || "moonshot-v1-32k",
|
||||||
|
|
||||||
// Generic OpenAI Keys
|
// Generic OpenAI Keys
|
||||||
GenericOpenAiBasePath: process.env.GENERIC_OPEN_AI_BASE_PATH,
|
GenericOpenAiBasePath: process.env.GENERIC_OPEN_AI_BASE_PATH,
|
||||||
GenericOpenAiModelPref: process.env.GENERIC_OPEN_AI_MODEL_PREF,
|
GenericOpenAiModelPref: process.env.GENERIC_OPEN_AI_MODEL_PREF,
|
||||||
|
|||||||
@ -18,6 +18,7 @@ class ContextWindowFinder {
|
|||||||
groq: "groq",
|
groq: "groq",
|
||||||
xai: "xai",
|
xai: "xai",
|
||||||
deepseek: "deepseek",
|
deepseek: "deepseek",
|
||||||
|
moonshot: "moonshot",
|
||||||
};
|
};
|
||||||
static expiryMs = 1000 * 60 * 60 * 24 * 3; // 3 days
|
static expiryMs = 1000 * 60 * 60 * 24 * 3; // 3 days
|
||||||
static remoteUrl =
|
static remoteUrl =
|
||||||
|
|||||||
169
server/utils/AiProviders/moonshotAi/index.js
Normal file
169
server/utils/AiProviders/moonshotAi/index.js
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
const { NativeEmbedder } = require("../../EmbeddingEngines/native");
|
||||||
|
const {
|
||||||
|
LLMPerformanceMonitor,
|
||||||
|
} = require("../../helpers/chat/LLMPerformanceMonitor");
|
||||||
|
const {
|
||||||
|
handleDefaultStreamResponseV2,
|
||||||
|
formatChatHistory,
|
||||||
|
} = require("../../helpers/chat/responses");
|
||||||
|
const { MODEL_MAP } = require("../modelMap");
|
||||||
|
|
||||||
|
class MoonshotAiLLM {
|
||||||
|
constructor(embedder = null, modelPreference = null) {
|
||||||
|
if (!process.env.MOONSHOT_AI_API_KEY)
|
||||||
|
throw new Error("No Moonshot AI API key was set.");
|
||||||
|
const { OpenAI: OpenAIApi } = require("openai");
|
||||||
|
|
||||||
|
this.openai = new OpenAIApi({
|
||||||
|
baseURL: "https://api.moonshot.ai/v1",
|
||||||
|
apiKey: process.env.MOONSHOT_AI_API_KEY,
|
||||||
|
});
|
||||||
|
this.model =
|
||||||
|
modelPreference ||
|
||||||
|
process.env.MOONSHOT_AI_MODEL_PREF ||
|
||||||
|
"moonshot-v1-32k";
|
||||||
|
this.limits = {
|
||||||
|
history: this.promptWindowLimit() * 0.15,
|
||||||
|
system: this.promptWindowLimit() * 0.15,
|
||||||
|
user: this.promptWindowLimit() * 0.7,
|
||||||
|
};
|
||||||
|
|
||||||
|
this.embedder = embedder ?? new NativeEmbedder();
|
||||||
|
this.defaultTemp = 0.7;
|
||||||
|
this.log(
|
||||||
|
`Initialized ${this.model} with context window ${this.promptWindowLimit()}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
log(text, ...args) {
|
||||||
|
console.log(`\x1b[36m[${this.constructor.name}]\x1b[0m ${text}`, ...args);
|
||||||
|
}
|
||||||
|
|
||||||
|
#appendContext(contextTexts = []) {
|
||||||
|
if (!contextTexts || !contextTexts.length) return "";
|
||||||
|
return (
|
||||||
|
"\nContext:\n" +
|
||||||
|
contextTexts
|
||||||
|
.map((text, i) => {
|
||||||
|
return `[CONTEXT ${i}]:\n${text}\n[END CONTEXT ${i}]\n\n`;
|
||||||
|
})
|
||||||
|
.join("")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates appropriate content array for a message + attachments.
|
||||||
|
* @param {{userPrompt:string, attachments: import("../../helpers").Attachment[]}}
|
||||||
|
* @returns {string|object[]}
|
||||||
|
*/
|
||||||
|
#generateContent({ userPrompt, attachments = [] }) {
|
||||||
|
if (!attachments.length) {
|
||||||
|
return userPrompt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const content = [{ type: "text", text: userPrompt }];
|
||||||
|
for (let attachment of attachments) {
|
||||||
|
content.push({
|
||||||
|
type: "image_url",
|
||||||
|
image_url: {
|
||||||
|
url: attachment.contentString,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return content.flat();
|
||||||
|
}
|
||||||
|
|
||||||
|
streamingEnabled() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
promptWindowLimit() {
|
||||||
|
return MODEL_MAP.get("moonshot", this.model) ?? 8_192;
|
||||||
|
}
|
||||||
|
|
||||||
|
constructPrompt({
|
||||||
|
systemPrompt = "",
|
||||||
|
contextTexts = [],
|
||||||
|
chatHistory = [],
|
||||||
|
userPrompt = "",
|
||||||
|
attachments = [],
|
||||||
|
}) {
|
||||||
|
const prompt = {
|
||||||
|
role: "system",
|
||||||
|
content: `${systemPrompt}${this.#appendContext(contextTexts)}`,
|
||||||
|
};
|
||||||
|
return [
|
||||||
|
prompt,
|
||||||
|
...formatChatHistory(chatHistory, this.#generateContent),
|
||||||
|
{
|
||||||
|
role: "user",
|
||||||
|
content: this.#generateContent({ userPrompt, attachments }),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
async compressMessages(promptArgs = {}, rawHistory = []) {
|
||||||
|
const { messageArrayCompressor } = require("../../helpers/chat");
|
||||||
|
const messageArray = this.constructPrompt(promptArgs);
|
||||||
|
return await messageArrayCompressor(this, messageArray, rawHistory);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getChatCompletion(messages = null, { temperature = 0.7 }) {
|
||||||
|
const result = await LLMPerformanceMonitor.measureAsyncFunction(
|
||||||
|
this.openai.chat.completions
|
||||||
|
.create({
|
||||||
|
model: this.model,
|
||||||
|
messages,
|
||||||
|
temperature,
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
throw new Error(e.message);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
if (
|
||||||
|
!Object.prototype.hasOwnProperty.call(result.output, "choices") ||
|
||||||
|
result.output.choices.length === 0
|
||||||
|
)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return {
|
||||||
|
textResponse: result.output.choices[0].message.content,
|
||||||
|
metrics: {
|
||||||
|
prompt_tokens: result.output.usage.prompt_tokens || 0,
|
||||||
|
completion_tokens: result.output.usage.completion_tokens || 0,
|
||||||
|
total_tokens: result.output.usage.total_tokens || 0,
|
||||||
|
outputTps: result.output.usage.completion_tokens / result.duration,
|
||||||
|
duration: result.duration,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async streamGetChatCompletion(messages = null, { temperature = 0.7 }) {
|
||||||
|
const measuredStreamRequest = await LLMPerformanceMonitor.measureStream(
|
||||||
|
this.openai.chat.completions.create({
|
||||||
|
model: this.model,
|
||||||
|
stream: true,
|
||||||
|
messages,
|
||||||
|
temperature,
|
||||||
|
}),
|
||||||
|
messages
|
||||||
|
);
|
||||||
|
|
||||||
|
return measuredStreamRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
handleStream(response, stream, responseProps) {
|
||||||
|
return handleDefaultStreamResponseV2(response, stream, responseProps);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Simple wrapper for dynamic embedder & normalize interface for all LLM implementations
|
||||||
|
async embedTextInput(textInput) {
|
||||||
|
return await this.embedder.embedTextInput(textInput);
|
||||||
|
}
|
||||||
|
async embedChunks(textChunks = []) {
|
||||||
|
return await this.embedder.embedChunks(textChunks);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { MoonshotAiLLM };
|
||||||
@ -812,6 +812,8 @@ ${this.getHistory({ to: route.to })
|
|||||||
return new Providers.FireworksAIProvider({ model: config.model });
|
return new Providers.FireworksAIProvider({ model: config.model });
|
||||||
case "nvidia-nim":
|
case "nvidia-nim":
|
||||||
return new Providers.NvidiaNimProvider({ model: config.model });
|
return new Providers.NvidiaNimProvider({ model: config.model });
|
||||||
|
case "moonshotai":
|
||||||
|
return new Providers.MoonshotAiProvider({ model: config.model });
|
||||||
case "deepseek":
|
case "deepseek":
|
||||||
return new Providers.DeepSeekProvider({ model: config.model });
|
return new Providers.DeepSeekProvider({ model: config.model });
|
||||||
case "litellm":
|
case "litellm":
|
||||||
|
|||||||
@ -184,7 +184,14 @@ class Provider {
|
|||||||
apiKey: process.env.GEMINI_API_KEY ?? null,
|
apiKey: process.env.GEMINI_API_KEY ?? null,
|
||||||
...config,
|
...config,
|
||||||
});
|
});
|
||||||
|
case "moonshotai":
|
||||||
|
return new ChatOpenAI({
|
||||||
|
configuration: {
|
||||||
|
baseURL: "https://api.moonshot.ai/v1",
|
||||||
|
},
|
||||||
|
apiKey: process.env.MOONSHOT_AI_API_KEY ?? null,
|
||||||
|
...config,
|
||||||
|
});
|
||||||
// OSS Model Runners
|
// OSS Model Runners
|
||||||
// case "anythingllm_ollama":
|
// case "anythingllm_ollama":
|
||||||
// return new ChatOllama({
|
// return new ChatOllama({
|
||||||
|
|||||||
@ -23,6 +23,7 @@ const NvidiaNimProvider = require("./nvidiaNim.js");
|
|||||||
const PPIOProvider = require("./ppio.js");
|
const PPIOProvider = require("./ppio.js");
|
||||||
const GeminiProvider = require("./gemini.js");
|
const GeminiProvider = require("./gemini.js");
|
||||||
const DellProAiStudioProvider = require("./dellProAiStudio.js");
|
const DellProAiStudioProvider = require("./dellProAiStudio.js");
|
||||||
|
const MoonshotAiProvider = require("./moonshotAi.js");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
OpenAIProvider,
|
OpenAIProvider,
|
||||||
@ -50,4 +51,5 @@ module.exports = {
|
|||||||
PPIOProvider,
|
PPIOProvider,
|
||||||
GeminiProvider,
|
GeminiProvider,
|
||||||
DellProAiStudioProvider,
|
DellProAiStudioProvider,
|
||||||
|
MoonshotAiProvider,
|
||||||
};
|
};
|
||||||
|
|||||||
103
server/utils/agents/aibitat/providers/moonshotAi.js
Normal file
103
server/utils/agents/aibitat/providers/moonshotAi.js
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
const OpenAI = require("openai");
|
||||||
|
const Provider = require("./ai-provider.js");
|
||||||
|
const InheritMultiple = require("./helpers/classes.js");
|
||||||
|
const UnTooled = require("./helpers/untooled.js");
|
||||||
|
|
||||||
|
class MoonshotAiProvider extends InheritMultiple([Provider, UnTooled]) {
|
||||||
|
model;
|
||||||
|
|
||||||
|
constructor(config = {}) {
|
||||||
|
const { model = "moonshot-v1-32k" } = config;
|
||||||
|
super();
|
||||||
|
const client = new OpenAI({
|
||||||
|
baseURL: "https://api.moonshot.ai/v1",
|
||||||
|
apiKey: process.env.MOONSHOT_AI_API_KEY,
|
||||||
|
maxRetries: 3,
|
||||||
|
});
|
||||||
|
|
||||||
|
this._client = client;
|
||||||
|
this.model = model;
|
||||||
|
this.verbose = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a completion based on the received messages.
|
||||||
|
*
|
||||||
|
* @param messages A list of messages to send to the API.
|
||||||
|
* @param functions
|
||||||
|
* @returns The completion.
|
||||||
|
*/
|
||||||
|
get client() {
|
||||||
|
return this._client;
|
||||||
|
}
|
||||||
|
|
||||||
|
async #handleFunctionCallChat({ messages = [] }) {
|
||||||
|
return await this.client.chat.completions
|
||||||
|
.create({
|
||||||
|
model: this.model,
|
||||||
|
temperature: 0,
|
||||||
|
messages,
|
||||||
|
})
|
||||||
|
.then((result) => {
|
||||||
|
if (!result.hasOwnProperty("choices"))
|
||||||
|
throw new Error("Moonshot chat: No results!");
|
||||||
|
if (result.choices.length === 0)
|
||||||
|
throw new Error("Moonshot chat: No results length!");
|
||||||
|
return result.choices[0].message.content;
|
||||||
|
})
|
||||||
|
.catch((_) => {
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async complete(messages, functions = []) {
|
||||||
|
try {
|
||||||
|
let completion;
|
||||||
|
if (functions.length > 0) {
|
||||||
|
const { toolCall, text } = await this.functionCall(
|
||||||
|
messages,
|
||||||
|
functions,
|
||||||
|
this.#handleFunctionCallChat.bind(this)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (toolCall !== null) {
|
||||||
|
this.providerLog(`Valid tool call found - running ${toolCall.name}.`);
|
||||||
|
this.deduplicator.trackRun(toolCall.name, toolCall.arguments);
|
||||||
|
return {
|
||||||
|
result: null,
|
||||||
|
functionCall: {
|
||||||
|
name: toolCall.name,
|
||||||
|
arguments: toolCall.arguments,
|
||||||
|
},
|
||||||
|
cost: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
completion = { content: text };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!completion?.content) {
|
||||||
|
this.providerLog(
|
||||||
|
"Will assume chat completion without tool call inputs."
|
||||||
|
);
|
||||||
|
const response = await this.client.chat.completions.create({
|
||||||
|
model: this.model,
|
||||||
|
messages: this.cleanMsgs(messages),
|
||||||
|
});
|
||||||
|
completion = response.choices[0].message;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The UnTooled class inherited Deduplicator is mostly useful to prevent the agent
|
||||||
|
// from calling the exact same function over and over in a loop within a single chat exchange
|
||||||
|
// _but_ we should enable it to call previously used tools in a new chat interaction.
|
||||||
|
this.deduplicator.reset("runs");
|
||||||
|
return {
|
||||||
|
result: completion.content,
|
||||||
|
cost: 0,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = MoonshotAiProvider;
|
||||||
@ -199,6 +199,10 @@ class AgentHandler {
|
|||||||
"Dell Pro AI Studio model must be set to use agents."
|
"Dell Pro AI Studio model must be set to use agents."
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
case "moonshotai":
|
||||||
|
if (!process.env.MOONSHOT_AI_MODEL_PREF)
|
||||||
|
throw new Error("Moonshot AI model must be set to use agents.");
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@ -254,6 +258,8 @@ class AgentHandler {
|
|||||||
return process.env.DEEPSEEK_MODEL_PREF ?? "deepseek-chat";
|
return process.env.DEEPSEEK_MODEL_PREF ?? "deepseek-chat";
|
||||||
case "litellm":
|
case "litellm":
|
||||||
return process.env.LITE_LLM_MODEL_PREF ?? null;
|
return process.env.LITE_LLM_MODEL_PREF ?? null;
|
||||||
|
case "moonshotai":
|
||||||
|
return process.env.MOONSHOT_AI_MODEL_PREF ?? "moonshot-v1-32k";
|
||||||
case "apipie":
|
case "apipie":
|
||||||
return process.env.APIPIE_LLM_MODEL_PREF ?? null;
|
return process.env.APIPIE_LLM_MODEL_PREF ?? null;
|
||||||
case "xai":
|
case "xai":
|
||||||
|
|||||||
@ -33,6 +33,7 @@ const SUPPORT_CUSTOM_MODELS = [
|
|||||||
"gemini",
|
"gemini",
|
||||||
"ppio",
|
"ppio",
|
||||||
"dpais",
|
"dpais",
|
||||||
|
"moonshotai",
|
||||||
];
|
];
|
||||||
|
|
||||||
async function getCustomModels(provider = "", apiKey = null, basePath = null) {
|
async function getCustomModels(provider = "", apiKey = null, basePath = null) {
|
||||||
@ -84,6 +85,8 @@ async function getCustomModels(provider = "", apiKey = null, basePath = null) {
|
|||||||
return await getPPIOModels(apiKey);
|
return await getPPIOModels(apiKey);
|
||||||
case "dpais":
|
case "dpais":
|
||||||
return await getDellProAiStudioModels(basePath);
|
return await getDellProAiStudioModels(basePath);
|
||||||
|
case "moonshotai":
|
||||||
|
return await getMoonshotAiModels(apiKey);
|
||||||
default:
|
default:
|
||||||
return { models: [], error: "Invalid provider for custom models" };
|
return { models: [], error: "Invalid provider for custom models" };
|
||||||
}
|
}
|
||||||
@ -675,6 +678,31 @@ async function getDellProAiStudioModels(basePath = null) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getMoonshotAiModels(_apiKey = null) {
|
||||||
|
const apiKey =
|
||||||
|
_apiKey === true
|
||||||
|
? process.env.MOONSHOT_AI_API_KEY
|
||||||
|
: _apiKey || process.env.MOONSHOT_AI_API_KEY || null;
|
||||||
|
|
||||||
|
const { OpenAI: OpenAIApi } = require("openai");
|
||||||
|
const openai = new OpenAIApi({
|
||||||
|
baseURL: "https://api.moonshot.ai/v1",
|
||||||
|
apiKey,
|
||||||
|
});
|
||||||
|
const models = await openai.models
|
||||||
|
.list()
|
||||||
|
.then((results) => results.data)
|
||||||
|
.catch((e) => {
|
||||||
|
console.error(`MoonshotAi:listModels`, e.message);
|
||||||
|
return [];
|
||||||
|
});
|
||||||
|
|
||||||
|
// Api Key was successful so lets save it for future uses
|
||||||
|
if (models.length > 0) process.env.MOONSHOT_AI_API_KEY = apiKey;
|
||||||
|
return { models, error: null };
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getCustomModels,
|
getCustomModels,
|
||||||
|
SUPPORT_CUSTOM_MODELS,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -203,6 +203,9 @@ function getLLMProvider({ provider = null, model = null } = {}) {
|
|||||||
case "ppio":
|
case "ppio":
|
||||||
const { PPIOLLM } = require("../AiProviders/ppio");
|
const { PPIOLLM } = require("../AiProviders/ppio");
|
||||||
return new PPIOLLM(embedder, model);
|
return new PPIOLLM(embedder, model);
|
||||||
|
case "moonshotai":
|
||||||
|
const { MoonshotAiLLM } = require("../AiProviders/moonshotAi");
|
||||||
|
return new MoonshotAiLLM(embedder, model);
|
||||||
case "dpais":
|
case "dpais":
|
||||||
const { DellProAiStudioLLM } = require("../AiProviders/dellProAiStudio");
|
const { DellProAiStudioLLM } = require("../AiProviders/dellProAiStudio");
|
||||||
return new DellProAiStudioLLM(embedder, model);
|
return new DellProAiStudioLLM(embedder, model);
|
||||||
@ -353,6 +356,9 @@ function getLLMProviderClass({ provider = null } = {}) {
|
|||||||
case "dpais":
|
case "dpais":
|
||||||
const { DellProAiStudioLLM } = require("../AiProviders/dellProAiStudio");
|
const { DellProAiStudioLLM } = require("../AiProviders/dellProAiStudio");
|
||||||
return DellProAiStudioLLM;
|
return DellProAiStudioLLM;
|
||||||
|
case "moonshotai":
|
||||||
|
const { MoonshotAiLLM } = require("../AiProviders/moonshotAi");
|
||||||
|
return MoonshotAiLLM;
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -419,6 +425,8 @@ function getBaseLLMProviderModel({ provider = null } = {}) {
|
|||||||
return process.env.PPIO_API_KEY;
|
return process.env.PPIO_API_KEY;
|
||||||
case "dpais":
|
case "dpais":
|
||||||
return process.env.DPAIS_LLM_MODEL_PREF;
|
return process.env.DPAIS_LLM_MODEL_PREF;
|
||||||
|
case "moonshotai":
|
||||||
|
return process.env.MOONSHOT_AI_MODEL_PREF;
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -676,6 +676,16 @@ const KEY_MAPPING = {
|
|||||||
envKey: "PPIO_MODEL_PREF",
|
envKey: "PPIO_MODEL_PREF",
|
||||||
checks: [isNotEmpty],
|
checks: [isNotEmpty],
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Moonshot AI Options
|
||||||
|
MoonshotAiApiKey: {
|
||||||
|
envKey: "MOONSHOT_AI_API_KEY",
|
||||||
|
checks: [isNotEmpty],
|
||||||
|
},
|
||||||
|
MoonshotAiModelPref: {
|
||||||
|
envKey: "MOONSHOT_AI_MODEL_PREF",
|
||||||
|
checks: [isNotEmpty],
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
function isNotEmpty(input = "") {
|
function isNotEmpty(input = "") {
|
||||||
@ -784,6 +794,7 @@ function supportedLLM(input = "") {
|
|||||||
"nvidia-nim",
|
"nvidia-nim",
|
||||||
"ppio",
|
"ppio",
|
||||||
"dpais",
|
"dpais",
|
||||||
|
"moonshotai",
|
||||||
].includes(input);
|
].includes(input);
|
||||||
return validSelection ? null : `${input} is not a valid LLM provider.`;
|
return validSelection ? null : `${input} is not a valid LLM provider.`;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user