|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, The Easegress Authors |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +// Package commandv2 provides the new version of commands. |
| 19 | +package commandv2 |
| 20 | + |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + "net/http" |
| 24 | + |
| 25 | + "github.com/megaease/easegress/v2/cmd/client/general" |
| 26 | + "github.com/megaease/easegress/v2/cmd/client/resources" |
| 27 | + "github.com/megaease/easegress/v2/pkg/object/aigatewaycontroller" |
| 28 | + "github.com/megaease/easegress/v2/pkg/object/aigatewaycontroller/metricshub" |
| 29 | + "github.com/megaease/easegress/v2/pkg/util/codectool" |
| 30 | + "github.com/spf13/cobra" |
| 31 | +) |
| 32 | + |
| 33 | +// AICmd returns AI command. |
| 34 | +func AICmd() *cobra.Command { |
| 35 | + examples := []general.Example{ |
| 36 | + {Desc: "Enable AI (Create the AIGatewayController)", Command: "egctl enable"}, |
| 37 | + {Desc: "Disable AI (Delete the AIGatewayController)", Command: "egctl disable"}, |
| 38 | + {Desc: "Get AI statistics", Command: "egctl ai stat"}, |
| 39 | + {Desc: "Check AI health of providers", Command: "egctl ai check"}, |
| 40 | + } |
| 41 | + |
| 42 | + cmd := &cobra.Command{ |
| 43 | + Use: "ai", |
| 44 | + Short: "Commands to manage AI Gateway", |
| 45 | + Args: cobra.NoArgs, |
| 46 | + Example: createMultiExample(examples), |
| 47 | + Run: aiCmdRun, |
| 48 | + } |
| 49 | + |
| 50 | + cmd.AddCommand( |
| 51 | + enableCmd(), |
| 52 | + disableCmd(), |
| 53 | + statCmd(), |
| 54 | + checkCmd(), |
| 55 | + editCmd(), |
| 56 | + ) |
| 57 | + |
| 58 | + return cmd |
| 59 | +} |
| 60 | + |
| 61 | +func aiCmdRun(cmd *cobra.Command, args []string) { |
| 62 | + cmd.Help() |
| 63 | +} |
| 64 | + |
| 65 | +func enableCmd() *cobra.Command { |
| 66 | + return &cobra.Command{ |
| 67 | + Use: "enable", |
| 68 | + Short: "Enable AI Gateway (create AIGatewayController)", |
| 69 | + Run: func(cmd *cobra.Command, args []string) { |
| 70 | + var err error |
| 71 | + defer func() { |
| 72 | + if err != nil { |
| 73 | + general.ExitWithError(err) |
| 74 | + } |
| 75 | + |
| 76 | + fmt.Println("AI Gateway enabled successfully.") |
| 77 | + fmt.Println("You can use `egctl ai edit` to add providers and middlewares.") |
| 78 | + }() |
| 79 | + |
| 80 | + s, err := general.GetSpecFromYaml(`kind: AIGatewayController |
| 81 | +name: AIGatewayController |
| 82 | +`) |
| 83 | + if err != nil { |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + err = resources.CreateObject(cmd, s) |
| 88 | + }, |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func disableCmd() *cobra.Command { |
| 93 | + return &cobra.Command{ |
| 94 | + Use: "disable", |
| 95 | + Short: "Disable AI Gateway (delete AIGatewayController)", |
| 96 | + Run: func(cmd *cobra.Command, args []string) { |
| 97 | + var err error |
| 98 | + defer func() { |
| 99 | + if err != nil { |
| 100 | + general.ExitWithError(err) |
| 101 | + } |
| 102 | + fmt.Println("AI Gateway disabled successfully.") |
| 103 | + }() |
| 104 | + |
| 105 | + err = resources.DeleteObject(cmd, "AIGatewayController", []string{"AIGatewayController"}, false) |
| 106 | + }, |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func statCmd() *cobra.Command { |
| 111 | + return &cobra.Command{ |
| 112 | + Use: "stat", |
| 113 | + Short: "Get AI Gateway statistics", |
| 114 | + Example: createExample("Get AI Gateway statistics.", "egctl ai stat"), |
| 115 | + Args: cobra.NoArgs, |
| 116 | + Run: func(cmd *cobra.Command, args []string) { |
| 117 | + body, err := general.HandleRequest(http.MethodGet, general.AIStatURL, nil) |
| 118 | + if err != nil { |
| 119 | + general.ExitWithError(err) |
| 120 | + } |
| 121 | + |
| 122 | + if !general.CmdGlobalFlags.DefaultFormat() { |
| 123 | + general.PrintBody(body) |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + type AIStatResponse struct { |
| 128 | + Stats []metricshub.MetricStats `json:"stats"` |
| 129 | + } |
| 130 | + |
| 131 | + var statResp AIStatResponse |
| 132 | + err = codectool.UnmarshalJSON(body, &statResp) |
| 133 | + if err != nil { |
| 134 | + general.ExitWithError(err) |
| 135 | + } |
| 136 | + |
| 137 | + // Output table: |
| 138 | + // PROVIDER (TYPE), MODEL @ BASEURL, RESP_TYPE, |
| 139 | + // TOTAL_REQUESTS, SUCCESS/FAILED, AVG_DURATION(ms), |
| 140 | + // TOKENS (INPUT/OUTPUT) |
| 141 | + |
| 142 | + table := [][]string{ |
| 143 | + { |
| 144 | + "PROVIDER(TYPE)", |
| 145 | + "MODEL@BASEURL", |
| 146 | + "RESP-TYPE", |
| 147 | + "TOTAL-REQ", |
| 148 | + "SUCCESS/FAILED", |
| 149 | + "AVG-DUR(ms)", |
| 150 | + "TOKENS(INPUT/OUTPUT)", |
| 151 | + }, |
| 152 | + } |
| 153 | + for _, stat := range statResp.Stats { |
| 154 | + table = append(table, []string{ |
| 155 | + fmt.Sprintf("%s(%s)", stat.Provider, stat.ProviderType), |
| 156 | + fmt.Sprintf("%s@%s", stat.Model, stat.BaseURL), |
| 157 | + stat.RespType, |
| 158 | + fmt.Sprintf("%d", stat.TotalRequests), |
| 159 | + fmt.Sprintf("%d/%d", stat.SuccessRequests, stat.FailedRequests), |
| 160 | + fmt.Sprintf("%d", stat.RequestAverageDuration), |
| 161 | + fmt.Sprintf("%d/%d", stat.PromptTokens, stat.CompletionTokens), |
| 162 | + }) |
| 163 | + } |
| 164 | + general.PrintTable(table) |
| 165 | + }, |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func checkCmd() *cobra.Command { |
| 170 | + return &cobra.Command{ |
| 171 | + Use: "check", |
| 172 | + Short: "Check AI Gateway health of providers", |
| 173 | + Args: cobra.NoArgs, |
| 174 | + Run: func(cmd *cobra.Command, args []string) { |
| 175 | + body, err := general.HandleRequest(http.MethodGet, general.AISProviderstatusURL, nil) |
| 176 | + if err != nil { |
| 177 | + general.ExitWithError(err) |
| 178 | + } |
| 179 | + if !general.CmdGlobalFlags.DefaultFormat() { |
| 180 | + general.PrintBody(body) |
| 181 | + return |
| 182 | + } |
| 183 | + |
| 184 | + var statusResp aigatewaycontroller.HealthCheckResponse |
| 185 | + err = codectool.UnmarshalJSON(body, &statusResp) |
| 186 | + if err != nil { |
| 187 | + general.ExitWithError(err) |
| 188 | + } |
| 189 | + |
| 190 | + table := [][]string{ |
| 191 | + {"NAME", "PROVIDER-TYPE", "HEALTHY"}, |
| 192 | + } |
| 193 | + for _, result := range statusResp.Results { |
| 194 | + var healthy string |
| 195 | + if result.Healthy { |
| 196 | + healthy = "YES" |
| 197 | + } else { |
| 198 | + healthy = fmt.Sprintf("NO(%s)", result.Error) |
| 199 | + } |
| 200 | + |
| 201 | + table = append(table, []string{ |
| 202 | + result.Name, |
| 203 | + result.ProviderType, |
| 204 | + healthy, |
| 205 | + }) |
| 206 | + } |
| 207 | + general.PrintTable(table) |
| 208 | + }, |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +func editCmd() *cobra.Command { |
| 213 | + return &cobra.Command{ |
| 214 | + Use: "edit", |
| 215 | + Short: "Edit AI Gateway providers", |
| 216 | + Run: func(cmd *cobra.Command, args []string) { |
| 217 | + args = []string{"AIGatewayController", "AIGatewayController"} |
| 218 | + editCmdRun(cmd, args) |
| 219 | + }, |
| 220 | + } |
| 221 | +} |
0 commit comments