@@ -5,7 +5,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/mudler/LocalAGI/core/action"
|
||||
"github.com/mudler/LocalAGI/core/types"
|
||||
@@ -80,6 +79,15 @@ func (m Messages) ToOpenAI() []openai.ChatCompletionMessage {
|
||||
return []openai.ChatCompletionMessage(m)
|
||||
}
|
||||
|
||||
func (m Messages) RemoveIf(f func(msg openai.ChatCompletionMessage) bool) Messages {
|
||||
for i := len(m) - 1; i >= 0; i-- {
|
||||
if f(m[i]) {
|
||||
m = append(m[:i], m[i+1:]...)
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func (m Messages) String() string {
|
||||
s := ""
|
||||
for _, cc := range m {
|
||||
@@ -412,27 +420,26 @@ func (a *Agent) pickAction(ctx context.Context, templ string, messages []openai.
|
||||
actionsID = append(actionsID, m.Definition().Name.String())
|
||||
}
|
||||
|
||||
thoughtPromptStringBuilder := strings.Builder{}
|
||||
thoughtPromptStringBuilder.WriteString("You have to pick an action based on the conversation and the prompt. Describe the full reasoning process for your choice. Here is a list of actions: ")
|
||||
for _, m := range a.availableActions() {
|
||||
thoughtPromptStringBuilder.WriteString(
|
||||
m.Definition().Name.String() + ": " + m.Definition().Description + "\n",
|
||||
)
|
||||
}
|
||||
// thoughtPromptStringBuilder := strings.Builder{}
|
||||
// thoughtPromptStringBuilder.WriteString("You have to pick an action based on the conversation and the prompt. Describe the full reasoning process for your choice. Here is a list of actions: ")
|
||||
// for _, m := range a.availableActions() {
|
||||
// thoughtPromptStringBuilder.WriteString(
|
||||
// m.Definition().Name.String() + ": " + m.Definition().Description + "\n",
|
||||
// )
|
||||
// }
|
||||
|
||||
thoughtPromptStringBuilder.WriteString("To not use any action, respond with 'none'")
|
||||
// thoughtPromptStringBuilder.WriteString("To not use any action, respond with 'none'")
|
||||
|
||||
//thoughtPromptStringBuilder.WriteString("\n\nConversation: " + Messages(c).String())
|
||||
//thoughtPromptStringBuilder.WriteString("\n\nConversation: " + Messages(c).RemoveIf(func(msg openai.ChatCompletionMessage) bool {
|
||||
// return msg.Role == "system"
|
||||
//}).String())
|
||||
|
||||
thoughtPrompt := thoughtPromptStringBuilder.String()
|
||||
//thoughtPrompt := thoughtPromptStringBuilder.String()
|
||||
|
||||
xlog.Debug("[pickAction] thought", "prompt", thoughtPrompt)
|
||||
//thoughtConv := []openai.ChatCompletionMessage{}
|
||||
|
||||
thought, err := a.askLLM(ctx,
|
||||
append(c, openai.ChatCompletionMessage{
|
||||
Role: "user",
|
||||
Content: thoughtPrompt,
|
||||
}),
|
||||
c,
|
||||
maxRetries,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -440,71 +447,89 @@ func (a *Agent) pickAction(ctx context.Context, templ string, messages []openai.
|
||||
}
|
||||
originalReasoning := thought.Content
|
||||
|
||||
xlog.Debug("[pickAction] thought", "reason", originalReasoning)
|
||||
|
||||
thought, err = a.askLLM(ctx,
|
||||
[]openai.ChatCompletionMessage{
|
||||
{
|
||||
Role: "system",
|
||||
Content: "Your only objective is to return the string of the action to take based on the user input",
|
||||
},
|
||||
{
|
||||
Role: "user",
|
||||
Content: "Given the following sentence, answer with the only action to take: " + originalReasoning,
|
||||
}},
|
||||
maxRetries,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, nil, "", err
|
||||
}
|
||||
reason := thought.Content
|
||||
|
||||
xlog.Debug("[pickAction] filtered thought", "reason", reason)
|
||||
|
||||
// From the thought, get the action call
|
||||
// Get all the available actions IDs
|
||||
|
||||
intentionsTools := action.NewIntention(append(actionsID, "none")...)
|
||||
// by grammar, let's decide if we have achieved the goal
|
||||
// 1. analyze response and check if goal is achieved
|
||||
|
||||
// NOTE: we do not give the full conversation here to pick the action
|
||||
// to avoid hallucinations
|
||||
params, err := a.decision(ctx,
|
||||
[]openai.ChatCompletionMessage{
|
||||
{
|
||||
Role: "system",
|
||||
Content: "You have to pick the correct action based on the user reasoning",
|
||||
Content: "Extract an action to perform from the following reasoning: ",
|
||||
},
|
||||
{
|
||||
Role: "user",
|
||||
Content: reason,
|
||||
},
|
||||
},
|
||||
types.Actions{intentionsTools}.ToTools(),
|
||||
intentionsTools.Definition().Name, maxRetries)
|
||||
Content: originalReasoning,
|
||||
}},
|
||||
types.Actions{action.NewGoal()}.ToTools(),
|
||||
action.NewGoal().Definition().Name, maxRetries)
|
||||
if err != nil {
|
||||
return nil, nil, "", fmt.Errorf("failed to get the action tool parameters: %v", err)
|
||||
}
|
||||
|
||||
actionChoice := action.IntentResponse{}
|
||||
|
||||
if params.actionParams == nil {
|
||||
return nil, nil, params.message, nil
|
||||
}
|
||||
|
||||
err = params.actionParams.Unmarshal(&actionChoice)
|
||||
goalResponse := action.GoalResponse{}
|
||||
err = params.actionParams.Unmarshal(&goalResponse)
|
||||
if err != nil {
|
||||
return nil, nil, "", err
|
||||
}
|
||||
|
||||
if actionChoice.Tool == "" || actionChoice.Tool == "none" {
|
||||
if goalResponse.Achieved {
|
||||
xlog.Debug("[pickAction] goal achieved", "goal", goalResponse.Goal)
|
||||
return nil, nil, "", nil
|
||||
}
|
||||
|
||||
// Find the action
|
||||
chosenAction := a.availableActions().Find(actionChoice.Tool)
|
||||
if chosenAction == nil {
|
||||
return nil, nil, "", fmt.Errorf("no action found for intent:" + actionChoice.Tool)
|
||||
// if the goal is not achieved, pick an action
|
||||
xlog.Debug("[pickAction] goal not achieved", "goal", goalResponse.Goal)
|
||||
|
||||
xlog.Debug("[pickAction] thought", "conv", c, "originalReasoning", originalReasoning)
|
||||
|
||||
// TODO: FORCE to select ana ction here
|
||||
// NOTE: we do not give the full conversation here to pick the action
|
||||
// to avoid hallucinations
|
||||
params, err = a.decision(ctx,
|
||||
[]openai.ChatCompletionMessage{
|
||||
{
|
||||
Role: "system",
|
||||
Content: "Extract an action to perform from the following reasoning: ",
|
||||
},
|
||||
{
|
||||
Role: "user",
|
||||
Content: originalReasoning,
|
||||
}},
|
||||
a.availableActions().ToTools(),
|
||||
nil, maxRetries)
|
||||
if err != nil {
|
||||
return nil, nil, "", fmt.Errorf("failed to get the action tool parameters: %v", err)
|
||||
}
|
||||
|
||||
chosenAction := a.availableActions().Find(params.actioName)
|
||||
|
||||
// xlog.Debug("[pickAction] params", "params", params)
|
||||
|
||||
// if params.actionParams == nil {
|
||||
// return nil, nil, params.message, nil
|
||||
// }
|
||||
|
||||
// xlog.Debug("[pickAction] actionChoice", "actionChoice", params.actionParams, "message", params.message)
|
||||
|
||||
// actionChoice := action.IntentResponse{}
|
||||
|
||||
// err = params.actionParams.Unmarshal(&actionChoice)
|
||||
// if err != nil {
|
||||
// return nil, nil, "", err
|
||||
// }
|
||||
|
||||
// if actionChoice.Tool == "" || actionChoice.Tool == "none" {
|
||||
// return nil, nil, "", nil
|
||||
// }
|
||||
|
||||
// // Find the action
|
||||
// chosenAction := a.availableActions().Find(actionChoice.Tool)
|
||||
// if chosenAction == nil {
|
||||
// return nil, nil, "", fmt.Errorf("no action found for intent:" + actionChoice.Tool)
|
||||
// }
|
||||
|
||||
return chosenAction, nil, originalReasoning, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user