coai/channel/worker.go
2023-12-12 19:25:39 +08:00

44 lines
1.1 KiB
Go

package channel
import (
"chat/adapter"
"chat/globals"
"chat/utils"
"fmt"
"github.com/spf13/viper"
)
func NewChatRequest(props *adapter.ChatProps, hook globals.Hook) error {
if !ManagerInstance.HasChannel(props.Model) {
return fmt.Errorf("cannot find channel for model %s", props.Model)
}
ticker := ManagerInstance.GetTicker(props.Model)
debug := viper.GetBool("debug")
var err error
for !ticker.IsDone() {
if channel := ticker.Next(); channel != nil {
if debug {
fmt.Println(fmt.Sprintf("[channel] try channel %s for model %s", channel.GetName(), props.Model))
}
props.MaxRetries = utils.ToPtr(channel.GetRetry())
if err = adapter.NewChatRequest(channel, props, hook); err == nil {
if debug {
fmt.Println(fmt.Sprintf("[channel] hit channel %s for model %s", channel.GetName(), props.Model))
}
return nil
}
fmt.Println(fmt.Sprintf("[channel] caught error %s for model %s at channel %s -> goto next channel", err.Error(), props.Model, channel.GetName()))
}
}
if debug {
fmt.Println(fmt.Sprintf("[channel] channels are exhausted for model %s", props.Model))
}
return err
}