From 1011302721e1e2e47f6adfe2105aae774e2fbb2c Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Tue, 18 Feb 2025 10:20:33 +0800 Subject: [PATCH 1/2] fix: cancel deploy error --- go.mod | 5 ++--- module/ai-local/iml.go | 9 +++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index e5baca0e..d7878223 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,6 @@ go 1.23.4 toolchain go1.23.6 -//toolchain go1.21.1 - require ( github.com/eolinker/ap-account v1.0.15 github.com/eolinker/eosc v0.18.3 @@ -83,6 +81,7 @@ require ( // github.com/eolinker/eosc => ../../eolinker/eosc //) -//replace github.com/eolinker/ap-account => ../aoaccount +//replace github.com/eolinker/ap-account => ../../eolinker/ap-account + // //replace github.com/eolinker/go-common => ../../eolinker/go-common diff --git a/module/ai-local/iml.go b/module/ai-local/iml.go index d3dcabeb..913c8ef5 100644 --- a/module/ai-local/iml.go +++ b/module/ai-local/iml.go @@ -354,6 +354,15 @@ func (i *imlLocalModel) SaveCache(ctx context.Context, model string, target stri func (i *imlLocalModel) CancelDeploy(ctx context.Context, model string) error { return i.transaction.Transaction(ctx, func(txCtx context.Context) error { + item, err := i.localModelCacheService.GetByTarget(ctx, ai_local.CacheTypeService, model) + if err != nil { + if !errors.Is(err, gorm.ErrRecordNotFound) { + return err + } + + } else { + model = item.Model + } list, err := i.localModelCacheService.List(ctx, model, ai_local.CacheTypeService) if err != nil { return err From e9b40d8b2734cc4523456290b6e0ac3509ab40be Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Tue, 18 Feb 2025 15:42:57 +0800 Subject: [PATCH 2/2] add api: get/set ollama api address --- controller/ai-local/controller.go | 2 ++ controller/ai-local/iml.go | 18 +++++++++++++++++ module/ai-local/dto/output.go | 4 ++++ module/system/dto/input.go | 33 ++++++++++++++++++++++++------- module/system/dto/input_test.go | 6 ++++-- module/system/dto/output.go | 1 + plugins/core/ai-local.go | 3 +++ 7 files changed, 58 insertions(+), 9 deletions(-) diff --git a/controller/ai-local/controller.go b/controller/ai-local/controller.go index 8e79e7eb..82139df5 100644 --- a/controller/ai-local/controller.go +++ b/controller/ai-local/controller.go @@ -18,6 +18,8 @@ type ILocalModelController interface { Update(ctx *gin.Context, model string, input *ai_local_dto.Update) error State(ctx *gin.Context, model string) (*ai_local_dto.DeployState, *ai_local_dto.ModelInfo, error) SimpleList(ctx *gin.Context) ([]*ai_local_dto.SimpleItem, error) + OllamaConfig(ctx *gin.Context) (*ai_local_dto.OllamaConfig, error) + OllamaConfigUpdate(ctx *gin.Context, input *ai_local_dto.OllamaConfig) error } func init() { diff --git a/controller/ai-local/iml.go b/controller/ai-local/iml.go index c46b4083..013e4b42 100644 --- a/controller/ai-local/iml.go +++ b/controller/ai-local/iml.go @@ -9,6 +9,10 @@ import ( "net/http" "strings" + system_dto "github.com/APIParkLab/APIPark/module/system/dto" + + "github.com/APIParkLab/APIPark/module/system" + "github.com/APIParkLab/APIPark/module/subscribe" subscribe_dto "github.com/APIParkLab/APIPark/module/subscribe/dto" @@ -51,9 +55,23 @@ type imlLocalModelController struct { routerModule router.IRouterModule `autowired:""` subscribeModule subscribe.ISubscribeModule `autowired:""` docModule service.IServiceDocModule `autowired:""` + settingModule system.ISettingModule `autowired:""` transaction store.ITransaction `autowired:""` } +func (i *imlLocalModelController) OllamaConfig(ctx *gin.Context) (*ai_local_dto.OllamaConfig, error) { + cfg := i.settingModule.Get(ctx) + return &ai_local_dto.OllamaConfig{ + Address: cfg.OllamaAddress, + }, nil +} + +func (i *imlLocalModelController) OllamaConfigUpdate(ctx *gin.Context, input *ai_local_dto.OllamaConfig) error { + return i.settingModule.Set(ctx, &system_dto.InputSetting{ + OllamaAddress: &input.Address, + }) +} + func (i *imlLocalModelController) SimpleList(ctx *gin.Context) ([]*ai_local_dto.SimpleItem, error) { return i.module.SimpleList(ctx) } diff --git a/module/ai-local/dto/output.go b/module/ai-local/dto/output.go index e0397e3e..62550016 100644 --- a/module/ai-local/dto/output.go +++ b/module/ai-local/dto/output.go @@ -58,6 +58,10 @@ func FromLocalModelState(state int) LocalModelState { } } +type OllamaConfig struct { + Address string `json:"address"` +} + type SimpleItem struct { Id string `json:"id"` Name string `json:"name"` diff --git a/module/system/dto/input.go b/module/system/dto/input.go index 1b7d8f35..ddb8c615 100644 --- a/module/system/dto/input.go +++ b/module/system/dto/input.go @@ -6,14 +6,24 @@ import ( ) type InputSetting struct { - InvokeAddress string `json:"invoke_address" key:"system.node.invoke_address"` - SitePrefix string `json:"site_prefix" key:"system.setting.site_prefix"` + InvokeAddress *string `json:"invoke_address" key:"system.node.invoke_address"` + SitePrefix *string `json:"site_prefix" key:"system.setting.site_prefix"` + OllamaAddress *string `json:"ollama_address" key:"system.ai_model.ollama_address"` } func (i *InputSetting) Validate() error { - _, err := url.Parse(i.InvokeAddress) - if err != nil { - return err + if i.InvokeAddress != nil { + _, err := url.Parse(*i.InvokeAddress) + if err != nil { + return err + } + } + + if i.OllamaAddress != nil { + _, err := url.Parse(*i.OllamaAddress) + if err != nil { + return err + } } return nil } @@ -31,9 +41,18 @@ func ToKeyMap(i interface{}) map[string]string { { for i := 0; i < typ.NumField(); i++ { f := typ.Field(i) - if f.Tag.Get("key") != "" { - result[f.Tag.Get("key")] = val.Field(i).String() + v := val.Field(i) + if f.Type.Kind() == reflect.Ptr { + if v.IsNil() { + continue + } + v = v.Elem() } + + if f.Tag.Get("key") != "" { + result[f.Tag.Get("key")] = v.String() + } + } } } diff --git a/module/system/dto/input_test.go b/module/system/dto/input_test.go index e33908ed..1b3838de 100644 --- a/module/system/dto/input_test.go +++ b/module/system/dto/input_test.go @@ -6,9 +6,11 @@ import ( ) func TestMap(t *testing.T) { - + invokeAddress := "http://127.0.0.1:8080" + ollamaAddress := "http://127.0.0.1:8081" input := &InputSetting{ - InvokeAddress: "http://127.0.0.1:8080", + InvokeAddress: &invokeAddress, + OllamaAddress: &ollamaAddress, } err := input.Validate() if err != nil { diff --git a/module/system/dto/output.go b/module/system/dto/output.go index d6ea63d3..2185f57a 100644 --- a/module/system/dto/output.go +++ b/module/system/dto/output.go @@ -8,6 +8,7 @@ import ( type Setting struct { InvokeAddress string `json:"invoke_address" key:"system.node.invoke_address"` SitePrefix string `json:"site_prefix" key:"system.setting.site_prefix"` + OllamaAddress string `json:"ollama_address" key:"system.ai_model.ollama_address"` } func MapStringToStruct[T any](m map[string]string) *T { diff --git a/plugins/core/ai-local.go b/plugins/core/ai-local.go index 3671ad86..3c8f3205 100644 --- a/plugins/core/ai-local.go +++ b/plugins/core/ai-local.go @@ -17,5 +17,8 @@ func (p *plugin) aiLocalApis() []pm3.Api { pm3.CreateApiWidthDoc(http.MethodPut, "/api/v1/model/local/info", []string{"context", "query:model", "body"}, nil, p.aiLocalController.Update), pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/model/local/state", []string{"context", "query:model"}, []string{"state", "info"}, p.aiLocalController.State), pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/simple/ai/models/local/configured", []string{"context"}, []string{"models"}, p.aiLocalController.SimpleList), + + pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/model/local/source/ollama", []string{"context"}, []string{"config"}, p.aiLocalController.OllamaConfig), + pm3.CreateApiWidthDoc(http.MethodPut, "/api/v1/model/local/source/ollama", []string{"context", "body"}, nil, p.aiLocalController.OllamaConfigUpdate), } }