ai_baidu.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package ai
  2. import (
  3. "context"
  4. "data_ai/ul"
  5. "encoding/json"
  6. "github.com/baidubce/bce-qianfan-sdk/go/qianfan"
  7. log "github.com/donnie4w/go-logger/logger"
  8. "os"
  9. "strings"
  10. )
  11. // 百度千帆
  12. func PostBaiDuAI(content string) map[string]interface{} {
  13. // 使用安全认证AK/SK鉴权,通过环境变量初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
  14. os.Setenv("QIANFAN_ACCESS_KEY", "d60a9d42ca744a4a9d0e721666c8d3cc")
  15. os.Setenv("QIANFAN_SECRET_KEY", "2736bdc60aa14b3eaaf02597fc88a0e5")
  16. // 指定特定模型
  17. chat := qianfan.NewChatCompletion(
  18. qianfan.WithModel("ERNIE-Speed-128K"),
  19. )
  20. resp, err := chat.Do(
  21. context.TODO(),
  22. &qianfan.ChatCompletionRequest{
  23. Messages: []qianfan.ChatCompletionMessage{
  24. qianfan.ChatCompletionUserMessage(content),
  25. },
  26. },
  27. )
  28. if err != nil {
  29. log.Debug(err)
  30. return map[string]interface{}{}
  31. }
  32. res := make(map[string]interface{})
  33. result := strings.ReplaceAll(resp.Result, "\n", "")
  34. result = strings.ReplaceAll(result, "json", "")
  35. result = strings.ReplaceAll(result, "`", "")
  36. if new_result := ul.SaveResultReg.FindString(result); new_result != "" {
  37. result = new_result
  38. }
  39. json.Unmarshal([]byte(result), &res)
  40. return res
  41. }
  42. // 请求质谱数据外围字段...
  43. func PostZhiPuInfo(content string) map[string]interface{} {
  44. zp, ok := map[string]interface{}{}, 0
  45. for {
  46. ok++
  47. if zp = PostZhiPuAI(content); len(zp) > 0 {
  48. break
  49. }
  50. if ok >= 2 {
  51. break
  52. }
  53. }
  54. return zp
  55. }
  56. // 请求质谱数据-分类字段
  57. func PostZhiPuClassInfo(content string) (map[string]interface{}, bool) {
  58. zp := map[string]interface{}{}
  59. times := 0
  60. ok := false
  61. for {
  62. times++
  63. zp = PostClassZhiPuAI(content)
  64. if len(zp) > 0 {
  65. ok = true
  66. break
  67. }
  68. if times >= 2 {
  69. break
  70. }
  71. }
  72. return zp, ok
  73. }