context.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package chatgpt
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/base64"
  6. "encoding/gob"
  7. "errors"
  8. "fmt"
  9. "github.com/eryajf/chatgpt-dingtalk/pkg/dingbot"
  10. "github.com/pandodao/tokenizer-go"
  11. "image/png"
  12. "os"
  13. "strings"
  14. "time"
  15. "github.com/eryajf/chatgpt-dingtalk/public"
  16. openai "github.com/sashabaranov/go-openai"
  17. )
  18. var (
  19. DefaultAiRole = "AI"
  20. DefaultHumanRole = "Human"
  21. DefaultCharacter = []string{"helpful", "creative", "clever", "friendly", "lovely", "talkative"}
  22. DefaultBackground = "The following is a conversation with AI assistant. The assistant is %s"
  23. DefaultPreset = "\n%s: 你好,让我们开始愉快的谈话!\n%s: 我是 AI assistant ,请问你有什么问题?"
  24. )
  25. type (
  26. ChatContext struct {
  27. background string // 对话背景
  28. preset string // 预设对话
  29. maxSeqTimes int // 最大对话次数
  30. aiRole *role // AI角色
  31. humanRole *role // 人类角色
  32. old []conversation // 旧对话
  33. restartSeq string // 重新开始对话的标识
  34. startSeq string // 开始对话的标识
  35. seqTimes int // 对话次数
  36. maintainSeqTimes bool // 是否维护对话次数 (自动移除旧对话)
  37. }
  38. ChatContextOption func(*ChatContext)
  39. conversation struct {
  40. Role *role
  41. Prompt string
  42. }
  43. role struct {
  44. Name string
  45. }
  46. )
  47. func NewContext(options ...ChatContextOption) *ChatContext {
  48. ctx := &ChatContext{
  49. aiRole: &role{Name: DefaultAiRole},
  50. humanRole: &role{Name: DefaultHumanRole},
  51. background: "",
  52. maxSeqTimes: 1000,
  53. preset: "",
  54. old: []conversation{},
  55. seqTimes: 0,
  56. restartSeq: "\n" + DefaultHumanRole + ": ",
  57. startSeq: "\n" + DefaultAiRole + ": ",
  58. maintainSeqTimes: false,
  59. }
  60. for _, option := range options {
  61. option(ctx)
  62. }
  63. return ctx
  64. }
  65. // PollConversation 移除最旧的一则对话
  66. func (c *ChatContext) PollConversation() {
  67. c.old = c.old[1:]
  68. c.seqTimes--
  69. }
  70. // ResetConversation 重置对话
  71. func (c *ChatContext) ResetConversation(userid string) {
  72. public.UserService.ClearUserSessionContext(userid)
  73. }
  74. // SaveConversation 保存对话
  75. func (c *ChatContext) SaveConversation(userid string) error {
  76. var buffer bytes.Buffer
  77. enc := gob.NewEncoder(&buffer)
  78. err := enc.Encode(c.old)
  79. if err != nil {
  80. return err
  81. }
  82. public.UserService.SetUserSessionContext(userid, buffer.String())
  83. return nil
  84. }
  85. // LoadConversation 加载对话
  86. func (c *ChatContext) LoadConversation(userid string) error {
  87. dec := gob.NewDecoder(strings.NewReader(public.UserService.GetUserSessionContext(userid)))
  88. err := dec.Decode(&c.old)
  89. if err != nil {
  90. return err
  91. }
  92. c.seqTimes = len(c.old)
  93. return nil
  94. }
  95. func (c *ChatContext) SetHumanRole(role string) {
  96. c.humanRole.Name = role
  97. c.restartSeq = "\n" + c.humanRole.Name + ": "
  98. }
  99. func (c *ChatContext) SetAiRole(role string) {
  100. c.aiRole.Name = role
  101. c.startSeq = "\n" + c.aiRole.Name + ": "
  102. }
  103. func (c *ChatContext) SetMaxSeqTimes(times int) {
  104. c.maxSeqTimes = times
  105. }
  106. func (c *ChatContext) GetMaxSeqTimes() int {
  107. return c.maxSeqTimes
  108. }
  109. func (c *ChatContext) SetBackground(background string) {
  110. c.background = background
  111. }
  112. func (c *ChatContext) SetPreset(preset string) {
  113. c.preset = preset
  114. }
  115. func (c *ChatGPT) ChatWithContext(question string) (answer string, err error) {
  116. question = question + "."
  117. if tokenizer.MustCalToken(question) > c.maxQuestionLen {
  118. return "", OverMaxQuestionLength
  119. }
  120. if c.ChatContext.seqTimes >= c.ChatContext.maxSeqTimes {
  121. if c.ChatContext.maintainSeqTimes {
  122. c.ChatContext.PollConversation()
  123. } else {
  124. return "", OverMaxSequenceTimes
  125. }
  126. }
  127. var promptTable []string
  128. promptTable = append(promptTable, c.ChatContext.background)
  129. promptTable = append(promptTable, c.ChatContext.preset)
  130. for _, v := range c.ChatContext.old {
  131. if v.Role == c.ChatContext.humanRole {
  132. promptTable = append(promptTable, "\n"+v.Role.Name+": "+v.Prompt)
  133. } else {
  134. promptTable = append(promptTable, v.Role.Name+": "+v.Prompt)
  135. }
  136. }
  137. promptTable = append(promptTable, "\n"+c.ChatContext.restartSeq+question)
  138. prompt := strings.Join(promptTable, "\n")
  139. prompt += c.ChatContext.startSeq
  140. if tokenizer.MustCalToken(prompt) > c.maxText-c.maxAnswerLen {
  141. return "", OverMaxTextLength
  142. }
  143. model := public.Config.Model
  144. if model == openai.GPT3Dot5Turbo0301 ||
  145. model == openai.GPT3Dot5Turbo ||
  146. model == openai.GPT4 || model == openai.GPT40314 ||
  147. model == openai.GPT432K || model == openai.GPT432K0314 {
  148. req := openai.ChatCompletionRequest{
  149. Model: model,
  150. Messages: []openai.ChatCompletionMessage{
  151. {
  152. Role: "user",
  153. Content: prompt,
  154. },
  155. },
  156. MaxTokens: c.maxAnswerLen,
  157. Temperature: 0.6,
  158. User: c.userId,
  159. }
  160. resp, err := c.client.CreateChatCompletion(c.ctx, req)
  161. if err != nil {
  162. return "", err
  163. }
  164. resp.Choices[0].Message.Content = formatAnswer(resp.Choices[0].Message.Content)
  165. c.ChatContext.old = append(c.ChatContext.old, conversation{
  166. Role: c.ChatContext.humanRole,
  167. Prompt: question,
  168. })
  169. c.ChatContext.old = append(c.ChatContext.old, conversation{
  170. Role: c.ChatContext.aiRole,
  171. Prompt: resp.Choices[0].Message.Content,
  172. })
  173. c.ChatContext.seqTimes++
  174. return resp.Choices[0].Message.Content, nil
  175. } else {
  176. req := openai.CompletionRequest{
  177. Model: model,
  178. MaxTokens: c.maxAnswerLen,
  179. Prompt: prompt,
  180. Temperature: 0.6,
  181. User: c.userId,
  182. Stop: []string{c.ChatContext.aiRole.Name + ":", c.ChatContext.humanRole.Name + ":"},
  183. }
  184. resp, err := c.client.CreateCompletion(c.ctx, req)
  185. if err != nil {
  186. return "", err
  187. }
  188. resp.Choices[0].Text = formatAnswer(resp.Choices[0].Text)
  189. c.ChatContext.old = append(c.ChatContext.old, conversation{
  190. Role: c.ChatContext.humanRole,
  191. Prompt: question,
  192. })
  193. c.ChatContext.old = append(c.ChatContext.old, conversation{
  194. Role: c.ChatContext.aiRole,
  195. Prompt: resp.Choices[0].Text,
  196. })
  197. c.ChatContext.seqTimes++
  198. return resp.Choices[0].Text, nil
  199. }
  200. }
  201. func (c *ChatGPT) GenreateImage(ctx context.Context, prompt string) (string, error) {
  202. model := public.Config.Model
  203. if model == openai.GPT3Dot5Turbo0301 ||
  204. model == openai.GPT3Dot5Turbo ||
  205. model == openai.GPT4 || model == openai.GPT40314 ||
  206. model == openai.GPT432K || model == openai.GPT432K0314 {
  207. req := openai.ImageRequest{
  208. Prompt: prompt,
  209. Size: openai.CreateImageSize1024x1024,
  210. ResponseFormat: openai.CreateImageResponseFormatB64JSON,
  211. N: 1,
  212. User: c.userId,
  213. }
  214. respBase64, err := c.client.CreateImage(c.ctx, req)
  215. if err != nil {
  216. return "", err
  217. }
  218. imgBytes, err := base64.StdEncoding.DecodeString(respBase64.Data[0].B64JSON)
  219. if err != nil {
  220. return "", err
  221. }
  222. r := bytes.NewReader(imgBytes)
  223. imgData, err := png.Decode(r)
  224. if err != nil {
  225. return "", err
  226. }
  227. imageName := time.Now().Format("20060102-150405") + ".png"
  228. clientId, _ := ctx.Value(public.DingTalkClientIdKeyName).(string)
  229. client := public.DingTalkClientManager.GetClientByOAuthClientID(clientId)
  230. mediaResult, uploadErr := &dingbot.MediaUploadResult{}, errors.New(fmt.Sprintf("unknown clientId: %s", clientId))
  231. if client != nil {
  232. mediaResult, uploadErr = client.UploadMedia(imgBytes, imageName, dingbot.MediaTypeImage, dingbot.MimeTypeImagePng)
  233. }
  234. err = os.MkdirAll("data/images", 0755)
  235. if err != nil {
  236. return "", err
  237. }
  238. file, err := os.Create("data/images/" + imageName)
  239. if err != nil {
  240. return "", err
  241. }
  242. defer file.Close()
  243. if err := png.Encode(file, imgData); err != nil {
  244. return "", err
  245. }
  246. if uploadErr == nil {
  247. return mediaResult.MediaID, nil
  248. } else {
  249. return public.Config.ServiceURL + "/images/" + imageName, nil
  250. }
  251. }
  252. return "", nil
  253. }
  254. func WithMaxSeqTimes(times int) ChatContextOption {
  255. return func(c *ChatContext) {
  256. c.SetMaxSeqTimes(times)
  257. }
  258. }
  259. // WithOldConversation 从文件中加载对话
  260. func WithOldConversation(userid string) ChatContextOption {
  261. return func(c *ChatContext) {
  262. _ = c.LoadConversation(userid)
  263. }
  264. }
  265. func WithMaintainSeqTimes(maintain bool) ChatContextOption {
  266. return func(c *ChatContext) {
  267. c.maintainSeqTimes = maintain
  268. }
  269. }