Return JobResult
This commit is contained in:
@@ -50,7 +50,7 @@ type Agent struct {
|
|||||||
options *options
|
options *options
|
||||||
Character Character
|
Character Character
|
||||||
client *openai.Client
|
client *openai.Client
|
||||||
jobQueue, selfJobQueue chan *Job
|
jobQueue chan *Job
|
||||||
actionContext *action.ActionContext
|
actionContext *action.ActionContext
|
||||||
context *action.ActionContext
|
context *action.ActionContext
|
||||||
availableActions []Action
|
availableActions []Action
|
||||||
@@ -79,7 +79,6 @@ func New(opts ...Option) (*Agent, error) {
|
|||||||
ctx, cancel := context.WithCancel(c)
|
ctx, cancel := context.WithCancel(c)
|
||||||
a := &Agent{
|
a := &Agent{
|
||||||
jobQueue: make(chan *Job),
|
jobQueue: make(chan *Job),
|
||||||
selfJobQueue: make(chan *Job),
|
|
||||||
options: options,
|
options: options,
|
||||||
client: client,
|
client: client,
|
||||||
Character: options.character,
|
Character: options.character,
|
||||||
@@ -108,12 +107,11 @@ func (a *Agent) StopAction() {
|
|||||||
|
|
||||||
// Ask is a pre-emptive, blocking call that returns the response as soon as it's ready.
|
// Ask is a pre-emptive, blocking call that returns the response as soon as it's ready.
|
||||||
// It discards any other computation.
|
// It discards any other computation.
|
||||||
func (a *Agent) Ask(opts ...JobOption) []ActionState {
|
func (a *Agent) Ask(opts ...JobOption) *JobResult {
|
||||||
a.StopAction()
|
a.StopAction()
|
||||||
j := NewJob(opts...)
|
j := NewJob(opts...)
|
||||||
// fmt.Println("Job created", text)
|
// fmt.Println("Job created", text)
|
||||||
a.jobQueue <- j
|
a.jobQueue <- j
|
||||||
|
|
||||||
return j.Result.WaitResult()
|
return j.Result.WaitResult()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -336,10 +334,6 @@ func (a *Agent) Run() error {
|
|||||||
todoTimer := time.NewTicker(1 * time.Minute)
|
todoTimer := time.NewTicker(1 * time.Minute)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case job := <-a.selfJobQueue:
|
|
||||||
|
|
||||||
// XXX: is it needed?
|
|
||||||
a.consumeJob(job, SystemRole)
|
|
||||||
case job := <-a.jobQueue:
|
case job := <-a.jobQueue:
|
||||||
|
|
||||||
// Consume the job and generate a response
|
// Consume the job and generate a response
|
||||||
@@ -349,6 +343,9 @@ func (a *Agent) Run() error {
|
|||||||
// Agent has been canceled, return error
|
// Agent has been canceled, return error
|
||||||
return ErrContextCanceled
|
return ErrContextCanceled
|
||||||
case <-todoTimer.C:
|
case <-todoTimer.C:
|
||||||
|
if !a.options.standaloneJob {
|
||||||
|
continue
|
||||||
|
}
|
||||||
a.periodicallyRun()
|
a.periodicallyRun()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,8 +80,10 @@ var _ = Describe("Agent test", func() {
|
|||||||
WithText("can you get the weather in boston, and afterward of Milano, Italy?"),
|
WithText("can you get the weather in boston, and afterward of Milano, Italy?"),
|
||||||
)...,
|
)...,
|
||||||
)
|
)
|
||||||
|
Expect(res.Error).ToNot(HaveOccurred())
|
||||||
reasons := []string{}
|
reasons := []string{}
|
||||||
for _, r := range res {
|
for _, r := range res.State {
|
||||||
|
|
||||||
reasons = append(reasons, r.Result)
|
reasons = append(reasons, r.Result)
|
||||||
}
|
}
|
||||||
Expect(reasons).To(ContainElement(testActionResult), fmt.Sprint(res))
|
Expect(reasons).To(ContainElement(testActionResult), fmt.Sprint(res))
|
||||||
@@ -94,7 +96,7 @@ var _ = Describe("Agent test", func() {
|
|||||||
)...)
|
)...)
|
||||||
conversation := agent.CurrentConversation()
|
conversation := agent.CurrentConversation()
|
||||||
Expect(len(conversation)).To(Equal(10), fmt.Sprint(conversation))
|
Expect(len(conversation)).To(Equal(10), fmt.Sprint(conversation))
|
||||||
for _, r := range res {
|
for _, r := range res.State {
|
||||||
reasons = append(reasons, r.Result)
|
reasons = append(reasons, r.Result)
|
||||||
}
|
}
|
||||||
Expect(reasons).ToNot(ContainElement(testActionResult), fmt.Sprint(res))
|
Expect(reasons).ToNot(ContainElement(testActionResult), fmt.Sprint(res))
|
||||||
@@ -118,7 +120,7 @@ var _ = Describe("Agent test", func() {
|
|||||||
WithText("can you get the weather in boston?"))...,
|
WithText("can you get the weather in boston?"))...,
|
||||||
)
|
)
|
||||||
reasons := []string{}
|
reasons := []string{}
|
||||||
for _, r := range res {
|
for _, r := range res.State {
|
||||||
reasons = append(reasons, r.Result)
|
reasons = append(reasons, r.Result)
|
||||||
}
|
}
|
||||||
Expect(reasons).To(ContainElement(testActionResult), fmt.Sprint(res))
|
Expect(reasons).To(ContainElement(testActionResult), fmt.Sprint(res))
|
||||||
|
|||||||
@@ -105,9 +105,9 @@ func (j *JobResult) Finish(e error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WaitResult waits for the result of a job
|
// WaitResult waits for the result of a job
|
||||||
func (j *JobResult) WaitResult() []ActionState {
|
func (j *JobResult) WaitResult() *JobResult {
|
||||||
<-j.ready
|
<-j.ready
|
||||||
j.Lock()
|
j.Lock()
|
||||||
defer j.Unlock()
|
defer j.Unlock()
|
||||||
return j.State
|
return j
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ type options struct {
|
|||||||
randomIdentityGuidance string
|
randomIdentityGuidance string
|
||||||
randomIdentity bool
|
randomIdentity bool
|
||||||
actions Actions
|
actions Actions
|
||||||
enableHUD bool
|
enableHUD, standaloneJob bool
|
||||||
context context.Context
|
context context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,6 +53,13 @@ var EnableHUD = func(o *options) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EnableStandaloneJob is an option to enable the agent
|
||||||
|
// to run jobs in the background automatically
|
||||||
|
var EnableStandaloneJob = func(o *options) error {
|
||||||
|
o.standaloneJob = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func WithLLMAPIURL(url string) Option {
|
func WithLLMAPIURL(url string) Option {
|
||||||
return func(o *options) error {
|
return func(o *options) error {
|
||||||
o.LLMAPI.APIURL = url
|
o.LLMAPI.APIURL = url
|
||||||
|
|||||||
Reference in New Issue
Block a user