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

@@ -35,8 +35,9 @@ func (a *IntentAction) Definition() ActionDefinition {
Description: "A detailed reasoning on why you want to call this tool.", Description: "A detailed reasoning on why you want to call this tool.",
}, },
"tool": { "tool": {
Type: jsonschema.String, Type: jsonschema.String,
Enum: a.tools, Description: "The tool you want to use",
Enum: a.tools,
}, },
}, },
Required: []string{"tool", "reasoning"}, Required: []string{"tool", "reasoning"},

View File

@@ -52,6 +52,7 @@ func (a Actions) Find(name string) Action {
type decisionResult struct { type decisionResult struct {
actionParams action.ActionParams actionParams action.ActionParams
message string message string
actioName string
} }
// decision forces the agent to take one of the available actions // decision forces the agent to take one of the available actions
@@ -85,7 +86,7 @@ func (a *Agent) decision(
return nil, err return nil, err
} }
return &decisionResult{actionParams: params}, nil return &decisionResult{actionParams: params, actioName: msg.ToolCalls[0].Function.Name}, nil
} }
type Messages []openai.ChatCompletionMessage type Messages []openai.ChatCompletionMessage
@@ -130,11 +131,16 @@ func (a *Agent) generateParameters(ctx context.Context, pickTemplate string, act
}, conversation...) }, conversation...)
} }
return a.decision(ctx, cc := conversation
append(conversation, openai.ChatCompletionMessage{ if a.options.forceReasoning {
cc = append(conversation, openai.ChatCompletionMessage{
Role: "system", Role: "system",
Content: fmt.Sprintf("The agent decided to use the tool %s with the following reasoning: %s", act.Definition().Name, reasoning), 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(), a.systemInternalActions().ToTools(),
openai.ToolChoice{ openai.ToolChoice{
Type: openai.ToolTypeFunction, 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) { func (a *Agent) pickAction(ctx context.Context, templ string, messages []openai.ChatCompletionMessage) (Action, string, error) {
c := messages 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 var promptHUD *PromptHUD
if a.options.enableHUD { if a.options.enableHUD {
h := a.prepareHUD() h := a.prepareHUD()
@@ -241,7 +265,7 @@ func (a *Agent) pickAction(ctx context.Context, templ string, messages []openai.
params, err := a.decision(ctx, params, err := a.decision(ctx,
append(c, openai.ChatCompletionMessage{ append(c, openai.ChatCompletionMessage{
Role: "system", Role: "system",
Content: "The assistant thought: " + reason, Content: "Given the assistant thought, pick the relevant action: " + reason,
}), }),
Actions{intentionsTools}.ToTools(), Actions{intentionsTools}.ToTools(),
intentionsTools.Definition().Name) intentionsTools.Definition().Name)

View File

@@ -23,6 +23,7 @@ type options struct {
canStopItself bool canStopItself bool
initiateConversations bool initiateConversations bool
forceReasoning bool
characterfile string characterfile string
statefile string statefile string
context context.Context context context.Context
@@ -78,6 +79,11 @@ var EnableHUD = func(o *options) error {
return nil return nil
} }
var EnableForceReasoning = func(o *options) error {
o.forceReasoning = true
return nil
}
var EnableKnowledgeBase = func(o *options) error { var EnableKnowledgeBase = func(o *options) error {
o.enableKB = true o.enableKB = true
o.kbResults = 5 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.` We already have called other tools. Evaluate the current situation and decide if we need to execute other tools.`
const pickActionTemplate = hudTemplate + ` 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 -}} {{range .Actions -}}
- {{.Name}}: {{.Description }} - {{.Name}}: {{.Description }}
{{ end }} {{ 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'. 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}} {{if .Reasoning}}Reasoning: {{.Reasoning}}{{end}}

View File

@@ -24,6 +24,7 @@ type AgentConfig struct {
PeriodicRuns string `json:"periodic_runs" form:"periodic_runs"` PeriodicRuns string `json:"periodic_runs" form:"periodic_runs"`
PermanentGoal string `json:"permanent_goal" form:"permanent_goal"` PermanentGoal string `json:"permanent_goal" form:"permanent_goal"`
EnableKnowledgeBase bool `json:"enable_kb" form:"enable_kb"` EnableKnowledgeBase bool `json:"enable_kb" form:"enable_kb"`
EnableReasoning bool `json:"enable_reasoning" form:"enable_reasoning"`
KnowledgeBaseResults int `json:"kb_results" form:"kb_results"` KnowledgeBaseResults int `json:"kb_results" form:"kb_results"`
CanStopItself bool `json:"can_stop_itself" form:"can_stop_itself"` CanStopItself bool `json:"can_stop_itself" form:"can_stop_itself"`
SystemPrompt string `json:"system_prompt" form:"system_prompt"` 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 { if config.EnableKnowledgeBase {
opts = append(opts, EnableKnowledgeBase) opts = append(opts, EnableKnowledgeBase)
} }
if config.EnableReasoning {
opts = append(opts, EnableForceReasoning)
}
if config.KnowledgeBaseResults > 0 { if config.KnowledgeBaseResults > 0 {
opts = append(opts, EnableKnowledgeBaseWithResults(config.KnowledgeBaseResults)) opts = append(opts, EnableKnowledgeBaseWithResults(config.KnowledgeBaseResults))

View File

@@ -86,6 +86,8 @@
<div class="mb-4"> <div class="mb-4">
<label for="enable_kb" class="block text-lg font-medium text-gray-400">Enable Knowledge Base</label> <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"> <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"> <div class="mb-6">
<label for="kb_results" class="block text-lg font-medium text-gray-400">Knowledge base results</label> <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"> <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">