feat(job): add finalizers and save conversation after job is result is released (#114)
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
committed by
GitHub
parent
e0703cdb7c
commit
dff678fc4e
@@ -504,7 +504,9 @@ func (a *Agent) consumeJob(job *types.Job, role string) {
|
|||||||
|
|
||||||
xlog.Debug("Finish job with reasoning", "reasoning", reasoning, "agent", a.Character.Name, "conversation", fmt.Sprintf("%+v", conv))
|
xlog.Debug("Finish job with reasoning", "reasoning", reasoning, "agent", a.Character.Name, "conversation", fmt.Sprintf("%+v", conv))
|
||||||
job.Result.Conversation = conv
|
job.Result.Conversation = conv
|
||||||
|
job.Result.AddFinalizer(func(conv []openai.ChatCompletionMessage) {
|
||||||
a.saveCurrentConversation(conv)
|
a.saveCurrentConversation(conv)
|
||||||
|
})
|
||||||
job.Result.SetResponse(reasoning)
|
job.Result.SetResponse(reasoning)
|
||||||
job.Result.Finish(nil)
|
job.Result.Finish(nil)
|
||||||
return
|
return
|
||||||
@@ -669,9 +671,11 @@ func (a *Agent) consumeJob(job *types.Job, role string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
conv = append(conv, msg)
|
conv = append(conv, msg)
|
||||||
a.saveCurrentConversation(conv)
|
|
||||||
job.Result.SetResponse(msg.Content)
|
job.Result.SetResponse(msg.Content)
|
||||||
job.Result.Conversation = conv
|
job.Result.Conversation = conv
|
||||||
|
job.Result.AddFinalizer(func(conv []openai.ChatCompletionMessage) {
|
||||||
|
a.saveCurrentConversation(conv)
|
||||||
|
})
|
||||||
job.Result.Finish(nil)
|
job.Result.Finish(nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -751,7 +755,9 @@ func (a *Agent) consumeJob(job *types.Job, role string) {
|
|||||||
conv = append(conv, msg)
|
conv = append(conv, msg)
|
||||||
job.Result.Conversation = conv
|
job.Result.Conversation = conv
|
||||||
job.Result.SetResponse(msg.Content)
|
job.Result.SetResponse(msg.Content)
|
||||||
|
job.Result.AddFinalizer(func(conv []openai.ChatCompletionMessage) {
|
||||||
a.saveCurrentConversation(conv)
|
a.saveCurrentConversation(conv)
|
||||||
|
})
|
||||||
job.Result.Finish(nil)
|
job.Result.Finish(nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -780,7 +786,9 @@ func (a *Agent) consumeJob(job *types.Job, role string) {
|
|||||||
job.Result.SetResponse(msg.Content)
|
job.Result.SetResponse(msg.Content)
|
||||||
xlog.Info("Response from LLM", "response", msg.Content, "agent", a.Character.Name)
|
xlog.Info("Response from LLM", "response", msg.Content, "agent", a.Character.Name)
|
||||||
job.Result.Conversation = conv
|
job.Result.Conversation = conv
|
||||||
|
job.Result.AddFinalizer(func(conv []openai.ChatCompletionMessage) {
|
||||||
a.saveCurrentConversation(conv)
|
a.saveCurrentConversation(conv)
|
||||||
|
})
|
||||||
job.Result.Finish(nil)
|
job.Result.Finish(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package types
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/sashabaranov/go-openai"
|
"github.com/sashabaranov/go-openai"
|
||||||
@@ -29,18 +28,6 @@ type Job struct {
|
|||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// JobResult is the result of a job
|
|
||||||
type JobResult struct {
|
|
||||||
sync.Mutex
|
|
||||||
// The result of a job
|
|
||||||
State []ActionState
|
|
||||||
Conversation []openai.ChatCompletionMessage
|
|
||||||
|
|
||||||
Response string
|
|
||||||
Error error
|
|
||||||
ready chan bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type JobOption func(*Job)
|
type JobOption func(*Job)
|
||||||
|
|
||||||
func WithConversationHistory(history []openai.ChatCompletionMessage) JobOption {
|
func WithConversationHistory(history []openai.ChatCompletionMessage) JobOption {
|
||||||
|
|||||||
@@ -1,5 +1,25 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/sashabaranov/go-openai"
|
||||||
|
)
|
||||||
|
|
||||||
|
// JobResult is the result of a job
|
||||||
|
type JobResult struct {
|
||||||
|
sync.Mutex
|
||||||
|
// The result of a job
|
||||||
|
State []ActionState
|
||||||
|
Conversation []openai.ChatCompletionMessage
|
||||||
|
|
||||||
|
Finalizers []func([]openai.ChatCompletionMessage)
|
||||||
|
|
||||||
|
Response string
|
||||||
|
Error error
|
||||||
|
ready chan bool
|
||||||
|
}
|
||||||
|
|
||||||
// SetResult sets the result of a job
|
// SetResult sets the result of a job
|
||||||
func (j *JobResult) SetResult(text ActionState) {
|
func (j *JobResult) SetResult(text ActionState) {
|
||||||
j.Lock()
|
j.Lock()
|
||||||
@@ -10,11 +30,24 @@ func (j *JobResult) SetResult(text ActionState) {
|
|||||||
|
|
||||||
// SetResult sets the result of a job
|
// SetResult sets the result of a job
|
||||||
func (j *JobResult) Finish(e error) {
|
func (j *JobResult) Finish(e error) {
|
||||||
|
j.Lock()
|
||||||
|
j.Error = e
|
||||||
|
j.Unlock()
|
||||||
|
|
||||||
|
close(j.ready)
|
||||||
|
|
||||||
|
for _, f := range j.Finalizers {
|
||||||
|
f(j.Conversation)
|
||||||
|
}
|
||||||
|
j.Finalizers = []func([]openai.ChatCompletionMessage){}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddFinalizer adds a finalizer to the job result
|
||||||
|
func (j *JobResult) AddFinalizer(f func([]openai.ChatCompletionMessage)) {
|
||||||
j.Lock()
|
j.Lock()
|
||||||
defer j.Unlock()
|
defer j.Unlock()
|
||||||
|
|
||||||
j.Error = e
|
j.Finalizers = append(j.Finalizers, f)
|
||||||
close(j.ready)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetResult sets the result of a job
|
// SetResult sets the result of a job
|
||||||
|
|||||||
Reference in New Issue
Block a user