biz_model.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "go-admin/app/observe/service/dto"
  6. "go-admin/utils"
  7. )
  8. type BigModel struct {
  9. // service.Service
  10. utils.OtService
  11. }
  12. func (b *BigModel) RequestAiPlatform(req *dto.AskAiModel, resp *dto.BigModelResp) error {
  13. token := "2029160c1fdae2de4a30d989080dc2ef.YsXIveZGpmeUhROe"
  14. url := "https://open.bigmodel.cn/api/paas/v4/chat/completions"
  15. tips := "是什么问题,请言简意赅解释“问题原因”以及“排查思路”,必须使用中文回答,只分析客观的技术问题,不需要加最后的简单总结或者其他帮助方法,不要加任何总结性文字。问题答案输出为标准且好看的html格式"
  16. fmt.Println("content:", req.Content)
  17. content := fmt.Sprintf(
  18. "“%s”%s",
  19. req.Content,
  20. tips,
  21. )
  22. bpm := []dto.BigModelParamsMessage{
  23. {
  24. Role: "user",
  25. Content: content,
  26. },
  27. }
  28. ps := &dto.BigModelParams{
  29. Model: "glm-4",
  30. Messages: bpm,
  31. }
  32. params, _ := json.Marshal(ps)
  33. header := map[string]string{
  34. "Authorization": fmt.Sprintf("Bearer %s", token),
  35. }
  36. response, err := utils.Post(url, params, header)
  37. if err != nil {
  38. fmt.Sprintln("post error: ", err.Error())
  39. return err
  40. }
  41. if err := json.Unmarshal(response, resp); err != nil {
  42. fmt.Sprintln("unmarshal error: ", err.Error())
  43. return err
  44. }
  45. return nil
  46. }