Add support for creating online models and integrating custom model providers

This commit is contained in:
sunanzhi
2025-03-06 17:42:17 +08:00
parent 7a84c5aec3
commit b9f6abc9b3
53 changed files with 1796 additions and 33 deletions
+12
View File
@@ -0,0 +1,12 @@
package model_dto
type Model struct {
Name string `json:"name"`
AccessConfiguration string `json:"access_configuration"`
ModelParameters string `json:"model_parameters"`
}
type EditModel struct {
Id string `json:"id"`
Model
}
+6
View File
@@ -0,0 +1,6 @@
package model_dto
type SimpleModel struct {
Id string `json:"id"`
Name string `json:"name"`
}
+111
View File
@@ -0,0 +1,111 @@
package ai_model
import (
"fmt"
model_runtime "github.com/APIParkLab/APIPark/ai-provider/model-runtime"
model_dto "github.com/APIParkLab/APIPark/module/ai-model/dto"
"github.com/APIParkLab/APIPark/service/ai"
ai_model "github.com/APIParkLab/APIPark/service/ai-model"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/eolinker/go-common/store"
)
var (
_ IProviderModelModule = (*imlProviderModelModule)(nil)
)
type imlProviderModelModule struct {
providerService ai.IProviderService `autowired:""`
providerModelService ai_model.IProviderModelService `autowired:""`
transaction store.ITransaction `autowired:""`
}
func (i imlProviderModelModule) UpdateProviderModel(ctx *gin.Context, provider string, input *model_dto.EditModel) error {
p, has := model_runtime.GetProvider(provider)
if !has {
return fmt.Errorf("ai provider not found")
}
// check provider exist
providerInfo, err := i.providerService.Get(ctx, provider)
if err != nil {
return err
}
if providerInfo == nil {
return fmt.Errorf("provider not found")
}
modelInfo, _ := i.providerModelService.Get(ctx, input.Id)
if modelInfo == nil || modelInfo.Provider != provider {
return fmt.Errorf("model not found")
}
// check model name duplicate
if has := i.providerModelService.CheckNameDuplicate(ctx, provider, input.Name, input.Id); has {
return fmt.Errorf("model name: `%s` duplicate", input.Name)
}
if err := i.providerModelService.Save(ctx, input.Id, &ai_model.Model{
Name: &input.Name,
AccessConfiguration: &input.AccessConfiguration,
ModelParameters: &input.ModelParameters,
}); err != nil {
return err
}
// update provider model
iModel, _ := model_runtime.NewCustomizeModel(input.Id, input.Name, p.Logo(), input.AccessConfiguration, input.ModelParameters)
p.SetModel(input.Id, iModel)
return nil
}
func (i imlProviderModelModule) DeleteProviderModel(ctx *gin.Context, provider string, id string) error {
// check provider exist
providerInfo, err := i.providerService.Get(ctx, provider)
if err != nil {
return err
}
if providerInfo == nil {
return fmt.Errorf("provider not found")
}
modelInfo, _ := i.providerModelService.Get(ctx, id)
if modelInfo == nil || modelInfo.Provider != provider {
return fmt.Errorf("model not found")
}
return i.providerModelService.Delete(ctx, id)
}
func (i imlProviderModelModule) AddProviderModel(ctx *gin.Context, provider string, input *model_dto.Model) (*model_dto.SimpleModel, error) {
p, has := model_runtime.GetProvider(provider)
if !has {
return nil, fmt.Errorf("ai provider not found")
}
// check provider exist
providerInfo, err := i.providerService.Get(ctx, provider)
if err != nil {
return nil, err
}
if providerInfo == nil {
return nil, fmt.Errorf("provider not found")
}
// check model name duplicate
if has := i.providerModelService.CheckNameDuplicate(ctx, provider, input.Name, ""); has {
return nil, fmt.Errorf("model name: `%s` duplicate", input.Name)
}
id := uuid.New().String()
typeValue := "chat"
if err := i.providerModelService.Save(ctx, id, &ai_model.Model{
Name: &input.Name,
Type: &typeValue,
Provider: &provider,
AccessConfiguration: &input.AccessConfiguration,
ModelParameters: &input.ModelParameters,
}); err != nil {
return nil, err
}
// update provider model
iModel, _ := model_runtime.NewCustomizeModel(id, input.Name, p.Logo(), input.AccessConfiguration, input.ModelParameters)
p.SetModel(id, iModel)
return &model_dto.SimpleModel{
Id: id,
Name: input.Name,
}, nil
}
+22
View File
@@ -0,0 +1,22 @@
package ai_model
import (
model_dto "github.com/APIParkLab/APIPark/module/ai-model/dto"
"github.com/gin-gonic/gin"
"reflect"
"github.com/eolinker/go-common/autowire"
)
type IProviderModelModule interface {
AddProviderModel(ctx *gin.Context, provider string, input *model_dto.Model) (*model_dto.SimpleModel, error)
UpdateProviderModel(ctx *gin.Context, provider string, input *model_dto.EditModel) error
DeleteProviderModel(ctx *gin.Context, provider string, id string) error
}
func init() {
autowire.Auto[IProviderModelModule](func() reflect.Value {
module := new(imlProviderModelModule)
return reflect.ValueOf(module)
})
}