Skip to content
This repository has been archived by the owner on Jan 27, 2024. It is now read-only.

Commit

Permalink
reduce false positives for activations
Browse files Browse the repository at this point in the history
  • Loading branch information
theomonnom committed Apr 9, 2023
1 parent b45f164 commit 128b4af
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
35 changes: 21 additions & 14 deletions lkgpt-service/pkg/service/gptparticipant.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
lksdk "github.com/livekit/server-sdk-go"
"github.com/pion/webrtc/v3"
"github.com/sashabaranov/go-openai"
"golang.org/x/exp/slices"
)

var (
Expand All @@ -26,8 +27,9 @@ var (

BotIdentity = "KITT"

// Naive trigger implementation
// Naive trigger/activation implementation
GreetingWords = []string{"hi", "hello", "hey", "hallo", "salut", "bonjour", "hola", "eh", "ey", "嘿", "你好", "やあ", "おい"}
NameWords = []string{"kit", "gpt", "kitt", "livekit", "live-kit"}

Languages = map[string]*Language{
"en-US": {
Expand Down Expand Up @@ -298,24 +300,30 @@ func (p *GPTParticipant) onTranscriptionReceived(result RecognizeResult, rp *lks
} else {
// Check if the participant is activating the KITT
justActivated := false
words := strings.Split(strings.TrimSpace(result.Text), " ")
if len(words) >= 2 { // No max length but only check the first 4 words
words := strings.Split(strings.ToLower(strings.TrimSpace(result.Text)), " ")
if len(words) >= 2 { // No max length but only check the first 3 words
limit := len(words)
if limit > 4 {
limit = 4
if limit > 3 {
limit = 3
}
text := strings.ToLower(strings.Join(words[:limit], ""))
activationWords := words[:limit]

// Check if text contains at least one GreentingWords
greeting := false
greetIndex := -1
for _, greet := range GreetingWords {
if strings.Contains(text, greet) {
greeting = true
if greetIndex = slices.Index(activationWords, greet); greetIndex != -1 {
break
}
}
subject := strings.Contains(text, "kit") || strings.Contains(text, "gpt")
if greeting && subject {

nameIndex := -1
for _, name := range NameWords {
if nameIndex = slices.Index(activationWords, name); nameIndex != -1 {
break
}
}

if greetIndex < nameIndex && greetIndex != -1 {
justActivated = true
if activeParticipant != rp {
activeParticipant = rp
Expand All @@ -324,16 +332,15 @@ func (p *GPTParticipant) onTranscriptionReceived(result RecognizeResult, rp *lks
p.activeParticipant = rp
p.lock.Unlock()

logger.Debugw("activating KITT for participant", "participant", rp.Identity())
logger.Debugw("activating KITT for participant", "activationText", strings.Join(activationWords, " "), "participant", rp.Identity())
_ = p.sendStatePacket(state_Active)
}

}
}

if result.IsFinal {
shouldAnswer = activeParticipant == rp
if justActivated && len(words) <= 4 {
if justActivated && len(words) <= 3 {
// Ignore if the participant stopped speaking after the activation
// Answer his next sentence
shouldAnswer = false
Expand Down
4 changes: 2 additions & 2 deletions lkgpt-service/pkg/service/transcriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (t *Transcriber) start() error {
}

// Read the whole transcription and put inside one string
// We don't need to process each part individually
// We don't need to process each part individually (atm?)
var sb strings.Builder
final := false
for _, result := range resp.Results {
Expand Down Expand Up @@ -244,7 +244,7 @@ func (t *Transcriber) newStream() (sttpb.Speech_StreamingRecognizeClient, error)
{Value: "Kit-t"},
{Value: "Kit"},
},
Boost: 8,
Boost: 10,
},
},
CustomClasses: []*sttpb.CustomClass{
Expand Down

0 comments on commit 128b4af

Please sign in to comment.