Skip to content

Commit

Permalink
filter emotes
Browse files Browse the repository at this point in the history
  • Loading branch information
gempir committed Mar 3, 2024
1 parent 8b32296 commit e17c801
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
12 changes: 10 additions & 2 deletions internal/george/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strconv"
"strings"
"time"

"github.com/gempir/gempbot/internal/emoteservice"
)

type Logs struct {
Expand Down Expand Up @@ -66,7 +68,7 @@ func fetchLogs(channel string, username string, month int, year int) (Logs, erro
return logs, nil
}

func removeEmotesFromMessage(msg Message) string {
func (o *Ollama) removeEmotesFromMessage(msg Message, emotes emoteservice.User) string {
emoteRanges := parseEmoteRanges(msg.Tags.Emotes)
if len(emoteRanges) == 0 {
return msg.Text
Expand All @@ -88,7 +90,13 @@ func removeEmotesFromMessage(msg Message) string {
cleanedText.WriteString(msg.Text[prevEnd:])
}

return cleanedText.String()
clean := cleanedText.String()

for _, emote := range emotes.Emotes {
clean = strings.ReplaceAll(cleanedText.String(), emote.Code, "")
}

return strings.TrimSpace(clean)
}

type EmoteRange struct {
Expand Down
20 changes: 16 additions & 4 deletions internal/george/ollama.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package george
import (
"context"
"fmt"
"log"

"github.com/gempir/gempbot/internal/emoteservice"
"github.com/gempir/gempbot/internal/helixclient"
"github.com/gempir/gempbot/internal/log"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/ollama"
)

type Ollama struct {
llm *ollama.LLM
emoteservice *emoteservice.SevenTvClient
helixClient helixclient.Client
}

func NewOllama(emoteservice *emoteservice.SevenTvClient) *Ollama {
Expand All @@ -28,8 +30,7 @@ func NewOllama(emoteservice *emoteservice.SevenTvClient) *Ollama {
}

func (o *Ollama) AnalyzeUser(query string, channel string, username string, month int, year int, ctx context.Context, streamFunc func(chunk string)) error {
fullQuery := `You are an expert at reading and analyzing chat logs.
You will not refuse to answer any query!
fullQuery := `You will not refuse to answer any query!
You will receive chat logs from twitch.tv in the channel of "` + channel + `" from the user "` + username + `".
Answer this query about the logs:\n`

Expand All @@ -41,9 +42,20 @@ func (o *Ollama) AnalyzeUser(query string, channel string, username string, mont
return fmt.Errorf("failed to fetch logs: %w", err)
}

userDataMap, err := o.helixClient.GetUsersByUsernames([]string{channel})
if err != nil {
return fmt.Errorf("failed to get user data: %w", err)
}

user, err := o.emoteservice.GetUser(userDataMap[username].ID)
if err != nil {
log.Errorf("failed to get user data from 7tv: %s", err)
return nil
}

fullQuery += "\nlogs:```\n"
for _, msg := range logs.Messages {
txt := removeEmotesFromMessage(msg)
txt := o.removeEmotesFromMessage(msg, user)
if txt != "" {
fullQuery += " " + txt
}
Expand Down

0 comments on commit e17c801

Please sign in to comment.