feat: conf hot reload

This commit is contained in:
Zhang Minghan 2023-12-03 04:44:53 +08:00
parent 1315782930
commit e6ca4c7015
2 changed files with 7 additions and 5 deletions

View File

@ -3,7 +3,6 @@ package chatgpt
import (
"chat/globals"
"fmt"
"github.com/spf13/viper"
)
type ChatInstance struct {
@ -40,7 +39,7 @@ func NewChatInstance(endpoint, apiKey string) *ChatInstance {
func NewChatInstanceFromConfig(conf globals.ChannelConfig) *ChatInstance {
return NewChatInstance(
viper.GetString(conf.GetEndpoint()),
viper.GetString(conf.GetRandomSecret()),
conf.GetEndpoint(),
conf.GetRandomSecret(),
)
}

View File

@ -25,13 +25,14 @@ func NewManager() *Manager {
Models: []string{},
PreflightSequence: map[string]Sequence{},
}
manager.Init()
manager.Load()
return manager
}
func (m *Manager) Init() {
func (m *Manager) Load() {
// init support models
m.Models = []string{}
for _, channel := range m.GetActiveSequence() {
for _, model := range channel.GetModels() {
if !utils.Contains(model, m.Models) {
@ -41,6 +42,7 @@ func (m *Manager) Init() {
}
// init preflight sequence
m.PreflightSequence = map[string]Sequence{}
for _, model := range m.Models {
var seq Sequence
for _, channel := range m.GetActiveSequence() {
@ -108,6 +110,7 @@ func (m *Manager) GetMaxId() int {
func (m *Manager) SaveConfig() error {
viper.Set("channel", m.Sequence)
m.Load()
return viper.WriteConfig()
}