mirror of
https://github.com/APIParkLab/APIPark.git
synced 2026-06-26 16:01:56 +08:00
Add support for creating online models and integrating custom model providers
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package ai_model
|
||||
|
||||
import (
|
||||
model_dto "github.com/APIParkLab/APIPark/module/ai-model/dto"
|
||||
"github.com/eolinker/go-common/autowire"
|
||||
"github.com/gin-gonic/gin"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type IProviderModelController 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[IProviderModelController](func() reflect.Value {
|
||||
return reflect.ValueOf(&imlProviderModelController{})
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package ai_model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
ai_model "github.com/APIParkLab/APIPark/module/ai-model"
|
||||
model_dto "github.com/APIParkLab/APIPark/module/ai-model/dto"
|
||||
"github.com/gin-gonic/gin"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
_ IProviderModelController = (*imlProviderModelController)(nil)
|
||||
)
|
||||
|
||||
type imlProviderModelController struct {
|
||||
module ai_model.IProviderModelModule `autowired:""`
|
||||
}
|
||||
|
||||
func (i *imlProviderModelController) UpdateProviderModel(ctx *gin.Context, provider string, input *model_dto.EditModel) error {
|
||||
if strings.TrimSpace(input.Name) == "" {
|
||||
return fmt.Errorf("name is empty")
|
||||
}
|
||||
if strings.TrimSpace(input.Id) == "" {
|
||||
return fmt.Errorf("id is empty")
|
||||
}
|
||||
if strings.TrimSpace(provider) == "" {
|
||||
return fmt.Errorf("provider is empty")
|
||||
}
|
||||
// check access config & model parameter is json format
|
||||
if strings.TrimSpace(input.AccessConfiguration) != "" && !json.Valid([]byte(input.AccessConfiguration)) {
|
||||
return fmt.Errorf("access configuration is not json format")
|
||||
}
|
||||
if strings.TrimSpace(input.ModelParameters) != "" && !json.Valid([]byte(input.ModelParameters)) {
|
||||
return fmt.Errorf("model parameters is not json format")
|
||||
}
|
||||
|
||||
return i.module.UpdateProviderModel(ctx, provider, input)
|
||||
}
|
||||
|
||||
func (i *imlProviderModelController) DeleteProviderModel(ctx *gin.Context, provider string, id string) error {
|
||||
if strings.TrimSpace(id) == "" {
|
||||
return fmt.Errorf("id is empty")
|
||||
}
|
||||
if strings.TrimSpace(provider) == "" {
|
||||
return fmt.Errorf("provider is empty")
|
||||
}
|
||||
|
||||
return i.module.DeleteProviderModel(ctx, provider, id)
|
||||
}
|
||||
|
||||
func (i *imlProviderModelController) AddProviderModel(ctx *gin.Context, provider string, input *model_dto.Model) (*model_dto.SimpleModel, error) {
|
||||
if strings.TrimSpace(input.Name) == "" {
|
||||
return nil, fmt.Errorf("name is empty")
|
||||
}
|
||||
if strings.TrimSpace(provider) == "" {
|
||||
return nil, fmt.Errorf("provider is empty")
|
||||
}
|
||||
// check access config & model parameter is json format
|
||||
if strings.TrimSpace(input.AccessConfiguration) != "" && !json.Valid([]byte(input.AccessConfiguration)) {
|
||||
return nil, fmt.Errorf("access configuration is not json format")
|
||||
}
|
||||
if strings.TrimSpace(input.ModelParameters) != "" && !json.Valid([]byte(input.ModelParameters)) {
|
||||
return nil, fmt.Errorf("model parameters is not json format")
|
||||
}
|
||||
return i.module.AddProviderModel(ctx, provider, input)
|
||||
}
|
||||
Reference in New Issue
Block a user