feat(AI): add done channel for faster response
This commit is contained in:
parent
5e65a2ce32
commit
39868f4f07
|
@ -134,7 +134,10 @@ func assistantStreamedResponse(request openai.ChatRequest, cb assistantStreamedR
|
||||||
if minWaitSatisfied {
|
if minWaitSatisfied {
|
||||||
break Drain
|
break Drain
|
||||||
}
|
}
|
||||||
<-minWait
|
select {
|
||||||
|
case <-minWait:
|
||||||
|
case <-resp.Done:
|
||||||
|
}
|
||||||
minWaitSatisfied = true
|
minWaitSatisfied = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -155,7 +158,7 @@ func assistantStreamedResponse(request openai.ChatRequest, cb assistantStreamedR
|
||||||
minWaitDurSecs := lo.Min([]int{nUpdates, 4}) + nErrs*3
|
minWaitDurSecs := lo.Min([]int{nUpdates, 4}) + nErrs*3
|
||||||
minWait = time.After(time.Duration(minWaitDurSecs) * time.Second)
|
minWait = time.After(time.Duration(minWaitDurSecs) * time.Second)
|
||||||
|
|
||||||
// send the partial message
|
// Send the partial message
|
||||||
respText := respBuilder.String() + assistantWritingSign
|
respText := respBuilder.String() + assistantWritingSign
|
||||||
if err := cb(respText, false); err != nil {
|
if err := cb(respText, false); err != nil {
|
||||||
logger.Warnw("failed to send partial update", "error", err)
|
logger.Warnw("failed to send partial update", "error", err)
|
||||||
|
|
|
@ -51,6 +51,7 @@ type ChatResponseStream struct {
|
||||||
Created int
|
Created int
|
||||||
Model string
|
Model string
|
||||||
Stream chan string
|
Stream chan string
|
||||||
|
Done chan struct{}
|
||||||
Err error
|
Err error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -77,11 +77,15 @@ func (c *Client) ChatCompletionStream(request ChatRequest) (*ChatResponseStream,
|
||||||
return nil, errors.Errorf("status code: %d, body: %q", resp.StatusCode(), respBodyStr)
|
return nil, errors.Errorf("status code: %d, body: %q", resp.StatusCode(), respBodyStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
ret := &ChatResponseStream{Stream: make(chan string, 1024)}
|
ret := &ChatResponseStream{
|
||||||
|
Stream: make(chan string, 1024),
|
||||||
|
Done: make(chan struct{}),
|
||||||
|
}
|
||||||
go func() {
|
go func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
rbody.Close()
|
rbody.Close()
|
||||||
close(ret.Stream)
|
close(ret.Stream)
|
||||||
|
close(ret.Done)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var contentBegan bool
|
var contentBegan bool
|
||||||
|
|
Loading…
Reference in New Issue