Enable reasoning separately

This commit is contained in:
mudler
2024-05-12 22:47:36 +02:00
parent 5c3497a433
commit 24ef65f70d
7 changed files with 46 additions and 9 deletions

View File

@@ -36,6 +36,7 @@ func (a *IntentAction) Definition() ActionDefinition {
},
"tool": {
Type: jsonschema.String,
Description: "The tool you want to use",
Enum: a.tools,
},
},

View File

@@ -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)

View File

@@ -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

View File

@@ -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}}

View File

@@ -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"`

View File

@@ -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))

View File

@@ -86,6 +86,8 @@
<div class="mb-4">
<label for="enable_kb" class="block text-lg font-medium text-gray-400">Enable Knowledge Base</label>
<input type="checkbox" name="enable_kb" id="enable_kb" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-lg border-gray-300 rounded-md bg-gray-700 text-white">
<label for="enable_reasoning" class="block text-lg font-medium text-gray-400">Enable Reasoning</label>
<input type="checkbox" name="enable_reasoning" id="enable_reasoning" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-lg border-gray-300 rounded-md bg-gray-700 text-white">
<div class="mb-6">
<label for="kb_results" class="block text-lg font-medium text-gray-400">Knowledge base results</label>
<input type="text" name="kb_results" id="kb_results" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-lg border-gray-300 rounded-md bg-gray-700 text-white" placeholder="3">