-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bcead82
commit f127fc0
Showing
7 changed files
with
135 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package chatgpt | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/patrickmn/go-cache" | ||
|
||
gpt35 "github.com/poorjobless/wechatbot/gpt-client" | ||
) | ||
|
||
type SessionService struct { | ||
cache *cache.Cache | ||
} | ||
|
||
type SessionMeta struct { | ||
Msg []gpt35.Message `json:"msg,omitempty"` | ||
} | ||
|
||
type CacheInterface interface { | ||
GetMsg(sessionId string) []gpt35.Message | ||
SetMsg(sessionId string, msg []gpt35.Message) | ||
Clear(sessionId string) | ||
} | ||
|
||
var sessionServices *SessionService | ||
|
||
func getLength(strPool []gpt35.Message) int { | ||
var total int | ||
for _, v := range strPool { | ||
bytes, _ := json.Marshal(v) | ||
total += len(string(bytes)) | ||
} | ||
return total | ||
} | ||
|
||
func (s *SessionService) GetMsg(sessionId string) (msg []gpt35.Message) { | ||
sessionContext, ok := s.cache.Get(sessionId) | ||
if !ok { | ||
return nil | ||
} | ||
sessionMeta := sessionContext.(*SessionMeta) | ||
return sessionMeta.Msg | ||
} | ||
|
||
func (s *SessionService) SetMsg(sessionId string, msg []gpt35.Message) { | ||
maxLength := 4096 | ||
maxCacheTime := time.Hour * 12 | ||
|
||
for getLength(msg) > maxLength { | ||
msg = append(msg[:1], msg[2:]...) | ||
} | ||
|
||
sessionContext, ok := s.cache.Get(sessionId) | ||
if !ok { | ||
sessionMeta := &SessionMeta{Msg: msg} | ||
s.cache.Set(sessionId, sessionMeta, maxCacheTime) | ||
return | ||
} | ||
sessionMeta := sessionContext.(*SessionMeta) | ||
sessionMeta.Msg = msg | ||
s.cache.Set(sessionId, sessionMeta, maxCacheTime) | ||
} | ||
|
||
func (s *SessionService) Clear(sessionId string) { | ||
s.cache.Delete(sessionId) | ||
} | ||
|
||
func GetSessionCache() CacheInterface { | ||
if sessionServices == nil { | ||
sessionServices = &SessionService{cache: cache.New(time.Hour*12, time.Hour*1)} | ||
} | ||
return sessionServices | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
github.com/eatmoreapple/openwechat v1.4.1 h1:hIVEr2Xaj+r1SXzdTigqhIXiuu6TZd+NPWdEVVt/qeM= | ||
github.com/eatmoreapple/openwechat v1.4.1/go.mod h1:ZxMcq7IpVWVU9JG7ERjExnm5M8/AQ6yZTtX30K3rwRQ= | ||
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= | ||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters