From cd1984e26e2647a564b796a414daf232c2bb4241 Mon Sep 17 00:00:00 2001 From: JianFeeeee Date: Wed, 26 Aug 2026 19:47:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(ai=5Fimage):=20base=5Furl=20=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E9=A1=B9=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=20OpenAI=20=E5=85=BC=E5=AE=B9=E7=BD=91=E5=85=B3=20v1.1.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit generateOpenAI 支持配置 base_url 指向 OpenAI 兼容网关(如本机 llmsproxy),为空保持官方直连。已实测经 llmsproxy→siliconflow (Kwai-Kolors/Kolors) 生图出有效 PNG。 --- example/ai_image/plg.json | 11 ++++++++--- example/ai_image/plugin.go | 19 +++++++++++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/example/ai_image/plg.json b/example/ai_image/plg.json index f2fab0f..3d15c16 100644 --- a/example/ai_image/plg.json +++ b/example/ai_image/plg.json @@ -2,14 +2,19 @@ "name": "ai_image", "name_zh": "AI绘图", "name_en": "AI Image", - "version": "1.0.0", + "version": "1.1.0", "description": "AI 图像生成插件,支持 OpenAI DALL·E / Stable Diffusion", "author": "HomeAgent", "entry": "plugin.so", - "tags": ["ai", "image", "draw", "generate"], + "tags": [ + "ai", + "image", + "draw", + "generate" + ], "targets": "linux/amd64", "outdir": "dist", "bundle": true, "replaces": {}, "source_dirs": [] -} +} \ No newline at end of file diff --git a/example/ai_image/plugin.go b/example/ai_image/plugin.go index 61213ab..f1c421a 100644 --- a/example/ai_image/plugin.go +++ b/example/ai_image/plugin.go @@ -21,6 +21,7 @@ type Plugin struct { provider string model string size string + baseURL string } func NewPluginFactory(name string, config map[string]interface{}) (sdk.Plugin, error) { @@ -111,10 +112,15 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error { DisplayName: "API Key", Description: "OpenAI / Stable Diffusion API Key", Category: "ai_image", Secret: true, }) + s.Settings().RegisterDef(sdk.ConfigDef{ + Key: "base_url", Default: "", Type: "string", + DisplayName: "Base URL", Description: "自定义 OpenAI 兼容网关地址(不带 /v1 尾缀,如 http://127.0.0.1:8081);为空走官方 https://api.openai.com", + Category: "ai_image", + }) s.Settings().RegisterDef(sdk.ConfigDef{ Key: "provider", Default: "openai", Type: "string", DisplayName: "Provider", Description: "Image generation provider: openai / stability", - Category: "ai_image", + Category: "ai_image", }) s.Settings().RegisterDef(sdk.ConfigDef{ Key: "model", Default: "dall-e-3", Type: "string", @@ -131,6 +137,7 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error { p.provider = getSetting(s.Settings(), "provider", "openai") p.model = getSetting(s.Settings(), "model", "dall-e-3") p.size = getSetting(s.Settings(), "size", "1024x1024") + p.baseURL = strings.TrimRight(strings.TrimSpace(getSetting(s.Settings(), "base_url", "")), "/") tp := p.name + "_" s.RegisterTool(tp+"generate", sdk.ToolDef{ @@ -209,6 +216,14 @@ func (p *Plugin) handleGenerate(args map[string]interface{}) (interface{}, error } func (p *Plugin) generateOpenAI(prompt, model, size string, n int, apiKey string) (interface{}, error) { + // 上游地址:base_url 非空时走自定义网关(如本机 llmsproxy),约定不带 /v1 尾缀; + // 为空保持官方直连。兼容误配了 /v1 尾缀的情况(去重)。 + endpoint := "https://api.openai.com/v1/images/generations" + if p.baseURL != "" { + base := strings.TrimSuffix(p.baseURL, "/v1") + endpoint = base + "/v1/images/generations" + } + body := openAIReq{ Model: model, Prompt: prompt, @@ -218,7 +233,7 @@ func (p *Plugin) generateOpenAI(prompt, model, size string, n int, apiKey string } b, _ := json.Marshal(body) - req, _ := http.NewRequest("POST", "https://api.openai.com/v1/images/generations", bytes.NewReader(b)) + req, _ := http.NewRequest("POST", endpoint, bytes.NewReader(b)) req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+apiKey)