Reply when skipping loops
This commit is contained in:
@@ -502,7 +502,8 @@ func (a *Agent) consumeJob(job *types.Job, role string) {
|
|||||||
}
|
}
|
||||||
if count[chosenAction.Definition().Name.String()] > a.options.loopDetectionSteps {
|
if count[chosenAction.Definition().Name.String()] > a.options.loopDetectionSteps {
|
||||||
xlog.Info("Loop detected, stopping agent", "agent", a.Character.Name, "action", chosenAction.Definition().Name)
|
xlog.Info("Loop detected, stopping agent", "agent", a.Character.Name, "action", chosenAction.Definition().Name)
|
||||||
chosenAction = nil
|
a.reply(job, role, conv, actionParams, chosenAction, reasoning)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -570,13 +571,6 @@ func (a *Agent) consumeJob(job *types.Job, role string) {
|
|||||||
|
|
||||||
job.AddPastAction(chosenAction, &actionParams)
|
job.AddPastAction(chosenAction, &actionParams)
|
||||||
|
|
||||||
var err error
|
|
||||||
conv, err = a.handlePlanning(job.GetContext(), job, chosenAction, actionParams, reasoning, pickTemplate, conv)
|
|
||||||
if err != nil {
|
|
||||||
job.Result.Finish(fmt.Errorf("error running action: %w", err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !job.Callback(types.ActionCurrentState{
|
if !job.Callback(types.ActionCurrentState{
|
||||||
Job: job,
|
Job: job,
|
||||||
Action: chosenAction,
|
Action: chosenAction,
|
||||||
@@ -595,6 +589,13 @@ func (a *Agent) consumeJob(job *types.Job, role string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
conv, err = a.handlePlanning(job.GetContext(), job, chosenAction, actionParams, reasoning, pickTemplate, conv)
|
||||||
|
if err != nil {
|
||||||
|
job.Result.Finish(fmt.Errorf("error running action: %w", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if selfEvaluation && a.options.initiateConversations &&
|
if selfEvaluation && a.options.initiateConversations &&
|
||||||
chosenAction.Definition().Name.Is(action.ConversationActionName) {
|
chosenAction.Definition().Name.Is(action.ConversationActionName) {
|
||||||
|
|
||||||
@@ -625,81 +626,87 @@ func (a *Agent) consumeJob(job *types.Job, role string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we don't have to reply , run the action!
|
// if we have a reply action, we need to run it
|
||||||
if !chosenAction.Definition().Name.Is(action.ReplyActionName) {
|
if chosenAction.Definition().Name.Is(action.ReplyActionName) {
|
||||||
|
a.reply(job, role, conv, actionParams, chosenAction, reasoning)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if !chosenAction.Definition().Name.Is(action.PlanActionName) {
|
if !chosenAction.Definition().Name.Is(action.PlanActionName) {
|
||||||
result, err := a.runAction(job.GetContext(), chosenAction, actionParams)
|
result, err := a.runAction(job.GetContext(), chosenAction, actionParams)
|
||||||
if err != nil {
|
|
||||||
//job.Result.Finish(fmt.Errorf("error running action: %w", err))
|
|
||||||
//return
|
|
||||||
// make the LLM aware of the error of running the action instead of stopping the job here
|
|
||||||
result.Result = fmt.Sprintf("Error running tool: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
stateResult := types.ActionState{
|
|
||||||
ActionCurrentState: types.ActionCurrentState{
|
|
||||||
Job: job,
|
|
||||||
Action: chosenAction,
|
|
||||||
Params: actionParams,
|
|
||||||
Reasoning: reasoning,
|
|
||||||
},
|
|
||||||
ActionResult: result,
|
|
||||||
}
|
|
||||||
job.Result.SetResult(stateResult)
|
|
||||||
job.CallbackWithResult(stateResult)
|
|
||||||
xlog.Debug("Action executed", "agent", a.Character.Name, "action", chosenAction.Definition().Name, "result", result)
|
|
||||||
|
|
||||||
conv = a.addFunctionResultToConversation(chosenAction, actionParams, result, conv)
|
|
||||||
}
|
|
||||||
|
|
||||||
//conv = append(conv, messages...)
|
|
||||||
//conv = messages
|
|
||||||
|
|
||||||
// given the result, we can now ask OpenAI to complete the conversation or
|
|
||||||
// to continue using another tool given the result
|
|
||||||
followingAction, followingParams, reasoning, err := a.pickAction(job.GetContext(), reEvaluationTemplate, conv, maxRetries)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
job.Result.Conversation = conv
|
//job.Result.Finish(fmt.Errorf("error running action: %w", err))
|
||||||
job.Result.Finish(fmt.Errorf("error picking action: %w", err))
|
//return
|
||||||
return
|
// make the LLM aware of the error of running the action instead of stopping the job here
|
||||||
|
result.Result = fmt.Sprintf("Error running tool: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if followingAction != nil &&
|
stateResult := types.ActionState{
|
||||||
!followingAction.Definition().Name.Is(action.ReplyActionName) &&
|
ActionCurrentState: types.ActionCurrentState{
|
||||||
!chosenAction.Definition().Name.Is(action.ReplyActionName) {
|
Job: job,
|
||||||
|
Action: chosenAction,
|
||||||
|
Params: actionParams,
|
||||||
|
Reasoning: reasoning,
|
||||||
|
},
|
||||||
|
ActionResult: result,
|
||||||
|
}
|
||||||
|
job.Result.SetResult(stateResult)
|
||||||
|
job.CallbackWithResult(stateResult)
|
||||||
|
xlog.Debug("Action executed", "agent", a.Character.Name, "action", chosenAction.Definition().Name, "result", result)
|
||||||
|
|
||||||
xlog.Info("Following action", "action", followingAction.Definition().Name, "agent", a.Character.Name)
|
conv = a.addFunctionResultToConversation(chosenAction, actionParams, result, conv)
|
||||||
|
}
|
||||||
|
|
||||||
// We need to do another action (?)
|
//conv = append(conv, messages...)
|
||||||
// The agent decided to do another action
|
//conv = messages
|
||||||
// call ourselves again
|
|
||||||
job.SetNextAction(&followingAction, &followingParams, reasoning)
|
|
||||||
a.consumeJob(job, role)
|
|
||||||
return
|
|
||||||
} else if followingAction == nil {
|
|
||||||
xlog.Info("Not following another action", "agent", a.Character.Name)
|
|
||||||
|
|
||||||
if !a.options.forceReasoning {
|
// given the result, we can now ask OpenAI to complete the conversation or
|
||||||
xlog.Info("Finish conversation with reasoning", "reasoning", reasoning, "agent", a.Character.Name)
|
// to continue using another tool given the result
|
||||||
|
followingAction, followingParams, reasoning, err := a.pickAction(job.GetContext(), reEvaluationTemplate, conv, maxRetries)
|
||||||
|
if err != nil {
|
||||||
|
job.Result.Conversation = conv
|
||||||
|
job.Result.Finish(fmt.Errorf("error picking action: %w", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
msg := openai.ChatCompletionMessage{
|
if followingAction != nil &&
|
||||||
Role: "assistant",
|
!followingAction.Definition().Name.Is(action.ReplyActionName) &&
|
||||||
Content: reasoning,
|
!chosenAction.Definition().Name.Is(action.ReplyActionName) {
|
||||||
}
|
|
||||||
|
|
||||||
conv = append(conv, msg)
|
xlog.Info("Following action", "action", followingAction.Definition().Name, "agent", a.Character.Name)
|
||||||
job.Result.SetResponse(msg.Content)
|
|
||||||
job.Result.Conversation = conv
|
// We need to do another action (?)
|
||||||
job.Result.AddFinalizer(func(conv []openai.ChatCompletionMessage) {
|
// The agent decided to do another action
|
||||||
a.saveCurrentConversation(conv)
|
// call ourselves again
|
||||||
})
|
job.SetNextAction(&followingAction, &followingParams, reasoning)
|
||||||
job.Result.Finish(nil)
|
a.consumeJob(job, role)
|
||||||
return
|
return
|
||||||
|
} else if followingAction == nil {
|
||||||
|
xlog.Info("Not following another action", "agent", a.Character.Name)
|
||||||
|
|
||||||
|
if !a.options.forceReasoning {
|
||||||
|
xlog.Info("Finish conversation with reasoning", "reasoning", reasoning, "agent", a.Character.Name)
|
||||||
|
|
||||||
|
msg := openai.ChatCompletionMessage{
|
||||||
|
Role: "assistant",
|
||||||
|
Content: reasoning,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conv = append(conv, msg)
|
||||||
|
job.Result.SetResponse(msg.Content)
|
||||||
|
job.Result.Conversation = conv
|
||||||
|
job.Result.AddFinalizer(func(conv []openai.ChatCompletionMessage) {
|
||||||
|
a.saveCurrentConversation(conv)
|
||||||
|
})
|
||||||
|
job.Result.Finish(nil)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
a.reply(job, role, conv, actionParams, chosenAction, reasoning)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Agent) reply(job *types.Job, role string, conv Messages, actionParams types.ActionParams, chosenAction types.Action, reasoning string) {
|
||||||
job.Result.Conversation = conv
|
job.Result.Conversation = conv
|
||||||
|
|
||||||
// At this point can only be a reply action
|
// At this point can only be a reply action
|
||||||
@@ -760,8 +767,8 @@ func (a *Agent) consumeJob(job *types.Job, role string) {
|
|||||||
// },
|
// },
|
||||||
// )
|
// )
|
||||||
|
|
||||||
if !a.options.forceReasoning {
|
if replyResponse.Message != "" {
|
||||||
xlog.Info("No reasoning, return reply message", "reply", replyResponse.Message, "agent", a.Character.Name)
|
xlog.Info("Return reply message", "reply", replyResponse.Message, "agent", a.Character.Name)
|
||||||
|
|
||||||
msg := openai.ChatCompletionMessage{
|
msg := openai.ChatCompletionMessage{
|
||||||
Role: "assistant",
|
Role: "assistant",
|
||||||
|
|||||||
Reference in New Issue
Block a user