Skip to content

Commit

Permalink
Add LLM command code
Browse files Browse the repository at this point in the history
  • Loading branch information
kercre123 committed Apr 12, 2024
1 parent 45b620e commit 5e41488
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 130 deletions.
24 changes: 24 additions & 0 deletions chipper/pkg/vars/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"

"github.com/fforchino/vector-go-sdk/pkg/vector"
"github.com/kercre123/wire-pod/chipper/pkg/logger"
"github.com/sashabaranov/go-openai"
)
Expand Down Expand Up @@ -417,3 +419,25 @@ func LoadChats() {
}
json.Unmarshal(file, &RememberedChats)
}

func GetRobot(esn string) (*vector.Vector, error) {
var guid string
var target string
matched := false
for _, bot := range BotInfo.Robots {
if esn == bot.Esn {
guid = bot.GUID
target = bot.IPAddress + ":443"
matched = true
break
}
}
if !matched {
return nil, errors.New("robot not in botsdkinfo")
}
robot, err := vector.New(vector.WithSerialNo(esn), vector.WithToken(guid), vector.WithTarget(target))
if err != nil {
return nil, err
}
return robot, nil
}
97 changes: 45 additions & 52 deletions chipper/pkg/wirepod/ttr/kgsim.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func StreamingKGSim(req interface{}, esn string, transcribedText string) (string
} else {
robName = "Vector"
}
defaultPrompt := "You are a helpful robot called " + robName + ". The prompt may not be punctuated or spelled correctly as the STT model is small. The answer will be put through TTS, so it should be a speakable string. Keep the answer concise yet informative."
defaultPrompt := "You are a helpful, animated robot called " + robName + ". Keep the response concise yet informative."

var nChat []openai.ChatCompletionMessage

Expand All @@ -105,6 +105,10 @@ func StreamingKGSim(req interface{}, esn string, transcribedText string) (string
smsg.Content = defaultPrompt
}

smsg.Content = CreatePrompt(smsg.Content)

logger.Println("Full prompt: " + smsg.Content)

nChat = append(nChat, smsg)
if vars.APIConfig.Knowledge.SaveChat {
rchat := GetChat(esn)
Expand All @@ -122,7 +126,7 @@ func StreamingKGSim(req interface{}, esn string, transcribedText string) (string
Stream: true,
}
if vars.APIConfig.Knowledge.Provider == "openai" {
aireq.Model = openai.GPT4Turbo1106
aireq.Model = "gpt-4-turbo"
logger.Println("Using " + aireq.Model)
} else {
logger.Println("Using " + vars.APIConfig.Knowledge.Model)
Expand Down Expand Up @@ -239,19 +243,19 @@ func StreamingKGSim(req interface{}, esn string, transcribedText string) (string
ctx,
)
if err != nil {
log.Println(err)
logger.Println(err)
return
}

if err := r.Send(controlRequest); err != nil {
log.Println(err)
logger.Println(err)
return
}

for {
ctrlresp, err := r.Recv()
if err != nil {
log.Println(err)
logger.Println(err)
return
}
if ctrlresp.GetControlGrantedResponse() != nil {
Expand Down Expand Up @@ -282,8 +286,8 @@ func StreamingKGSim(req interface{}, esn string, transcribedText string) (string
// * end - modified from official vector-go-sdk
}()

var stopTTSLoop bool
TTSLoopStopped := make(chan bool)
//var stopTTSLoop bool
//TTSLoopStopped := make(chan bool)
for range start {
time.Sleep(time.Millisecond * 300)
robot.Conn.PlayAnimation(
Expand All @@ -295,29 +299,29 @@ func StreamingKGSim(req interface{}, esn string, transcribedText string) (string
Loops: 1,
},
)
go func() {
for {
if stopTTSLoop {
TTSLoopStopped <- true
break
}
robot.Conn.PlayAnimation(
ctx,
&vectorpb.PlayAnimationRequest{
Animation: &vectorpb.Animation{
Name: "anim_tts_loop_02",
},
Loops: 1,
},
)
}
}()
// go func() {
// for {
// if stopTTSLoop {
// TTSLoopStopped <- true
// break
// }
// robot.Conn.PlayAnimation(
// ctx,
// &vectorpb.PlayAnimationRequest{
// Animation: &vectorpb.Animation{
// Name: "anim_tts_loop_02",
// },
// Loops: 1,
// },
// )
// }
// }()
numInResp := 0
for {
respSlice := fullRespSlice
if len(respSlice)-1 < numInResp {
if !isDone {
fmt.Println("waiting...")
logger.Println("Waiting for more content from LLM...")
for range speakReady {
respSlice = fullRespSlice
break
Expand All @@ -327,36 +331,25 @@ func StreamingKGSim(req interface{}, esn string, transcribedText string) (string
}
}
logger.Println(respSlice[numInResp])
_, err := robot.Conn.SayText(
ctx,
&vectorpb.SayTextRequest{
Text: respSlice[numInResp],
UseVectorVoice: true,
DurationScalar: 1.0,
},
)
if err != nil {
logger.Println("KG SayText error: " + err.Error())
stop <- true
break
}
acts := GetActionsFromString(respSlice[numInResp])
PerformActions(acts, robot)
numInResp = numInResp + 1
}
stopTTSLoop = true
for range TTSLoopStopped {
time.Sleep(time.Millisecond * 100)
robot.Conn.PlayAnimation(
ctx,
&vectorpb.PlayAnimationRequest{
Animation: &vectorpb.Animation{
Name: "anim_knowledgegraph_success_01",
},
Loops: 1,
//stopTTSLoop = true
// for range TTSLoopStopped {
time.Sleep(time.Millisecond * 100)
robot.Conn.PlayAnimation(
ctx,
&vectorpb.PlayAnimationRequest{
Animation: &vectorpb.Animation{
Name: "anim_knowledgegraph_success_01",
},
)
//time.Sleep(time.Millisecond * 3300)
stop <- true
}
Loops: 1,
},
)
//time.Sleep(time.Millisecond * 3300)
stop <- true
//}
}
return "", nil
}
Expand Down
Loading

1 comment on commit 5e41488

@kercre123
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explaining this more:

The LLM can now make the robot perform animations during the spoken response. I accidentally pushed this to main, thinking that I was pushing this to a branch. I was going to explain it more in a PR.
The word2num code for timer functionality is also better now.

Please sign in to comment.