This commit is contained in:
mudler
2024-03-31 17:20:06 +02:00
parent 8601956e53
commit aa62d9ef9e
6 changed files with 350 additions and 236 deletions

View File

@@ -4,7 +4,9 @@ import (
"context"
"fmt"
"sync"
"time"
"github.com/mudler/local-agent-framework/action"
"github.com/mudler/local-agent-framework/llm"
"github.com/sashabaranov/go-openai"
)
@@ -15,8 +17,8 @@ type Agent struct {
Character Character
client *openai.Client
jobQueue chan *Job
actionContext *ActionContext
context *ActionContext
actionContext *action.ActionContext
context *action.ActionContext
availableActions []Action
currentConversation []openai.ChatCompletionMessage
@@ -40,14 +42,11 @@ func New(opts ...Option) (*Agent, error) {
ctx, cancel := context.WithCancel(c)
a := &Agent{
jobQueue: make(chan *Job),
options: options,
client: client,
Character: options.character,
context: &ActionContext{
Context: ctx,
cancelFunc: cancel,
},
jobQueue: make(chan *Job),
options: options,
client: client,
Character: options.character,
context: action.NewContext(ctx, cancel),
availableActions: options.actions,
}
@@ -71,3 +70,63 @@ func (a *Agent) Ask(text, image string) []string {
return j.Result.WaitResult()
}
var ErrContextCanceled = fmt.Errorf("context canceled")
func (a *Agent) Stop() {
a.Lock()
defer a.Unlock()
a.context.Cancel()
}
func (a *Agent) Run() error {
// The agent run does two things:
// picks up requests from a queue
// and generates a response/perform actions
// It is also preemptive.
// That is, it can interrupt the current action
// if another one comes in.
// If there is no action, periodically evaluate if it has to do something on its own.
// Expose a REST API to interact with the agent to ask it things
fmt.Println("Agent is running")
clearConvTimer := time.NewTicker(1 * time.Minute)
for {
fmt.Println("Agent loop")
select {
case job := <-a.jobQueue:
fmt.Println("job from the queue")
// Consume the job and generate a response
// TODO: Give a short-term memory to the agent
a.consumeJob(job)
case <-a.context.Done():
fmt.Println("Context canceled, agent is stopping...")
// Agent has been canceled, return error
return ErrContextCanceled
case <-clearConvTimer.C:
fmt.Println("Removing chat history...")
// TODO: decide to do something on its own with the conversation result
// before clearing it out
// Clear the conversation
a.currentConversation = []openai.ChatCompletionMessage{}
}
}
}
// StopAction stops the current action
// if any. Can be called before adding a new job.
func (a *Agent) StopAction() {
a.Lock()
defer a.Unlock()
if a.actionContext != nil {
a.actionContext.Cancel()
}
}