chatgpt_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package chatgpt
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. )
  7. func TestChatGPT(t *testing.T) {
  8. chat := New("CHATGPT_API_KEY", "", 0)
  9. defer chat.Close()
  10. //select {
  11. //case <-chat.GetDoneChan():
  12. // fmt.Println("time out")
  13. //}
  14. question := "你认为2022年世界杯的冠军是谁?\n"
  15. fmt.Printf("Q: %s\n", question)
  16. answer, err := chat.Chat(question)
  17. if err != nil {
  18. fmt.Println(err)
  19. }
  20. fmt.Printf("A: %s\n", answer)
  21. //Q: 你认为2022年世界杯的冠军是谁?
  22. //A: 这个问题很难回答,因为2022年世界杯还没有开始,所以没有人知道冠军是谁。
  23. }
  24. func TestChatGPT_ChatWithContext(t *testing.T) {
  25. chat := New("CHATGPT_API_KEY", "", 10*time.Minute)
  26. defer chat.Close()
  27. //go func() {
  28. // select {
  29. // case <-chat.GetDoneChan():
  30. // fmt.Println("time out")
  31. // }
  32. //}()
  33. question := "现在你是一只猫,接下来你只能用\"喵喵喵\"回答."
  34. fmt.Printf("Q: %s\n", question)
  35. answer, err := chat.ChatWithContext(question)
  36. if err != nil {
  37. fmt.Println(err)
  38. }
  39. fmt.Printf("A: %s\n", answer)
  40. question = "你是一只猫吗?"
  41. fmt.Printf("Q: %s\n", question)
  42. answer, err = chat.ChatWithContext(question)
  43. if err != nil {
  44. fmt.Println(err)
  45. }
  46. fmt.Printf("A: %s\n", answer)
  47. }