Former-commit-id: ae4c26cce8
pull/278/head
| @@ -1827,8 +1827,8 @@ service AICore-api { | |||
| type ( | |||
| ChatReq{ | |||
| ApiUrl string `json:"apiUrl"` | |||
| Method string `json:"method,optional"` | |||
| id uint `json:"id,string"` | |||
| Method string `json:"method,optional"` | |||
| ReqData map[string]interface{} `json:"reqData"` | |||
| } | |||
| ChatResult{ | |||
| @@ -386,8 +386,8 @@ service pcm { | |||
| /***********chat***********/ | |||
| @doc "文本识别" | |||
| @handler ProxyApiHandler | |||
| post /ai/chat (ChatReq) returns (CommonResp) | |||
| @handler ChatHandler | |||
| post /ai/chat (ChatReq) returns (ChatResult) | |||
| /******chat end***********/ | |||
| } | |||
| @@ -1,24 +1,25 @@ | |||
| package ai | |||
| import ( | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" | |||
| "net/http" | |||
| "github.com/zeromicro/go-zero/rest/httpx" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/logic/ai" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" | |||
| "net/http" | |||
| ) | |||
| func ProxyApiHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | |||
| func ChatHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | |||
| return func(w http.ResponseWriter, r *http.Request) { | |||
| var req types.ChatReq | |||
| if err := httpx.Parse(r, &req); err != nil { | |||
| result.ParamErrorResult(r, w, err) | |||
| httpx.ErrorCtx(r.Context(), w, err) | |||
| return | |||
| } | |||
| l := ai.NewProxyApiLogic(r.Context(), svcCtx) | |||
| resp, err := l.ProxyApi(&req) | |||
| l := ai.NewChatLogic(r.Context(), svcCtx) | |||
| resp, err := l.Chat(&req) | |||
| result.HttpResult(r, w, resp, err) | |||
| } | |||
| } | |||
| @@ -460,7 +460,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | |||
| { | |||
| Method: http.MethodPost, | |||
| Path: "/ai/chat", | |||
| Handler: ai.ProxyApiHandler(serverCtx), | |||
| Handler: ai.ChatHandler(serverCtx), | |||
| }, | |||
| }, | |||
| rest.WithPrefix("/pcm/v1"), | |||
| @@ -0,0 +1,94 @@ | |||
| package ai | |||
| import ( | |||
| "bytes" | |||
| "context" | |||
| "crypto/tls" | |||
| "github.com/go-resty/resty/v2" | |||
| "github.com/pkg/errors" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils/hws" | |||
| "k8s.io/apimachinery/pkg/util/json" | |||
| "net/http" | |||
| "strings" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" | |||
| "github.com/zeromicro/go-zero/core/logx" | |||
| ) | |||
| type ChatLogic struct { | |||
| logx.Logger | |||
| ctx context.Context | |||
| svcCtx *svc.ServiceContext | |||
| } | |||
| func NewChatLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatLogic { | |||
| return &ChatLogic{ | |||
| Logger: logx.WithContext(ctx), | |||
| ctx: ctx, | |||
| svcCtx: svcCtx, | |||
| } | |||
| } | |||
| func (l *ChatLogic) Chat(req *types.ChatReq) (resp *types.ChatResult, err error) { | |||
| resp = &types.ChatResult{} | |||
| jsonBytes, err := json.Marshal(&req.ReqData) | |||
| if err != nil { | |||
| logx.Errorf("【序列化请求数据失败: %v】", err) | |||
| return nil, errors.New("请求数据序列化失败") | |||
| } | |||
| taskAi := models.TaskAi{} | |||
| l.svcCtx.DbEngin.Model(models.TaskAi{}).Where("id", req.Id).Scan(&taskAi) | |||
| logx.Infof("【开始处理请求,目标URL: %s】", taskAi.InferUrl) | |||
| // 构建 HTTP 请求 | |||
| request, err := http.NewRequest("POST", taskAi.InferUrl, bytes.NewBuffer(jsonBytes)) | |||
| if err != nil { | |||
| logx.Errorf("【构建 HTTP 请求失败: %v】", err) | |||
| return nil, errors.New("网络错误,请稍后重试") | |||
| } | |||
| client := resty.New().SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) | |||
| restReq := client.R() | |||
| //ModelArts | |||
| cluster := models.CloudModel{} | |||
| l.svcCtx.DbEngin.Table("t_cluster").Where("id", taskAi.ClusterId).Scan(&cluster) | |||
| if strings.EqualFold(cluster.Label, constants.MODELARTS) { | |||
| signer := &hws.Signer{ | |||
| Key: cluster.Ak, | |||
| Secret: cluster.Sk, | |||
| } | |||
| if err := signer.Sign(request); err != nil { | |||
| logx.Errorf("【接口签名错误: %v】", err) | |||
| return nil, errors.New("网络错误,请稍后重试") | |||
| } | |||
| restReq. | |||
| SetHeader("X-Project-Id", cluster.ProjectId). | |||
| SetHeader("x-stage", "RELEASE"). | |||
| SetHeader("Authorization", request.Header.Get(hws.HeaderXAuthorization)). | |||
| SetHeader("X-Sdk-Date", request.Header.Get(hws.HeaderXDateTime)) | |||
| } | |||
| response, err := restReq. | |||
| SetHeader("Content-Type", "application/json"). | |||
| SetBody(jsonBytes). | |||
| SetResult(&resp). | |||
| Post(taskAi.InferUrl) | |||
| if err != nil { | |||
| logx.Errorf("【远程调用接口URL:%s, 返回错误: %s】", taskAi.InferUrl, err.Error()) | |||
| return nil, errors.New("网络错误,请稍后重试") | |||
| } | |||
| if response.StatusCode() != 200 { | |||
| logx.Errorf("【远程调用接口URL:%s, 返回错误: %s】", taskAi.InferUrl, response.Body()) | |||
| return nil, errors.New("网络错误,请稍后重试") | |||
| } | |||
| logx.Infof("【请求处理成功,目标URL: %s】", taskAi.InferUrl) | |||
| return resp, nil | |||
| } | |||
| @@ -1,91 +0,0 @@ | |||
| package ai | |||
| import ( | |||
| "bytes" | |||
| "context" | |||
| "crypto/tls" | |||
| "encoding/json" | |||
| "github.com/go-resty/resty/v2" | |||
| "github.com/pkg/errors" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils/hws" | |||
| "net/http" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" | |||
| "github.com/zeromicro/go-zero/core/logx" | |||
| ) | |||
| type ProxyApiLogic struct { | |||
| logx.Logger | |||
| ctx context.Context | |||
| svcCtx *svc.ServiceContext | |||
| } | |||
| func NewProxyApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProxyApiLogic { | |||
| return &ProxyApiLogic{ | |||
| Logger: logx.WithContext(ctx), | |||
| ctx: ctx, | |||
| svcCtx: svcCtx, | |||
| } | |||
| } | |||
| const ( | |||
| XProjectID = "d18190e28e3f45a281ef0b0696ec9d52" | |||
| XStage = "RELEASE" | |||
| ContentType = "application/json" | |||
| ) | |||
| func (l *ProxyApiLogic) ProxyApi(req *types.ChatReq) (resp *types.ChatResult, err error) { | |||
| logx.Infof("【开始处理请求,目标URL: %s】", req.ApiUrl) | |||
| jsonBytes, err := json.Marshal(&req.ReqData) | |||
| if err != nil { | |||
| logx.Errorf("【序列化请求数据失败: %v】", err) | |||
| return nil, errors.New("请求数据序列化失败") | |||
| } | |||
| resp = &types.ChatResult{} | |||
| // 构建 HTTP 请求 | |||
| request, err := http.NewRequest("POST", req.ApiUrl, bytes.NewBuffer(jsonBytes)) | |||
| if err != nil { | |||
| logx.Errorf("【构建 HTTP 请求失败: %v】", err) | |||
| return nil, errors.New("网络错误,请稍后重试") | |||
| } | |||
| signer := &hws.Signer{ | |||
| Key: "UNEHPHO4Z7YSNPKRXFE4", | |||
| Secret: "JWXCE9qcYbc7RjpSRIWt4WgG3ZKF6Q4lPzkJReX9", | |||
| } | |||
| if err := signer.Sign(request); err != nil { | |||
| logx.Errorf("【接口签名错误: %v】", err) | |||
| return nil, errors.New("网络错误,请稍后重试") | |||
| } | |||
| client := resty.New().SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) | |||
| response, err := client.R(). | |||
| SetHeader("X-Project-Id", XProjectID). | |||
| SetHeader("x-stage", XStage). | |||
| SetHeader("Content-Type", ContentType). | |||
| SetHeader("Authorization", request.Header.Get(hws.HeaderXAuthorization)). | |||
| SetHeader("X-Sdk-Date", request.Header.Get(hws.HeaderXDateTime)). | |||
| SetBody(jsonBytes). | |||
| SetResult(&resp). | |||
| Post(req.ApiUrl) | |||
| if err != nil { | |||
| logx.Errorf("【远程调用接口URL:%s, 返回错误: %s】", req.ApiUrl, err.Error()) | |||
| return nil, errors.New("网络错误,请稍后重试") | |||
| } | |||
| if response.StatusCode() != 200 { | |||
| logx.Errorf("【远程调用接口URL:%s, 返回错误: %s】", req.ApiUrl, response.Body()) | |||
| return nil, errors.New("网络错误,请稍后重试") | |||
| } | |||
| logx.Infof("【请求处理成功,目标URL: %s】", req.ApiUrl) | |||
| return resp, nil | |||
| } | |||
| @@ -201,10 +201,11 @@ func (m *ModelArtsLink) GetResourceStats(ctx context.Context) (*collector.Resour | |||
| num32, _ := strconv.Atoi(resp1.Items[0].Spec.Npu.Size) | |||
| var cards []*collector.Card | |||
| card := &collector.Card{ | |||
| Platform: MODELARTS, | |||
| Type: CARD, | |||
| Name: Npu, | |||
| CardNum: int32(num32), | |||
| Platform: MODELARTS, | |||
| Type: CARD, | |||
| Name: Npu, | |||
| CardNum: int32(num32), | |||
| TOpsAtFp16: float64(num32 * 320), | |||
| } | |||
| cards = append(cards, card) | |||
| resourceStats.CardsAvail = cards | |||
| @@ -21,7 +21,7 @@ import ( | |||
| "github.com/robfig/cron/v3" | |||
| "github.com/zeromicro/go-zero/core/logx" | |||
| "github.com/zeromicro/go-zero/zrpc" | |||
| hpcacclient "gitlink.org.cn/JointCloud/pcm-ac/hpcacclient" | |||
| "gitlink.org.cn/JointCloud/pcm-ac/hpcacclient" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/config" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/database" | |||
| @@ -2908,7 +2908,7 @@ type TrainingTaskStatResp struct { | |||
| } | |||
| type ChatReq struct { | |||
| ApiUrl string `json:"apiUrl"` | |||
| Id uint `json:"id,string"` | |||
| Method string `json:"method,optional"` | |||
| ReqData map[string]interface{} `json:"reqData"` | |||
| } | |||
| @@ -28,4 +28,5 @@ const ( | |||
| WaitStart = "WaitStart" | |||
| Pending = "Pending" | |||
| Stopped = "Stopped" | |||
| Deploying = "Deploying" | |||
| ) | |||