mirror of
https://github.com/APIParkLab/APIPark.git
synced 2026-06-04 10:13:53 +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)
|
||||
}
|
||||
@@ -22,6 +22,7 @@ type IProviderController interface {
|
||||
UpdateProviderDefaultLLM(ctx *gin.Context, id string, input *ai_dto.UpdateLLM) error
|
||||
Delete(ctx *gin.Context, id string) error
|
||||
//Sort(ctx *gin.Context, input *ai_dto.Sort) error
|
||||
AddProvider(ctx *gin.Context, input *ai_dto.NewProvider) (*ai_dto.SimpleProvider, error)
|
||||
}
|
||||
|
||||
type IStatisticController interface {
|
||||
|
||||
@@ -2,7 +2,9 @@ package ai
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/APIParkLab/APIPark/module/ai"
|
||||
ai_dto "github.com/APIParkLab/APIPark/module/ai/dto"
|
||||
@@ -21,6 +23,13 @@ func (i *imlProviderController) Delete(ctx *gin.Context, id string) error {
|
||||
return i.module.Delete(ctx, id)
|
||||
}
|
||||
|
||||
func (i *imlProviderController) AddProvider(ctx *gin.Context, input *ai_dto.NewProvider) (*ai_dto.SimpleProvider, error) {
|
||||
if strings.TrimSpace(input.Name) == "" {
|
||||
return nil, fmt.Errorf("name is empty")
|
||||
}
|
||||
return i.module.AddProvider(ctx, input)
|
||||
}
|
||||
|
||||
//func (i *imlProviderController) Sort(ctx *gin.Context, input *ai_dto.Sort) error {
|
||||
// return i.module.Sort(ctx, input)
|
||||
//}
|
||||
@@ -67,6 +76,9 @@ func (i *imlProviderController) Disable(ctx *gin.Context, id string) error {
|
||||
}
|
||||
|
||||
func (i *imlProviderController) UpdateProviderConfig(ctx *gin.Context, id string, input *ai_dto.UpdateConfig) error {
|
||||
if strings.TrimSpace(id) == "" {
|
||||
return fmt.Errorf("id is empty")
|
||||
}
|
||||
return i.module.UpdateProviderConfig(ctx, id, input)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user