gpt.go 713 B

123456789101112131415161718192021222324252627
  1. package public
  2. import (
  3. "fmt"
  4. "github.com/eryajf/chatgpt-dingtalk/config"
  5. "github.com/solywsh/chatgpt"
  6. )
  7. func SingleQa(question, userId string) (answer string, err error) {
  8. cfg := config.LoadConfig()
  9. chat := chatgpt.New(cfg.ApiKey, userId, cfg.SessionTimeout)
  10. defer chat.Close()
  11. return chat.ChatWithContext(question)
  12. }
  13. func ContextQa(question, userId string) (chat *chatgpt.ChatGPT, answer string, err error) {
  14. cfg := config.LoadConfig()
  15. chat = chatgpt.New(cfg.ApiKey, userId, cfg.SessionTimeout)
  16. path := "openaiCache/" + userId
  17. err = chat.ChatContext.LoadConversation(path)
  18. if err != nil {
  19. fmt.Printf("load station failed: %v\n", err)
  20. }
  21. answer, err = chat.ChatWithContext(question)
  22. return
  23. }