Skip to content

Commit

Permalink
handle openai stop event, close #198 & fix EventsOff
Browse files Browse the repository at this point in the history
  • Loading branch information
juzeon committed Jan 20, 2024
1 parent 2a06f0a commit 33c7c91
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion app_chatbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (a *App) askSydney(options AskOptions) {
runtime.EventsOn(a.ctx, EventChatStop, func(optionalData ...interface{}) {
slog.Info("Received EventChatStop")
cancel()
runtime.EventsOff(a.ctx, EventChatStop)
})
ch := sydneyIns.AskStream(sydney.AskStreamOptions{
StopCtx: stopCtx,
Expand Down Expand Up @@ -207,6 +208,12 @@ func (a *App) askOpenAI(options AskOptions) {
slog.Info("invoke EventChatFinish", "result", chatFinishResult)
runtime.EventsEmit(a.ctx, EventChatFinish, chatFinishResult)
}()
stopCtx, cancel := context.WithCancel(context.Background())
runtime.EventsOn(a.ctx, EventChatStop, func(optionalData ...interface{}) {
slog.Info("Received EventChatStop")
cancel()
runtime.EventsOff(a.ctx, EventChatStop)
})
backend, err := slices.Find(a.settings.config.OpenAIBackends, func(el OpenAIBackend) bool {
return el.Name == options.OpenAIBackend
})
Expand Down Expand Up @@ -241,7 +248,7 @@ func (a *App) askOpenAI(options AskOptions) {
}},
})
}
stream, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{
stream, err := client.CreateChatCompletionStream(stopCtx, openai.ChatCompletionRequest{
Model: backend.OpenaiShortModel,
Messages: messages,
Temperature: backend.OpenaiTemperature,
Expand All @@ -263,6 +270,9 @@ func (a *App) askOpenAI(options AskOptions) {
slog.Info("openai chat completed")
return
}
if errors.Is(err, context.Canceled) {
return
}
if err != nil {
handleErr(err)
return
Expand Down

0 comments on commit 33c7c91

Please sign in to comment.