mirror of
https://github.com/APIParkLab/APIPark.git
synced 2026-06-14 20:41:15 +08:00
Merge branch 'feature/1.5-local-model' into 'main'
Feature/1.5 local model See merge request apipark/APIPark!213
This commit is contained in:
@@ -18,6 +18,8 @@ type ILocalModelController interface {
|
||||
Update(ctx *gin.Context, model string, input *ai_local_dto.Update) error
|
||||
State(ctx *gin.Context, model string) (*ai_local_dto.DeployState, *ai_local_dto.ModelInfo, error)
|
||||
SimpleList(ctx *gin.Context) ([]*ai_local_dto.SimpleItem, error)
|
||||
OllamaConfig(ctx *gin.Context) (*ai_local_dto.OllamaConfig, error)
|
||||
OllamaConfigUpdate(ctx *gin.Context, input *ai_local_dto.OllamaConfig) error
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -9,6 +9,10 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
system_dto "github.com/APIParkLab/APIPark/module/system/dto"
|
||||
|
||||
"github.com/APIParkLab/APIPark/module/system"
|
||||
|
||||
"github.com/APIParkLab/APIPark/module/subscribe"
|
||||
subscribe_dto "github.com/APIParkLab/APIPark/module/subscribe/dto"
|
||||
|
||||
@@ -51,9 +55,23 @@ type imlLocalModelController struct {
|
||||
routerModule router.IRouterModule `autowired:""`
|
||||
subscribeModule subscribe.ISubscribeModule `autowired:""`
|
||||
docModule service.IServiceDocModule `autowired:""`
|
||||
settingModule system.ISettingModule `autowired:""`
|
||||
transaction store.ITransaction `autowired:""`
|
||||
}
|
||||
|
||||
func (i *imlLocalModelController) OllamaConfig(ctx *gin.Context) (*ai_local_dto.OllamaConfig, error) {
|
||||
cfg := i.settingModule.Get(ctx)
|
||||
return &ai_local_dto.OllamaConfig{
|
||||
Address: cfg.OllamaAddress,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (i *imlLocalModelController) OllamaConfigUpdate(ctx *gin.Context, input *ai_local_dto.OllamaConfig) error {
|
||||
return i.settingModule.Set(ctx, &system_dto.InputSetting{
|
||||
OllamaAddress: &input.Address,
|
||||
})
|
||||
}
|
||||
|
||||
func (i *imlLocalModelController) SimpleList(ctx *gin.Context) ([]*ai_local_dto.SimpleItem, error) {
|
||||
return i.module.SimpleList(ctx)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -58,6 +58,10 @@ func FromLocalModelState(state int) LocalModelState {
|
||||
}
|
||||
}
|
||||
|
||||
type OllamaConfig struct {
|
||||
Address string `json:"address"`
|
||||
}
|
||||
|
||||
type SimpleItem struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
|
||||
@@ -354,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
|
||||
|
||||
@@ -6,14 +6,24 @@ import (
|
||||
)
|
||||
|
||||
type InputSetting struct {
|
||||
InvokeAddress string `json:"invoke_address" key:"system.node.invoke_address"`
|
||||
SitePrefix string `json:"site_prefix" key:"system.setting.site_prefix"`
|
||||
InvokeAddress *string `json:"invoke_address" key:"system.node.invoke_address"`
|
||||
SitePrefix *string `json:"site_prefix" key:"system.setting.site_prefix"`
|
||||
OllamaAddress *string `json:"ollama_address" key:"system.ai_model.ollama_address"`
|
||||
}
|
||||
|
||||
func (i *InputSetting) Validate() error {
|
||||
_, err := url.Parse(i.InvokeAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
if i.InvokeAddress != nil {
|
||||
_, err := url.Parse(*i.InvokeAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if i.OllamaAddress != nil {
|
||||
_, err := url.Parse(*i.OllamaAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -31,9 +41,18 @@ func ToKeyMap(i interface{}) map[string]string {
|
||||
{
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
f := typ.Field(i)
|
||||
if f.Tag.Get("key") != "" {
|
||||
result[f.Tag.Get("key")] = val.Field(i).String()
|
||||
v := val.Field(i)
|
||||
if f.Type.Kind() == reflect.Ptr {
|
||||
if v.IsNil() {
|
||||
continue
|
||||
}
|
||||
v = v.Elem()
|
||||
}
|
||||
|
||||
if f.Tag.Get("key") != "" {
|
||||
result[f.Tag.Get("key")] = v.String()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,11 @@ import (
|
||||
)
|
||||
|
||||
func TestMap(t *testing.T) {
|
||||
|
||||
invokeAddress := "http://127.0.0.1:8080"
|
||||
ollamaAddress := "http://127.0.0.1:8081"
|
||||
input := &InputSetting{
|
||||
InvokeAddress: "http://127.0.0.1:8080",
|
||||
InvokeAddress: &invokeAddress,
|
||||
OllamaAddress: &ollamaAddress,
|
||||
}
|
||||
err := input.Validate()
|
||||
if err != nil {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
type Setting struct {
|
||||
InvokeAddress string `json:"invoke_address" key:"system.node.invoke_address"`
|
||||
SitePrefix string `json:"site_prefix" key:"system.setting.site_prefix"`
|
||||
OllamaAddress string `json:"ollama_address" key:"system.ai_model.ollama_address"`
|
||||
}
|
||||
|
||||
func MapStringToStruct[T any](m map[string]string) *T {
|
||||
|
||||
@@ -17,5 +17,8 @@ func (p *plugin) aiLocalApis() []pm3.Api {
|
||||
pm3.CreateApiWidthDoc(http.MethodPut, "/api/v1/model/local/info", []string{"context", "query:model", "body"}, nil, p.aiLocalController.Update),
|
||||
pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/model/local/state", []string{"context", "query:model"}, []string{"state", "info"}, p.aiLocalController.State),
|
||||
pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/simple/ai/models/local/configured", []string{"context"}, []string{"models"}, p.aiLocalController.SimpleList),
|
||||
|
||||
pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/model/local/source/ollama", []string{"context"}, []string{"config"}, p.aiLocalController.OllamaConfig),
|
||||
pm3.CreateApiWidthDoc(http.MethodPut, "/api/v1/model/local/source/ollama", []string{"context", "body"}, nil, p.aiLocalController.OllamaConfigUpdate),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user