From 6d12f251a10c39f186a9070e0d47dca4a79d1149 Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Mon, 17 Feb 2025 15:23:57 +0800 Subject: [PATCH] fix: ai deploy bug --- controller/ai-local/iml.go | 160 +++++++++++++++++------------------- module/ai-api/schema.go | 8 +- module/ai-local/iml.go | 63 ++++++++------ service/ai-local/iml.go | 4 + service/ai-local/service.go | 1 + service/api/iml.go | 3 +- service/api/model.go | 2 +- stores/api/model.go | 2 +- stores/service/model.go | 2 +- 9 files changed, 126 insertions(+), 119 deletions(-) diff --git a/controller/ai-local/iml.go b/controller/ai-local/iml.go index 4ba66ca7..10a6e860 100644 --- a/controller/ai-local/iml.go +++ b/controller/ai-local/iml.go @@ -6,14 +6,9 @@ import ( "fmt" "io" "math" - "net/http" - "strings" "github.com/APIParkLab/APIPark/module/router" - "github.com/APIParkLab/APIPark/model/plugin_model" - "github.com/APIParkLab/APIPark/service/api" - ai_api "github.com/APIParkLab/APIPark/module/ai-api" "github.com/APIParkLab/APIPark/module/catalogue" @@ -24,9 +19,6 @@ import ( service_dto "github.com/APIParkLab/APIPark/module/service/dto" - ai_api_dto "github.com/APIParkLab/APIPark/module/ai-api/dto" - router_dto "github.com/APIParkLab/APIPark/module/router/dto" - ai_provider_local "github.com/APIParkLab/APIPark/ai-provider/local" "github.com/eolinker/eosc/log" @@ -199,7 +191,7 @@ func (i *imlLocalModelController) initAILocalService(ctx context.Context, model serviceId := uuid.NewString() prefix := fmt.Sprintf("/%s", serviceId[:8]) providerId := "ollama" - info, err := i.serviceModule.Create(ctx, teamID, &service_dto.CreateService{ + _, err = i.serviceModule.Create(ctx, teamID, &service_dto.CreateService{ Id: serviceId, Name: model, Prefix: prefix, @@ -214,83 +206,81 @@ func (i *imlLocalModelController) initAILocalService(ctx context.Context, model if err != nil { return err } - err = i.module.SaveCache(ctx, model, serviceId) - if err != nil { - return err - } - path := fmt.Sprintf("/%s/chat", strings.Trim(prefix, "/")) - timeout := 300000 - retry := 0 - aiPrompt := &ai_api_dto.AiPrompt{ - Variables: []*ai_api_dto.AiPromptVariable{}, - Prompt: "", - } - aiModel := &ai_api_dto.AiModel{ - Id: model, - Config: ai_provider_local.OllamaConfig, - Provider: providerId, - Type: "local", - } - name := "Demo AI API" - description := "A demo that shows you how to use a e a Chat API." - apiId := uuid.NewString() - err = i.aiAPIModule.Create( - ctx, - info.Id, - &ai_api_dto.CreateAPI{ - Id: apiId, - Name: name, - Path: path, - Description: description, - Disable: false, - AiPrompt: aiPrompt, - AiModel: aiModel, - Timeout: timeout, - Retry: retry, - }, - ) - if err != nil { - return err - } - plugins := make(map[string]api.PluginSetting) - plugins["ai_prompt"] = api.PluginSetting{ - Config: plugin_model.ConfigType{ - "prompt": aiPrompt.Prompt, - "variables": aiPrompt.Variables, - }, - } - plugins["ai_formatter"] = api.PluginSetting{ - Config: plugin_model.ConfigType{ - "model": aiModel.Id, - "provider": info.Provider.Id, - "config": aiModel.Config, - }, - } - _, err = i.routerModule.Create(ctx, info.Id, &router_dto.Create{ - Id: apiId, - Name: name, - Path: path, - Methods: []string{ - http.MethodPost, - }, - Description: description, - Protocols: []string{"http", "https"}, - MatchRules: nil, - Proxy: &router_dto.InputProxy{ - Path: path, - Timeout: timeout, - Retry: retry, - Plugins: plugins, - }, - Disable: false, - }) - if err != nil { - return err - } + return i.module.SaveCache(ctx, model, serviceId) - return i.docModule.SaveServiceDoc(ctx, info.Id, &service_dto.SaveServiceDoc{ - Doc: "", - }) + //path := fmt.Sprintf("/%s/chat", strings.Trim(prefix, "/")) + //timeout := 300000 + //retry := 0 + //aiPrompt := &ai_api_dto.AiPrompt{ + // Variables: []*ai_api_dto.AiPromptVariable{}, + // Prompt: "", + //} + //aiModel := &ai_api_dto.AiModel{ + // Id: model, + // Config: ai_provider_local.OllamaConfig, + // Provider: providerId, + // Type: "local", + //} + //name := "Demo AI API" + //description := "A demo that shows you how to use a e a Chat API." + //apiId := uuid.NewString() + //err = i.aiAPIModule.Create( + // ctx, + // info.Id, + // &ai_api_dto.CreateAPI{ + // Id: apiId, + // Name: name, + // Path: path, + // Description: description, + // Disable: false, + // AiPrompt: aiPrompt, + // AiModel: aiModel, + // Timeout: timeout, + // Retry: retry, + // }, + //) + //if err != nil { + // return err + //} + //plugins := make(map[string]api.PluginSetting) + //plugins["ai_prompt"] = api.PluginSetting{ + // Config: plugin_model.ConfigType{ + // "prompt": aiPrompt.Prompt, + // "variables": aiPrompt.Variables, + // }, + //} + //plugins["ai_formatter"] = api.PluginSetting{ + // Config: plugin_model.ConfigType{ + // "model": aiModel.Id, + // "provider": info.Provider.Id, + // "config": aiModel.Config, + // }, + //} + //_, err = i.routerModule.Create(ctx, info.Id, &router_dto.Create{ + // Id: apiId, + // Name: name, + // Path: path, + // Methods: []string{ + // http.MethodPost, + // }, + // Description: description, + // Protocols: []string{"http", "https"}, + // MatchRules: nil, + // Proxy: &router_dto.InputProxy{ + // Path: path, + // Timeout: timeout, + // Retry: retry, + // Plugins: plugins, + // }, + // Disable: false, + //}) + //if err != nil { + // return err + //} + // + //return i.docModule.SaveServiceDoc(ctx, info.Id, &service_dto.SaveServiceDoc{ + // Doc: "", + //}) }) return err diff --git a/module/ai-api/schema.go b/module/ai-api/schema.go index 2511d0ab..5c7c7c5c 100644 --- a/module/ai-api/schema.go +++ b/module/ai-api/schema.go @@ -72,9 +72,13 @@ func genResponse() *openapi3.ResponseRef { func genRequestBodySchema(variables []*ai_api_dto.AiPromptVariable) *openapi3.Schema { result := openapi3.NewObjectSchema() - result.WithProperty("variables", genVariableSchema(variables)) + if len(variables) > 0 { + result.WithProperty("variables", genVariableSchema(variables)) + result.WithRequired([]string{"variables", "messages"}) + } + result.WithPropertyRef("messages", messagesSchemaRef) - result.WithRequired([]string{"variables", "messages"}) + return result } diff --git a/module/ai-local/iml.go b/module/ai-local/iml.go index f6606b75..b32395be 100644 --- a/module/ai-local/iml.go +++ b/module/ai-local/iml.go @@ -130,34 +130,41 @@ func (i *imlLocalModel) Search(ctx context.Context, keyword string) ([]*ai_local } func (i *imlLocalModel) ListCanInstall(ctx context.Context, keyword string) ([]*ai_local_dto.LocalModelPackageItem, error) { - list, err := i.localModelPackageService.Search(ctx, keyword, nil) - if err != nil { - return nil, err - } - if keyword != "" { + + if keyword == "" { + list, err := i.localModelPackageService.Search(ctx, keyword, nil) + if err != nil { + return nil, err + } + return utils.SliceToSlice(list, func(s *ai_local.LocalModelPackage) *ai_local_dto.LocalModelPackageItem { + return &ai_local_dto.LocalModelPackageItem{ + Id: s.Id, + Name: s.Name, + Size: s.Size, + IsPopular: s.IsPopular, + } + }), nil + } else { + info, err := i.localModelPackageService.Get(ctx, keyword) + if err != nil { + return nil, err + } result := make([]*ai_local_dto.LocalModelPackageItem, 0) - for _, v := range list { - models := ai_provider_local.ModelsCanInstallById(v.Id) - for _, model := range models { - result = append(result, &ai_local_dto.LocalModelPackageItem{ - Id: model.Id, - Name: model.Name, - Size: model.Size, - IsPopular: model.IsPopular, - }) - } + //for _, v := range list { + models := ai_provider_local.ModelsCanInstallById(info.Id) + for _, model := range models { + result = append(result, &ai_local_dto.LocalModelPackageItem{ + Id: model.Id, + Name: model.Name, + Size: model.Size, + IsPopular: model.IsPopular, + }) } + //} return result, nil } - return utils.SliceToSlice(list, func(s *ai_local.LocalModelPackage) *ai_local_dto.LocalModelPackageItem { - return &ai_local_dto.LocalModelPackageItem{ - Id: s.Id, - Name: s.Name, - Size: s.Size, - IsPopular: s.IsPopular, - } - }), nil + } func (i *imlLocalModel) pullHook() func(msg ai_provider_local.PullMessage) error { @@ -292,7 +299,7 @@ func (i *imlLocalModel) syncGateway(ctx context.Context, clusterId string, relea func (i *imlLocalModel) Deploy(ctx context.Context, model string, session string) (*ai_provider_local.Pipeline, error) { var p *ai_provider_local.Pipeline err := i.transaction.Transaction(ctx, func(txCtx context.Context) error { - _, err := i.localModelService.Get(ctx, model) + info, err := i.localModelService.Get(ctx, model) if err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { return err @@ -305,8 +312,10 @@ func (i *imlLocalModel) Deploy(ctx context.Context, model string, session string }) } else { - state := ai_local_dto.LocalModelStateDeploying.Int() - err = i.localModelService.Save(ctx, model, &ai_local.EditLocalModel{State: &state}) + if info.State == ai_local_dto.LocalModelStateDeployingError.Int() { + state := ai_local_dto.LocalModelStateDeploying.Int() + err = i.localModelService.Save(ctx, model, &ai_local.EditLocalModel{State: &state}) + } } if err != nil { return err @@ -428,6 +437,7 @@ func (i *imlLocalModel) OnInit() { }) models, version := ai_provider_local.ModelsCanInstall() for _, model := range models { + delete(oldModels, model.Id) if v, ok := oldModels[model.Id]; ok { if v.Version == version { continue @@ -456,7 +466,6 @@ func (i *imlLocalModel) OnInit() { return } } - delete(oldModels, model.Id) } for id := range oldModels { err = i.localModelPackageService.Delete(ctx, id) diff --git a/service/ai-local/iml.go b/service/ai-local/iml.go index a970ba9d..e037d0a8 100644 --- a/service/ai-local/iml.go +++ b/service/ai-local/iml.go @@ -66,6 +66,10 @@ func (i *imlLocalModelService) fromEntity(e *ai.LocalModel) *LocalModel { } } +var ( + _ ILocalModelPackageService = &imlLocalModelPackageService{} +) + type imlLocalModelPackageService struct { store ai.ILocalModelPackageStore `autowired:""` universally.IServiceGet[LocalModelPackage] diff --git a/service/ai-local/service.go b/service/ai-local/service.go index 276cec8c..260d3029 100644 --- a/service/ai-local/service.go +++ b/service/ai-local/service.go @@ -20,6 +20,7 @@ type ILocalModelPackageService interface { universally.IServiceCreate[CreateLocalModelPackage] universally.IServiceEdit[EditLocalModelPackage] universally.IServiceDelete + //SearchByModel(ctx context.Context, model string) ([]*LocalModelPackage, error) } type ILocalModelInstallStateService interface { diff --git a/service/api/iml.go b/service/api/iml.go index 19117a72..a784fed5 100644 --- a/service/api/iml.go +++ b/service/api/iml.go @@ -42,10 +42,9 @@ type imlAPIService struct { } func (i *imlAPIService) DeleteByService(ctx context.Context, serviceId string) error { - _, err := i.store.DeleteWhere(ctx, map[string]interface{}{ + return i.store.SoftDelete(ctx, map[string]interface{}{ "service": serviceId, }) - return err } func (i *imlAPIService) ListForServices(ctx context.Context, serviceIds ...string) ([]*API, error) { diff --git a/service/api/model.go b/service/api/model.go index e4d5e662..4254f94a 100644 --- a/service/api/model.go +++ b/service/api/model.go @@ -48,7 +48,7 @@ func FromEntity(e *api.API) *API { return &API{ UUID: e.UUID, CreateAt: e.CreateAt, - IsDelete: e.IsDelete != 0, + IsDelete: e.IsDelete, Service: e.Service, Team: e.Team, Creator: e.Creator, diff --git a/stores/api/model.go b/stores/api/model.go index ba72ffda..9447da32 100644 --- a/stores/api/model.go +++ b/stores/api/model.go @@ -12,10 +12,10 @@ type API struct { Upstream string `gorm:"size:36;not null;column:upstream;comment:上游;index:upstream"` // 上游ID Creator string `gorm:"size:36;not null;column:creator;comment:创建人;index:creator" aovalue:"creator"` // 创建人 CreateAt time.Time `gorm:"type:timestamp;NOT NULL;DEFAULT:CURRENT_TIMESTAMP;column:create_at;comment:创建时间"` - IsDelete int `gorm:"type:tinyint(1);not null;column:is_delete;comment:是否删除 0:未删除 1:已删除"` Method []string `gorm:"size:255;not null;column:method;comment:请求方法;serializer:json"` Protocol []string `gorm:"type:text;not null;column:protocol;comment:协议;serializer:json"` Path string `gorm:"size:255;not null;column:path;comment:请求路径"` + IsDelete bool `gorm:"type:tinyint(1);not null;column:is_delete;comment:是否删除 0:未删除 1:已删除"` } type Info struct { Id int64 `gorm:"column:id;type:BIGINT(20);NOT NULL;comment:id;primary_key;comment:主键ID;"` diff --git a/stores/service/model.go b/stores/service/model.go index eafc2933..d0d42d30 100644 --- a/stores/service/model.go +++ b/stores/service/model.go @@ -15,7 +15,7 @@ type Service struct { Catalogue string `gorm:"type:text;not null;column:catalogue;comment:目录"` CreateAt time.Time `gorm:"type:timestamp;NOT NULL;DEFAULT:CURRENT_TIMESTAMP;column:create_at;comment:创建时间"` UpdateAt time.Time `gorm:"type:timestamp;NOT NULL;DEFAULT:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;column:update_at;comment:修改时间"` - IsDelete int `gorm:"type:tinyint(1);not null;column:is_delete;comment:是否删除"` + IsDelete bool `gorm:"type:tinyint(1);not null;column:is_delete;comment:是否删除"` Kind int `gorm:"type:tinyint(4);not null;column:kind;comment:服务种类,0:Rest服务,1:AI服务"` State int `gorm:"type:tinyint(4);not null;column:state;comment:状态"` AdditionalConfig string `gorm:"type:text;not null;column:additional_config;comment:额外配置"`