From 24ef65f70dbda268ccf266f7248dfc77e0f77cdd Mon Sep 17 00:00:00 2001 From: mudler Date: Sun, 12 May 2024 22:47:36 +0200 Subject: [PATCH] Enable reasoning separately --- action/intention.go | 5 +++-- agent/actions.go | 34 ++++++++++++++++++++++++++++----- agent/options.go | 6 ++++++ agent/templates.go | 4 ++-- example/webui/agentconfig.go | 1 + example/webui/agentpool.go | 3 +++ example/webui/views/create.html | 2 ++ 7 files changed, 46 insertions(+), 9 deletions(-) diff --git a/action/intention.go b/action/intention.go index eb99145..2148a42 100644 --- a/action/intention.go +++ b/action/intention.go @@ -35,8 +35,9 @@ func (a *IntentAction) Definition() ActionDefinition { Description: "A detailed reasoning on why you want to call this tool.", }, "tool": { - Type: jsonschema.String, - Enum: a.tools, + Type: jsonschema.String, + Description: "The tool you want to use", + Enum: a.tools, }, }, Required: []string{"tool", "reasoning"}, diff --git a/agent/actions.go b/agent/actions.go index c19a0b8..a731bbf 100644 --- a/agent/actions.go +++ b/agent/actions.go @@ -52,6 +52,7 @@ func (a Actions) Find(name string) Action { type decisionResult struct { actionParams action.ActionParams message string + actioName string } // decision forces the agent to take one of the available actions @@ -85,7 +86,7 @@ func (a *Agent) decision( return nil, err } - return &decisionResult{actionParams: params}, nil + return &decisionResult{actionParams: params, actioName: msg.ToolCalls[0].Function.Name}, nil } type Messages []openai.ChatCompletionMessage @@ -130,11 +131,16 @@ func (a *Agent) generateParameters(ctx context.Context, pickTemplate string, act }, conversation...) } - return a.decision(ctx, - append(conversation, openai.ChatCompletionMessage{ + cc := conversation + if a.options.forceReasoning { + cc = append(conversation, openai.ChatCompletionMessage{ Role: "system", Content: fmt.Sprintf("The agent decided to use the tool %s with the following reasoning: %s", act.Definition().Name, reasoning), - }), + }) + } + + return a.decision(ctx, + cc, a.systemInternalActions().ToTools(), openai.ToolChoice{ Type: openai.ToolTypeFunction, @@ -187,6 +193,24 @@ func (a *Agent) prepareHUD() PromptHUD { func (a *Agent) pickAction(ctx context.Context, templ string, messages []openai.ChatCompletionMessage) (Action, string, error) { c := messages + if !a.options.forceReasoning { + // We also could avoid to use functions here and get just a reply from the LLM + // and then use the reply to get the action + thought, err := a.decision(ctx, + messages, + a.systemInternalActions().ToTools(), + nil) + if err != nil { + return nil, "", err + } + // Find the action + chosenAction := a.systemInternalActions().Find(thought.actioName) + if chosenAction == nil { + return nil, "", fmt.Errorf("no action found for intent:" + thought.actioName) + } + return chosenAction, thought.message, nil + } + var promptHUD *PromptHUD if a.options.enableHUD { h := a.prepareHUD() @@ -241,7 +265,7 @@ func (a *Agent) pickAction(ctx context.Context, templ string, messages []openai. params, err := a.decision(ctx, append(c, openai.ChatCompletionMessage{ Role: "system", - Content: "The assistant thought: " + reason, + Content: "Given the assistant thought, pick the relevant action: " + reason, }), Actions{intentionsTools}.ToTools(), intentionsTools.Definition().Name) diff --git a/agent/options.go b/agent/options.go index 77590be..0c850a5 100644 --- a/agent/options.go +++ b/agent/options.go @@ -23,6 +23,7 @@ type options struct { canStopItself bool initiateConversations bool + forceReasoning bool characterfile string statefile string context context.Context @@ -78,6 +79,11 @@ var EnableHUD = func(o *options) error { return nil } +var EnableForceReasoning = func(o *options) error { + o.forceReasoning = true + return nil +} + var EnableKnowledgeBase = func(o *options) error { o.enableKB = true o.kbResults = 5 diff --git a/agent/templates.go b/agent/templates.go index 4c63643..e162f12 100644 --- a/agent/templates.go +++ b/agent/templates.go @@ -84,12 +84,12 @@ const reSelfEvalTemplate = pickSelfTemplate + ` We already have called other tools. Evaluate the current situation and decide if we need to execute other tools.` const pickActionTemplate = hudTemplate + ` -You can take any of the following tools: +When deciding what to do, consider that you can take any of the following tools: {{range .Actions -}} - {{.Name}}: {{.Description }} {{ end }} -To answer back to the user, use the "reply" tool. +To answer back to the user, use the "reply" or the "answer" tool. Given the text below, decide which action to take and explain the detailed reasoning behind it. For answering without picking a choice, reply with 'none'. {{if .Reasoning}}Reasoning: {{.Reasoning}}{{end}} diff --git a/example/webui/agentconfig.go b/example/webui/agentconfig.go index 35a3175..b3f692c 100644 --- a/example/webui/agentconfig.go +++ b/example/webui/agentconfig.go @@ -24,6 +24,7 @@ type AgentConfig struct { PeriodicRuns string `json:"periodic_runs" form:"periodic_runs"` PermanentGoal string `json:"permanent_goal" form:"permanent_goal"` EnableKnowledgeBase bool `json:"enable_kb" form:"enable_kb"` + EnableReasoning bool `json:"enable_reasoning" form:"enable_reasoning"` KnowledgeBaseResults int `json:"kb_results" form:"kb_results"` CanStopItself bool `json:"can_stop_itself" form:"can_stop_itself"` SystemPrompt string `json:"system_prompt" form:"system_prompt"` diff --git a/example/webui/agentpool.go b/example/webui/agentpool.go index 37c3556..805bc3f 100644 --- a/example/webui/agentpool.go +++ b/example/webui/agentpool.go @@ -265,6 +265,9 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error if config.EnableKnowledgeBase { opts = append(opts, EnableKnowledgeBase) } + if config.EnableReasoning { + opts = append(opts, EnableForceReasoning) + } if config.KnowledgeBaseResults > 0 { opts = append(opts, EnableKnowledgeBaseWithResults(config.KnowledgeBaseResults)) diff --git a/example/webui/views/create.html b/example/webui/views/create.html index 87f7799..b6c770c 100644 --- a/example/webui/views/create.html +++ b/example/webui/views/create.html @@ -86,6 +86,8 @@
+ +