46 lines
845 B
Go
46 lines
845 B
Go
package openai
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
type Context struct {
|
|
Request string
|
|
Response string
|
|
Time int64
|
|
}
|
|
|
|
type ContextMgr struct {
|
|
contextList []*Context
|
|
}
|
|
|
|
func (m *ContextMgr) Init() {
|
|
m.contextList = make([]*Context, 10)
|
|
}
|
|
|
|
func (m *ContextMgr) checkExpire() {
|
|
timeNow := time.Now().Unix()
|
|
if len(m.contextList) > 0 {
|
|
startPos := len(m.contextList) - 1
|
|
for i := 0; i < len(m.contextList); i++ {
|
|
if timeNow-m.contextList[i].Time < 1*60 {
|
|
startPos = i
|
|
break
|
|
}
|
|
}
|
|
|
|
m.contextList = m.contextList[startPos:]
|
|
}
|
|
}
|
|
|
|
func (m *ContextMgr) AppendMsg(request string, response string) {
|
|
m.checkExpire()
|
|
context := &Context{Request: request, Response: response, Time: time.Now().Unix()}
|
|
m.contextList = append(m.contextList, context)
|
|
}
|
|
|
|
func (m *ContextMgr) GetData() []*Context {
|
|
m.checkExpire()
|
|
return m.contextList
|
|
}
|