Cleanup context, more logging

This commit is contained in:
Ettore Di Giacinto
2024-04-20 00:19:17 +02:00
parent 34f6d821b9
commit 22160cbf9e
4 changed files with 32 additions and 7 deletions

View File

@@ -241,6 +241,13 @@ func (a *Agent) consumeJob(job *Job, role string) {
}
a.Unlock()
defer func() {
a.Lock()
a.actionContext.Cancel()
a.actionContext = nil
a.Unlock()
}()
if selfEvaluation {
defer func() {
a.Lock()
@@ -711,6 +718,7 @@ func (a *Agent) Run() error {
//todoTimer := time.NewTicker(a.options.periodicRuns)
timer := time.NewTimer(a.options.periodicRuns)
for {
xlog.Debug("Agent is waiting for a job", "agent", a.Character.Name)
select {
case job := <-a.jobQueue:
// Consume the job and generate a response
@@ -719,15 +727,18 @@ func (a *Agent) Run() error {
if !timer.Stop() {
<-timer.C
}
xlog.Debug("Agent is consuming a job", "agent", a.Character.Name, "job", job)
a.consumeJob(job, UserRole)
timer.Reset(a.options.periodicRuns)
case <-a.context.Done():
// Agent has been canceled, return error
xlog.Warn("Agent has been canceled", "agent", a.Character.Name)
return ErrContextCanceled
case <-timer.C:
if !a.options.standaloneJob {
continue
}
xlog.Debug("Agent is running periodically", "agent", a.Character.Name)
a.periodicallyRun()
timer.Reset(a.options.periodicRuns)
}

View File

@@ -51,12 +51,12 @@ func defaultOptions() *options {
periodicRuns: 15 * time.Minute,
LLMAPI: llmOptions{
APIURL: "http://localhost:8080",
Model: "echidna",
Model: "gpt-4",
},
character: Character{
Name: "John Doe",
Name: "",
Age: "",
Occupation: "Unemployed",
Occupation: "",
Hobbies: []string{},
MusicTaste: []string{},
},

View File

@@ -39,7 +39,7 @@ func (a *AgentConfig) availableActions(ctx context.Context) []Action {
for _, action := range a.Actions {
var config map[string]string
if err := json.Unmarshal([]byte(action.Config), &config); err != nil {
xlog.Info("Error unmarshalling action config", err)
xlog.Info("Error unmarshalling action config", "error", err)
continue
}

View File

@@ -181,7 +181,14 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
WithTimeout(timeout),
WithRAGDB(a.ragDB),
WithAgentReasoningCallback(func(state ActionCurrentState) bool {
xlog.Info("Reasoning", state.Reasoning)
xlog.Info(
"Agent is thinking",
"agent", name,
"reasoning", state.Reasoning,
"action", state.Action.Definition().Name,
"params", state.Params,
)
manager.Send(
NewMessage(
fmt.Sprintf(`Thinking: %s`, htmlIfy(state.Reasoning)),
@@ -204,7 +211,14 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
a.agentStatus[name].addResult(state)
a.Unlock()
xlog.Info("Reasoning", state.Reasoning)
xlog.Info(
"Agent executed an action",
"agent", name,
"reasoning", state.Reasoning,
"action", state.ActionCurrentState.Action.Definition().Name,
"params", state.ActionCurrentState.Params,
"result", state.Result,
)
text := fmt.Sprintf(`Reasoning: %s
Action taken: %+v
@@ -267,7 +281,7 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
go func() {
if err := agent.Run(); err != nil {
xlog.Info("Agent stop: ", err.Error())
xlog.Error("Agent stopped", "error", err.Error())
}
}()