model.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package service
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/os/gctx"
  5. "github.com/safejob/dify-sdk-go"
  6. "github.com/safejob/dify-sdk-go/types"
  7. "log"
  8. )
  9. type Model struct {
  10. }
  11. func (ml *Model) Execute(b *BidInfo) (bool, string, int) {
  12. return ml.Do(b, 2)
  13. }
  14. func (ml *Model) Do(b *BidInfo, t int) (bool, string, int) {
  15. result := ml.post(b.Id, b.Detail, t)
  16. if result == nil {
  17. return false, "", 0
  18. }
  19. for k, v := range result {
  20. if v == nil {
  21. delete(result, k)
  22. }
  23. }
  24. quoteMode, _ := result["报价模式"].(string)
  25. bidCommonwealth := 0
  26. if result["中标联合体"] != nil {
  27. if result["中标联合体"] == "是" {
  28. bidCommonwealth = 1
  29. } else {
  30. bidCommonwealth = -1
  31. }
  32. }
  33. return true, quoteMode, bidCommonwealth
  34. }
  35. func (ml *Model) post(_id, detail string, t int) map[string]interface{} {
  36. ctx := gctx.New()
  37. client, err := dify.NewClient(dify.ClientConfig{
  38. ApiServer: g.Config().MustGet(ctx, "model.apiServer").String(),
  39. ApiKey: g.Config().MustGet(ctx, "model.apiKey").String(),
  40. User: g.Config().MustGet(ctx, "model.user").String(),
  41. })
  42. if err != nil {
  43. log.Println(_id, "%v :Error running client: %v", err)
  44. return nil
  45. }
  46. resp, err := client.WorkflowApp().RunBlock(ctx, types.WorkflowRequest{
  47. Inputs: map[string]interface{}{
  48. "detail": detail,
  49. "type": t,
  50. },
  51. })
  52. if err != nil {
  53. log.Println(_id, "%v :Error running client: %v", err)
  54. return nil
  55. }
  56. log.Println(_id, "大模型", resp.Data.Outputs)
  57. return resp.Data.Outputs
  58. }