Merge pull request #205 from APIParkLab/feature/1.5-local-model

Feature/1.5 local model
This commit is contained in:
Dot.L
2025-02-18 10:24:34 +08:00
committed by GitHub
15 changed files with 147 additions and 87 deletions
+1
View File
@@ -10,6 +10,7 @@ variables:
VIEW_ADDR: http://172.18.166.219:8288
SAVE_DIR: /opt/${APP}
NODE_OPTIONS: --max_old_space_size=8192
APIPARK_OLLAMA_BASE: http://127.0.0.1:11434
stages:
- notice
+44 -39
View File
@@ -9,11 +9,17 @@ import (
"net/http"
"strings"
"github.com/APIParkLab/APIPark/module/router"
"github.com/APIParkLab/APIPark/module/subscribe"
subscribe_dto "github.com/APIParkLab/APIPark/module/subscribe/dto"
"github.com/APIParkLab/APIPark/model/plugin_model"
"github.com/APIParkLab/APIPark/service/api"
ai_api_dto "github.com/APIParkLab/APIPark/module/ai-api/dto"
router_dto "github.com/APIParkLab/APIPark/module/router/dto"
"github.com/APIParkLab/APIPark/module/router"
ai_api "github.com/APIParkLab/APIPark/module/ai-api"
"github.com/APIParkLab/APIPark/module/catalogue"
@@ -24,9 +30,6 @@ import (
service_dto "github.com/APIParkLab/APIPark/module/service/dto"
ai_api_dto "github.com/APIParkLab/APIPark/module/ai-api/dto"
router_dto "github.com/APIParkLab/APIPark/module/router/dto"
ai_provider_local "github.com/APIParkLab/APIPark/ai-provider/local"
"github.com/eolinker/eosc/log"
@@ -44,7 +47,9 @@ type imlLocalModelController struct {
serviceModule service.IServiceModule `autowired:""`
catalogueModule catalogue.ICatalogueModule `autowired:""`
aiAPIModule ai_api.IAPIModule `autowired:""`
appModule service.IAppModule `autowired:""`
routerModule router.IRouterModule `autowired:""`
subscribeModule subscribe.ISubscribeModule `autowired:""`
docModule service.IServiceDocModule `autowired:""`
transaction store.ITransaction `autowired:""`
}
@@ -91,15 +96,8 @@ func (i *imlLocalModelController) Deploy(ctx *gin.Context) {
"code": -1, "msg": "model is required", "success": "fail",
})
return
}
//err = i.initAILocalService(ctx, input.Model, input.Team)
//if err != nil {
// ctx.JSON(200, gin.H{
// "code": -1, "msg": err.Error(), "success": "fail",
// })
// return
//}
id := uuid.NewString()
p, err := i.module.Deploy(ctx, input.Model, id)
if err != nil {
@@ -113,10 +111,11 @@ func (i *imlLocalModelController) Deploy(ctx *gin.Context) {
go func() {
select {
case <-ctx.Writer.CloseNotify():
log.Info("client closed connection,close pipeline")
ai_provider_local.CancelPipeline(input.Model, id)
case <-done:
}
ai_provider_local.CancelPipeline(input.Model, id)
}()
var complete int64
var total int64
@@ -177,12 +176,12 @@ func (i *imlLocalModelController) Deploy(ctx *gin.Context) {
}
func (i *imlLocalModelController) DeployStart(ctx *gin.Context, input *ai_local_dto.DeployInput) error {
err := i.initAILocalService(ctx, input.Model, input.Team)
fn, err := i.initAILocalService(ctx, input.Model, input.Team)
if err != nil {
return err
}
id := uuid.NewString()
_, err = i.module.Deploy(ctx, input.Model, id)
_, err = i.module.Deploy(ctx, input.Model, id, fn)
if err != nil {
return err
}
@@ -190,16 +189,16 @@ func (i *imlLocalModelController) DeployStart(ctx *gin.Context, input *ai_local_
return nil
}
func (i *imlLocalModelController) initAILocalService(ctx context.Context, model string, teamID string) error {
err := i.transaction.Transaction(ctx, func(ctx context.Context) error {
catalogueInfo, err := i.catalogueModule.DefaultCatalogue(ctx)
if err != nil {
return err
}
serviceId := uuid.NewString()
prefix := fmt.Sprintf("/%s", serviceId[:8])
providerId := "ollama"
info, err := i.serviceModule.Create(ctx, teamID, &service_dto.CreateService{
func (i *imlLocalModelController) initAILocalService(ctx context.Context, model string, teamID string) (func() error, error) {
catalogueInfo, err := i.catalogueModule.DefaultCatalogue(ctx)
if err != nil {
return nil, err
}
serviceId := uuid.NewString()
prefix := fmt.Sprintf("/%s", serviceId[:8])
providerId := "ollama"
err = i.transaction.Transaction(ctx, func(ctx context.Context) error {
_, err = i.serviceModule.Create(ctx, teamID, &service_dto.CreateService{
Id: serviceId,
Name: model,
Prefix: prefix,
@@ -214,10 +213,10 @@ func (i *imlLocalModelController) initAILocalService(ctx context.Context, model
if err != nil {
return err
}
err = i.module.SaveCache(ctx, model, serviceId)
if err != nil {
return err
}
return i.module.SaveCache(ctx, model, serviceId)
})
return func() error {
path := fmt.Sprintf("/%s/chat", strings.Trim(prefix, "/"))
timeout := 300000
retry := 0
@@ -232,11 +231,11 @@ func (i *imlLocalModelController) initAILocalService(ctx context.Context, model
Type: "local",
}
name := "Demo AI API"
description := "A demo that shows you how to use a e a Chat API."
description := "This is a demo that shows you how to use a Chat API."
apiId := uuid.NewString()
err = i.aiAPIModule.Create(
ctx,
info.Id,
serviceId,
&ai_api_dto.CreateAPI{
Id: apiId,
Name: name,
@@ -262,11 +261,11 @@ func (i *imlLocalModelController) initAILocalService(ctx context.Context, model
plugins["ai_formatter"] = api.PluginSetting{
Config: plugin_model.ConfigType{
"model": aiModel.Id,
"provider": info.Provider.Id,
"provider": providerId,
"config": aiModel.Config,
},
}
_, err = i.routerModule.Create(ctx, info.Id, &router_dto.Create{
_, err = i.routerModule.Create(ctx, serviceId, &router_dto.Create{
Id: apiId,
Name: name,
Path: path,
@@ -287,13 +286,19 @@ func (i *imlLocalModelController) initAILocalService(ctx context.Context, model
if err != nil {
return err
}
return i.docModule.SaveServiceDoc(ctx, info.Id, &service_dto.SaveServiceDoc{
apps, err := i.appModule.Search(ctx, teamID, "")
if err != nil {
return err
}
for _, app := range apps {
i.subscribeModule.AddSubscriber(ctx, serviceId, &subscribe_dto.AddSubscriber{
Application: app.Id,
})
}
return i.docModule.SaveServiceDoc(ctx, serviceId, &service_dto.SaveServiceDoc{
Doc: "",
})
})
return err
}, err
}
func (i *imlLocalModelController) Search(ctx *gin.Context, keyword string) ([]*ai_local_dto.LocalModelItem, error) {
+1 -1
View File
@@ -386,7 +386,7 @@ func (i *imlServiceController) createAIService(ctx *gin.Context, teamID string,
Type: modelType,
}
name := "Demo AI API "
description := "A demo that shows you how to use Chat API."
description := "This is a demo that shows you how to use a Chat API."
apiId := uuid.New().String()
err = i.aiAPIModule.Create(
ctx,
+2 -3
View File
@@ -4,8 +4,6 @@ go 1.23.4
toolchain go1.23.6
//toolchain go1.21.1
require (
github.com/eolinker/ap-account v1.0.15
github.com/eolinker/eosc v0.18.3
@@ -83,6 +81,7 @@ require (
// github.com/eolinker/eosc => ../../eolinker/eosc
//)
//replace github.com/eolinker/ap-account => ../aoaccount
//replace github.com/eolinker/ap-account => ../../eolinker/ap-account
//
//replace github.com/eolinker/go-common => ../../eolinker/go-common
+6 -2
View File
@@ -72,9 +72,13 @@ func genResponse() *openapi3.ResponseRef {
func genRequestBodySchema(variables []*ai_api_dto.AiPromptVariable) *openapi3.Schema {
result := openapi3.NewObjectSchema()
result.WithProperty("variables", genVariableSchema(variables))
if len(variables) > 0 {
result.WithProperty("variables", genVariableSchema(variables))
result.WithRequired([]string{"variables", "messages"})
}
result.WithPropertyRef("messages", messagesSchemaRef)
result.WithRequired([]string{"variables", "messages"})
return result
}
+3 -2
View File
@@ -9,6 +9,7 @@ type CancelDeploy struct {
}
type DeployInput struct {
Model string `json:"model"`
Team string `json:"team"`
Model string `json:"model"`
Service string `json:"service"`
Team string `json:"team"`
}
+63 -30
View File
@@ -130,37 +130,44 @@ func (i *imlLocalModel) Search(ctx context.Context, keyword string) ([]*ai_local
}
func (i *imlLocalModel) ListCanInstall(ctx context.Context, keyword string) ([]*ai_local_dto.LocalModelPackageItem, error) {
list, err := i.localModelPackageService.Search(ctx, keyword, nil)
if err != nil {
return nil, err
}
if keyword != "" {
if keyword == "" {
list, err := i.localModelPackageService.Search(ctx, keyword, nil)
if err != nil {
return nil, err
}
return utils.SliceToSlice(list, func(s *ai_local.LocalModelPackage) *ai_local_dto.LocalModelPackageItem {
return &ai_local_dto.LocalModelPackageItem{
Id: s.Id,
Name: s.Name,
Size: s.Size,
IsPopular: s.IsPopular,
}
}), nil
} else {
info, err := i.localModelPackageService.Get(ctx, keyword)
if err != nil {
return nil, err
}
result := make([]*ai_local_dto.LocalModelPackageItem, 0)
for _, v := range list {
models := ai_provider_local.ModelsCanInstallById(v.Id)
for _, model := range models {
result = append(result, &ai_local_dto.LocalModelPackageItem{
Id: model.Id,
Name: model.Name,
Size: model.Size,
IsPopular: model.IsPopular,
})
}
//for _, v := range list {
models := ai_provider_local.ModelsCanInstallById(info.Id)
for _, model := range models {
result = append(result, &ai_local_dto.LocalModelPackageItem{
Id: model.Id,
Name: model.Name,
Size: model.Size,
IsPopular: model.IsPopular,
})
}
//}
return result, nil
}
return utils.SliceToSlice(list, func(s *ai_local.LocalModelPackage) *ai_local_dto.LocalModelPackageItem {
return &ai_local_dto.LocalModelPackageItem{
Id: s.Id,
Name: s.Name,
Size: s.Size,
IsPopular: s.IsPopular,
}
}), nil
}
func (i *imlLocalModel) pullHook() func(msg ai_provider_local.PullMessage) error {
func (i *imlLocalModel) pullHook(fn ...func() error) func(msg ai_provider_local.PullMessage) error {
return func(msg ai_provider_local.PullMessage) error {
return i.transaction.Transaction(context.Background(), func(ctx context.Context) error {
@@ -233,6 +240,12 @@ func (i *imlLocalModel) pullHook() func(msg ai_provider_local.PullMessage) error
return err
}
if state == ai_local_dto.DeployStateFinish.Int() {
for _, f := range fn {
err = f()
if err != nil {
return err
}
}
cfg := make(map[string]interface{})
cfg["provider"] = "ollama"
cfg["model"] = msg.Model
@@ -289,10 +302,19 @@ func (i *imlLocalModel) syncGateway(ctx context.Context, clusterId string, relea
return nil
}
func (i *imlLocalModel) Deploy(ctx context.Context, model string, session string) (*ai_provider_local.Pipeline, error) {
func (i *imlLocalModel) Deploy(ctx context.Context, model string, session string, fn ...func() error) (*ai_provider_local.Pipeline, error) {
var p *ai_provider_local.Pipeline
err := i.transaction.Transaction(ctx, func(txCtx context.Context) error {
_, err := i.localModelService.Get(ctx, model)
item, err := i.localModelCacheService.GetByTarget(ctx, ai_local.CacheTypeService, model)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
} else {
model = item.Model
}
info, err := i.localModelService.Get(ctx, model)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return err
@@ -305,13 +327,15 @@ func (i *imlLocalModel) Deploy(ctx context.Context, model string, session string
})
} else {
state := ai_local_dto.LocalModelStateDeploying.Int()
err = i.localModelService.Save(ctx, model, &ai_local.EditLocalModel{State: &state})
if info.State == ai_local_dto.LocalModelStateDeployingError.Int() {
state := ai_local_dto.LocalModelStateDeploying.Int()
err = i.localModelService.Save(ctx, model, &ai_local.EditLocalModel{State: &state})
}
}
if err != nil {
return err
}
p, err = ai_provider_local.PullModel(model, session, i.pullHook())
p, err = ai_provider_local.PullModel(model, session, i.pullHook(fn...))
if err != nil {
return err
}
@@ -330,6 +354,15 @@ func (i *imlLocalModel) SaveCache(ctx context.Context, model string, target stri
func (i *imlLocalModel) CancelDeploy(ctx context.Context, model string) error {
return i.transaction.Transaction(ctx, func(txCtx context.Context) error {
item, err := i.localModelCacheService.GetByTarget(ctx, ai_local.CacheTypeService, model)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
} else {
model = item.Model
}
list, err := i.localModelCacheService.List(ctx, model, ai_local.CacheTypeService)
if err != nil {
return err
@@ -428,6 +461,7 @@ func (i *imlLocalModel) OnInit() {
})
models, version := ai_provider_local.ModelsCanInstall()
for _, model := range models {
delete(oldModels, model.Id)
if v, ok := oldModels[model.Id]; ok {
if v.Version == version {
continue
@@ -456,7 +490,6 @@ func (i *imlLocalModel) OnInit() {
return
}
}
delete(oldModels, model.Id)
}
for id := range oldModels {
err = i.localModelPackageService.Delete(ctx, id)
+1 -1
View File
@@ -16,7 +16,7 @@ import (
type ILocalModelModule interface {
Search(ctx context.Context, keyword string) ([]*ai_local_dto.LocalModelItem, error)
ListCanInstall(ctx context.Context, keyword string) ([]*ai_local_dto.LocalModelPackageItem, error)
Deploy(ctx context.Context, model string, session string) (*ai_provider_local.Pipeline, error)
Deploy(ctx context.Context, model string, session string, fn ...func() error) (*ai_provider_local.Pipeline, error)
CancelDeploy(ctx context.Context, model string) error
RemoveModel(ctx context.Context, model string) error
Enable(ctx context.Context, model string) error
+4 -4
View File
@@ -125,7 +125,7 @@ func (i *imlServiceModule) searchMyServices(ctx context.Context, teamId string,
return nil, err
}
condition["team"] = teamId
return i.serviceService.Search(ctx, keyword, condition, "update_at desc")
return i.serviceService.Search(ctx, keyword, condition, "create_at desc")
} else {
membersForUser, err := i.teamMemberService.FilterMembersForUser(ctx, userID)
if err != nil {
@@ -133,7 +133,7 @@ func (i *imlServiceModule) searchMyServices(ctx context.Context, teamId string,
}
teamIds := membersForUser[userID]
condition["team"] = teamIds
return i.serviceService.Search(ctx, keyword, condition, "update_at desc")
return i.serviceService.Search(ctx, keyword, condition, "create_at desc")
}
}
@@ -235,9 +235,9 @@ func (i *imlServiceModule) Search(ctx context.Context, teamID string, keyword st
if err != nil {
return nil, err
}
list, err = i.serviceService.Search(ctx, keyword, map[string]interface{}{"team": teamID, "as_server": true}, "update_at desc")
list, err = i.serviceService.Search(ctx, keyword, map[string]interface{}{"team": teamID, "as_server": true}, "create_at desc")
} else {
list, err = i.serviceService.Search(ctx, keyword, map[string]interface{}{"as_server": true}, "update_at desc")
list, err = i.serviceService.Search(ctx, keyword, map[string]interface{}{"as_server": true}, "create_at desc")
}
if err != nil {
return nil, err
+16
View File
@@ -66,6 +66,10 @@ func (i *imlLocalModelService) fromEntity(e *ai.LocalModel) *LocalModel {
}
}
var (
_ ILocalModelPackageService = &imlLocalModelPackageService{}
)
type imlLocalModelPackageService struct {
store ai.ILocalModelPackageStore `autowired:""`
universally.IServiceGet[LocalModelPackage]
@@ -197,6 +201,18 @@ type imlLocalModelCacheService struct {
store ai.ILocalModelCacheStore `autowired:""`
}
func (i *imlLocalModelCacheService) GetByTarget(ctx context.Context, typ CacheType, target string) (*LocalModelCache, error) {
item, err := i.store.First(ctx, map[string]interface{}{"target": target, "type": typ.Int()})
if err != nil {
return nil, err
}
return &LocalModelCache{
Model: item.Model,
Target: item.Target,
Type: CacheType(item.Type),
}, nil
}
func (i *imlLocalModelCacheService) List(ctx context.Context, model string, typ CacheType) ([]*LocalModelCache, error) {
list, err := i.store.List(ctx, map[string]interface{}{"model": model, "type": typ.Int()})
if err != nil {
+2
View File
@@ -20,6 +20,7 @@ type ILocalModelPackageService interface {
universally.IServiceCreate[CreateLocalModelPackage]
universally.IServiceEdit[EditLocalModelPackage]
universally.IServiceDelete
//SearchByModel(ctx context.Context, model string) ([]*LocalModelPackage, error)
}
type ILocalModelInstallStateService interface {
@@ -33,6 +34,7 @@ type ILocalModelCacheService interface {
List(ctx context.Context, model string, typ CacheType) ([]*LocalModelCache, error)
Delete(ctx context.Context, model string) error
Save(ctx context.Context, model string, typ CacheType, target string) error
GetByTarget(ctx context.Context, typ CacheType, target string) (*LocalModelCache, error)
}
func init() {
+1 -2
View File
@@ -42,10 +42,9 @@ type imlAPIService struct {
}
func (i *imlAPIService) DeleteByService(ctx context.Context, serviceId string) error {
_, err := i.store.DeleteWhere(ctx, map[string]interface{}{
return i.store.SoftDelete(ctx, map[string]interface{}{
"service": serviceId,
})
return err
}
func (i *imlAPIService) ListForServices(ctx context.Context, serviceIds ...string) ([]*API, error) {
+1 -1
View File
@@ -48,7 +48,7 @@ func FromEntity(e *api.API) *API {
return &API{
UUID: e.UUID,
CreateAt: e.CreateAt,
IsDelete: e.IsDelete != 0,
IsDelete: e.IsDelete,
Service: e.Service,
Team: e.Team,
Creator: e.Creator,
+1 -1
View File
@@ -12,10 +12,10 @@ type API struct {
Upstream string `gorm:"size:36;not null;column:upstream;comment:上游;index:upstream"` // 上游ID
Creator string `gorm:"size:36;not null;column:creator;comment:创建人;index:creator" aovalue:"creator"` // 创建人
CreateAt time.Time `gorm:"type:timestamp;NOT NULL;DEFAULT:CURRENT_TIMESTAMP;column:create_at;comment:创建时间"`
IsDelete int `gorm:"type:tinyint(1);not null;column:is_delete;comment:是否删除 0:未删除 1:已删除"`
Method []string `gorm:"size:255;not null;column:method;comment:请求方法;serializer:json"`
Protocol []string `gorm:"type:text;not null;column:protocol;comment:协议;serializer:json"`
Path string `gorm:"size:255;not null;column:path;comment:请求路径"`
IsDelete bool `gorm:"type:tinyint(1);not null;column:is_delete;comment:是否删除 0:未删除 1:已删除"`
}
type Info struct {
Id int64 `gorm:"column:id;type:BIGINT(20);NOT NULL;comment:id;primary_key;comment:主键ID;"`
+1 -1
View File
@@ -15,7 +15,7 @@ type Service struct {
Catalogue string `gorm:"type:text;not null;column:catalogue;comment:目录"`
CreateAt time.Time `gorm:"type:timestamp;NOT NULL;DEFAULT:CURRENT_TIMESTAMP;column:create_at;comment:创建时间"`
UpdateAt time.Time `gorm:"type:timestamp;NOT NULL;DEFAULT:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;column:update_at;comment:修改时间"`
IsDelete int `gorm:"type:tinyint(1);not null;column:is_delete;comment:是否删除"`
IsDelete bool `gorm:"type:tinyint(1);not null;column:is_delete;comment:是否删除"`
Kind int `gorm:"type:tinyint(4);not null;column:kind;comment:服务种类,0:Rest服务,1:AI服务"`
State int `gorm:"type:tinyint(4);not null;column:state;comment:状态"`
AdditionalConfig string `gorm:"type:text;not null;column:additional_config;comment:额外配置"`