You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

chatlogic.go 2.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package ai
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/tls"
  6. "github.com/go-resty/resty/v2"
  7. "github.com/pkg/errors"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
  9. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  10. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils/hws"
  11. "k8s.io/apimachinery/pkg/util/json"
  12. "net/http"
  13. "strings"
  14. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
  15. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
  16. "github.com/zeromicro/go-zero/core/logx"
  17. )
  18. type ChatLogic struct {
  19. logx.Logger
  20. ctx context.Context
  21. svcCtx *svc.ServiceContext
  22. }
  23. func NewChatLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatLogic {
  24. return &ChatLogic{
  25. Logger: logx.WithContext(ctx),
  26. ctx: ctx,
  27. svcCtx: svcCtx,
  28. }
  29. }
  30. func (l *ChatLogic) Chat(req *types.ChatReq) (resp *types.ChatResult, err error) {
  31. resp = &types.ChatResult{}
  32. jsonBytes, err := json.Marshal(&req.ReqData)
  33. if err != nil {
  34. logx.Errorf("【序列化请求数据失败: %v】", err)
  35. return nil, errors.New("请求数据序列化失败")
  36. }
  37. taskAi := models.TaskAi{}
  38. l.svcCtx.DbEngin.Model(models.TaskAi{}).Where("id", req.Id).Scan(&taskAi)
  39. logx.Infof("【开始处理请求,目标URL: %s】", taskAi.InferUrl)
  40. // 构建 HTTP 请求
  41. request, err := http.NewRequest("POST", taskAi.InferUrl, bytes.NewBuffer(jsonBytes))
  42. if err != nil {
  43. logx.Errorf("【构建 HTTP 请求失败: %v】", err)
  44. return nil, errors.New("网络错误,请稍后重试")
  45. }
  46. client := resty.New().SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
  47. restReq := client.R()
  48. //ModelArts
  49. cluster := models.CloudModel{}
  50. l.svcCtx.DbEngin.Table("t_cluster").Where("id", taskAi.ClusterId).Scan(&cluster)
  51. if strings.EqualFold(cluster.Label, constants.MODELARTS) {
  52. signer := &hws.Signer{
  53. Key: cluster.Ak,
  54. Secret: cluster.Sk,
  55. }
  56. if err := signer.Sign(request); err != nil {
  57. logx.Errorf("【接口签名错误: %v】", err)
  58. return nil, errors.New("网络错误,请稍后重试")
  59. }
  60. restReq.
  61. SetHeader("X-Project-Id", cluster.ProjectId).
  62. SetHeader("x-stage", "RELEASE").
  63. SetHeader("Authorization", request.Header.Get(hws.HeaderXAuthorization)).
  64. SetHeader("X-Sdk-Date", request.Header.Get(hws.HeaderXDateTime))
  65. }
  66. response, err := restReq.
  67. SetHeader("Content-Type", "application/json").
  68. SetBody(jsonBytes).
  69. SetResult(&resp).
  70. Post(taskAi.InferUrl)
  71. if err != nil {
  72. logx.Errorf("【远程调用接口URL:%s, 返回错误: %s】", taskAi.InferUrl, err.Error())
  73. return nil, errors.New("网络错误,请稍后重试")
  74. }
  75. if response.StatusCode() != 200 {
  76. logx.Errorf("【远程调用接口URL:%s, 返回错误: %s】", taskAi.InferUrl, response.Body())
  77. return nil, errors.New("网络错误,请稍后重试")
  78. }
  79. logx.Infof("【请求处理成功,目标URL: %s】", taskAi.InferUrl)
  80. return resp, nil
  81. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.