diff --git a/controller/ai/controller.go b/controller/ai/controller.go index bb6e7c9f..14a2822a 100644 --- a/controller/ai/controller.go +++ b/controller/ai/controller.go @@ -26,7 +26,7 @@ type IProviderController interface { } type IStatisticController interface { - APIs(ctx *gin.Context, keyword string, providerId string, start string, end string, page string, pageSize string, sortCondition string, asc string) ([]*ai_dto.APIItem, int64, error) + APIs(ctx *gin.Context, keyword string, providerId string, start string, end string, page string, pageSize string, sortCondition string, asc string, models string, services string) ([]*ai_dto.APIItem, *ai_dto.Condition, int64, error) } func init() { diff --git a/controller/ai/iml.go b/controller/ai/iml.go index 1ca21cdc..019a6564 100644 --- a/controller/ai/iml.go +++ b/controller/ai/iml.go @@ -1,6 +1,7 @@ package ai import ( + "encoding/json" "strconv" "github.com/APIParkLab/APIPark/module/ai" @@ -71,21 +72,21 @@ type imlStatisticController struct { module ai.IAIAPIModule `autowired:""` } -func (i *imlStatisticController) APIs(ctx *gin.Context, keyword string, providerId string, start string, end string, page string, pageSize string, sortCondition string, asc string) ([]*ai_dto.APIItem, int64, error) { +func (i *imlStatisticController) APIs(ctx *gin.Context, keyword string, providerId string, start string, end string, page string, pageSize string, sortCondition string, asc string, models string, services string) ([]*ai_dto.APIItem, *ai_dto.Condition, int64, error) { s, err := strconv.ParseInt(start, 10, 64) if err != nil { - return nil, 0, err + return nil, nil, 0, err } e, err := strconv.ParseInt(end, 10, 64) if err != nil { - return nil, 0, err + return nil, nil, 0, err } p, err := strconv.Atoi(page) if err != nil { if page != "" { - return nil, 0, err + return nil, nil, 0, err } p = 1 } @@ -93,9 +94,19 @@ func (i *imlStatisticController) APIs(ctx *gin.Context, keyword string, provider ps, err := strconv.Atoi(pageSize) if err != nil { if pageSize != "" { - return nil, 0, err + return nil, nil, 0, err } - ps = 15 + ps = 20 } - return i.module.APIs(ctx, keyword, providerId, s, e, p, ps, sortCondition, asc == "true") + ms := make([]string, 0) + if models != "" { + json.Unmarshal([]byte(models), &ms) + ms = append(ms, models) + } + ss := make([]string, 0) + if services != "" { + json.Unmarshal([]byte(services), &ss) + ss = append(ss, services) + } + return i.module.APIs(ctx, keyword, providerId, s, e, p, ps, sortCondition, asc == "true", ms, ss) } diff --git a/module/ai/dto/output.go b/module/ai/dto/output.go index 3e9350f6..bfb8ef09 100644 --- a/module/ai/dto/output.go +++ b/module/ai/dto/output.go @@ -77,3 +77,13 @@ type APIItem struct { UseToken int `json:"use_token"` Disable bool `json:"disable"` } + +type Condition struct { + Models []*BasicInfo `json:"models"` + Services []*BasicInfo `json:"services"` +} + +type BasicInfo struct { + Id string `json:"id"` + Name string `json:"name"` +} diff --git a/module/ai/iml.go b/module/ai/iml.go index ea6dcb45..10009114 100644 --- a/module/ai/iml.go +++ b/module/ai/iml.go @@ -9,6 +9,8 @@ import ( "sort" "time" + "github.com/APIParkLab/APIPark/service/service" + ai_key_dto "github.com/APIParkLab/APIPark/module/ai-key/dto" ai_key "github.com/APIParkLab/APIPark/service/ai-key" @@ -691,26 +693,55 @@ func (i *imlProviderModule) syncGateway(ctx context.Context, clusterId string, r var _ IAIAPIModule = (*imlAIApiModule)(nil) type imlAIApiModule struct { - aiAPIService ai_api.IAPIService `autowired:""` - aiAPIUseService ai_api.IAPIUseService `autowired:""` + aiAPIService ai_api.IAPIService `autowired:""` + aiAPIUseService ai_api.IAPIUseService `autowired:""` + serviceService service.IServiceService `autowired:""` } -func (i *imlAIApiModule) APIs(ctx context.Context, keyword string, providerId string, start int64, end int64, page int, pageSize int, sortCondition string, asc bool) ([]*ai_dto.APIItem, int64, error) { +func (i *imlAIApiModule) APIs(ctx context.Context, keyword string, providerId string, start int64, end int64, page int, pageSize int, sortCondition string, asc bool, models []string, serviceIds []string) ([]*ai_dto.APIItem, *ai_dto.Condition, int64, error) { + p, has := model_runtime.GetProvider(providerId) + if !has { + return nil, nil, 0, fmt.Errorf("ai provider not found") + } sortRule := "desc" if asc { sortRule = "asc" } + services, err := i.serviceService.ServiceListByKind(ctx, service.AIService) + if err != nil { + return nil, nil, 0, err + } + serviceItems := utils.SliceToSlice(services, func(e *service.Service) *ai_dto.BasicInfo { + return &ai_dto.BasicInfo{ + Id: e.Id, + Name: e.Name, + } + }) + modelItems := utils.SliceToSlice(p.Models(), func(e model_runtime.IModel) *ai_dto.BasicInfo { + return &ai_dto.BasicInfo{ + Id: e.ID(), + Name: e.ID(), + } + }) + condition := &ai_dto.Condition{Services: serviceItems, Models: modelItems} switch sortCondition { default: - apis, err := i.aiAPIService.Search(ctx, keyword, map[string]interface{}{ + w := map[string]interface{}{ "provider": providerId, - }, "update_at desc") + } + if len(models) > 0 { + w["model"] = models + } + if len(serviceIds) > 0 { + w["service"] = serviceIds + } + apis, err := i.aiAPIService.Search(ctx, keyword, w, "update_at desc") if err != nil { - return nil, 0, err + return nil, nil, 0, err } if len(apis) <= 0 { - return nil, 0, nil + return nil, condition, 0, nil } apiMap := make(map[string]*ai_api.API) apiIds := make([]string, 0, len(apis)) @@ -721,7 +752,7 @@ func (i *imlAIApiModule) APIs(ctx context.Context, keyword string, providerId st offset := (page - 1) * pageSize results, _, err := i.aiAPIUseService.SumByApisPage(ctx, providerId, start, end, offset, pageSize, fmt.Sprintf("total_token %s", sortRule), apiIds...) if err != nil { - return nil, 0, err + return nil, nil, 0, err } apiItems := utils.SliceToSlice(results, func(e *ai_api.APIUse) *ai_dto.APIItem { @@ -768,7 +799,8 @@ func (i *imlAIApiModule) APIs(ctx context.Context, keyword string, providerId st for i := offset; i < offset+size && i < len(sortApis); i++ { apiItems = append(apiItems, sortApis[i]) } + total := int64(len(apis)) - return apiItems, total, nil + return apiItems, condition, total, nil } } diff --git a/module/ai/module.go b/module/ai/module.go index 1037aea7..b5387dd6 100644 --- a/module/ai/module.go +++ b/module/ai/module.go @@ -26,7 +26,7 @@ type IProviderModule interface { } type IAIAPIModule interface { - APIs(ctx context.Context, keyword string, providerId string, start int64, end int64, page int, pageSize int, sortCondition string, asc bool) ([]*ai_dto.APIItem, int64, error) + APIs(ctx context.Context, keyword string, providerId string, start int64, end int64, page int, pageSize int, sortCondition string, asc bool, models []string, services []string) ([]*ai_dto.APIItem, *ai_dto.Condition, int64, error) } func init() { diff --git a/plugins/core/ai.go b/plugins/core/ai.go index eea9eb22..57749315 100644 --- a/plugins/core/ai.go +++ b/plugins/core/ai.go @@ -20,7 +20,7 @@ func (p *plugin) aiAPIs() []pm3.Api { pm3.CreateApiWidthDoc(http.MethodPut, "/api/v1/ai/provider/sort", []string{"context", "body"}, nil, p.aiProviderController.Sort), pm3.CreateApiWidthDoc(http.MethodPut, "/api/v1/ai/provider/config", []string{"context", "query:provider", "body"}, nil, p.aiProviderController.UpdateProviderConfig, access.SystemSettingsAiProviderManager), - pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/ai/apis", []string{"context", "query:keyword", "query:provider", "query:start", "query:end", "query:page", "query:page_size", "query:sort", "query:asc"}, []string{"apis", "total"}, p.aiStatisticController.APIs), + pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/ai/apis", []string{"context", "query:keyword", "query:provider", "query:start", "query:end", "query:page", "query:page_size", "query:sort", "query:asc", "query:models", "query:services"}, []string{"apis", "condition", "total"}, p.aiStatisticController.APIs), } } diff --git a/service/service/iml.go b/service/service/iml.go index c3090ea0..719e55e7 100644 --- a/service/service/iml.go +++ b/service/service/iml.go @@ -46,7 +46,7 @@ func (i *imlServiceService) ServiceListByKind(ctx context.Context, kind Kind, se w["uuid"] = serviceIds } w["as_server"] = true - w["kind"] = kind + w["kind"] = kind.Int() w["is_delete"] = false list, err := i.store.List(ctx, w) if err != nil {