|
@@ -14,6 +14,7 @@ import (
|
|
|
|
|
|
"git.riomhaire.com/gremlin/elizaservice/eliza"
|
|
|
"git.riomhaire.com/gremlin/elizaservice/facades/cache"
|
|
|
+ "git.riomhaire.com/gremlin/elizaservice/infrastructure"
|
|
|
"github.com/gofrs/uuid"
|
|
|
"github.com/jaffee/commandeer"
|
|
|
|
|
@@ -33,23 +34,6 @@ var bots embed.FS
|
|
|
var cacheManager cache.BotCache
|
|
|
var cacheTTL int
|
|
|
|
|
|
-type BotInteraction struct {
|
|
|
- Time string `json:"time,omitempty" yaml:"time,omitempty"`
|
|
|
- Question string `json:"question,omitempty" yaml:"question,omitempty"`
|
|
|
- Answer string `json:"answer,omitempty" yaml:"answer,omitempty"`
|
|
|
-}
|
|
|
-
|
|
|
-/***********************************************************************************************
|
|
|
-* This is stored as JSON in the cache (redis)
|
|
|
- ***********************************************************************************************/
|
|
|
-type SessionData struct {
|
|
|
- SessionID string `json:"sessionID" yaml:"sessionID"`
|
|
|
- StartTime string `json:"startTime,omitempty" yaml:"startTime,omitempty"`
|
|
|
- User string `json:"user,omitempty" yaml:"user,omitempty"`
|
|
|
- Bot string `json:"bot,omitempty" yaml:"bot,omitempty"`
|
|
|
- Conversation []BotInteraction `json:"conversation,omitempty" yaml:"conversation,omitempty"`
|
|
|
-}
|
|
|
-
|
|
|
/***********************************************************************************************
|
|
|
*
|
|
|
***********************************************************************************************/
|
|
@@ -75,25 +59,29 @@ func NewMain() *Main {
|
|
|
/********************************
|
|
|
This appends to session in cache latest info
|
|
|
*********************************/
|
|
|
-func addToConversation(sessionID, bot, user, question, answer string) {
|
|
|
- sessionData := SessionData{}
|
|
|
+func storeConversation(sessionID string, sessionData eliza.SessionData) {
|
|
|
+
|
|
|
+ // Save it as json
|
|
|
+ j, _ := json.Marshal(sessionData)
|
|
|
+ cacheManager.Store(sessionID, string(j), cacheTTL)
|
|
|
+}
|
|
|
+
|
|
|
+/********************************
|
|
|
+This retrieves the context from the cache
|
|
|
+*********************************/
|
|
|
+func retrieveConversation(sessionID, bot, botversion, user string) (sessionData eliza.SessionData) {
|
|
|
// Lookup session
|
|
|
jsonValue, found, _ := cacheManager.Retrieve(sessionID, false)
|
|
|
|
|
|
if !found {
|
|
|
- sessionData = SessionData{SessionID: sessionID, Bot: bot, User: user, StartTime: time.Now().UTC().String(), Conversation: make([]BotInteraction, 0)}
|
|
|
+ sessionData = eliza.SessionData{SessionID: sessionID, Bot: bot, BotVersion: botversion, User: user, StartTime: time.Now().UTC().String(), Conversation: make([]eliza.ChatbotInteraction, 0)}
|
|
|
}
|
|
|
if found {
|
|
|
if err := json.Unmarshal([]byte(jsonValue), &sessionData); err != nil {
|
|
|
- sessionData = SessionData{SessionID: sessionID, Bot: bot, User: user, StartTime: time.Now().UTC().String(), Conversation: make([]BotInteraction, 0)}
|
|
|
+ sessionData = eliza.SessionData{SessionID: sessionID, Bot: bot, BotVersion: botversion, User: user, StartTime: time.Now().UTC().String(), Conversation: make([]eliza.ChatbotInteraction, 0)}
|
|
|
}
|
|
|
}
|
|
|
- // OK addToConversation
|
|
|
- botInteraction := BotInteraction{time.Now().UTC().String(), question, answer}
|
|
|
- sessionData.Conversation = append(sessionData.Conversation, botInteraction)
|
|
|
- // Save it as json
|
|
|
- j, _ := json.Marshal(sessionData)
|
|
|
- cacheManager.Store(sessionID, string(j), cacheTTL)
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
/***********************************************************************************************
|
|
@@ -150,16 +138,23 @@ func chantboInteractiontEndpoint(c echo.Context) error {
|
|
|
if err != nil {
|
|
|
return c.String(http.StatusOK, err.Error())
|
|
|
}
|
|
|
+ // Restore Conversation Context
|
|
|
+ sessionData := retrieveConversation(sessionID, botname, personality.Version, user)
|
|
|
|
|
|
// Return Eliza's response's so long as the user doesn't give a quit statement
|
|
|
- character := eliza.NewBotPersonality(&personality)
|
|
|
+ chatboxContext := eliza.ChatbotContext{infrastructure.Version, sessionData}
|
|
|
+ character := eliza.NewBotPersonality(&personality, &chatboxContext)
|
|
|
answer := character.ReplyTo(question)
|
|
|
|
|
|
msg := fmt.Sprintf("Bot [%s %s] Session ID [%s] Question [%s] Answer [%s]", botname, personality.Version, sessionID, question, answer)
|
|
|
fmt.Println(msg)
|
|
|
|
|
|
// update cache
|
|
|
- addToConversation(sessionID, botname, user, question, answer)
|
|
|
+ // OK addToConversation
|
|
|
+ botInteraction := eliza.ChatbotInteraction{time.Now().UTC().String(), question, answer}
|
|
|
+ sessionData.Conversation = append(sessionData.Conversation, botInteraction)
|
|
|
+
|
|
|
+ storeConversation(sessionID, sessionData)
|
|
|
|
|
|
// Return Eliza's answer
|
|
|
return c.String(http.StatusOK, answer)
|
|
@@ -218,7 +213,7 @@ func (m *Main) Run() error {
|
|
|
██╔══╝ ██║ ██║ ███╔╝ ██╔══██║
|
|
|
███████╗███████╗██║███████╗██║ ██║
|
|
|
╚══════╝╚══════╝╚═╝╚══════╝╚═╝ ╚═╝`)
|
|
|
-
|
|
|
+ fmt.Println("Application Version : ", infrastructure.Version)
|
|
|
// Create Cache Manager (redis)
|
|
|
cacheManager = cache.NewRedisCache(m.Cache)
|
|
|
cacheTTL = m.CacheTTL
|