feat(assistant): add retry when requesting completion

This commit is contained in:
Yiyang Kang 2023-03-20 19:50:24 +08:00
parent 08985f6936
commit 7e9d4e5052
Signed by: kkyy
GPG Key ID: 80FD317ECAF06CC3
1 changed files with 15 additions and 4 deletions

View File

@ -93,9 +93,19 @@ func assistantStreamedResponse(request openai.ChatRequest, cb assistantStreamedR
logger.Debugw("Openai chat request", "req", request) logger.Debugw("Openai chat request", "req", request)
ai := openai.NewClient(config.OpenAIApiKey) ai := openai.NewClient(config.OpenAIApiKey)
resp, err := ai.ChatCompletionStream(request) var (
if err != nil { resp *openai.ChatResponseStream
return err err error
)
tries := 2
for {
if tries--; tries < 0 {
return err
}
if resp, err = ai.ChatCompletionStream(request); err == nil {
break
}
logger.Warnw("assistant: failed to get response", "error", err)
} }
nErrs := 0 nErrs := 0
@ -242,8 +252,9 @@ func handleAssistantConversation(c tele.Context, thread []*tele.Message) error {
if reqErr != nil { if reqErr != nil {
logger.Errorw("assistant: failed to complete conversation", "error", reqErr) logger.Errorw("assistant: failed to complete conversation", "error", reqErr)
return c.Reply("Sorry, there's a technical issue. 😵💫 Please try again later.", tele.Silent)
} }
return reqErr return nil
} }
func assistantRemoveMention(msg, name string) string { func assistantRemoveMention(msg, name string) string {