From 56124fd90da8cadef54cdbc078810bf97c58db23 Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Thu, 26 Dec 2024 15:41:07 +0800 Subject: [PATCH] update simple configured providers --- controller/ai/controller.go | 1 + controller/ai/iml.go | 4 +++ module/ai/dto/output.go | 11 +++---- module/ai/iml.go | 60 ++++++++++++++++++++++++++++++++++--- module/ai/module.go | 1 + module/cluster/dto/input.go | 4 +-- plugins/core/ai.go | 1 + 7 files changed, 71 insertions(+), 11 deletions(-) diff --git a/controller/ai/controller.go b/controller/ai/controller.go index 5c9fed77..bb6e7c9f 100644 --- a/controller/ai/controller.go +++ b/controller/ai/controller.go @@ -14,6 +14,7 @@ type IProviderController interface { ConfiguredProviders(ctx *gin.Context) ([]*ai_dto.ConfiguredProviderItem, *auto.Label, error) UnConfiguredProviders(ctx *gin.Context) ([]*ai_dto.ProviderItem, error) SimpleProviders(ctx *gin.Context) ([]*ai_dto.SimpleProviderItem, error) + SimpleConfiguredProviders(ctx *gin.Context) ([]*ai_dto.SimpleProviderItem, error) Provider(ctx *gin.Context, id string) (*ai_dto.Provider, error) SimpleProvider(ctx *gin.Context, id string) (*ai_dto.SimpleProvider, error) LLMs(ctx *gin.Context, driver string) ([]*ai_dto.LLMItem, *ai_dto.ProviderItem, error) diff --git a/controller/ai/iml.go b/controller/ai/iml.go index dd9200e0..1ca21cdc 100644 --- a/controller/ai/iml.go +++ b/controller/ai/iml.go @@ -33,6 +33,10 @@ func (i *imlProviderController) SimpleProviders(ctx *gin.Context) ([]*ai_dto.Sim return i.module.SimpleProviders(ctx) } +func (i *imlProviderController) SimpleConfiguredProviders(ctx *gin.Context) ([]*ai_dto.SimpleProviderItem, error) { + return i.module.SimpleConfiguredProviders(ctx) +} + func (i *imlProviderController) Provider(ctx *gin.Context, id string) (*ai_dto.Provider, error) { return i.module.Provider(ctx, id) } diff --git a/module/ai/dto/output.go b/module/ai/dto/output.go index 53481441..3e9350f6 100644 --- a/module/ai/dto/output.go +++ b/module/ai/dto/output.go @@ -5,11 +5,11 @@ import ( ) type SimpleProvider struct { - Id string `json:"id"` - Name string `json:"name"` - Config string `json:"config"` - Logo string `json:"logo"` - GetAPIKeyUrl string `json:"get_apikey_url"` + Id string `json:"id"` + Name string `json:"name"` + DefaultConfig string `json:"default_config"` + Logo string `json:"logo"` + GetAPIKeyUrl string `json:"get_apikey_url"` } type Provider struct { @@ -56,6 +56,7 @@ type SimpleProviderItem struct { Configured bool `json:"configured"` DefaultConfig string `json:"default_config"` Status ProviderStatus `json:"status"` + Priority int `json:"-"` } type LLMItem struct { diff --git a/module/ai/iml.go b/module/ai/iml.go index 45c64bd0..ea6dcb45 100644 --- a/module/ai/iml.go +++ b/module/ai/iml.go @@ -66,10 +66,11 @@ func (i *imlProviderModule) SimpleProvider(ctx context.Context, id string) (*ai_ return nil, fmt.Errorf("ai provider not found") } return &ai_dto.SimpleProvider{ - Id: p.ID(), - Name: p.Name(), - Logo: p.Logo(), - GetAPIKeyUrl: p.HelpUrl(), + Id: p.ID(), + Name: p.Name(), + Logo: p.Logo(), + DefaultConfig: p.DefaultConfig(), + GetAPIKeyUrl: p.HelpUrl(), }, nil } @@ -216,9 +217,60 @@ func (i *imlProviderModule) SimpleProviders(ctx context.Context) ([]*ai_dto.Simp if info, has := providerMap[v.ID()]; has { item.Configured = true item.Status = ai_dto.ToProviderStatus(info.Status) + item.Priority = info.Priority } items = append(items, item) } + sort.Slice(items, func(i, j int) bool { + if items[i].Priority != items[j].Priority { + if items[i].Priority == 0 { + return false + } + if items[j].Priority == 0 { + return true + } + return items[i].Priority < items[j].Priority + } + return items[i].Name < items[j].Name + }) + return items, nil +} + +func (i *imlProviderModule) SimpleConfiguredProviders(ctx context.Context) ([]*ai_dto.SimpleProviderItem, error) { + list, err := i.providerService.List(ctx) + if err != nil { + return nil, err + } + items := make([]*ai_dto.SimpleProviderItem, 0, len(list)) + for _, l := range list { + p, has := model_runtime.GetProvider(l.Id) + if !has { + continue + } + item := &ai_dto.SimpleProviderItem{ + Id: l.Id, + Name: l.Name, + Logo: p.Logo(), + DefaultConfig: p.DefaultConfig(), + Status: ai_dto.ToProviderStatus(l.Status), + Priority: l.Priority, + Configured: true, + } + + items = append(items, item) + } + sort.Slice(items, func(i, j int) bool { + if items[i].Priority != items[j].Priority { + if items[i].Priority == 0 { + return false + } + if items[j].Priority == 0 { + return true + } + return items[i].Priority < items[j].Priority + } + return items[i].Name < items[j].Name + }) return items, nil } diff --git a/module/ai/module.go b/module/ai/module.go index 176c5c38..1037aea7 100644 --- a/module/ai/module.go +++ b/module/ai/module.go @@ -15,6 +15,7 @@ type IProviderModule interface { ConfiguredProviders(ctx context.Context) ([]*ai_dto.ConfiguredProviderItem, *auto.Label, error) UnConfiguredProviders(ctx context.Context) ([]*ai_dto.ProviderItem, error) SimpleProviders(ctx context.Context) ([]*ai_dto.SimpleProviderItem, error) + SimpleConfiguredProviders(ctx context.Context) ([]*ai_dto.SimpleProviderItem, error) Provider(ctx context.Context, id string) (*ai_dto.Provider, error) SimpleProvider(ctx context.Context, id string) (*ai_dto.SimpleProvider, error) LLMs(ctx context.Context, driver string) ([]*ai_dto.LLMItem, *ai_dto.ProviderItem, error) diff --git a/module/cluster/dto/input.go b/module/cluster/dto/input.go index 9d690526..43036f37 100644 --- a/module/cluster/dto/input.go +++ b/module/cluster/dto/input.go @@ -17,12 +17,12 @@ package cluster_dto //type SaveMonitorConfig struct { // Driver string `json:"driver"` -// Config map[string]interface{} `json:"config"` +// DefaultConfig map[string]interface{} `json:"config"` //} //type MonitorConfig struct { // Driver string `json:"driver"` -// Config map[string]interface{} `json:"config"` +// DefaultConfig map[string]interface{} `json:"config"` //} //type MonitorPartition struct { diff --git a/plugins/core/ai.go b/plugins/core/ai.go index cc45281b..eea9eb22 100644 --- a/plugins/core/ai.go +++ b/plugins/core/ai.go @@ -13,6 +13,7 @@ func (p *plugin) aiAPIs() []pm3.Api { pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/ai/providers/unconfigured", []string{"context"}, []string{"providers"}, p.aiProviderController.UnConfiguredProviders, access.SystemSettingsAiProviderView), pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/ai/providers/configured", []string{"context"}, []string{"providers", "backup"}, p.aiProviderController.ConfiguredProviders, access.SystemSettingsAiProviderView), pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/simple/ai/providers", []string{"context"}, []string{"providers"}, p.aiProviderController.SimpleProviders), + pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/simple/ai/providers/configured", []string{"context"}, []string{"providers"}, p.aiProviderController.SimpleConfiguredProviders), pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/ai/provider/config", []string{"context", "query:provider"}, []string{"provider"}, p.aiProviderController.Provider, access.SystemSettingsAiProviderView), pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/simple/ai/provider", []string{"context", "query:provider"}, []string{"provider"}, p.aiProviderController.SimpleProvider), pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/ai/provider/llms", []string{"context", "query:provider"}, []string{"llms", "provider"}, p.aiProviderController.LLMs),