ai api list add filter condition

This commit is contained in:
Liujian
2024-12-26 17:25:36 +08:00
parent 55aec4cc8b
commit 1bdd4720bb
7 changed files with 73 additions and 20 deletions
+10
View File
@@ -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"`
}
+41 -9
View File
@@ -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
}
}
+1 -1
View File
@@ -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() {