feat: add moonshot kimi k2.5 support
This commit is contained in:
@@ -15,6 +15,7 @@ LLM_PROXY__ENCRYPTION_KEY=your_secure_32_byte_key_here
|
|||||||
OPENAI_API_KEY=sk-...
|
OPENAI_API_KEY=sk-...
|
||||||
GEMINI_API_KEY=AIza...
|
GEMINI_API_KEY=AIza...
|
||||||
DEEPSEEK_API_KEY=sk-...
|
DEEPSEEK_API_KEY=sk-...
|
||||||
|
MOONSHOT_API_KEY=sk-...
|
||||||
GROK_API_KEY=xai-...
|
GROK_API_KEY=xai-...
|
||||||
|
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
@@ -38,6 +39,9 @@ LLM_PROXY__DATABASE__MAX_CONNECTIONS=10
|
|||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
# LLM_PROXY__PROVIDERS__OPENAI__BASE_URL=https://api.openai.com/v1
|
# LLM_PROXY__PROVIDERS__OPENAI__BASE_URL=https://api.openai.com/v1
|
||||||
# LLM_PROXY__PROVIDERS__GEMINI__ENABLED=true
|
# LLM_PROXY__PROVIDERS__GEMINI__ENABLED=true
|
||||||
|
# LLM_PROXY__PROVIDERS__MOONSHOT__BASE_URL=https://api.moonshot.ai/v1
|
||||||
|
# LLM_PROXY__PROVIDERS__MOONSHOT__ENABLED=true
|
||||||
|
# LLM_PROXY__PROVIDERS__MOONSHOT__DEFAULT_MODEL=kimi-k2.5
|
||||||
# LLM_PROXY__PROVIDERS__OLLAMA__BASE_URL=http://localhost:11434/v1
|
# LLM_PROXY__PROVIDERS__OLLAMA__BASE_URL=http://localhost:11434/v1
|
||||||
# LLM_PROXY__PROVIDERS__OLLAMA__ENABLED=true
|
# LLM_PROXY__PROVIDERS__OLLAMA__ENABLED=true
|
||||||
# LLM_PROXY__PROVIDERS__OLLAMA__MODELS=llama3,mistral,llava
|
# LLM_PROXY__PROVIDERS__OLLAMA__MODELS=llama3,mistral,llava
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# GopherGate
|
# GopherGate
|
||||||
|
|
||||||
A unified, high-performance LLM proxy gateway built in Go. It provides a single OpenAI-compatible API to access multiple providers (OpenAI, Gemini, DeepSeek, Grok, Ollama) with built-in token tracking, real-time cost calculation, multi-user authentication, and a management dashboard.
|
A unified, high-performance LLM proxy gateway built in Go. It provides a single OpenAI-compatible API to access multiple providers (OpenAI, Gemini, DeepSeek, Moonshot, Grok, Ollama) with built-in token tracking, real-time cost calculation, multi-user authentication, and a management dashboard.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
@@ -9,6 +9,7 @@ A unified, high-performance LLM proxy gateway built in Go. It provides a single
|
|||||||
- **OpenAI:** GPT-4o, GPT-4o Mini, o1, o3 reasoning models.
|
- **OpenAI:** GPT-4o, GPT-4o Mini, o1, o3 reasoning models.
|
||||||
- **Google Gemini:** Gemini 2.0 Flash, Pro, and vision models (with native CoT support).
|
- **Google Gemini:** Gemini 2.0 Flash, Pro, and vision models (with native CoT support).
|
||||||
- **DeepSeek:** DeepSeek Chat and Reasoner (R1) models.
|
- **DeepSeek:** DeepSeek Chat and Reasoner (R1) models.
|
||||||
|
- **Moonshot:** Kimi K2.5 and other Kimi models.
|
||||||
- **xAI Grok:** Grok-4 models.
|
- **xAI Grok:** Grok-4 models.
|
||||||
- **Ollama:** Local LLMs running on your network.
|
- **Ollama:** Local LLMs running on your network.
|
||||||
- **Observability & Tracking:**
|
- **Observability & Tracking:**
|
||||||
@@ -66,6 +67,7 @@ GopherGate is designed with security in mind:
|
|||||||
# LLM_PROXY__ENCRYPTION_KEY=... (32-byte hex or base64 string)
|
# LLM_PROXY__ENCRYPTION_KEY=... (32-byte hex or base64 string)
|
||||||
# OPENAI_API_KEY=sk-...
|
# OPENAI_API_KEY=sk-...
|
||||||
# GEMINI_API_KEY=AIza...
|
# GEMINI_API_KEY=AIza...
|
||||||
|
# MOONSHOT_API_KEY=...
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Run the proxy:
|
3. Run the proxy:
|
||||||
@@ -114,6 +116,10 @@ You can reset the admin password to default by running:
|
|||||||
|
|
||||||
The proxy is a drop-in replacement for OpenAI. Configure your client:
|
The proxy is a drop-in replacement for OpenAI. Configure your client:
|
||||||
|
|
||||||
|
Moonshot models are available through the same OpenAI-compatible endpoint. For
|
||||||
|
example, use `kimi-k2.5` as the model name after setting `MOONSHOT_API_KEY` in
|
||||||
|
your environment.
|
||||||
|
|
||||||
### Python
|
### Python
|
||||||
```python
|
```python
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ type ProviderConfig struct {
|
|||||||
OpenAI OpenAIConfig `mapstructure:"openai"`
|
OpenAI OpenAIConfig `mapstructure:"openai"`
|
||||||
Gemini GeminiConfig `mapstructure:"gemini"`
|
Gemini GeminiConfig `mapstructure:"gemini"`
|
||||||
DeepSeek DeepSeekConfig `mapstructure:"deepseek"`
|
DeepSeek DeepSeekConfig `mapstructure:"deepseek"`
|
||||||
|
Moonshot MoonshotConfig `mapstructure:"moonshot"`
|
||||||
Grok GrokConfig `mapstructure:"grok"`
|
Grok GrokConfig `mapstructure:"grok"`
|
||||||
Ollama OllamaConfig `mapstructure:"ollama"`
|
Ollama OllamaConfig `mapstructure:"ollama"`
|
||||||
}
|
}
|
||||||
@@ -58,6 +59,13 @@ type DeepSeekConfig struct {
|
|||||||
Enabled bool `mapstructure:"enabled"`
|
Enabled bool `mapstructure:"enabled"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MoonshotConfig struct {
|
||||||
|
APIKeyEnv string `mapstructure:"api_key_env"`
|
||||||
|
BaseURL string `mapstructure:"base_url"`
|
||||||
|
DefaultModel string `mapstructure:"default_model"`
|
||||||
|
Enabled bool `mapstructure:"enabled"`
|
||||||
|
}
|
||||||
|
|
||||||
type GrokConfig struct {
|
type GrokConfig struct {
|
||||||
APIKeyEnv string `mapstructure:"api_key_env"`
|
APIKeyEnv string `mapstructure:"api_key_env"`
|
||||||
BaseURL string `mapstructure:"base_url"`
|
BaseURL string `mapstructure:"base_url"`
|
||||||
@@ -97,6 +105,11 @@ func Load() (*Config, error) {
|
|||||||
v.SetDefault("providers.deepseek.default_model", "deepseek-reasoner")
|
v.SetDefault("providers.deepseek.default_model", "deepseek-reasoner")
|
||||||
v.SetDefault("providers.deepseek.enabled", true)
|
v.SetDefault("providers.deepseek.enabled", true)
|
||||||
|
|
||||||
|
v.SetDefault("providers.moonshot.api_key_env", "MOONSHOT_API_KEY")
|
||||||
|
v.SetDefault("providers.moonshot.base_url", "https://api.moonshot.ai/v1")
|
||||||
|
v.SetDefault("providers.moonshot.default_model", "kimi-k2.5")
|
||||||
|
v.SetDefault("providers.moonshot.enabled", true)
|
||||||
|
|
||||||
v.SetDefault("providers.grok.api_key_env", "GROK_API_KEY")
|
v.SetDefault("providers.grok.api_key_env", "GROK_API_KEY")
|
||||||
v.SetDefault("providers.grok.base_url", "https://api.x.ai/v1")
|
v.SetDefault("providers.grok.base_url", "https://api.x.ai/v1")
|
||||||
v.SetDefault("providers.grok.default_model", "grok-4-1-fast-non-reasoning")
|
v.SetDefault("providers.grok.default_model", "grok-4-1-fast-non-reasoning")
|
||||||
@@ -178,6 +191,8 @@ func (c *Config) GetAPIKey(provider string) (string, error) {
|
|||||||
envVar = c.Providers.Gemini.APIKeyEnv
|
envVar = c.Providers.Gemini.APIKeyEnv
|
||||||
case "deepseek":
|
case "deepseek":
|
||||||
envVar = c.Providers.DeepSeek.APIKeyEnv
|
envVar = c.Providers.DeepSeek.APIKeyEnv
|
||||||
|
case "moonshot":
|
||||||
|
envVar = c.Providers.Moonshot.APIKeyEnv
|
||||||
case "grok":
|
case "grok":
|
||||||
envVar = c.Providers.Grok.APIKeyEnv
|
envVar = c.Providers.Grok.APIKeyEnv
|
||||||
default:
|
default:
|
||||||
|
|||||||
106
internal/providers/moonshot.go
Normal file
106
internal/providers/moonshot.go
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
package providers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gophergate/internal/config"
|
||||||
|
"gophergate/internal/models"
|
||||||
|
"github.com/go-resty/resty/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MoonshotProvider struct {
|
||||||
|
client *resty.Client
|
||||||
|
config config.MoonshotConfig
|
||||||
|
apiKey string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMoonshotProvider(cfg config.MoonshotConfig, apiKey string) *MoonshotProvider {
|
||||||
|
return &MoonshotProvider{
|
||||||
|
client: resty.New(),
|
||||||
|
config: cfg,
|
||||||
|
apiKey: apiKey,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *MoonshotProvider) Name() string {
|
||||||
|
return "moonshot"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *MoonshotProvider) ChatCompletion(ctx context.Context, req *models.UnifiedRequest) (*models.ChatCompletionResponse, error) {
|
||||||
|
messagesJSON, err := MessagesToOpenAIJSON(req.Messages)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to convert messages: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
body := BuildOpenAIBody(req, messagesJSON, false)
|
||||||
|
if strings.Contains(strings.ToLower(req.Model), "kimi-k2.5") {
|
||||||
|
if maxTokens, ok := body["max_tokens"]; ok {
|
||||||
|
delete(body, "max_tokens")
|
||||||
|
body["max_completion_tokens"] = maxTokens
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := p.client.R().
|
||||||
|
SetContext(ctx).
|
||||||
|
SetHeader("Authorization", "Bearer "+p.apiKey).
|
||||||
|
SetBody(body).
|
||||||
|
Post(fmt.Sprintf("%s/chat/completions", p.config.BaseURL))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("request failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !resp.IsSuccess() {
|
||||||
|
return nil, fmt.Errorf("Moonshot API error (%d): %s", resp.StatusCode(), resp.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
var respJSON map[string]interface{}
|
||||||
|
if err := json.Unmarshal(resp.Body(), &respJSON); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ParseOpenAIResponse(respJSON, req.Model)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *MoonshotProvider) ChatCompletionStream(ctx context.Context, req *models.UnifiedRequest) (<-chan *models.ChatCompletionStreamResponse, error) {
|
||||||
|
messagesJSON, err := MessagesToOpenAIJSON(req.Messages)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to convert messages: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
body := BuildOpenAIBody(req, messagesJSON, true)
|
||||||
|
if strings.Contains(strings.ToLower(req.Model), "kimi-k2.5") {
|
||||||
|
if maxTokens, ok := body["max_tokens"]; ok {
|
||||||
|
delete(body, "max_tokens")
|
||||||
|
body["max_completion_tokens"] = maxTokens
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := p.client.R().
|
||||||
|
SetContext(ctx).
|
||||||
|
SetHeader("Authorization", "Bearer "+p.apiKey).
|
||||||
|
SetBody(body).
|
||||||
|
SetDoNotParseResponse(true).
|
||||||
|
Post(fmt.Sprintf("%s/chat/completions", p.config.BaseURL))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("request failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !resp.IsSuccess() {
|
||||||
|
return nil, fmt.Errorf("Moonshot API error (%d): %s", resp.StatusCode(), resp.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
ch := make(chan *models.ChatCompletionStreamResponse)
|
||||||
|
go func() {
|
||||||
|
defer close(ch)
|
||||||
|
if err := StreamOpenAI(resp.RawBody(), ch); err != nil {
|
||||||
|
fmt.Printf("Moonshot Stream error: %v\n", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return ch, nil
|
||||||
|
}
|
||||||
@@ -54,9 +54,26 @@ func NewServer(cfg *config.Config, database *db.DB) *Server {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Initialize providers from DB and Config
|
// Initialize providers
|
||||||
if err := s.RefreshProviders(); err != nil {
|
if cfg.Providers.OpenAI.Enabled {
|
||||||
fmt.Printf("Warning: Failed to initial refresh providers: %v\n", err)
|
apiKey, _ := cfg.GetAPIKey("openai")
|
||||||
|
s.providers["openai"] = providers.NewOpenAIProvider(cfg.Providers.OpenAI, apiKey)
|
||||||
|
}
|
||||||
|
if cfg.Providers.Gemini.Enabled {
|
||||||
|
apiKey, _ := cfg.GetAPIKey("gemini")
|
||||||
|
s.providers["gemini"] = providers.NewGeminiProvider(cfg.Providers.Gemini, apiKey)
|
||||||
|
}
|
||||||
|
if cfg.Providers.DeepSeek.Enabled {
|
||||||
|
apiKey, _ := cfg.GetAPIKey("deepseek")
|
||||||
|
s.providers["deepseek"] = providers.NewDeepSeekProvider(cfg.Providers.DeepSeek, apiKey)
|
||||||
|
}
|
||||||
|
if cfg.Providers.Moonshot.Enabled {
|
||||||
|
apiKey, _ := cfg.GetAPIKey("moonshot")
|
||||||
|
s.providers["moonshot"] = providers.NewMoonshotProvider(cfg.Providers.Moonshot, apiKey)
|
||||||
|
}
|
||||||
|
if cfg.Providers.Grok.Enabled {
|
||||||
|
apiKey, _ := cfg.GetAPIKey("grok")
|
||||||
|
s.providers["grok"] = providers.NewGrokProvider(cfg.Providers.Grok, apiKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.setupRoutes()
|
s.setupRoutes()
|
||||||
@@ -238,6 +255,7 @@ func (s *Server) handleListModels(c *gin.Context) {
|
|||||||
"openai": true,
|
"openai": true,
|
||||||
"google": true, // Models from models.dev use 'google' ID for Gemini
|
"google": true, // Models from models.dev use 'google' ID for Gemini
|
||||||
"deepseek": true,
|
"deepseek": true,
|
||||||
|
"moonshot": true,
|
||||||
"xai": true, // Models from models.dev use 'xai' ID for Grok
|
"xai": true, // Models from models.dev use 'xai' ID for Grok
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -277,6 +295,8 @@ func (s *Server) handleChatCompletions(c *gin.Context) {
|
|||||||
providerName = "gemini"
|
providerName = "gemini"
|
||||||
} else if strings.Contains(req.Model, "deepseek") {
|
} else if strings.Contains(req.Model, "deepseek") {
|
||||||
providerName = "deepseek"
|
providerName = "deepseek"
|
||||||
|
} else if strings.Contains(req.Model, "kimi") || strings.Contains(req.Model, "moonshot") {
|
||||||
|
providerName = "moonshot"
|
||||||
} else if strings.Contains(req.Model, "grok") {
|
} else if strings.Contains(req.Model, "grok") {
|
||||||
providerName = "grok"
|
providerName = "grok"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user