MCPcopy Index your code
hub / github.com/APIParkLab/APIPark / Create

Method Create

module/ai-balance/iml.go:54–115  ·  view source on GitHub ↗
(ctx context.Context, input *ai_balance_dto.Create)

Source from the content-addressed store, hash-verified

52}
53
54func (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 }

Callers

nothing calls this directly

Calls 12

syncGatewayMethod · 0.95
newReleaseFunction · 0.85
ExistMethod · 0.65
MaxPriorityMethod · 0.65
NameMethod · 0.65
SchemeMethod · 0.65
URIMethod · 0.65
HostMethod · 0.65
GetMethod · 0.65
CreateMethod · 0.65
ModelTypeMethod · 0.65
IntMethod · 0.45

Tested by

no test coverage detected