Merge pull request #2976 from cblh/feature/20241118-moonshot-provider

moonshot provider
This commit is contained in:
Nate Sesti 2024-11-18 12:09:40 -08:00 • committed by GitHub
commit dbcb4069d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 290 additions and 9 deletions

9
core/index.d.ts vendored
View file

@ -659,7 +659,8 @@ type ModelProvider =
| "askSage"
| "vertexai"
| "nebius"
| "xAI";
| "xAI"
| "moonshot";
export type ModelName =
| "AUTODETECT"
@ -754,7 +755,11 @@ export type ModelName =
| "starcoder-1b"
| "starcoder-3b"
| "starcoder2-3b"
| "stable-code-3b";
| "stable-code-3b"
// Moonshot
| "moonshot-v1-8k"
| "moonshot-v1-32k"
| "moonshot-v1-128k";
export interface RequestOptions {
timeout?: number;

View file

@ -117,6 +117,7 @@ const PARALLEL_PROVIDERS: ModelProvider[] = [
"huggingface-inference-api",
"huggingface-tgi",
"mistral",
"moonshot",
"free-trial",
"replicate",
"together",
@ -149,9 +150,10 @@ function autodetectTemplateType(model: string): TemplateType | undefined {
lower.includes("chat-bison") ||
lower.includes("pplx") ||
lower.includes("gemini") ||
lower.includes("grok")
lower.includes("grok")||
lower.includes("moonshot")
) {
return undefined;
return undefined;
}
if (lower.includes("llama3")) {

59
core/llm/llms/Moonshot.ts Normal file
View file

@ -0,0 +1,59 @@
import { CompletionOptions, LLMOptions, ModelProvider } from "../../index.js";
import { streamSse } from "../stream.js";
import { osModelsEditPrompt } from "../templates/edit.js";
import OpenAI from "./OpenAI.js";
class Moonshot extends OpenAI {
static providerName: ModelProvider = "moonshot";
static defaultOptions: Partial<LLMOptions> = {
apiBase: "https://api.moonshot.cn/v1/",
model: "moonshot-v1-8k",
promptTemplates: {
edit: osModelsEditPrompt,
},
useLegacyCompletionsEndpoint: false,
};
maxStopWords: number | undefined = 16;
supportsFim(): boolean {
return true;
}
async *_streamFim(
prefix: string,
suffix: string,
options: CompletionOptions,
): AsyncGenerator<string> {
const endpoint = new URL("v1/chat/completions", this.apiBase);
const resp = await this.fetch(endpoint, {
method: "POST",
body: JSON.stringify({
model: options.model,
messages: [
{
role: "user",
content: prefix + "[fill]" + suffix,
},
],
max_tokens: options.maxTokens,
temperature: options.temperature,
top_p: options.topP,
frequency_penalty: options.frequencyPenalty,
presence_penalty: options.presencePenalty,
stop: options.stop,
stream: true,
}),
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${this.apiKey}`,
},
});
for await (const chunk of streamSse(resp)) {
yield chunk.choices[0].delta.content;
}
}
}
export default Moonshot;

View file

@ -31,6 +31,7 @@ import Llamafile from "./Llamafile";
import LMStudio from "./LMStudio";
import Mistral from "./Mistral";
import Mock from "./Mock";
import Moonshot from "./Moonshot";
import Msty from "./Msty";
import Nebius from "./Nebius";
import Nvidia from "./Nvidia";
@ -54,6 +55,7 @@ const LLMs = [
FreeTrial,
Gemini,
Llamafile,
Moonshot,
Ollama,
Replicate,
TextGenWebUI,

View file

@ -0,0 +1,60 @@
# Moonshot
[Moonshot AI](https://platform.moonshot.cn/) provides high-quality large language model services with competitive pricing and excellent performance.
## Configuration
To use Moonshot AI models, you need to:
1. Get an API key from [Moonshot AI Platform](https://platform.moonshot.cn/)
2. Add the following configuration to your `config.json`:
```json
{
"models": [
{
"title": "Moonshot",
"provider": "moonshot",
"model": "moonshot-v1-8k",
"apiKey": "YOUR_API_KEY"
}
]
}
```
## Available Models
Moonshot AI currently provides the following models:
- `moonshot-v1-8k`: Base model with 8K context window
- `moonshot-v1-32k`: Base model with 32K context window
- `moonshot-v1-128k`: Base model with 128K context window
## Configuration Options
| Option | Description | Default |
|--------|-------------|---------|
| `apiKey` | Moonshot API key | Required |
| `apiBase` | API base URL | `https://api.moonshot.cn/v1/` |
| `model` | Model name to use | `moonshot-v1-8k` |
## Example
Here's a complete configuration example:
```json
{
"models": [
{
"title": "Moonshot-8K",
"provider": "moonshot",
"model": "moonshot-v1-8k",
"apiKey": "YOUR_API_KEY",
"completionOptions": {
"temperature": 0.7,
"topP": 0.95,
"maxTokens": 2048
}
}
]
}

View file

@ -205,7 +205,8 @@
"nebius",
"vertexai",
"xAI",
"kindo"
"kindo",
"moonshot"
],
"markdownEnumDescriptions": [
"### OpenAI\nUse gpt-4, gpt-3.5-turbo, or any other OpenAI model. See [here](https://openai.com/product#made-for-developers) to obtain an API key.\n\n> [Reference](https://docs.continue.dev/reference/Model%20Providers/openai)",
@ -242,7 +243,8 @@
"### Cerebras\nCerebras Inference uses specialized silicon to provides superfast inference. To get started, get your API keys from [their console](https://cloud.cerebras.ai/).",
"### Ask Sage\nAsk Sage is an agnostic hosted service that provides language models. To get started with Ask Sage:\n1. Obtain an API key from your account. For more information, visit [Ask Sage](https://docs.asksage.ai/).\n2. Paste the API key below.\n3. Select a model preset.\n> [Reference](https://docs.asksage.ai/)",
"### xAI offers a world class developer tool set to build scalable applications powered by Grok. To get started, obtain an API key from [the x Console](https://console.x.ai/), and see the [docs](https://docs.x.ai/docs/)",
"### Secure AI management software that helps enterprises adopt and manage AI across their workforce. To get started, obtain an API key from [the Kindo console](https://app.kindo.ai/settings/api), and see the [website](https://app.kindo.ai//)"
"### Secure AI management software that helps enterprises adopt and manage AI across their workforce. To get started, obtain an API key from [the Kindo console](https://app.kindo.ai/settings/api), and see the [website](https://app.kindo.ai//)",
"### Moonshot\nTo get started with Moonshot AI, obtain your API key from [Moonshot AI](https://platform.moonshot.cn/). Moonshot AI provides high-quality large language models with competitive pricing.\n> [Reference](https://platform.moonshot.cn/docs/api)"
],
"type": "string"
},
@ -769,9 +771,6 @@
"starcoder-1b",
"starcoder-3b",
"starcoder2-3b",
"mistral-tiny",
"mistral-small",
"mistral-medium",
"qwen2.5-coder:1.5b",
"qwen2.5-coder:3b",
"qwen2.5-coder:7b",
@ -1147,6 +1146,35 @@
"required": ["projectId", "region"]
}
},
{
"if": {
"properties": {
"provider": {
"enum": ["moonshot"]
}
},
"required": ["provider"]
},
"then": {
"properties": {
"model": {
"anyOf": [
{
"enum": [
"moonshot-v1-8k",
"moonshot-v1-32k",
"moonshot-v1-128k",
"AUTODETECT"
]
},
{
"type": "string"
}
]
}
}
}
},
{
"if": {
"properties": {

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

View file

@ -182,6 +182,42 @@ export const models: { [key: string]: ModelPackage } = {
providerOptions: ["nebius"],
isOpenSource: true,
},
moonshotChat: {
title: "Moonshot Chat",
description: "Moonshot AI provides high-performance large language models",
refUrl: "https://platform.moonshot.cn/",
params: {
title: "Moonshot-v1-8k",
model: "moonshot-v1-8k",
contextLength: 8192,
},
icon: "moonshot.png",
dimensions: [
{
name: "Context Window",
description: "The size of the model's context window",
options: {
"8K": {
model: "moonshot-v1-8k",
title: "Moonshot-v1-8k",
contextLength: 8192,
},
"32K": {
model: "moonshot-v1-32k",
title: "Moonshot-v1-32k",
contextLength: 32768,
},
"128K": {
model: "moonshot-v1-128k",
title: "Moonshot-v1-128k",
contextLength: 131072,
},
},
},
],
providerOptions: ["moonshot"],
isOpenSource: false,
},
mistralOs: {
title: "Mistral",
description:

View file

@ -116,6 +116,29 @@ export const providers: Partial<Record<ModelProvider, ProviderInfo>> = {
],
apiKeyUrl: "https://console.anthropic.com/account/keys",
},
moonshot: {
title: "Moonshot",
provider: "moonshot",
description: "Use the Moonshot API for LLMs",
longDescription: `[Visit our documentation](https://docs.continue.dev/reference/Model%20Providers/moonshot) for information on obtaining an API key.`,
icon: "moonshot.png",
tags: [ModelProviderTags.RequiresApiKey],
refPage: "moonshot",
apiKeyUrl: "https://docs.moonshot.cn/docs/getting-started",
packages: [
models.moonshotChat,
],
collectInputFor: [
{
inputType: "text",
key: "apiKey",
label: "API Key",
placeholder: "Enter your Moonshot API key",
required: true,
},
...completionParamsInputsConfigs,
],
},
azure: {
title: "Azure OpenAI",
provider: "azure",

View file

@ -0,0 +1,63 @@
import { streamSse } from "@continuedev/fetch";
import fetch from "node-fetch";
import { ChatCompletionChunk } from "openai/resources/index.mjs";
import { LlmApiConfig } from "../index.js";
import { OpenAIApi } from "./OpenAI.js";
import { FimCreateParamsStreaming } from "./base.js";
export class MoonshotApi extends OpenAIApi {
constructor(config: LlmApiConfig) {
super(config);
this.apiBase = "https://api.moonshot.cn/";
}
async *fimStream(
body: FimCreateParamsStreaming,
): AsyncGenerator<ChatCompletionChunk, any, unknown> {
const endpoint = new URL("v1/chat/completions", this.apiBase);
const resp = await fetch(endpoint, {
method: "POST",
body: JSON.stringify({
model: body.model,
messages: [
{
role: "user",
content: body.prompt + "[fill]" + body.suffix,
},
],
max_tokens: body.max_tokens,
temperature: body.temperature,
top_p: body.top_p,
frequency_penalty: body.frequency_penalty,
presence_penalty: body.presence_penalty,
stop: body.stop,
stream: true,
}),
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${this.config.apiKey}`,
},
});
for await (const chunk of streamSse(resp as any)) {
yield {
choices: [
{
delta: {
content: chunk.choices[0].delta.content,
role: "assistant",
},
finish_reason: chunk.finish_reason,
index: 0,
logprobs: null,
},
],
created: Date.now(),
id: "",
model: body.model,
object: "chat.completion.chunk",
};
}
}
}

View file

@ -6,6 +6,7 @@ import { CohereApi } from "./apis/Cohere.js";
import { DeepSeekApi } from "./apis/DeepSeek.js";
import { GeminiApi } from "./apis/Gemini.js";
import { JinaApi } from "./apis/Jina.js";
import { MoonshotApi } from "./apis/Moonshot.js";
import { OpenAIApi } from "./apis/OpenAI.js";
dotenv.config();
@ -48,6 +49,8 @@ export function constructLlmApi(config: LlmApiConfig): BaseLlmApi {
});
case "deepseek":
return new DeepSeekApi(config);
case "moonshot":
return new MoonshotApi(config);
case "groq":
return new OpenAIApi({
...config,