|
| 1 | +/* |
| 2 | + * Copyright 2025 coze-dev Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package volcengine_maas |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "strconv" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/coze-dev/coze-studio/backend/infra/contract/chatmodel" |
| 27 | + "github.com/coze-dev/coze-studio/backend/infra/contract/modelmgr" |
| 28 | + "github.com/coze-dev/coze-studio/backend/pkg/lang/ptr" |
| 29 | + "github.com/coze-dev/coze-studio/backend/pkg/lang/sets" |
| 30 | + "github.com/coze-dev/coze-studio/backend/types/consts" |
| 31 | + "github.com/volcengine/volcengine-go-sdk/service/ark" |
| 32 | + "github.com/volcengine/volcengine-go-sdk/volcengine" |
| 33 | + "github.com/volcengine/volcengine-go-sdk/volcengine/credentials" |
| 34 | + "github.com/volcengine/volcengine-go-sdk/volcengine/session" |
| 35 | +) |
| 36 | + |
| 37 | +var _ modelmgr.Manager = (*volcModelManager)(nil) |
| 38 | + |
| 39 | +type volcModelManager struct { |
| 40 | + models []*modelmgr.Model |
| 41 | + modelMapping map[int64]*modelmgr.Model |
| 42 | + arkClient *ark.ARK |
| 43 | +} |
| 44 | + |
| 45 | +func NewModelMgr(staticModels []*modelmgr.Model) (modelmgr.Manager, error) { |
| 46 | + |
| 47 | + cfg := volcengine.NewConfig(). |
| 48 | + WithCredentials(credentials.NewStaticCredentials(os.Getenv(consts.VolcengineMAASAccessKey), os.Getenv(consts.VolcengineMAASSecretKey), "")). |
| 49 | + WithRegion(os.Getenv(consts.VolcengineMAASRegion)) |
| 50 | + |
| 51 | + sess, err := session.NewSession(cfg) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + svc := ark.New(sess) |
| 56 | + |
| 57 | + mapping := make(map[int64]*modelmgr.Model, len(staticModels)) |
| 58 | + for i := range staticModels { |
| 59 | + mapping[staticModels[i].ID] = staticModels[i] |
| 60 | + } |
| 61 | + |
| 62 | + manager := &volcModelManager{ |
| 63 | + arkClient: svc, |
| 64 | + models: staticModels, |
| 65 | + modelMapping: mapping, |
| 66 | + } |
| 67 | + manager.initModelList(context.Background()) |
| 68 | + return manager, nil |
| 69 | +} |
| 70 | + |
| 71 | +func (v *volcModelManager) initModelList(ctx context.Context) error { |
| 72 | + newModels := make([]*modelmgr.Model, 0) |
| 73 | + for i := range v.models { |
| 74 | + m := v.models[i] |
| 75 | + if m.Meta.Protocol != chatmodel.ProtocolArk || !strings.Contains(m.Meta.ConnConfig.BaseURL, "volces") { |
| 76 | + continue |
| 77 | + } |
| 78 | + item, err := v.listEndpoints(ctx, m.Name) |
| 79 | + if err != nil { |
| 80 | + continue |
| 81 | + } |
| 82 | + m.Meta.ConnConfig.Model = *item.Id |
| 83 | + m.Meta.ConnConfig.APIKey = os.Getenv(consts.VolcengineMAASAPIKey) |
| 84 | + m.Meta.Status = modelmgr.StatusInUse |
| 85 | + newModels = append(newModels, m) |
| 86 | + } |
| 87 | + v.models = newModels |
| 88 | + mapping := make(map[int64]*modelmgr.Model, len(newModels)) |
| 89 | + for i := range newModels { |
| 90 | + mapping[newModels[i].ID] = newModels[i] |
| 91 | + } |
| 92 | + v.modelMapping = mapping |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +func (v *volcModelManager) listEndpoints(ctx context.Context, modelName string) (*ark.ItemForListEndpointsOutput, error) { |
| 97 | + m := strings.ReplaceAll(strings.ReplaceAll(modelName, ".", "-"), "-VolcEngine", "") |
| 98 | + input := &ark.ListEndpointsInput{ |
| 99 | + Filter: &ark.FilterForListEndpointsInput{ |
| 100 | + FoundationModelName: volcengine.String(m), |
| 101 | + }, |
| 102 | + } |
| 103 | + resp, err := v.arkClient.ListEndpointsWithContext(ctx, input) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + if len(resp.Items) == 0 { |
| 108 | + return nil, fmt.Errorf("model %s not found", modelName) |
| 109 | + } |
| 110 | + return resp.Items[0], nil |
| 111 | +} |
| 112 | + |
| 113 | +func (v *volcModelManager) ListModel(ctx context.Context, req *modelmgr.ListModelRequest) (*modelmgr.ListModelResponse, error) { |
| 114 | + startIdx := 0 |
| 115 | + if req.Cursor != nil { |
| 116 | + start, err := strconv.ParseInt(*req.Cursor, 10, 64) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + startIdx = int(start) |
| 121 | + } |
| 122 | + |
| 123 | + limit := req.Limit |
| 124 | + if limit == 0 { |
| 125 | + limit = 100 |
| 126 | + } |
| 127 | + |
| 128 | + var ( |
| 129 | + i int |
| 130 | + respList []*modelmgr.Model |
| 131 | + statSet = sets.FromSlice(req.Status) |
| 132 | + ) |
| 133 | + |
| 134 | + for i = startIdx; i < len(v.models) && len(respList) < limit; i++ { |
| 135 | + m := v.models[i] |
| 136 | + if req.FuzzyModelName != nil && !strings.Contains(m.Name, *req.FuzzyModelName) { |
| 137 | + continue |
| 138 | + } |
| 139 | + if len(statSet) > 0 && !statSet.Contains(m.Meta.Status) { |
| 140 | + continue |
| 141 | + } |
| 142 | + respList = append(respList, m) |
| 143 | + } |
| 144 | + |
| 145 | + resp := &modelmgr.ListModelResponse{ |
| 146 | + ModelList: respList, |
| 147 | + } |
| 148 | + resp.HasMore = i != len(v.models) |
| 149 | + if resp.HasMore { |
| 150 | + resp.NextCursor = ptr.Of(strconv.FormatInt(int64(i), 10)) |
| 151 | + } |
| 152 | + |
| 153 | + return resp, nil |
| 154 | +} |
| 155 | + |
| 156 | +func (v *volcModelManager) ListInUseModel(ctx context.Context, limit int, Cursor *string) (*modelmgr.ListModelResponse, error) { |
| 157 | + return v.ListModel(ctx, &modelmgr.ListModelRequest{ |
| 158 | + Status: []modelmgr.ModelStatus{modelmgr.StatusInUse}, |
| 159 | + Limit: limit, |
| 160 | + Cursor: Cursor, |
| 161 | + }) |
| 162 | +} |
| 163 | + |
| 164 | +func (v *volcModelManager) MGetModelByID(ctx context.Context, req *modelmgr.MGetModelRequest) ([]*modelmgr.Model, error) { |
| 165 | + resp := make([]*modelmgr.Model, 0) |
| 166 | + for _, id := range req.IDs { |
| 167 | + if m, found := v.modelMapping[id]; found { |
| 168 | + resp = append(resp, m) |
| 169 | + } |
| 170 | + } |
| 171 | + return resp, nil |
| 172 | +} |
0 commit comments