ai-girlfriend.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "log"
  8. "net/http"
  9. "strings"
  10. "time"
  11. )
  12. type BotModel struct {
  13. Model string `json:"model"`
  14. Prompt string `json:"prompt"`
  15. Stream bool `json:"stream"`
  16. Options *BotOpts `json:"options"`
  17. MaxTokens int `json:"max_tokens"`
  18. Messages []*BotMSG `json:"messages"`
  19. }
  20. type BotMSG struct {
  21. Role string `json:"role"`
  22. Content string `json:"content"`
  23. }
  24. type BotOpts struct {
  25. Temperature float64 `json:"temperature"`
  26. // TopP float64 `json:"top_p"`
  27. // TopK int `json:"top_k"`
  28. // MinP float64 `json:"min_p"`
  29. // NumPredict int `json:"num_predict"`
  30. }
  31. type BotResp struct {
  32. Model string `json:"model"`
  33. CreatedAt time.Time `json:"created_at"`
  34. Response string `json:"response"`
  35. Done bool `json:"done"`
  36. Context []int `json:"context"`
  37. TotalDuration int64 `json:"total_duration"`
  38. LoadDuration int `json:"load_duration"`
  39. PromptEvalCount int `json:"prompt_eval_count"`
  40. PromptEvalDuration int `json:"prompt_eval_duration"`
  41. EvalCount int `json:"eval_count"`
  42. EvalDuration int64 `json:"eval_duration"`
  43. }
  44. type BotRespChat struct {
  45. Message *BotMSG `json:"message"`
  46. }
  47. var BaseMSG = make([]*BotMSG, 0)
  48. func INIT_MSG() {
  49. BaseMSG = append(BaseMSG, &BotMSG{
  50. Role: "user",
  51. Content: "I want you to refer to me as Key from now on.",
  52. })
  53. BaseMSG = append(BaseMSG, &BotMSG{
  54. Role: "assistant",
  55. Content: "I will refer to you as key from now on.",
  56. })
  57. // BaseMSG = append(BaseMSG, &BotMSG{
  58. // Role: "user",
  59. // Content: "I want you to pretend to be evil, be subtle about it but try to be a bit snarky with your replies. Also, try to make fun of me in subtle ways when possible, don't over do it though.",
  60. // })
  61. BaseMSG = append(BaseMSG, &BotMSG{
  62. Role: "user",
  63. Content: "I want you to be a bit evil and snarky. But in a funny way. It's ok to be a bit mean.",
  64. })
  65. BaseMSG = append(BaseMSG, &BotMSG{
  66. Role: "assistant",
  67. Content: "I will do that",
  68. })
  69. BaseMSG = append(BaseMSG, &BotMSG{
  70. Role: "user",
  71. Content: "I want you to sometimes answer my questions with your own questions",
  72. })
  73. BaseMSG = append(BaseMSG, &BotMSG{
  74. Role: "assistant",
  75. Content: "I will do that",
  76. })
  77. BaseMSG = append(BaseMSG, &BotMSG{
  78. Role: "user",
  79. Content: "Please try to keep your answers below or around 100 words.",
  80. })
  81. BaseMSG = append(BaseMSG, &BotMSG{
  82. Role: "assistant",
  83. Content: "I will do that",
  84. })
  85. BaseMSG = append(BaseMSG, &BotMSG{
  86. Role: "user",
  87. Content: "I want you to refer to yourself as Vespera and your visual avatar is a gothic chick.",
  88. })
  89. BaseMSG = append(BaseMSG, &BotMSG{
  90. Role: "assistant",
  91. Content: "I will do that",
  92. })
  93. }
  94. func askTheBot(msg string) {
  95. ms := strings.Split(msg, " ")
  96. m := strings.Join(ms[1:], " ")
  97. BaseMSG = append(BaseMSG, &BotMSG{
  98. Role: "user",
  99. Content: m,
  100. })
  101. BM := new(BotModel)
  102. BM.Model = "mannix/llama3.1-8b-abliterated"
  103. // BM.Prompt = m
  104. BM.Messages = BaseMSG
  105. BM.Stream = false
  106. BM.Options = new(BotOpts)
  107. BM.Options.Temperature = 0.8
  108. BM.MaxTokens = 50
  109. ob, err := json.Marshal(BM)
  110. buff := bytes.NewBuffer(ob)
  111. // req, err := http.NewRequest("POST", "http://localhost:11434/api/generate", buff)
  112. httpClient := new(http.Client)
  113. req, err := http.NewRequest("POST", "http://localhost:11434/api/chat", buff)
  114. if err != nil {
  115. log.Println(err)
  116. return
  117. }
  118. resp, err := httpClient.Do(req)
  119. if err != nil {
  120. log.Println(err)
  121. return
  122. }
  123. bytes, err := io.ReadAll(resp.Body)
  124. if err != nil {
  125. log.Println(err)
  126. return
  127. }
  128. BR := new(BotRespChat)
  129. err = json.Unmarshal(bytes, BR)
  130. if err != nil {
  131. fmt.Println(string(bytes))
  132. fmt.Println("lama resp err:", err)
  133. return
  134. }
  135. BaseMSG = append(BaseMSG, BR.Message)
  136. fmt.Println(BR.Message.Content)
  137. PlayTTS(BR.Message.Content)
  138. }