diff --git a/ai-provider/model-runtime/loader.go b/ai-provider/model-runtime/loader.go index 1d6ee446..5288299f 100644 --- a/ai-provider/model-runtime/loader.go +++ b/ai-provider/model-runtime/loader.go @@ -6,6 +6,8 @@ import ( "fmt" "strings" + "github.com/APIParkLab/APIPark/gateway" + "github.com/eolinker/eosc" ) @@ -110,6 +112,10 @@ func LoadProvider(name string) error { if err != nil { return err } + gateway.RegisterDynamicResourceDriver(provider.ID(), gateway.Worker{ + Profession: gateway.ProfessionAIProvider, + Driver: provider.ID(), + }) Register(provider.ID(), provider) return nil } diff --git a/ai-provider/model-runtime/model-providers/authropic/anthropic.yaml b/ai-provider/model-runtime/model-providers/authropic/anthropic.yaml index c6ee0dba..10cad537 100644 --- a/ai-provider/model-runtime/model-providers/authropic/anthropic.yaml +++ b/ai-provider/model-runtime/model-providers/authropic/anthropic.yaml @@ -37,4 +37,4 @@ provider_credential_schema: placeholder: zh_Hans: 在此输入您的 API URL en_US: Enter your API URL -address: https://api.openai.com +address: https://api.anthropic.com diff --git a/ai-provider/model-runtime/model-providers/authropic/llm/claude-2.1.yaml b/ai-provider/model-runtime/model-providers/authropic/llm/claude-2.1.yaml index 6707c345..97fe6299 100644 --- a/ai-provider/model-runtime/model-providers/authropic/llm/claude-2.1.yaml +++ b/ai-provider/model-runtime/model-providers/authropic/llm/claude-2.1.yaml @@ -21,7 +21,7 @@ parameter_rules: zh_Hans: 仅从每个后续标记的前 K 个选项中采样。 en_US: Only sample from the top K options for each subsequent token. required: false - - name: max_tokens_to_sample + - name: max_tokens use_template: max_tokens required: true default: 4096 diff --git a/ai-provider/model-runtime/model-providers/authropic/llm/claude-2.yaml b/ai-provider/model-runtime/model-providers/authropic/llm/claude-2.yaml index 19869471..4adff56b 100644 --- a/ai-provider/model-runtime/model-providers/authropic/llm/claude-2.yaml +++ b/ai-provider/model-runtime/model-providers/authropic/llm/claude-2.yaml @@ -21,7 +21,7 @@ parameter_rules: zh_Hans: 仅从每个后续标记的前 K 个选项中采样。 en_US: Only sample from the top K options for each subsequent token. required: false - - name: max_tokens_to_sample + - name: max_tokens use_template: max_tokens required: true default: 4096 diff --git a/ai-provider/model-runtime/model-providers/authropic/llm/claude-instant-1.yaml b/ai-provider/model-runtime/model-providers/authropic/llm/claude-instant-1.yaml index 5e76d5b1..d61c5d95 100644 --- a/ai-provider/model-runtime/model-providers/authropic/llm/claude-instant-1.yaml +++ b/ai-provider/model-runtime/model-providers/authropic/llm/claude-instant-1.yaml @@ -20,7 +20,7 @@ parameter_rules: zh_Hans: 仅从每个后续标记的前 K 个选项中采样。 en_US: Only sample from the top K options for each subsequent token. required: false - - name: max_tokens_to_sample + - name: max_tokens use_template: max_tokens required: true default: 4096 diff --git a/controller/service/iml.go b/controller/service/iml.go index fae4a479..a8a68a6a 100644 --- a/controller/service/iml.go +++ b/controller/service/iml.go @@ -148,10 +148,12 @@ func (i *imlServiceController) CreateAIService(ctx *gin.Context, teamID string, } name := "Demo Translation API" description := "A demo that shows you how to use a prompt to create a Translation API." + apiId := uuid.New().String() err = i.aiAPIModule.Create( ctx, info.Id, &ai_api_dto.CreateAPI{ + Id: apiId, Name: name, Path: path, Description: description, @@ -180,7 +182,7 @@ func (i *imlServiceController) CreateAIService(ctx *gin.Context, teamID string, }, } _, err = i.routerModule.Create(ctx, info.Id, &router_dto.Create{ - Id: input.Id, + Id: apiId, Name: name, Path: path, Methods: []string{ diff --git a/gateway/apinto/application.go b/gateway/apinto/application.go index 31472eae..540df6c7 100644 --- a/gateway/apinto/application.go +++ b/gateway/apinto/application.go @@ -3,11 +3,11 @@ package apinto import ( "context" "strings" - + "github.com/APIParkLab/APIPark/gateway/apinto/auth" - + "github.com/APIParkLab/APIPark/gateway/apinto/entity" - + "github.com/APIParkLab/APIPark/gateway" admin_client "github.com/eolinker/eosc/process-admin/client" ) @@ -28,7 +28,7 @@ func (a *ApplicationClient) Online(ctx context.Context, applications ...*gateway return err } for _, app := range applications { - err = a.client.Set(ctx, genWorkerID(app.ID, ProfessionApplication), a.toApinto(app)) + err = a.client.Set(ctx, genWorkerID(app.ID, gateway.ProfessionApplication), a.toApinto(app)) if err != nil { a.client.Rollback(ctx) return err @@ -43,7 +43,7 @@ func (a *ApplicationClient) Offline(ctx context.Context, applications ...*gatewa return err } for _, app := range applications { - err = a.client.Del(ctx, genWorkerID(app.ID, ProfessionApplication)) + err = a.client.Del(ctx, genWorkerID(app.ID, gateway.ProfessionApplication)) if err != nil { a.client.Rollback(ctx) return err @@ -79,7 +79,7 @@ func (a *ApplicationClient) toApinto(app *gateway.ApplicationRelease) interface{ } return &entity.Application{ BasicInfo: &entity.BasicInfo{ - ID: genWorkerID(app.ID, ProfessionApplication), + ID: genWorkerID(app.ID, gateway.ProfessionApplication), Name: app.ID, Description: app.Description, Driver: "app", diff --git a/gateway/apinto/dynamic.go b/gateway/apinto/dynamic.go index 3654411b..d0e891dc 100644 --- a/gateway/apinto/dynamic.go +++ b/gateway/apinto/dynamic.go @@ -3,13 +3,13 @@ package apinto import ( "context" "errors" - + "github.com/eolinker/go-common/encoding" - + "github.com/eolinker/eosc/process-admin/cmd/proto" - + "github.com/APIParkLab/APIPark/gateway/apinto/entity" - + "github.com/APIParkLab/APIPark/gateway" admin_client "github.com/eolinker/eosc/process-admin/client" ) @@ -17,12 +17,12 @@ import ( var _ gateway.IDynamicClient = &DynamicClient{} func NewDynamicClient(client admin_client.Client, resource string) (*DynamicClient, error) { - cfg, has := dynamicResourceMap[resource] + cfg, has := gateway.GetDynamicResourceDriver(resource) if !has { return nil, errors.New("resource not found") - + } - + return &DynamicClient{client: client, profession: cfg.Profession, driver: cfg.Driver}, nil } diff --git a/gateway/apinto/profession.go b/gateway/apinto/profession.go deleted file mode 100644 index 174ca078..00000000 --- a/gateway/apinto/profession.go +++ /dev/null @@ -1,91 +0,0 @@ -package apinto - -const ( - ProfessionOutput = "output" - ProfessionCertificate = "certificate" - ProfessionRouter = "router" - ProfessionApplication = "app" - ProfessionService = "service" - ProfessionAIProvider = "ai-provider" -) - -var dynamicResourceMap = map[string]Worker{ - "file-access-log": { - Profession: ProfessionOutput, - Driver: "file", - }, - "http-access-log": { - Profession: ProfessionOutput, - Driver: "http_output", - }, - "nsqd-access-log": { - Profession: ProfessionOutput, - Driver: "nsqd", - }, - "syslog-access-log": { - Profession: ProfessionOutput, - Driver: "syslog_output", - }, - "kafka-access-log": { - Profession: ProfessionOutput, - Driver: "kafka_output", - }, - "influxdbv2": { - Profession: ProfessionOutput, - Driver: "influxdbv2", - }, - "redis": { - Profession: ProfessionOutput, - Driver: "redis", - }, - // 证书 - "certificate": { - Profession: ProfessionCertificate, - Driver: "server", - }, - "openai": { - Profession: ProfessionAIProvider, - Driver: "openai", - }, - "google": { - Profession: ProfessionAIProvider, - Driver: "google", - }, - "moonshot": { - Profession: ProfessionAIProvider, - Driver: "moonshot", - }, - "tongyi": { - Profession: ProfessionAIProvider, - Driver: "tongyi", - }, - "zhipuai": { - Profession: ProfessionAIProvider, - Driver: "zhipuai", - }, - "fireworks": { - Profession: ProfessionAIProvider, - Driver: "fireworks", - }, - "novita": { - Profession: ProfessionAIProvider, - Driver: "novita", - }, - "mistralai": { - Profession: ProfessionAIProvider, - Driver: "mistralai", - }, - "baichuan": { - Profession: ProfessionAIProvider, - Driver: "baichuan", - }, - "stepfun": { - Profession: ProfessionAIProvider, - Driver: "stepfun", - }, -} - -type Worker struct { - Profession string - Driver string -} diff --git a/gateway/apinto/project.go b/gateway/apinto/project.go index 9d76215b..8df723b3 100644 --- a/gateway/apinto/project.go +++ b/gateway/apinto/project.go @@ -4,16 +4,17 @@ import ( "context" "errors" "fmt" + "github.com/APIParkLab/APIPark/gateway/apinto/driver" - + "github.com/eolinker/eosc/process-admin/cmd/proto" - + "github.com/eolinker/go-common/encoding" - + "github.com/eolinker/go-common/utils" - + "github.com/APIParkLab/APIPark/gateway/apinto/entity" - + "github.com/APIParkLab/APIPark/gateway" admin_client "github.com/eolinker/eosc/process-admin/client" ) @@ -23,7 +24,7 @@ var _ gateway.IProjectClient = &ProjectClient{} func init() { driver.RegisterApiPublishHandler(func(ctx context.Context, client admin_client.Client, api *entity.Router, extends map[string]any) error { return client.Set(ctx, api.ID, api) - + }) } @@ -61,8 +62,8 @@ func (p *ProjectClient) online(ctx context.Context, project *gateway.ProjectRele matches := map[string]string{ "project": project.Id, } - - upstreams, err := matchLabels[entity.Service](ctx, p.client, ProfessionService, matches) + + upstreams, err := matchLabels[entity.Service](ctx, p.client, gateway.ProfessionService, matches) if err != nil { if !errors.Is(err, proto.Nil) { return err @@ -71,14 +72,14 @@ func (p *ProjectClient) online(ctx context.Context, project *gateway.ProjectRele upstreamMap := utils.SliceToMap(upstreams, func(t *entity.Service) string { return t.ID }) - - upstreamId := genWorkerID(project.Upstream.ID, ProfessionService) + + upstreamId := genWorkerID(project.Upstream.ID, gateway.ProfessionService) err = p.client.Set(ctx, upstreamId, entity.ToService(project.Upstream, project.Version, matches)) if err != nil { return err } delete(upstreamMap, upstreamId) - routers, err := matchLabels[entity.Router](ctx, p.client, ProfessionRouter, matches) + routers, err := matchLabels[entity.Router](ctx, p.client, gateway.ProfessionRouter, matches) if err != nil { if !errors.Is(err, proto.Nil) { return err @@ -87,16 +88,16 @@ func (p *ProjectClient) online(ctx context.Context, project *gateway.ProjectRele routerMap := utils.SliceToMap(routers, func(t *entity.Router) string { return t.ID }) - + for _, api := range project.Apis { - id := genWorkerID(api.ID, ProfessionRouter) + id := genWorkerID(api.ID, gateway.ProfessionRouter) if api.Labels == nil { api.Labels = make(map[string]string) } api.Service = upstreamId api.Labels["provider"] = project.Id routerInfo := entity.ToRouter(api, project.Version, matches) - + err = driver.ApiPublish(ctx, p.client, routerInfo, api.Extends) if err != nil { return err @@ -113,7 +114,7 @@ func (p *ProjectClient) online(ctx context.Context, project *gateway.ProjectRele if err != nil { return err } - + } for id := range upstreamMap { err = p.client.Del(ctx, id) @@ -121,7 +122,7 @@ func (p *ProjectClient) online(ctx context.Context, project *gateway.ProjectRele return err } } - + return nil } @@ -137,16 +138,16 @@ func (p *ProjectClient) Offline(ctx context.Context, projects ...*gateway.Projec return err } } - + return p.client.Commit(ctx) } func (p *ProjectClient) delete(ctx context.Context, id string) error { - err := p.deleteByLabels(ctx, ProfessionRouter, map[string]string{"project": id}) + err := p.deleteByLabels(ctx, gateway.ProfessionRouter, map[string]string{"project": id}) if err != nil { return err } - return p.deleteByLabels(ctx, ProfessionService, map[string]string{"project": id}) + return p.deleteByLabels(ctx, gateway.ProfessionService, map[string]string{"project": id}) } func matchLabels[T any](ctx context.Context, client admin_client.Client, profession string, labels map[string]string, t ...[]*T) ([]*T, error) { list, err := client.MatchLabels(ctx, profession, labels) diff --git a/gateway/profession.go b/gateway/profession.go new file mode 100644 index 00000000..251bf6b7 --- /dev/null +++ b/gateway/profession.go @@ -0,0 +1,96 @@ +package gateway + +const ( + ProfessionOutput = "output" + ProfessionCertificate = "certificate" + ProfessionRouter = "router" + ProfessionApplication = "app" + ProfessionService = "service" + ProfessionAIProvider = "ai-provider" +) + +func RegisterDynamicResourceDriver(key string, worker Worker) { + dynamicResourceMap[key] = worker +} + +func GetDynamicResourceDriver(key string) (Worker, bool) { + v, ok := dynamicResourceMap[key] + return v, ok +} + +var dynamicResourceMap = map[string]Worker{ + "file-access-log": { + Profession: ProfessionOutput, + Driver: "file", + }, + "http-access-log": { + Profession: ProfessionOutput, + Driver: "http_output", + }, + "nsqd-access-log": { + Profession: ProfessionOutput, + Driver: "nsqd", + }, + "syslog-access-log": { + Profession: ProfessionOutput, + Driver: "syslog_output", + }, + "kafka-access-log": { + Profession: ProfessionOutput, + Driver: "kafka_output", + }, + "influxdbv2": { + Profession: ProfessionOutput, + Driver: "influxdbv2", + }, + "redis": { + Profession: ProfessionOutput, + Driver: "redis", + }, + // 证书 + "certificate": { + Profession: ProfessionCertificate, + Driver: "server", + }, + //"openai": { + // Profession: ProfessionAIProvider, + // Driver: "openai", + //}, + //"google": { + // Profession: ProfessionAIProvider, + // Driver: "google", + //}, + //"anthropic": { + // Profession: ProfessionAIProvider, + // Driver: "anthropic", + //}, + //"moonshot": { + // Profession: ProfessionAIProvider, + // Driver: "moonshot", + //}, + //"tongyi": { + // Profession: ProfessionAIProvider, + // Driver: "tongyi", + //}, + //"zhipuai": { + // Profession: ProfessionAIProvider, + // Driver: "zhipuai", + //}, + //"fireworks": { + // Profession: ProfessionAIProvider, + // Driver: "fireworks", + //}, + //"novita": { + // Profession: ProfessionAIProvider, + // Driver: "novita", + //}, + //"mistralai": { + // Profession: ProfessionAIProvider, + // Driver: "mistralai", + //}, +} + +type Worker struct { + Profession string + Driver string +} diff --git a/module/catalogue/iml.go b/module/catalogue/iml.go index 62a38dac..0c25f718 100644 --- a/module/catalogue/iml.go +++ b/module/catalogue/iml.go @@ -7,6 +7,11 @@ import ( "math" "sort" + "github.com/eolinker/eosc/log" + + "github.com/APIParkLab/APIPark/gateway" + "github.com/APIParkLab/APIPark/service/cluster" + api_doc "github.com/APIParkLab/APIPark/service/api-doc" service_doc "github.com/APIParkLab/APIPark/service/service-doc" @@ -54,10 +59,22 @@ type imlCatalogueModule struct { subscribeService subscribe.ISubscribeService `autowired:""` subscribeApplyService subscribe.ISubscribeApplyService `autowired:""` transaction store.ITransaction `autowired:""` + clusterService cluster.IClusterService `autowired:""` root *Root } +func (i *imlCatalogueModule) onlineSubscriber(ctx context.Context, clusterId string, sub *gateway.SubscribeRelease) error { + client, err := i.clusterService.GatewayClient(ctx, clusterId) + if err != nil { + return err + } + defer func() { + _ = client.Close(ctx) + }() + return client.Subscribe().Online(ctx, sub) +} + func (i *imlCatalogueModule) Get(ctx context.Context, id string) (*catalogue_dto.Catalogue, error) { info, err := i.catalogueService.Get(ctx, id) if err != nil { @@ -122,6 +139,21 @@ func (i *imlCatalogueModule) Subscribe(ctx context.Context, subscribeInfo *catal status := subscribe.ApplyStatusReview if s.ApprovalType == service.ApprovalTypeAuto { status = subscribe.ApplyStatusSubscribe + cs, err := i.clusterService.List(ctx) + if err != nil { + return err + } + for _, c := range cs { + err := i.onlineSubscriber(ctx, c.Uuid, &gateway.SubscribeRelease{ + Service: subscribeInfo.Service, + Application: appId, + Expired: "0", + }) + + if err != nil { + log.Errorf("online subscriber for cluster[%s] %v", c.Uuid, err) + } + } } err = i.subscribeApplyService.Create(ctx, &subscribe.CreateApply{ diff --git a/module/router/iml.go b/module/router/iml.go index bc8af099..c028abdf 100644 --- a/module/router/iml.go +++ b/module/router/iml.go @@ -164,6 +164,9 @@ func (i *imlRouterModule) Search(ctx context.Context, keyword string, serviceId if err != nil { return nil, err } + if len(list) == 0 { + return []*router_dto.Item{}, nil + } apiInfos, err := i.apiService.ListInfo(ctx, utils.SliceToSlice(list, func(s *api.API) string { return s.UUID })...) @@ -229,10 +232,10 @@ func (i *imlRouterModule) Create(ctx context.Context, serviceId string, dto *rou if err != nil { return nil, err } - prefix, err := i.Prefix(ctx, serviceId) - if err != nil { - return nil, err - } + //prefix, err := i.Prefix(ctx, serviceId) + //if err != nil { + // return nil, err + //} err = i.transaction.Transaction(ctx, func(ctx context.Context) error { if dto.Id == "" { dto.Id = uuid.New().String() @@ -242,7 +245,7 @@ func (i *imlRouterModule) Create(ctx context.Context, serviceId string, dto *rou return err } - path := fmt.Sprintf("%s%s", prefix, dto.Path) + //path := fmt.Sprintf("%s%s", prefix, dto.Path) err = i.apiService.Exist(ctx, "", &api.Exist{Path: dto.Path, Methods: dto.Methods}) if err != nil { @@ -267,7 +270,7 @@ func (i *imlRouterModule) Create(ctx context.Context, serviceId string, dto *rou Team: info.Team, Methods: dto.Methods, Protocols: dto.Protocols, - Path: path, + Path: dto.Path, Match: string(match), }) })