ChatHome.go 818 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package chat
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. "time"
  6. )
  7. import chatgpt "github.com/chatgp/chatgpt-go"
  8. func FootApiChatAsk(c *gin.Context) { // /ping是路由
  9. //输出json结果给调用方
  10. token := ""
  11. cfValue := ""
  12. cookies := []*http.Cookie{
  13. {
  14. Name: "__Secure-next-auth.session-token",
  15. Value: token,
  16. },
  17. {
  18. Name: "cf_clearance",
  19. Value: cfValue,
  20. },
  21. }
  22. chatClient := chatgpt.NewClient(
  23. chatgpt.WithDebug(true),
  24. chatgpt.WithTimeout(60*time.Second),
  25. chatgpt.WithCookies(cookies),
  26. )
  27. question := "hello"
  28. stream, err := chatClient.GetChatStream(question)
  29. if err != nil {
  30. c.JSON(200, gin.H{
  31. "message": err.Error(),
  32. })
  33. return
  34. }
  35. var answer string
  36. for text := range stream.Stream {
  37. answer += text.Content
  38. }
  39. c.JSON(200, gin.H{
  40. "message": answer,
  41. })
  42. }