Avoid compression

This commit is contained in:
mudler
2024-04-04 20:25:02 +02:00
parent 98c53e042d
commit 5c58072ad7
2 changed files with 55 additions and 5 deletions

View File

@@ -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) { 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! // prepare the prompt
conversation, _, _, err := a.prepareConversationParse(pickTemplate, c, false, reasoning) stateHUD := bytes.NewBuffer([]byte{})
promptTemplate, err := template.New("pickAction").Parse(hudTemplate)
if err != nil { if err != nil {
return nil, err 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, return a.decision(ctx,
conversation, conversation,
a.systemActions().ToTools(), a.systemActions().ToTools(),

View File

@@ -1,6 +1,6 @@
package agent 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}} {{if .Character.Name}}Name: {{.Character.Name}}
{{end}}{{if .Character.Age}}Age: {{.Character.Age}} {{end}}{{if .Character.Age}}Age: {{.Character.Age}}
{{end}}{{if .Character.Occupation}}Occupation: {{.Character.Occupation}} {{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. You can't ask things to the user as you are thinking by yourself. You are autonomous.
{{if .Reasoning}}Reasoning: {{.Reasoning}}{{end}} {{if .Reasoning}}Reasoning: {{.Reasoning}}{{end}}
` + hud ` + hudTemplate
const reSelfEvalTemplate = pickSelfTemplate + ` const reSelfEvalTemplate = pickSelfTemplate + `
We already have called other tools. Evaluate the current situation and decide if we need to execute other tools.` 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: You can take any of the following tools:
{{range .Actions -}} {{range .Actions -}}