From 690c2fe2f7348f77a232a6d564fb38f2e3598a35 Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Wed, 19 Feb 2025 10:57:39 +0800 Subject: [PATCH] Fix: Issue where the service status is not updated after the local model download is completed --- ai-provider/local/executor.go | 12 +++++ ai-provider/local/local.go | 17 ++----- ai-provider/local/ollama.go | 1 - controller/system/iml.go | 2 +- module/ai-balance/iml.go | 25 ++++++---- module/ai-local/iml.go | 92 +++++++++++++++++------------------ module/system/iml.go | 20 ++++++++ 7 files changed, 97 insertions(+), 72 deletions(-) diff --git a/ai-provider/local/executor.go b/ai-provider/local/executor.go index b6648d23..56959ceb 100644 --- a/ai-provider/local/executor.go +++ b/ai-provider/local/executor.go @@ -210,6 +210,9 @@ func (e *AsyncExecutor) DistributeToModelPipelines(model string, msg PullMessage type PullCallback func(msg PullMessage) error func PullModel(model string, id string, fn PullCallback) (*Pipeline, error) { + if client == nil { + return nil, fmt.Errorf("client not initialized") + } mp, has := taskExecutor.GetModelPipeline(model) if !has { mp = newModelPipeline(taskExecutor.ctx, 100) @@ -279,6 +282,9 @@ func PullModel(model string, id string, fn PullCallback) (*Pipeline, error) { } func StopPull(model string) { + if client == nil { + return + } taskExecutor.CloseModelPipeline(model) } @@ -287,6 +293,9 @@ func CancelPipeline(model string, id string) { } func RemoveModel(model string) error { + if client == nil { + return fmt.Errorf("client not initialized") + } taskExecutor.CloseModelPipeline(model) err := client.Delete(context.Background(), &api.DeleteRequest{Model: model}) if err != nil { @@ -298,6 +307,9 @@ func RemoveModel(model string) error { } func ModelsInstalled() ([]Model, error) { + if client == nil { + return nil, fmt.Errorf("client not initialized") + } result, err := client.List(context.Background()) if err != nil { return nil, err diff --git a/ai-provider/local/local.go b/ai-provider/local/local.go index 93659245..5e084eeb 100644 --- a/ai-provider/local/local.go +++ b/ai-provider/local/local.go @@ -4,27 +4,18 @@ import ( "net/http" "net/url" - "github.com/eolinker/eosc/env" "github.com/ollama/ollama/api" ) var ( - ollamaAddress = "http://127.0.0.1:11434" - EnvOllamaAddress = "OLLAMA_ADDRESS" - client *api.Client + client *api.Client ) -func init() { - address, has := env.GetEnv(EnvOllamaAddress) - if !has { - address = ollamaAddress - } +func ResetOllamaAddress(address string) error { u, err := url.Parse(address) if err != nil { - u, err = url.Parse(ollamaAddress) - if err != nil { - panic(err) - } + return err } client = api.NewClient(u, http.DefaultClient) + return nil } diff --git a/ai-provider/local/ollama.go b/ai-provider/local/ollama.go index 31660541..be765848 100644 --- a/ai-provider/local/ollama.go +++ b/ai-provider/local/ollama.go @@ -1,7 +1,6 @@ package ai_provider_local var ( - OllamaBase = "http://apipark-ollama:11434" OllamaConfig = "{\n \"mirostat\": 0,\n \"mirostat_eta\": 0.1,\n \"mirostat_tau\": 5.0,\n \"num_ctx\": 4096,\n \"repeat_last_n\":64,\n \"repeat_penalty\": 1.1,\n \"temperature\": 0.7,\n \"seed\": 42,\n \"num_predict\": 42,\n \"top_k\": 40,\n \"top_p\": 0.9,\n \"min_p\": 0.5\n}\n" OllamaSvg = ` 0 { return fmt.Errorf("model %s has api, can not remove", model) } + info, err := i.localModelService.Get(ctx, model) + if err != nil { + if !errors.Is(err, gorm.ErrRecordNotFound) { + return err + } + return ai_provider_local.RemoveModel(model) + } + if info.State == ai_local_dto.LocalModelStateDeploying.Int() { + return fmt.Errorf("model %s is deploying, can not remove", model) + } return i.transaction.Transaction(ctx, func(txCtx context.Context) error { err = i.localModelService.Delete(ctx, model) if err != nil { @@ -528,13 +520,17 @@ func (i *imlLocalModel) getLocalModels(ctx context.Context) ([]*gateway.DynamicR if err != nil { return nil, err } + v, has := i.settingService.Get(ctx, "system.ai_model.ollama_address") + if !has { + return nil, errors.New("ollama_address not set") + } releases := make([]*gateway.DynamicRelease, 0, len(list)) for _, l := range list { cfg := make(map[string]interface{}) cfg["provider"] = "ollama" cfg["model"] = l.Id cfg["model_config"] = ai_provider_local.OllamaSvg - cfg["base"] = ollamaBase + cfg["base"] = v releases = append(releases, &gateway.DynamicRelease{ BasicItem: &gateway.BasicItem{ ID: l.Id, diff --git a/module/system/iml.go b/module/system/iml.go index b83cefe5..b74a231b 100644 --- a/module/system/iml.go +++ b/module/system/iml.go @@ -3,6 +3,11 @@ package system import ( "context" + "github.com/eolinker/go-common/server" + + ai_provider_local "github.com/APIParkLab/APIPark/ai-provider/local" + "github.com/eolinker/go-common/register" + "github.com/eolinker/go-common/store" "github.com/eolinker/go-common/utils" @@ -43,6 +48,21 @@ func (i *imlSettingModule) Set(ctx context.Context, input *system_dto.InputSetti return err } } + if input.OllamaAddress != nil { + ai_provider_local.ResetOllamaAddress(*input.OllamaAddress) + } return nil }) } + +func (i *imlSettingModule) OnInit() { + register.Handle(func(v server.Server) { + ctx := context.Background() + + address, has := i.settingService.Get(ctx, "system.ai_model.ollama_address") + if has { + ai_provider_local.ResetOllamaAddress(address) + } + + }) +}