From 4e647b23ac271012c4f98e821d5b18df2e05fafd Mon Sep 17 00:00:00 2001 From: BlueSkyXN <63384277+BlueSkyXN@users.noreply.github.com> Date: Sun, 16 Feb 2025 18:28:18 +0800 Subject: [PATCH] 0.0.5 --- .gitignore | 1 + config-example.yaml | 29 +++++++++-------------------- main.go | 18 ++++++++++++++---- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 6f72f89..deb225b 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ go.work.sum # env file .env +/dist diff --git a/config-example.yaml b/config-example.yaml index 910d612..e677a56 100644 --- a/config-example.yaml +++ b/config-example.yaml @@ -3,7 +3,8 @@ thinking_services: # 主要思考服务 - DeepSeek - id: 1 name: "deepseek-thinking" - base_url: "https://api.deepseek.com" + base_url: "https://api-inference.modelscope.cn" + api_path: "/v1/chat/completions" api_key: "sk-xxxxxxxxxxxxxxxx" timeout: 10 # 秒 retry: 3 @@ -13,6 +14,7 @@ thinking_services: - id: 2 name: "openai-thinking" base_url: "https://api.openai.com" + api_path: "/v1/chat/completions" api_key: "sk-xxxxxxxxxxxxxxxx" timeout: 15 retry: 2 @@ -23,43 +25,30 @@ channels: # DeepSeek 通道 "1": name: "deepseek-channel" - base_url: "https://api.deepseek.com" - key_prefix: "sk-deep" + base_url: "https://api-inference.modelscope.cn" + api_path: "/v1/chat/completions" timeout: 30 - models: - - "deepseek-chat" - - "deepseek-code" # OpenAI 通道 "2": name: "openai-channel" base_url: "https://api.openai.com" - key_prefix: "sk-op" + api_path: "/v1/chat/completions" timeout: 30 - models: - - "gpt-3.5-turbo" - - "gpt-4" # Anthropic 通道 "3": name: "anthropic-channel" base_url: "https://api.anthropic.com" - key_prefix: "sk-ant" + api_path: "/v1/messages" timeout: 30 - models: - - "claude-3-opus" - - "claude-3-sonnet" # Moonshot 通道 "4": name: "moonshot-channel" base_url: "https://api.moonshot.cn" - key_prefix: "sk-moon" + api_path: "/v1/chat/completions" timeout: 30 - models: - - "moonshot-v1-8k" - - "moonshot-v1-32k" - - "moonshot-v1-128k" # 全局配置 global: @@ -85,7 +74,7 @@ global: # 服务配置 server: - port: 8080 + port: 8888 host: "0.0.0.0" read_timeout: 30 write_timeout: 30 diff --git a/main.go b/main.go index 3859d95..a05783b 100644 --- a/main.go +++ b/main.go @@ -24,18 +24,28 @@ type ThinkingService struct { ID int `mapstructure:"id"` Name string `mapstructure:"name"` BaseURL string `mapstructure:"base_url"` + APIPath string `mapstructure:"api_path"` APIKey string `mapstructure:"api_key"` Timeout int `mapstructure:"timeout"` Retry int `mapstructure:"retry"` Weight int `mapstructure:"weight"` } +// GetFullURL returns the complete API URL +func (s *ThinkingService) GetFullURL() string { + return s.BaseURL + s.APIPath +} + type Channel struct { Name string `mapstructure:"name"` BaseURL string `mapstructure:"base_url"` - KeyPrefix string `mapstructure:"key_prefix"` + APIPath string `mapstructure:"api_path"` Timeout int `mapstructure:"timeout"` - Models []string `mapstructure:"models"` +} + +// GetFullURL returns the complete API URL +func (c *Channel) GetFullURL() string { + return c.BaseURL + c.APIPath } type GlobalConfig struct { @@ -190,7 +200,7 @@ func (h *StreamHandler) streamThinking(ctx context.Context, req *ChatCompletionR } request, err := http.NewRequestWithContext(ctx, "POST", - h.thinkingService.BaseURL+"/v1/chat/completions", + h.thinkingService.GetFullURL(), bytes.NewBuffer(jsonData)) if err != nil { return "", err @@ -277,7 +287,7 @@ func (h *StreamHandler) streamFinalResponse(ctx context.Context, req *ChatComple // 创建请求 request, err := http.NewRequestWithContext(ctx, "POST", - h.targetChannel.BaseURL+"/v1/chat/completions", + h.targetChannel.GetFullURL(), bytes.NewBuffer(jsonData)) if err != nil { return err