(ctx context.Context, input *ai_balance_dto.Create)
| 52 | } |
| 53 | |
| 54 | func (i *imlBalanceModule) Create(ctx context.Context, input *ai_balance_dto.Create) error { |
| 55 | has, err := i.balanceService.Exist(ctx, input.Provider, input.Model) |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | if has { |
| 60 | return fmt.Errorf("model already exists") |
| 61 | } |
| 62 | priority, err := i.balanceService.MaxPriority(ctx) |
| 63 | if err != nil { |
| 64 | if !errors.Is(err, gorm.ErrRecordNotFound) { |
| 65 | return err |
| 66 | } |
| 67 | priority = 0 |
| 68 | } |
| 69 | if input.Id == "" { |
| 70 | input.Id = uuid.NewString() |
| 71 | } |
| 72 | providerName := "" |
| 73 | modelName := "" |
| 74 | base := "" |
| 75 | switch input.Type { |
| 76 | case ai_balance_dto.ModelTypeOnline: |
| 77 | p, has := model_runtime.GetProvider(input.Provider) |
| 78 | if !has { |
| 79 | return fmt.Errorf("provider not found") |
| 80 | } |
| 81 | providerName = p.Name() |
| 82 | modelName = input.Model |
| 83 | base = fmt.Sprintf("%s://%s", p.URI().Scheme(), p.URI().Host()) |
| 84 | case ai_balance_dto.ModelTypeLocal: |
| 85 | input.Provider = ai_provider_local.ProviderLocal |
| 86 | providerName = ai_provider_local.ProviderLocal |
| 87 | modelName = input.Model |
| 88 | v, has := i.settingService.Get(ctx, "system.ai_model.ollama_address") |
| 89 | if !has { |
| 90 | return fmt.Errorf("ollama address not found") |
| 91 | } |
| 92 | base = v |
| 93 | } |
| 94 | |
| 95 | return i.transaction.Transaction(ctx, func(ctx context.Context) error { |
| 96 | err = i.balanceService.Create(ctx, &ai_balance.Create{ |
| 97 | Id: input.Id, |
| 98 | Priority: priority + 1, |
| 99 | Provider: input.Provider, |
| 100 | ProviderName: providerName, |
| 101 | Model: input.Model, |
| 102 | ModelName: modelName, |
| 103 | Type: ai_balance_dto.ModelType(input.Type).Int(), |
| 104 | }) |
| 105 | if err != nil { |
| 106 | return err |
| 107 | } |
| 108 | item, err := i.balanceService.Get(ctx, input.Id) |
| 109 | if err != nil { |
| 110 | return err |
| 111 | } |
nothing calls this directly
no test coverage detected