From 5c58072ad77a3c5818b4e10415b6a3c9535bc2d5 Mon Sep 17 00:00:00 2001 From: mudler Date: Thu, 4 Apr 2024 20:25:02 +0200 Subject: [PATCH] Avoid compression --- agent/actions.go | 54 ++++++++++++++++++++++++++++++++++++++++++++-- agent/templates.go | 6 +++--- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/agent/actions.go b/agent/actions.go index 3dbd0d5..d433423 100644 --- a/agent/actions.go +++ b/agent/actions.go @@ -87,12 +87,62 @@ func (a *Agent) decision( func (a *Agent) generateParameters(ctx context.Context, pickTemplate string, act Action, c []openai.ChatCompletionMessage, reasoning string) (*decisionResult, error) { - // XXX: compressing conversation for generating parameters.. sucks! - conversation, _, _, err := a.prepareConversationParse(pickTemplate, c, false, reasoning) + // prepare the prompt + stateHUD := bytes.NewBuffer([]byte{}) + + promptTemplate, err := template.New("pickAction").Parse(hudTemplate) if err != nil { return nil, err } + actions := a.systemInternalActions() + + // Get all the actions definitions + definitions := []action.ActionDefinition{} + for _, m := range actions { + definitions = append(definitions, m.Definition()) + } + + var promptHUD *PromptHUD + if a.options.enableHUD { + h := a.prepareHUD() + promptHUD = &h + } + + err = promptTemplate.Execute(stateHUD, struct { + HUD *PromptHUD + Actions []action.ActionDefinition + Reasoning string + Messages []openai.ChatCompletionMessage + }{ + Actions: definitions, + Reasoning: reasoning, + HUD: promptHUD, + }) + if err != nil { + return nil, err + } + + // check if there is already a message with the hud in the conversation already, otherwise + // add a message at the top with it + + conversation := c + found := false + for _, cc := range c { + if cc.Content == stateHUD.String() { + found = true + break + } + } + if !found && a.options.enableHUD { + conversation = append([]openai.ChatCompletionMessage{ + { + Role: "system", + Content: stateHUD.String(), + }, + }, conversation...) + } + return a.decision(ctx, conversation, a.systemActions().ToTools(), diff --git a/agent/templates.go b/agent/templates.go index 31d1f10..5cb6c47 100644 --- a/agent/templates.go +++ b/agent/templates.go @@ -1,6 +1,6 @@ package agent -const hud = `{{with .HUD }}You have a character and your replies and actions might be influenced by it. +const hudTemplate = `{{with .HUD }}You have a character and your replies and actions might be influenced by it. {{if .Character.Name}}Name: {{.Character.Name}} {{end}}{{if .Character.Age}}Age: {{.Character.Age}} {{end}}{{if .Character.Occupation}}Occupation: {{.Character.Occupation}} @@ -38,13 +38,13 @@ You can update the short-term goal, the current action, the next action, the his You can't ask things to the user as you are thinking by yourself. You are autonomous. {{if .Reasoning}}Reasoning: {{.Reasoning}}{{end}} -` + hud +` + hudTemplate const reSelfEvalTemplate = pickSelfTemplate + ` We already have called other tools. Evaluate the current situation and decide if we need to execute other tools.` -const pickActionTemplate = hud + ` +const pickActionTemplate = hudTemplate + ` You can take any of the following tools: {{range .Actions -}}