Browse Source

Merge branch 'master' of https://gitlink.org.cn/JointCloud/pcm-coordinator

Former-commit-id: ae4c26cce8
pull/278/head
tzwang 1 year ago
parent
commit
1ec58ac8f2
10 changed files with 114 additions and 108 deletions
  1. +2
    -2
      desc/ai/pcm-ai.api
  2. +2
    -2
      desc/pcm.api
  3. +7
    -6
      internal/handler/ai/chathandler.go
  4. +1
    -1
      internal/handler/routes.go
  5. +94
    -0
      internal/logic/ai/chatlogic.go
  6. +0
    -91
      internal/logic/ai/proxyapilogic.go
  7. +5
    -4
      internal/storeLink/modelarts.go
  8. +1
    -1
      internal/svc/servicecontext.go
  9. +1
    -1
      internal/types/types.go
  10. +1
    -0
      pkg/constants/task.go

+ 2
- 2
desc/ai/pcm-ai.api View File

@@ -1827,8 +1827,8 @@ service AICore-api {


type ( type (
ChatReq{ 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"` ReqData map[string]interface{} `json:"reqData"`
} }
ChatResult{ ChatResult{


+ 2
- 2
desc/pcm.api View File

@@ -386,8 +386,8 @@ service pcm {


/***********chat***********/ /***********chat***********/
@doc "文本识别" @doc "文本识别"
@handler ProxyApiHandler
post /ai/chat (ChatReq) returns (CommonResp)
@handler ChatHandler
post /ai/chat (ChatReq) returns (ChatResult)
/******chat end***********/ /******chat end***********/
} }




internal/handler/ai/proxyapihandler.go → internal/handler/ai/chathandler.go View File

@@ -1,24 +1,25 @@
package ai package ai


import ( import (
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result"
"net/http"

"github.com/zeromicro/go-zero/rest/httpx" "github.com/zeromicro/go-zero/rest/httpx"
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/logic/ai" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/logic/ai"
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" "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) { return func(w http.ResponseWriter, r *http.Request) {
var req types.ChatReq var req types.ChatReq
if err := httpx.Parse(r, &req); err != nil { if err := httpx.Parse(r, &req); err != nil {
result.ParamErrorResult(r, w, err)
httpx.ErrorCtx(r.Context(), w, err)
return 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) result.HttpResult(r, w, resp, err)
} }
} }

+ 1
- 1
internal/handler/routes.go View File

@@ -460,7 +460,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
{ {
Method: http.MethodPost, Method: http.MethodPost,
Path: "/ai/chat", Path: "/ai/chat",
Handler: ai.ProxyApiHandler(serverCtx),
Handler: ai.ChatHandler(serverCtx),
}, },
}, },
rest.WithPrefix("/pcm/v1"), rest.WithPrefix("/pcm/v1"),


+ 94
- 0
internal/logic/ai/chatlogic.go View File

@@ -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
}

+ 0
- 91
internal/logic/ai/proxyapilogic.go View File

@@ -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
}

+ 5
- 4
internal/storeLink/modelarts.go View File

@@ -201,10 +201,11 @@ func (m *ModelArtsLink) GetResourceStats(ctx context.Context) (*collector.Resour
num32, _ := strconv.Atoi(resp1.Items[0].Spec.Npu.Size) num32, _ := strconv.Atoi(resp1.Items[0].Spec.Npu.Size)
var cards []*collector.Card var cards []*collector.Card
card := &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) cards = append(cards, card)
resourceStats.CardsAvail = cards resourceStats.CardsAvail = cards


+ 1
- 1
internal/svc/servicecontext.go View File

@@ -21,7 +21,7 @@ import (
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/zrpc" "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/config"
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler"
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/database" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/database"


+ 1
- 1
internal/types/types.go View File

@@ -2908,7 +2908,7 @@ type TrainingTaskStatResp struct {
} }


type ChatReq struct { type ChatReq struct {
ApiUrl string `json:"apiUrl"`
Id uint `json:"id,string"`
Method string `json:"method,optional"` Method string `json:"method,optional"`
ReqData map[string]interface{} `json:"reqData"` ReqData map[string]interface{} `json:"reqData"`
} }


+ 1
- 0
pkg/constants/task.go View File

@@ -28,4 +28,5 @@ const (
WaitStart = "WaitStart" WaitStart = "WaitStart"
Pending = "Pending" Pending = "Pending"
Stopped = "Stopped" Stopped = "Stopped"
Deploying = "Deploying"
) )

Loading…
Cancel
Save