export.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package chatgpt
  2. import (
  3. "time"
  4. "github.com/avast/retry-go"
  5. "github.com/eryajf/chatgpt-dingtalk/public"
  6. "github.com/eryajf/chatgpt-dingtalk/public/logger"
  7. )
  8. func SingleQa(question, userId string) (answer string, err error) {
  9. chat := New(userId)
  10. defer chat.Close()
  11. // 定义一个重试策略
  12. retryStrategy := []retry.Option{
  13. retry.Delay(100 * time.Millisecond),
  14. retry.Attempts(3),
  15. retry.LastErrorOnly(true),
  16. }
  17. // 使用重试策略进行重试
  18. err = retry.Do(
  19. func() error {
  20. answer, err = chat.ChatWithContext(question)
  21. if err != nil {
  22. return err
  23. }
  24. return nil
  25. },
  26. retryStrategy...)
  27. return
  28. }
  29. func ContextQa(question, userId string) (chat *ChatGPT, answer string, err error) {
  30. chat = New(userId)
  31. if public.UserService.GetUserSessionContext(userId) != "" {
  32. err := chat.ChatContext.LoadConversation(userId)
  33. if err != nil {
  34. logger.Warning("load station failed: %v\n", err)
  35. }
  36. }
  37. retryStrategy := []retry.Option{
  38. retry.Delay(100 * time.Millisecond),
  39. retry.Attempts(3),
  40. retry.LastErrorOnly(true)}
  41. // 使用重试策略进行重试
  42. err = retry.Do(
  43. func() error {
  44. answer, err = chat.ChatWithContext(question)
  45. if err != nil {
  46. return err
  47. }
  48. return nil
  49. },
  50. retryStrategy...)
  51. return
  52. }