From 273f9ace58b139e555a7e07629aca416d245f831 Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Wed, 19 Feb 2025 17:24:12 +0800 Subject: [PATCH] Add model information field to service information --- controller/ai-local/iml.go | 1 + controller/service/iml.go | 6 +++++- controller/system/iml.go | 6 ++++++ module/ai-local/iml.go | 2 +- module/service/dto/input.go | 2 ++ module/service/dto/output.go | 7 ++++++- module/service/iml.go | 32 ++++++++++++++++++++++++++++++++ service/ai-local/iml.go | 8 ++++++++ service/ai-local/service.go | 1 + 9 files changed, 62 insertions(+), 3 deletions(-) diff --git a/controller/ai-local/iml.go b/controller/ai-local/iml.go index 013e4b42..aa722518 100644 --- a/controller/ai-local/iml.go +++ b/controller/ai-local/iml.go @@ -227,6 +227,7 @@ func (i *imlLocalModelController) initAILocalService(ctx context.Context, model ApprovalType: "auto", Kind: "ai", Provider: &providerId, + Model: &model, }) if err != nil { return err diff --git a/controller/service/iml.go b/controller/service/iml.go index 310b5876..5def0907 100644 --- a/controller/service/iml.go +++ b/controller/service/iml.go @@ -94,7 +94,10 @@ func (i *imlServiceController) QuickCreateAIService(ctx *gin.Context, input *ser if err != nil { return err } - + p, err := i.providerModule.Provider(ctx, input.Provider) + if err != nil { + return err + } id := uuid.NewString() prefix := fmt.Sprintf("/%s", id[:8]) catalogueInfo, err := i.catalogueModule.DefaultCatalogue(ctx) @@ -111,6 +114,7 @@ func (i *imlServiceController) QuickCreateAIService(ctx *gin.Context, input *ser Catalogue: catalogueInfo.Id, ApprovalType: "auto", Provider: &input.Provider, + Model: &p.DefaultLLM, Kind: "ai", }) return err diff --git a/controller/system/iml.go b/controller/system/iml.go index 2744e707..7c020379 100644 --- a/controller/system/iml.go +++ b/controller/system/iml.go @@ -351,6 +351,12 @@ func (i *imlInitController) createAIService(ctx context.Context, teamID string, if input.Id == "" { input.Id = uuid.New().String() } + providerInfo, err := i.providerModule.Provider(ctx, *input.Provider) + if err != nil { + return err + } + input.Model = &providerInfo.DefaultLLM + if input.Prefix == "" { if len(input.Id) < 9 { input.Prefix = input.Id diff --git a/module/ai-local/iml.go b/module/ai-local/iml.go index 7e44b6a6..145b62d9 100644 --- a/module/ai-local/iml.go +++ b/module/ai-local/iml.go @@ -529,7 +529,7 @@ func (i *imlLocalModel) getLocalModels(ctx context.Context) ([]*gateway.DynamicR cfg := make(map[string]interface{}) cfg["provider"] = "ollama" cfg["model"] = l.Id - cfg["model_config"] = ai_provider_local.OllamaSvg + cfg["model_config"] = ai_provider_local.OllamaConfig cfg["base"] = v releases = append(releases, &gateway.DynamicRelease{ BasicItem: &gateway.BasicItem{ diff --git a/module/service/dto/input.go b/module/service/dto/input.go index 6cfab3da..dcea95b4 100644 --- a/module/service/dto/input.go +++ b/module/service/dto/input.go @@ -20,6 +20,7 @@ type CreateService struct { Kind string `json:"service_kind"` State string `json:"state"` Provider *string `json:"provider"` + Model *string `json:"model"` AsApp *bool `json:"as_app"` AsServer *bool `json:"as_server"` } @@ -32,6 +33,7 @@ type EditService struct { Logo *string `json:"logo"` Tags *[]string `json:"tags"` Provider *string `json:"provider"` + Model *string `json:"model"` ApprovalType *string `json:"approval_type"` State *string `json:"state"` } diff --git a/module/service/dto/output.go b/module/service/dto/output.go index 016c1007..ef4e6820 100644 --- a/module/service/dto/output.go +++ b/module/service/dto/output.go @@ -97,7 +97,8 @@ type Service struct { Tags []auto.Label `json:"tags" aolabel:"tag"` Logo string `json:"logo"` Provider *auto.Label `json:"provider,omitempty" aolabel:"ai_provider"` - ProviderType string `json:"provider_type"` + ProviderType string `json:"provider_type,omitempty"` + Model string `json:"model,omitempty"` ApprovalType string `json:"approval_type"` AsServer bool `json:"as_server"` AsApp bool `json:"as_app"` @@ -152,6 +153,10 @@ func ToService(model *service.Service) *Service { if provider.Id != "ollama" { s.ProviderType = "online" } + modelId := model.AdditionalConfig["model"] + if modelId != "" { + s.Model = modelId + } } return s } diff --git a/module/service/iml.go b/module/service/iml.go index 60324a44..a36197cd 100644 --- a/module/service/iml.go +++ b/module/service/iml.go @@ -8,6 +8,10 @@ import ( "strings" "time" + ai_local "github.com/APIParkLab/APIPark/service/ai-local" + + model_runtime "github.com/APIParkLab/APIPark/ai-provider/model-runtime" + "github.com/eolinker/eosc/log" "github.com/APIParkLab/APIPark/resources/access" @@ -58,6 +62,7 @@ type imlServiceModule struct { teamService team.ITeamService `autowired:""` teamMemberService team_member.ITeamMemberService `autowired:""` tagService tag.ITagService `autowired:""` + localModelService ai_local.ILocalModelService `autowired:""` serviceTagService service_tag.ITagService `autowired:""` apiService api.IAPIService `autowired:""` @@ -223,6 +228,25 @@ func (i *imlServiceModule) Get(ctx context.Context, id string) (*service_dto.Ser s.Tags = auto.List(utils.SliceToSlice(tags, func(p *service_tag.Tag) string { return p.Tid })) + if s.Model == "" { + switch s.ProviderType { + case "online": + p, has := model_runtime.GetProvider(s.Provider.Id) + if has { + m, has := p.DefaultModel(model_runtime.ModelTypeLLM) + if has { + s.Model = m.ID() + } + } + case "local": + info, err := i.localModelService.DefaultModel(ctx) + if err != nil { + return nil, err + } + s.Model = info.Id + + } + } log.Infof("get service cost %d ms", time.Since(now).Milliseconds()) return s, nil } @@ -328,6 +352,11 @@ func (i *imlServiceModule) Create(ctx context.Context, teamID string, input *ser return nil, fmt.Errorf("ai service: provider can not be empty") } mo.AdditionalConfig["provider"] = *input.Provider + if input.Model == nil { + return nil, fmt.Errorf("ai service: model can not be empty") + } + mo.AdditionalConfig["model"] = *input.Model + } if input.AsApp == nil { // 默认值为false @@ -378,6 +407,9 @@ func (i *imlServiceModule) Edit(ctx context.Context, id string, input *service_d if input.Provider != nil { info.AdditionalConfig["provider"] = *input.Provider } + if input.Model != nil { + info.AdditionalConfig["model"] = *input.Model + } } err = i.transaction.Transaction(ctx, func(ctx context.Context) error { diff --git a/service/ai-local/iml.go b/service/ai-local/iml.go index 7c992c36..5d21cc2b 100644 --- a/service/ai-local/iml.go +++ b/service/ai-local/iml.go @@ -20,6 +20,14 @@ type imlLocalModelService struct { universally.IServiceDelete } +func (i *imlLocalModelService) DefaultModel(ctx context.Context) (*LocalModel, error) { + info, err := i.store.First(ctx, map[string]interface{}{"state": 1}) + if err != nil { + return nil, err + } + return i.fromEntity(info), nil +} + func (i *imlLocalModelService) OnComplete() { i.IServiceGet = universally.NewGet[LocalModel, ai.LocalModel](i.store, i.fromEntity) i.IServiceCreate = universally.NewCreator[CreateLocalModel, ai.LocalModel](i.store, "ai_local_model", i.createEntityHandler, i.uniquestHandler, i.labelHandler) diff --git a/service/ai-local/service.go b/service/ai-local/service.go index e32458ea..2c493b36 100644 --- a/service/ai-local/service.go +++ b/service/ai-local/service.go @@ -13,6 +13,7 @@ type ILocalModelService interface { universally.IServiceCreate[CreateLocalModel] universally.IServiceEdit[EditLocalModel] universally.IServiceDelete + DefaultModel(ctx context.Context) (*LocalModel, error) } type ILocalModelPackageService interface {